Logo AND Algorithmique Numérique Distribuée

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