Logo AND Algorithmique Numérique Distribuée

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