Logo AND Algorithmique Numérique Distribuée

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