Logo AND Algorithmique Numérique Distribuée

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