Logo AND Algorithmique Numérique Distribuée

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