Logo AND Algorithmique Numérique Distribuée

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