Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Check return value of (v)asprintf.
[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_IN1("(%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     VERB1("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_IN1("(%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     VERB1("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_IN3("(%s %s.%s;)", field_type->name, struct_type->name, name);
210   if (struct_type->category.struct_data.closed) {
211     VERB1
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   DEBUG0("----------------");
225   DEBUG3("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   DEBUG3("Push a %s into %s at offset %ld.",
249          field_type->name, struct_type->name,
250          field->offset[GRAS_THISARCH]);
251   DEBUG3("  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   DEBUG3("  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   DEBUG4("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_IN1("(%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     VERB1("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_IN3("(%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     VERB1("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_IN1("(%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     VERB1("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_IN1("(%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     VERB1("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_IN1("(%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       ERROR1
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     VERB1("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_IN1("(%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     VERB1("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   DEBUG1("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   if (asprintf(&buffname, "s_xbt_dynar_of_%s", elm_t->name) == -1)
698     xbt_die("asprintf failed");
699
700   res = gras_datadesc_struct(buffname);
701
702   gras_datadesc_struct_append(res, "size",
703                               gras_datadesc_by_name("unsigned long int"));
704
705   gras_datadesc_struct_append(res, "used",
706                               gras_datadesc_by_name("unsigned long int"));
707
708   gras_datadesc_struct_append(res, "elmsize",
709                               gras_datadesc_by_name("unsigned long int"));
710
711   gras_datadesc_struct_append(res, "data",
712                               gras_datadesc_ref_pop_arr(elm_t));
713
714   gras_datadesc_struct_append(res, "free_f",
715                               gras_datadesc_by_name("function pointer"));
716   memcpy(res->extra, &free_func, sizeof(free_func));
717
718   gras_datadesc_struct_append(res, "mutex",
719                               gras_datadesc_by_name("data pointer"));
720
721   gras_datadesc_struct_close(res);
722
723   gras_datadesc_cb_field_push(res, "used");
724   gras_datadesc_cb_recv(res, &gras_datadesc_dynar_cb);
725
726   /* build a ref to it */
727   free(buffname);
728   if (asprintf(&buffname, "xbt_dynar_of_%s", elm_t->name) == -1)
729     xbt_die("asprintf failed");
730   res = gras_datadesc_ref(buffname, res);
731   free(buffname);
732   return res;
733 }
734
735 #include "xbt/matrix.h"
736 static void gras_datadesc_matrix_cb(gras_datadesc_type_t typedesc,
737                                     gras_cbps_t vars, void *data)
738 {
739   gras_datadesc_type_t subtype;
740   xbt_matrix_t matrix = (xbt_matrix_t) data;
741
742   memcpy(&matrix->free_f, &typedesc->extra, sizeof(matrix->free_f));
743
744   /* search for the elemsize in what we have. If elements are "int", typedesc got is "int[]*" */
745   subtype = gras_dd_find_field(typedesc, "data")->type;
746
747   /* this is now a ref to array of what we're looking for */
748   subtype = subtype->category.ref_data.type;
749   subtype = subtype->category.array_data.type;
750
751   DEBUG1("subtype is %s", subtype->name);
752
753   matrix->elmsize = subtype->size[GRAS_THISARCH];
754 }
755
756 gras_datadesc_type_t
757 gras_datadesc_matrix(gras_datadesc_type_t elm_t,
758                      void_f_pvoid_t const free_f)
759 {
760   char *buffname;
761   gras_datadesc_type_t res;
762
763   if (asprintf(&buffname, "s_xbt_matrix_t(%s)", elm_t->name) == -1)
764     xbt_die("asprintf failed");
765   res = gras_datadesc_struct(buffname);
766
767   gras_datadesc_struct_append(res, "lines",
768                               gras_datadesc_by_name("unsigned int"));
769   gras_datadesc_struct_append(res, "rows",
770                               gras_datadesc_by_name("unsigned int"));
771
772   gras_datadesc_struct_append(res, "elmsize",
773                               gras_datadesc_by_name("unsigned long int"));
774
775   gras_datadesc_struct_append(res, "data",
776                               gras_datadesc_ref_pop_arr(elm_t));
777   gras_datadesc_struct_append(res, "free_f",
778                               gras_datadesc_by_name("function pointer"));
779   gras_datadesc_struct_close(res);
780
781   gras_datadesc_cb_field_push(res, "lines");
782   gras_datadesc_cb_field_push_multiplier(res, "rows");
783
784   gras_datadesc_cb_recv(res, &gras_datadesc_matrix_cb);
785   memcpy(res->extra, &free_f, sizeof(free_f));
786
787   /* build a ref to it */
788   free(buffname);
789   if (asprintf(&buffname, "xbt_matrix_t(%s)", elm_t->name) == -1)
790     xbt_die("asprintf failed");
791   res = gras_datadesc_ref(buffname, res);
792   free(buffname);
793   return res;
794 }
795
796 gras_datadesc_type_t
797 gras_datadesc_import_nws(const char *name,
798                          const DataDescriptor * desc,
799                          unsigned long howmany)
800 {
801   THROW_UNIMPLEMENTED;
802 }
803
804 /**
805  * (useful to push the sizes of the upcoming arrays, for example)
806  */
807 void gras_datadesc_cb_send(gras_datadesc_type_t type,
808                            gras_datadesc_type_cb_void_t send)
809 {
810   type->send = send;
811 }
812
813 /**
814  * (useful to put the function pointers to the rigth value, for example)
815  */
816 void gras_datadesc_cb_recv(gras_datadesc_type_t type,
817                            gras_datadesc_type_cb_void_t recv)
818 {
819   type->recv = recv;
820 }
821
822 /*
823  * gras_dd_find_field:
824  *
825  * Returns the type descriptor of the given field. Abort on error.
826  */
827 static gras_dd_cat_field_t
828 gras_dd_find_field(gras_datadesc_type_t type, const char *field_name)
829 {
830   xbt_dynar_t field_array;
831
832   gras_dd_cat_field_t field = NULL;
833   unsigned int field_num;
834
835   if (type->category_code == e_gras_datadesc_type_cat_union) {
836     field_array = type->category.union_data.fields;
837   } else if (type->category_code == e_gras_datadesc_type_cat_struct) {
838     field_array = type->category.struct_data.fields;
839   } else {
840     ERROR2("%s (%p) is not a struct nor an union. There is no field.",
841            type->name, (void *) type);
842     xbt_abort();
843   }
844   xbt_dynar_foreach(field_array, field_num, field) {
845     if (!strcmp(field_name, field->name)) {
846       return field;
847     }
848   }
849   ERROR2("No field named '%s' in '%s'", field_name, type->name);
850   xbt_abort();
851
852 }
853
854 /**
855  * The given datadesc must be a struct or union (abort if not).
856  * (useful to push the sizes of the upcoming arrays, for example)
857  */
858 void gras_datadesc_cb_field_send(gras_datadesc_type_t type,
859                                  const char *field_name,
860                                  gras_datadesc_type_cb_void_t send)
861 {
862
863   gras_dd_cat_field_t field = gras_dd_find_field(type, field_name);
864   field->send = send;
865 }
866
867
868 /**
869  * The value, which must be an int, unsigned int, long int or unsigned long int
870  * is pushed to the stacks of sizes and can then be retrieved with
871  * \ref gras_datadesc_ref_pop_arr or directly with \ref gras_cbps_i_pop.
872  */
873 void gras_datadesc_cb_field_push(gras_datadesc_type_t type,
874                                  const char *field_name)
875 {
876
877   gras_dd_cat_field_t field = gras_dd_find_field(type, field_name);
878   gras_datadesc_type_t sub_type = field->type;
879
880   DEBUG3("add a PUSHy cb to '%s' field (type '%s') of '%s'",
881          field_name, sub_type->name, type->name);
882   if (!strcmp("int", sub_type->name)) {
883     field->send = gras_datadesc_cb_push_int;
884   } else if (!strcmp("unsigned int", sub_type->name)) {
885     field->send = gras_datadesc_cb_push_uint;
886   } else if (!strcmp("long int", sub_type->name)) {
887     field->send = gras_datadesc_cb_push_lint;
888   } else if (!strcmp("unsigned long int", sub_type->name)) {
889     field->send = gras_datadesc_cb_push_ulint;
890   } else {
891     ERROR1
892         ("Field %s is not an int, unsigned int, long int neither unsigned long int",
893          sub_type->name);
894     xbt_abort();
895   }
896 }
897
898 /**
899  * Any previously pushed value is poped and the field value is multiplied to
900  * it. The result is then pushed back into the stack of sizes. It can then be
901  * retrieved with \ref gras_datadesc_ref_pop_arr or directly with \ref
902  * gras_cbps_i_pop.
903  *
904  * The field must be an int, unsigned int, long int or unsigned long int.
905  */
906 void gras_datadesc_cb_field_push_multiplier(gras_datadesc_type_t type,
907                                             const char *field_name)
908 {
909
910   gras_dd_cat_field_t field = gras_dd_find_field(type, field_name);
911   gras_datadesc_type_t sub_type = field->type;
912
913   DEBUG3("add a MPUSHy cb to '%s' field (type '%s') of '%s'",
914          field_name, sub_type->name, type->name);
915   if (!strcmp("int", sub_type->name)) {
916     field->send = gras_datadesc_cb_push_int_mult;
917   } else if (!strcmp("unsigned int", sub_type->name)) {
918     field->send = gras_datadesc_cb_push_uint_mult;
919   } else if (!strcmp("long int", sub_type->name)) {
920     field->send = gras_datadesc_cb_push_lint_mult;
921   } else if (!strcmp("unsigned long int", sub_type->name)) {
922     field->send = gras_datadesc_cb_push_ulint_mult;
923   } else {
924     ERROR1
925         ("Field %s is not an int, unsigned int, long int neither unsigned long int",
926          sub_type->name);
927     xbt_abort();
928   }
929 }
930
931 /**
932  * The given datadesc must be a struct or union (abort if not).
933  * (useful to put the function pointers to the right value, for example)
934  */
935 void gras_datadesc_cb_field_recv(gras_datadesc_type_t type,
936                                  const char *field_name,
937                                  gras_datadesc_type_cb_void_t recv)
938 {
939
940   gras_dd_cat_field_t field = gras_dd_find_field(type, field_name);
941   field->recv = recv;
942 }
943
944 /*
945  * Free a datadesc. Should only be called at xbt_exit.
946  */
947 void gras_datadesc_free(gras_datadesc_type_t * type)
948 {
949
950   DEBUG1("Let's free ddt %s", (*type)->name);
951
952   switch ((*type)->category_code) {
953   case e_gras_datadesc_type_cat_scalar:
954   case e_gras_datadesc_type_cat_ref:
955   case e_gras_datadesc_type_cat_array:
956     /* nothing to free in there */
957     break;
958
959   case e_gras_datadesc_type_cat_struct:
960     xbt_dynar_free(&((*type)->category.struct_data.fields));
961     break;
962
963   case e_gras_datadesc_type_cat_union:
964     xbt_dynar_free(&((*type)->category.union_data.fields));
965     break;
966
967   default:
968     /* datadesc was invalid. Killing it is like euthanasy, I guess */
969     break;
970   }
971   free((*type)->name);
972   free(*type);
973   type = NULL;
974 }
975
976 /**
977  * gras_datadesc_type_cmp:
978  *
979  * Compares two datadesc types with the same semantic than strcmp.
980  *
981  * This comparison does not take the set headers into account (name and ID),
982  * but only the payload (actual type description).
983  */
984 int gras_datadesc_type_cmp(const gras_datadesc_type_t d1,
985                            const gras_datadesc_type_t d2)
986 {
987   int ret;
988   unsigned int cpt;
989   gras_dd_cat_field_t field1, field2;
990   gras_datadesc_type_t field_desc_1, field_desc_2;
991
992   if (d1 == d2)
993     return 0;                   /* easy optimization */
994
995   if (!d1 && d2) {
996     DEBUG0("ddt_cmp: !d1 && d2 => 1");
997     return 1;
998   }
999   if (!d1 && !d2) {
1000     DEBUG0("ddt_cmp: !d1 && !d2 => 0");
1001     return 0;
1002   }
1003   if (d1 && !d2) {
1004     DEBUG0("ddt_cmp: d1 && !d2 => -1");
1005     return -1;
1006   }
1007
1008   for (cpt = 0; cpt < gras_arch_count; cpt++) {
1009     if (d1->size[cpt] != d2->size[cpt]) {
1010       DEBUG5("ddt_cmp: %s->size=%ld  !=  %s->size=%ld (on %s)",
1011              d1->name, d1->size[cpt], d2->name, d2->size[cpt],
1012              gras_arches[cpt].name);
1013       return d1->size[cpt] > d2->size[cpt] ? 1 : -1;
1014     }
1015
1016     if (d1->alignment[cpt] != d2->alignment[cpt]) {
1017       DEBUG5("ddt_cmp: %s->alignment=%ld  !=  %s->alignment=%ld (on %s)",
1018              d1->name, d1->alignment[cpt], d2->name, d2->alignment[cpt],
1019              gras_arches[cpt].name);
1020       return d1->alignment[cpt] > d2->alignment[cpt] ? 1 : -1;
1021     }
1022
1023     if (d1->aligned_size[cpt] != d2->aligned_size[cpt]) {
1024       DEBUG5
1025           ("ddt_cmp: %s->aligned_size=%ld  !=  %s->aligned_size=%ld (on %s)",
1026            d1->name, d1->aligned_size[cpt], d2->name,
1027            d2->aligned_size[cpt], gras_arches[cpt].name);
1028       return d1->aligned_size[cpt] > d2->aligned_size[cpt] ? 1 : -1;
1029     }
1030   }
1031
1032   if (d1->category_code != d2->category_code) {
1033     DEBUG4("ddt_cmp: %s->cat=%s  !=  %s->cat=%s",
1034            d1->name, gras_datadesc_cat_names[d1->category_code],
1035            d2->name, gras_datadesc_cat_names[d2->category_code]);
1036     return d1->category_code > d2->category_code ? 1 : -1;
1037   }
1038
1039   if (d1->send != d2->send) {
1040     DEBUG4("ddt_cmp: %s->send=%p  !=  %s->send=%p",
1041            d1->name, (void *) d1->send, d2->name, (void *) d2->send);
1042     return 1;                   /* ISO C forbids ordered comparisons of pointers to functions */
1043   }
1044
1045   if (d1->recv != d2->recv) {
1046     DEBUG4("ddt_cmp: %s->recv=%p  !=  %s->recv=%p",
1047            d1->name, (void *) d1->recv, d2->name, (void *) d2->recv);
1048     return 1;                   /* ISO C forbids ordered comparisons of pointers to functions */
1049   }
1050
1051   switch (d1->category_code) {
1052   case e_gras_datadesc_type_cat_scalar:
1053     if (d1->category.scalar_data.encoding !=
1054         d2->category.scalar_data.encoding)
1055       return d1->category.scalar_data.encoding >
1056           d2->category.scalar_data.encoding ? 1 : -1;
1057     break;
1058
1059   case e_gras_datadesc_type_cat_struct:
1060     if (xbt_dynar_length(d1->category.struct_data.fields) !=
1061         xbt_dynar_length(d2->category.struct_data.fields)) {
1062       DEBUG4("ddt_cmp: %s (having %lu fields) !=  %s (having %lu fields)",
1063              d1->name, xbt_dynar_length(d1->category.struct_data.fields),
1064              d2->name, xbt_dynar_length(d2->category.struct_data.fields));
1065
1066       return xbt_dynar_length(d1->category.struct_data.fields) >
1067           xbt_dynar_length(d2->category.struct_data.fields) ? 1 : -1;
1068     }
1069     xbt_dynar_foreach(d1->category.struct_data.fields, cpt, field1) {
1070
1071       field2 =
1072           xbt_dynar_get_as(d2->category.struct_data.fields, cpt,
1073                            gras_dd_cat_field_t);
1074       field_desc_1 = field1->type;
1075       field_desc_2 = field2->type;
1076       ret = gras_datadesc_type_cmp(field_desc_1, field_desc_2);
1077       if (ret) {
1078         DEBUG6("%s->field[%d]=%s != %s->field[%d]=%s",
1079                d1->name, cpt, field1->name, d2->name, cpt, field2->name);
1080         return ret;
1081       }
1082
1083     }
1084     break;
1085
1086   case e_gras_datadesc_type_cat_union:
1087     if (d1->category.union_data.selector !=
1088         d2->category.union_data.selector)
1089       return 1;                 /* ISO C forbids ordered comparisons of pointers to functions */
1090
1091     if (xbt_dynar_length(d1->category.union_data.fields) !=
1092         xbt_dynar_length(d2->category.union_data.fields))
1093       return xbt_dynar_length(d1->category.union_data.fields) >
1094           xbt_dynar_length(d2->category.union_data.fields) ? 1 : -1;
1095
1096     xbt_dynar_foreach(d1->category.union_data.fields, cpt, field1) {
1097
1098       field2 =
1099           xbt_dynar_get_as(d2->category.union_data.fields, cpt,
1100                            gras_dd_cat_field_t);
1101       field_desc_1 = field1->type;
1102       field_desc_2 = field2->type;
1103       ret = gras_datadesc_type_cmp(field_desc_1, field_desc_2);
1104       if (ret)
1105         return ret;
1106
1107     }
1108     break;
1109
1110
1111   case e_gras_datadesc_type_cat_ref:
1112     if (d1->category.ref_data.selector != d2->category.ref_data.selector)
1113       return 1;                 /* ISO C forbids ordered comparisons of pointers to functions */
1114
1115     if (d1->category.ref_data.type != d2->category.ref_data.type)
1116       return d1->category.ref_data.type >
1117           d2->category.ref_data.type ? 1 : -1;
1118     break;
1119
1120   case e_gras_datadesc_type_cat_array:
1121     if (d1->category.array_data.type != d2->category.array_data.type)
1122       return d1->category.array_data.type >
1123           d2->category.array_data.type ? 1 : -1;
1124
1125     if (d1->category.array_data.fixed_size !=
1126         d2->category.array_data.fixed_size)
1127       return d1->category.array_data.fixed_size >
1128           d2->category.array_data.fixed_size ? 1 : -1;
1129
1130     if (d1->category.array_data.dynamic_size !=
1131         d2->category.array_data.dynamic_size)
1132       return 1;                 /* ISO C forbids ordered comparisons of pointers to functions */
1133
1134     break;
1135
1136   default:
1137     /* two stupidly created ddt are equally stupid ;) */
1138     break;
1139   }
1140   return 0;
1141
1142 }