Logo AND Algorithmique Numérique Distribuée

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