Logo AND Algorithmique Numérique Distribuée

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