Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
move gras_datadesc_type_cmp() out of the way (from ddt_exchange.c to ddt_create.c)
[simgrid.git] / src / gras / DataDesc / ddt_create.c
1 /* $Id$ */
2
3 /* ddt_new - creation/deletion of datatypes structs (private to this module)*/
4
5 /* Copyright (c) 2003 Olivier Aumage.                                       */
6 /* Copyright (c) 2003, 2004 Martin Quinson.                                 */
7 /* All rights reserved.                                                     */
8
9 /* This program is free software; you can redistribute it and/or modify it
10  * under the terms of the license (GNU LGPL) which comes with this package. */
11
12 #include "xbt/misc.h" /* min()/max() */
13 #include "gras/DataDesc/datadesc_private.h"
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ddt_create,datadesc,"Creating new datadescriptions");
16
17 /**
18  * gras_ddt_freev:
19  *
20  * gime that memory back, dude. I mean it.
21  */
22 void gras_ddt_freev(void *ddt) {
23   gras_datadesc_type_t type= (gras_datadesc_type_t)ddt;
24   
25   if (type) {
26     gras_datadesc_free(&type);
27   }
28 }
29
30 static gras_datadesc_type_t gras_ddt_new(const char *name) {
31   gras_datadesc_type_t res;
32
33   XBT_IN1("(%s)",name);
34   res=xbt_new0(s_gras_datadesc_type_t,1);
35
36   res->name = (char*)strdup(name);
37   res->name_len = strlen(name);
38   res->cycle = 0;
39       
40   xbt_set_add(gras_datadesc_set_local,
41                (xbt_set_elm_t)res,&gras_ddt_freev);
42   XBT_OUT;
43   return res;
44 }
45
46 /**
47  * gras_datadesc_by_name:
48  *
49  * Retrieve a type from its name
50  */
51 gras_datadesc_type_t gras_datadesc_by_name(const char *name) {
52
53   gras_datadesc_type_t type;
54
55   XBT_IN1("(%s)",name);
56   if (xbt_set_get_by_name(gras_datadesc_set_local,
57                            name,(xbt_set_elm_t*)&type) == no_error) {
58     XBT_OUT;
59     return type;
60   } else { 
61     XBT_OUT;
62     return NULL;
63   }
64 }
65
66 /**
67  * gras_datadesc_by_id:
68  *
69  * Retrieve a type from its code
70  */
71 xbt_error_t gras_datadesc_by_id(long int              code,
72                                  gras_datadesc_type_t *type) {
73   XBT_IN;
74   return xbt_set_get_by_id(gras_datadesc_set_local,
75                             code,(xbt_set_elm_t*)type);
76 }
77
78 /**
79  * gras_datadesc_scalar:
80  *
81  * Create a new scalar and give a pointer to it 
82  */
83 gras_datadesc_type_t 
84   gras_datadesc_scalar(const char                      *name,
85                        gras_ddt_scalar_type_t           type,
86                        enum e_gras_dd_scalar_encoding   encoding) {
87
88   gras_datadesc_type_t res;
89   long int arch;
90
91   XBT_IN;
92   res = gras_datadesc_by_name(name);
93   if (res) {
94     xbt_assert1(res->category_code == e_gras_datadesc_type_cat_scalar,
95                  "Redefinition of type %s does not match", name);
96     xbt_assert1(res->category.scalar_data.encoding == encoding,
97                  "Redefinition of type %s does not match", name);
98     xbt_assert1(res->category.scalar_data.type == type,
99                  "Redefinition of type %s does not match", name);
100     VERB1("Discarding redefinition of %s",name);
101     return res;
102   }
103   res = gras_ddt_new(name);
104
105   for (arch = 0; arch < gras_arch_count; arch ++) {
106 #if 0
107     long int sz;
108     long int mask;
109 #endif
110     res->size[arch]         = gras_arches[arch].sizeofs[type];
111     res->alignment[arch]    = gras_arches[arch].boundaries[type];
112     res->aligned_size[arch] = aligned(res->size[arch], res->alignment[arch]);
113 /* FIXME: kill the following after discution with Oli */
114 #if 0
115     sz = res->size[arch];
116     mask = sz;
117     
118     /* just in case you wonder, x>>1 == x/2 on all architectures when x>=0
119        and a size is always>=0 */
120     
121     /* make sure mask have all the bits under the biggest one of size set to 1
122        Example: size=000100101 => mask=0000111111 */
123     while ((sz >>= 1)) {
124       mask |= sz;
125     } 
126     
127     if (res->size[arch] & (mask >> 1)) { /* if size have bits to one beside its biggest */
128       /* size is not a power of 2 */
129       /* alignment= next power of 2 after size */
130       res->alignment[arch] = (res->size[arch] & ~(mask >> 1)) << 1;
131       xbt_assert0(res->alignment[arch] != 0,
132                    "scalar type too large");
133       
134       res->aligned_size[arch] = aligned(res->size[arch], res->alignment[arch]);
135       xbt_assert0 (res->aligned_size[arch] >= 0,
136                     "scalar type too large");
137       
138     } else {
139       /* size is a power of 2, life is great */
140       res->alignment[arch]       = res->size[arch];
141       res->aligned_size[arch]    = res->size[arch];
142     }
143 #endif     
144   }
145
146   res->category_code                 = e_gras_datadesc_type_cat_scalar;
147   res->category.scalar_data.encoding = encoding;
148   res->category.scalar_data.type     = type;
149   XBT_OUT;
150   
151   return res;
152 }
153
154
155 /**
156  * gras_dd_cat_field_free:
157  *
158  * Frees one struct or union field
159  */
160 void gras_dd_cat_field_free(void *f) {
161   gras_dd_cat_field_t field = *(gras_dd_cat_field_t *)f;
162   XBT_IN;
163   if (field) {
164     if (field->name) 
165       xbt_free(field->name);
166     xbt_free(field);
167   }
168   XBT_OUT;
169 }
170
171 /**
172  * gras_datadesc_struct:
173  *
174  * Create a new struct and give a pointer to it 
175  */
176 gras_datadesc_type_t 
177   gras_datadesc_struct(const char            *name) {
178
179   gras_datadesc_type_t res;
180   long int arch;
181   
182   XBT_IN1("(%s)",name);
183   res = gras_datadesc_by_name(name);
184   if (res) {
185     /* FIXME: Check that field redefinition matches */
186     xbt_assert1(res->category_code == e_gras_datadesc_type_cat_struct,
187                  "Redefinition of type %s does not match", name);
188     VERB1("Discarding redefinition of %s",name);
189     return res;
190   }
191   res = gras_ddt_new(name);
192
193   for (arch=0; arch<gras_arch_count; arch ++) {
194     res->size[arch] = 0;
195     res->alignment[arch] = 0;
196     res->aligned_size[arch] = 0;
197   }
198   res->category_code = e_gras_datadesc_type_cat_struct;
199   res->category.struct_data.fields = 
200        xbt_dynar_new(sizeof(gras_dd_cat_field_t),
201                       &gras_dd_cat_field_free);
202
203   XBT_OUT;
204   return res;
205 }
206
207 /**
208  * gras_datadesc_struct_append:
209  *
210  * Append a field to the struct
211  */
212 void
213 gras_datadesc_struct_append(gras_datadesc_type_t struct_type,
214                             const char          *name,
215                             gras_datadesc_type_t field_type) {
216
217   gras_dd_cat_field_t field;
218   int arch;
219
220   xbt_assert2(field_type,
221                "Cannot add the field '%s' into struct '%s': its type is NULL. Typo in get_by_name?",
222                name,struct_type->name);
223   XBT_IN3("(%s %s.%s;)",field_type->name,struct_type->name,name);
224   if (struct_type->category.struct_data.closed) {
225     VERB1("Ignoring request to add field to struct %s (closed. Redefinition?)",
226           struct_type->name);
227     return;
228   }
229
230   xbt_assert1(field_type->size != 0,
231                "Cannot add a dynamically sized field in structure %s",
232                struct_type->name);
233     
234   field=xbt_new(s_gras_dd_cat_field_t,1);
235   field->name   = (char*)strdup(name);
236
237   DEBUG0("----------------");
238   DEBUG3("PRE s={size=%ld,align=%ld,asize=%ld}",
239          struct_type->size[GRAS_THISARCH], 
240          struct_type->alignment[GRAS_THISARCH], 
241          struct_type->aligned_size[GRAS_THISARCH]);
242      
243      
244   for (arch=0; arch<gras_arch_count; arch ++) {
245     field->offset[arch] = aligned(struct_type->size[arch],
246                                   field_type->alignment[arch]);
247
248     struct_type->size[arch] = field->offset[arch] + field_type->size[arch];
249     struct_type->alignment[arch] = max(struct_type->alignment[arch],
250                                        field_type->alignment[arch]);
251     struct_type->aligned_size[arch] = aligned(struct_type->size[arch],
252                                               struct_type->alignment[arch]);
253   }
254   field->type   = field_type;
255   field->pre    = NULL;
256   field->post   = NULL;
257   
258   xbt_dynar_push(struct_type->category.struct_data.fields, &field);
259
260   DEBUG3("Push a %s into %s at offset %ld.",
261          field_type->name, struct_type->name,field->offset[GRAS_THISARCH]);
262   DEBUG3("  f={size=%ld,align=%ld,asize=%ld}",
263          field_type->size[GRAS_THISARCH], 
264          field_type->alignment[GRAS_THISARCH], 
265          field_type->aligned_size[GRAS_THISARCH]);
266   DEBUG3("  s={size=%ld,align=%ld,asize=%ld}",
267          struct_type->size[GRAS_THISARCH], 
268          struct_type->alignment[GRAS_THISARCH], 
269          struct_type->aligned_size[GRAS_THISARCH]);
270   XBT_OUT;
271 }
272
273 void
274 gras_datadesc_struct_close(gras_datadesc_type_t struct_type) {
275   XBT_IN;
276   struct_type->category.struct_data.closed = 1;
277   DEBUG4("structure %s closed. size=%ld,align=%ld,asize=%ld",
278          struct_type->name,
279          struct_type->size[GRAS_THISARCH], 
280          struct_type->alignment[GRAS_THISARCH], 
281          struct_type->aligned_size[GRAS_THISARCH]);
282 }
283
284 /**
285  * gras_datadesc_cycle_set:
286  * 
287  * Tell GRAS that the pointers of the type described by @ddt may present
288  * some loop, and that the cycle detection mecanism is needed.
289  *
290  * Note that setting this option when not needed have a rather bad effect 
291  * on the performance (several times slower on big data).
292  */
293 void
294 gras_datadesc_cycle_set(gras_datadesc_type_t ddt) {
295   ddt->cycle = 1;
296 }
297 /**
298  * gras_datadesc_cycle_unset:
299  * 
300  * Tell GRAS that the pointers of the type described by @ddt do not present
301  * any loop and that cycle detection mecanism are not needed.
302  * (default)
303  */
304 void
305 gras_datadesc_cycle_unset(gras_datadesc_type_t ddt) {
306   ddt->cycle = 0;
307 }
308
309 /**
310  * gras_datadesc_union:
311  *
312  * Create a new union and give a pointer to it 
313  */
314 gras_datadesc_type_t 
315 gras_datadesc_union(const char                   *name,
316                     gras_datadesc_type_cb_int_t   selector) {
317
318   gras_datadesc_type_t res;
319   int arch;
320
321   XBT_IN1("(%s)",name);
322   xbt_assert0(selector,
323                "Attempt to creat an union without field_count function");
324
325   res = gras_datadesc_by_name(name);
326   if (res) {
327     /* FIXME: Check that field redefinition matches */
328     xbt_assert1(res->category_code == e_gras_datadesc_type_cat_union,
329                  "Redefinition of type %s does not match", name);
330     xbt_assert1(res->category.union_data.selector == selector,
331                  "Redefinition of type %s does not match", name);
332     VERB1("Discarding redefinition of %s",name);
333     return res;
334   }
335
336   res = gras_ddt_new(name);
337
338   for (arch=0; arch<gras_arch_count; arch ++) {
339      res->size[arch] = 0;
340      res->alignment[arch] = 0;
341      res->aligned_size[arch] = 0;
342   }
343
344   res->category_code            = e_gras_datadesc_type_cat_union;
345   res->category.union_data.fields =
346      xbt_dynar_new(sizeof(gras_dd_cat_field_t*),
347                     &gras_dd_cat_field_free);
348   res->category.union_data.selector = selector;
349
350   return res;
351 }
352
353 /**
354  * gras_datadesc_union_append:
355  *
356  * Append a field to the union
357  */
358 void
359 gras_datadesc_union_append(gras_datadesc_type_t  union_type,
360                            const char           *name,
361                            gras_datadesc_type_t  field_type) {
362
363   gras_dd_cat_field_t field;
364   int arch;
365
366   XBT_IN3("(%s %s.%s;)",field_type->name,union_type->name,name);
367   xbt_assert1(field_type->size != 0,
368                "Cannot add a dynamically sized field in union %s",
369                union_type->name);
370
371   if (union_type->category.union_data.closed) {
372     VERB1("Ignoring request to add field to union %s (closed)",
373            union_type->name);
374     return;
375   }
376     
377   field=xbt_new0(s_gras_dd_cat_field_t,1);
378
379   field->name   = (char*)strdup(name);
380   field->type   = field_type;
381   /* All offset are left to 0 in an union */
382   
383   xbt_dynar_push(union_type->category.union_data.fields, &field);
384
385   for (arch=0; arch<gras_arch_count; arch ++) {
386     union_type->size[arch] = max(union_type->size[arch],
387                                  field_type->size[arch]);
388     union_type->alignment[arch] = max(union_type->alignment[arch],
389                                       field_type->alignment[arch]);
390     union_type->aligned_size[arch] = aligned(union_type->size[arch],
391                                              union_type->alignment[arch]);
392   }
393 }
394
395 void
396 gras_datadesc_union_close(gras_datadesc_type_t union_type) {
397    union_type->category.union_data.closed = 1;
398 }
399 /**
400  * gras_datadesc_ref:
401  *
402  * Create a new ref to a fixed type and give a pointer to it 
403  */
404 gras_datadesc_type_t 
405   gras_datadesc_ref(const char           *name,
406                     gras_datadesc_type_t  referenced_type) {
407
408   gras_datadesc_type_t res;
409   gras_datadesc_type_t pointer_type = gras_datadesc_by_name("data pointer");
410   int arch;
411
412   XBT_IN1("(%s)",name);
413   res = gras_datadesc_by_name(name);
414   if (res) {
415     xbt_assert1(res->category_code == e_gras_datadesc_type_cat_ref,
416                  "Redefinition of %s does not match",name);
417     xbt_assert1(res->category.ref_data.type == referenced_type,
418                  "Redefinition of %s does not match",name);
419     xbt_assert1(res->category.ref_data.selector == NULL,
420                  "Redefinition of %s does not match",name);
421     VERB1("Discarding redefinition of %s",name);
422     return res;
423   }
424
425   res = gras_ddt_new(name);
426
427   xbt_assert0(pointer_type, "Cannot get the description of data pointer");
428       
429   for (arch=0; arch<gras_arch_count; arch ++){
430     res->size[arch] = pointer_type->size[arch];
431     res->alignment[arch] = pointer_type->alignment[arch];
432     res->aligned_size[arch] = pointer_type->aligned_size[arch];
433   }
434   
435   res->category_code              = e_gras_datadesc_type_cat_ref;
436   res->category.ref_data.type     = referenced_type;
437   res->category.ref_data.selector = NULL;
438
439   return res;
440 }
441 /**
442  * gras_datadesc_ref_generic:
443  *
444  * Create a new ref to a type given at use time, and give a pointer to it 
445  */
446 gras_datadesc_type_t 
447   gras_datadesc_ref_generic(const char                *name,
448                             gras_datadesc_selector_t   selector) {
449
450   gras_datadesc_type_t res;
451   gras_datadesc_type_t pointer_type = gras_datadesc_by_name("data pointer");
452   int arch;
453
454   XBT_IN1("(%s)",name);
455   res = gras_datadesc_by_name(name);
456   if (res) {
457     xbt_assert1(res->category_code == e_gras_datadesc_type_cat_ref,
458                  "Redefinition of type %s does not match", name);
459     xbt_assert1(res->category.ref_data.type == NULL,
460                  "Redefinition of type %s does not match", name);
461     xbt_assert1(res->category.ref_data.selector == selector,
462                  "Redefinition of type %s does not match", name);
463     VERB1("Discarding redefinition of %s",name);
464     return res;
465   }
466   res = gras_ddt_new(name);
467
468   xbt_assert0(pointer_type, "Cannot get the description of data pointer");
469       
470   for (arch=0; arch<gras_arch_count; arch ++) {
471     res->size[arch] = pointer_type->size[arch];
472     res->alignment[arch] = pointer_type->alignment[arch];
473     res->aligned_size[arch] = pointer_type->aligned_size[arch];
474   }
475
476   res->category_code            = e_gras_datadesc_type_cat_ref;
477
478   res->category.ref_data.type     = NULL;
479   res->category.ref_data.selector = selector;
480
481   return res;
482 }
483
484 /**
485  * gras_datadesc_array_fixed:
486  *
487  * Create a new array and give a pointer to it 
488  */
489 gras_datadesc_type_t 
490   gras_datadesc_array_fixed(const char           *name,
491                             gras_datadesc_type_t  element_type,
492                             long int              fixed_size) {
493
494   gras_datadesc_type_t res;
495   int arch;
496
497   XBT_IN1("(%s)",name);
498   res = gras_datadesc_by_name(name);
499   if (res) {
500     xbt_assert1(res->category_code == e_gras_datadesc_type_cat_array,
501                  "Redefinition of type %s does not match", name);
502     xbt_assert1(res->category.array_data.type == element_type,
503                  "Redefinition of type %s does not match", name);
504     xbt_assert1(res->category.array_data.fixed_size == fixed_size,
505                  "Redefinition of type %s does not match", name);
506     xbt_assert1(res->category.array_data.dynamic_size == NULL,
507                  "Redefinition of type %s does not match", name);
508     VERB1("Discarding redefinition of %s",name);
509
510     return res;
511   }
512   res = gras_ddt_new(name);
513
514   xbt_assert1(fixed_size > 0, "'%s' is a array of null fixed size",name);
515   for (arch=0; arch<gras_arch_count; arch ++) {
516     res->size[arch] = fixed_size * element_type->aligned_size[arch];
517     res->alignment[arch] = element_type->alignment[arch];
518     res->aligned_size[arch] = res->size[arch];
519   }  
520
521   res->category_code            = e_gras_datadesc_type_cat_array;
522
523   res->category.array_data.type         = element_type;
524   res->category.array_data.fixed_size   = fixed_size;
525   res->category.array_data.dynamic_size = NULL;
526
527   return res;
528 }
529 /**
530  * gras_datadesc_array_dyn:
531  *
532  * Create a new array and give a pointer to it 
533  */
534 gras_datadesc_type_t 
535   gras_datadesc_array_dyn(const char                  *name,
536                           gras_datadesc_type_t         element_type,
537                           gras_datadesc_type_cb_int_t  dynamic_size) {
538
539   gras_datadesc_type_t res;
540   int arch;
541
542   XBT_IN1("(%s)",name);
543   xbt_assert1(dynamic_size,
544                "'%s' is a dynamic array without size discriminant",
545                name);
546
547   res = gras_datadesc_by_name(name);
548   if (res) {
549     xbt_assert1(res->category_code == e_gras_datadesc_type_cat_array,
550                  "Redefinition of type %s does not match", name);
551     xbt_assert1(res->category.array_data.type == element_type,
552                  "Redefinition of type %s does not match", name);
553     xbt_assert1(res->category.array_data.fixed_size == 0,
554                  "Redefinition of type %s does not match", name);
555     xbt_assert1(res->category.array_data.dynamic_size == dynamic_size,
556                  "Redefinition of type %s does not match", name);
557     VERB1("Discarding redefinition of %s",name);
558
559     return res;
560   }
561
562   res = gras_ddt_new(name);
563
564   for (arch=0; arch<gras_arch_count; arch ++) {
565     res->size[arch] = 0; /* make sure it indicates "dynamic" */
566     res->alignment[arch] = element_type->alignment[arch];
567     res->aligned_size[arch] = 0; /*FIXME: That was so in GS, but looks stupid*/
568   }
569
570   res->category_code            = e_gras_datadesc_type_cat_array;
571
572   res->category.array_data.type         = element_type;
573   res->category.array_data.fixed_size   = 0;
574   res->category.array_data.dynamic_size = dynamic_size;
575
576   return res;
577 }
578
579 /**
580  * gras_datadesc_ref_pop_arr:
581  *
582  * Most of the time, you want to include a reference in your structure which
583  * is a pointer to a dynamic array whose size is fixed by another field of 
584  * your structure.
585  *
586  * This case pops up so often that this function was created to take care of
587  * this case. It creates a dynamic array type whose size is poped from the 
588  * current cbps, and then create a reference to it.
589  *
590  * The name of the created datatype will be the name of the element type, with
591  * '[]*' appended to it.
592  *
593  * Then to use it, you just have to make sure that your structure pre-callback
594  * does push the size of the array in the cbps (using #gras_cbps_i_push), and 
595  * you are set. 
596  *
597  * But be remember that this is a stack. If you have two different pop_arr, you
598  * should push the second one first, so that the first one is on the top of the 
599  * list when the first field gets transfered.
600  *
601  */
602 gras_datadesc_type_t 
603   gras_datadesc_ref_pop_arr(gras_datadesc_type_t element_type) {
604
605   gras_datadesc_type_t res;
606   char *name=(char*)xbt_malloc(strlen(element_type->name) + 4);
607
608   sprintf(name,"%s[]",element_type->name);
609
610   res = gras_datadesc_array_dyn(name,element_type,
611                                 gras_datadesc_cb_pop);
612
613   sprintf(name,"%s[]*",element_type->name);
614   res = gras_datadesc_ref(name,res);
615
616   xbt_free(name);
617
618   return res;
619 }
620 xbt_error_t
621 gras_datadesc_import_nws(const char           *name,
622                          const DataDescriptor *desc,
623                          unsigned long         howmany,
624                /* OUT */ gras_datadesc_type_t *dst) {
625   RAISE_UNIMPLEMENTED;
626 }
627
628 /**
629  * gras_datadesc_cb_send:
630  *
631  * Add a pre-send callback to this datadesc.
632  * (useful to push the sizes of the upcoming arrays, for example)
633  */
634 void gras_datadesc_cb_send (gras_datadesc_type_t          type,
635                             gras_datadesc_type_cb_void_t  send) {
636   type->send = send;
637 }
638 /**
639  * gras_datadesc_cb_recv:
640  *
641  * Add a post-receive callback to this datadesc.
642  * (useful to put the function pointers to the rigth value, for example)
643  */
644 void gras_datadesc_cb_recv(gras_datadesc_type_t          type,
645                            gras_datadesc_type_cb_void_t  recv) {
646   type->recv = recv;
647 }
648 /**
649  * gras_dd_find_field:
650  * 
651  * Returns the type descriptor of the given field. Abort on error.
652  */
653 static gras_datadesc_type_t 
654   gras_dd_find_field(gras_datadesc_type_t  type,
655                      const char           *field_name) {
656    xbt_dynar_t         field_array;
657    
658    gras_dd_cat_field_t  field=NULL;
659    int                  field_num;
660    
661    if (type->category_code == e_gras_datadesc_type_cat_union) {
662       field_array = type->category.union_data.fields;
663    } else if (type->category_code == e_gras_datadesc_type_cat_struct) {
664       field_array = type->category.struct_data.fields;
665    } else {
666       ERROR2("%s (%p) is not a struct nor an union. There is no field.", type->name,(void*)type);
667       xbt_abort();
668    }
669    xbt_dynar_foreach(field_array,field_num,field) {
670       if (!strcmp(field_name,field->name)) {
671          return field->type;
672       }
673    }
674    ERROR2("No field nammed %s in %s",field_name,type->name);
675    xbt_abort();
676
677 }
678                                   
679 /**
680  * gras_datadesc_cb_field_send:
681  *
682  * Add a pre-send callback to the given field of the datadesc (which must be a struct or union).
683  * (useful to push the sizes of the upcoming arrays, for example)
684  */
685 void gras_datadesc_cb_field_send (gras_datadesc_type_t          type,
686                                   const char                   *field_name,
687                                   gras_datadesc_type_cb_void_t  send) {
688    
689    gras_datadesc_type_t sub_type=gras_dd_find_field(type,field_name);   
690    sub_type->send = send;
691 }
692
693 /**
694  * gras_datadesc_cb_field_push:
695  *
696  * Add a pre-send callback to the given field resulting in its value to be pushed to
697  * the stack of sizes. It must be a int, unsigned int, long int or unsigned long int.
698  */
699 void gras_datadesc_cb_field_push (gras_datadesc_type_t  type,
700                                   const char           *field_name) {
701    
702    gras_datadesc_type_t sub_type=gras_dd_find_field(type,field_name);
703    if (!strcmp("int",sub_type->name)) {
704       sub_type->send = gras_datadesc_cb_push_int;
705    } else if (!strcmp("unsigned int",sub_type->name)) {
706       sub_type->send = gras_datadesc_cb_push_uint;
707    } else if (!strcmp("long int",sub_type->name)) {
708       sub_type->send = gras_datadesc_cb_push_lint;
709    } else if (!strcmp("unsigned long int",sub_type->name)) {
710       sub_type->send = gras_datadesc_cb_push_ulint;
711    } else {
712       ERROR1("Field %s is not an int, unsigned int, long int neither unsigned long int",
713              sub_type->name);
714       xbt_abort();
715    }
716 }
717 /**
718  * gras_datadesc_cb_field_recv:
719  *
720  * Add a post-receive callback to the given field of the datadesc (which must be a struct or union).
721  * (useful to put the function pointers to the right value, for example)
722  */
723 void gras_datadesc_cb_field_recv(gras_datadesc_type_t          type,
724                                  const char                   *field_name,
725                                  gras_datadesc_type_cb_void_t  recv) {
726    
727    gras_datadesc_type_t sub_type=gras_dd_find_field(type,field_name);   
728    sub_type->recv = recv;
729 }
730
731 /**
732  * gras_datadesc_free:
733  *
734  * Free a datadesc. Should only be called at xbt_exit. 
735  */
736 void gras_datadesc_free(gras_datadesc_type_t *type) {
737
738   DEBUG1("Let's free ddt %s",(*type)->name);
739   
740   switch ((*type)->category_code) {
741   case e_gras_datadesc_type_cat_scalar:
742   case e_gras_datadesc_type_cat_ref:
743   case e_gras_datadesc_type_cat_array:
744     /* nothing to free in there */
745     break;
746     
747   case e_gras_datadesc_type_cat_ignored:
748     if ((*type)->category.ignored_data.free_func) {
749       (*type)->category.ignored_data.free_func
750         ((*type)->category.ignored_data.default_value);
751     }
752     break;
753     
754   case e_gras_datadesc_type_cat_struct:
755     xbt_dynar_free(&( (*type)->category.struct_data.fields ));
756     break;
757     
758   case e_gras_datadesc_type_cat_union:
759     xbt_dynar_free(&( (*type)->category.union_data.fields ));
760     break;
761     
762   default:
763     /* datadesc was invalid. Killing it is like euthanasy, I guess */
764     break;
765   }
766   xbt_free((*type)->name);
767   xbt_free(*type);
768   type=NULL;
769 }
770
771 /**
772  * gras_datadesc_type_cmp:
773  *
774  * Compares two datadesc types with the same semantic than strcmp.
775  *
776  * This comparison does not take the set headers into account (name and ID), 
777  * but only the payload (actual type description).
778  */
779 int gras_datadesc_type_cmp(const gras_datadesc_type_t d1,
780                            const gras_datadesc_type_t d2) {
781   int ret,cpt;
782   gras_dd_cat_field_t field1,field2;
783   gras_datadesc_type_t field_desc_1,field_desc_2;
784
785   if (d1 == d2) return 0; /* easy optimization */
786
787   if (!d1 && d2) {
788     DEBUG0("ddt_cmp: !d1 && d2 => 1");
789     return 1;
790   }
791   if (!d1 && !d2) {
792     DEBUG0("ddt_cmp: !d1 && !d2 => 0");
793     return 0;
794   }
795   if ( d1 && !d2) {
796     DEBUG0("ddt_cmp: d1 && !d2 => -1");
797     return -1;
798   }
799
800   for (cpt=0; cpt<gras_arch_count; cpt++) {
801     if (d1->size[cpt] != d2->size[cpt]) {
802       DEBUG5("ddt_cmp: %s->size=%ld  !=  %s->size=%ld (on %s)",
803              d1->name,d1->size[cpt],d2->name,d2->size[cpt],
804              gras_arches[cpt].name);
805       return d1->size[cpt] >  d2->size[cpt] ? 1 : -1;
806     }
807
808     if (d1->alignment[cpt] != d2->alignment[cpt]) {
809       DEBUG5("ddt_cmp: %s->alignment=%ld  !=  %s->alignment=%ld (on %s)",
810              d1->name,d1->alignment[cpt],d2->name,d2->alignment[cpt],
811              gras_arches[cpt].name);
812       return d1->alignment[cpt] > d2->alignment[cpt] ? 1 : -1;
813     }
814
815     if (d1->aligned_size[cpt] != d2->aligned_size[cpt]) {
816       DEBUG5("ddt_cmp: %s->aligned_size=%ld  !=  %s->aligned_size=%ld (on %s)",
817              d1->name,d1->aligned_size[cpt],d2->name,d2->aligned_size[cpt],
818              gras_arches[cpt].name);
819       return d1->aligned_size[cpt] > d2->aligned_size[cpt] ? 1 : -1;
820     }
821   }
822
823   if (d1->category_code != d2->category_code) {
824     DEBUG4("ddt_cmp: %s->cat=%s  !=  %s->cat=%s",
825            d1->name,gras_datadesc_cat_names[d1->category_code],
826            d2->name,gras_datadesc_cat_names[d2->category_code]);
827     return d1->category_code > d2->category_code ? 1 : -1;
828   }
829
830   if (d1->send != d2->send) {
831     DEBUG4("ddt_cmp: %s->send=%p  !=  %s->send=%p",
832            d1->name,(void*)d1->send, d2->name,(void*)d2->send);
833     return 1; /* ISO C forbids ordered comparisons of pointers to functions */
834   }
835
836   if (d1->recv != d2->recv) {
837     DEBUG4("ddt_cmp: %s->recv=%p  !=  %s->recv=%p",
838            d1->name,(void*)d1->recv, d2->name,(void*)d2->recv);
839     return 1; /* ISO C forbids ordered comparisons of pointers to functions */
840   }
841
842   switch (d1->category_code) {
843   case e_gras_datadesc_type_cat_scalar:
844     if (d1->category.scalar_data.encoding != d2->category.scalar_data.encoding)
845       return d1->category.scalar_data.encoding > d2->category.scalar_data.encoding ? 1 : -1 ;
846     break;
847     
848   case e_gras_datadesc_type_cat_struct:    
849     if (xbt_dynar_length(d1->category.struct_data.fields) != 
850         xbt_dynar_length(d2->category.struct_data.fields)) {
851       DEBUG4("ddt_cmp: %s (having %lu fields) !=  %s (having %lu fields)",
852              d1->name, xbt_dynar_length(d1->category.struct_data.fields),
853              d2->name, xbt_dynar_length(d2->category.struct_data.fields));
854       
855       return xbt_dynar_length(d1->category.struct_data.fields) >
856         xbt_dynar_length(d2->category.struct_data.fields) ?
857         1 : -1;
858     }
859     xbt_dynar_foreach(d1->category.struct_data.fields, cpt, field1) {
860       
861       field2 = xbt_dynar_get_as(d2->category.struct_data.fields, cpt, gras_dd_cat_field_t);
862       field_desc_1 = field1->type;
863       field_desc_2 = field2->type;
864       ret = gras_datadesc_type_cmp(field_desc_1,field_desc_2);
865       if (ret) {
866         DEBUG6("%s->field[%d]=%s != %s->field[%d]=%s",
867                d1->name,cpt,field1->name,              
868                d2->name,cpt,field2->name);
869         return ret;
870       }
871       
872     }
873     break;
874     
875   case e_gras_datadesc_type_cat_union:
876     if (d1->category.union_data.selector != d2->category.union_data.selector) 
877       return 1;  /* ISO C forbids ordered comparisons of pointers to functions */
878     
879     if (xbt_dynar_length(d1->category.union_data.fields) != 
880         xbt_dynar_length(d2->category.union_data.fields))
881       return xbt_dynar_length(d1->category.union_data.fields) >
882              xbt_dynar_length(d2->category.union_data.fields) ?
883         1 : -1;
884     
885     xbt_dynar_foreach(d1->category.union_data.fields, cpt, field1) {
886       
887       field2 = xbt_dynar_get_as(d2->category.union_data.fields, cpt, gras_dd_cat_field_t);
888       field_desc_1 = field1->type;
889       field_desc_2 = field2->type;
890       ret = gras_datadesc_type_cmp(field_desc_1,field_desc_2);
891       if (ret)
892         return ret;
893       
894     }
895     break;
896     
897     
898   case e_gras_datadesc_type_cat_ref:
899     if (d1->category.ref_data.selector != d2->category.ref_data.selector) 
900       return 1; /* ISO C forbids ordered comparisons of pointers to functions */
901     
902     if (d1->category.ref_data.type != d2->category.ref_data.type) 
903       return d1->category.ref_data.type > d2->category.ref_data.type ? 1 : -1;
904     break;
905     
906   case e_gras_datadesc_type_cat_array:
907     if (d1->category.array_data.type != d2->category.array_data.type) 
908       return d1->category.array_data.type > d2->category.array_data.type ? 1 : -1;
909     
910     if (d1->category.array_data.fixed_size != d2->category.array_data.fixed_size) 
911       return d1->category.array_data.fixed_size > d2->category.array_data.fixed_size ? 1 : -1;
912     
913     if (d1->category.array_data.dynamic_size != d2->category.array_data.dynamic_size) 
914       return 1; /* ISO C forbids ordered comparisons of pointers to functions */
915     
916     break;
917     
918   case e_gras_datadesc_type_cat_ignored:
919     /* That's ignored... */
920   default:
921     /* two stupidly created ddt are equally stupid ;) */
922     break;
923   }
924   return 0;
925   
926 }