1 /* ddt_new - creation/deletion of datatypes structs (private to this module)*/
3 /* Copyright (c) 2004, 2005, 2006, 2007, 2009, 2010. The SimGrid Team.
4 * All rights reserved. */
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. */
9 #include "xbt/misc.h" /* min()/max() */
11 #include "datadesc_private.h"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_ddt_create, xbt_ddt,
14 "Creating new data descriptions");
17 static xbt_dd_cat_field_t
18 xbt_dd_find_field(xbt_datadesc_type_t type, const char *field_name);
22 * gime that memory back, dude. I mean it.
24 void xbt_ddt_freev(void *ddt)
26 xbt_datadesc_type_t type = (xbt_datadesc_type_t) ddt;
29 xbt_datadesc_free(&type);
33 static xbt_datadesc_type_t xbt_ddt_new(const char *name)
35 xbt_datadesc_type_t res;
38 res = xbt_new0(s_xbt_datadesc_type_t, 1);
40 res->name = (char *) strdup(name);
41 res->name_len = strlen(name);
44 xbt_set_add(xbt_datadesc_set_local, (xbt_set_elm_t) res,
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)
54 xbt_datadesc_type_t res = NULL;
57 res = xbt_datadesc_by_name(name);
67 * Search the given datadesc (or raises an exception if it can't be found)
69 xbt_datadesc_type_t xbt_datadesc_by_name(const char *name)
72 xbt_datadesc_type_t res = NULL;
73 volatile int found = 0;
76 (xbt_datadesc_type_t) xbt_set_get_by_name(xbt_datadesc_set_local,
81 if (e.category != not_found_error)
86 THROWF(not_found_error, 0, "No registred datatype of that name: %s",
93 * Retrieve a type from its code (or NULL if not found)
95 xbt_datadesc_type_t xbt_datadesc_by_id(long int code)
98 xbt_datadesc_type_t res = NULL;
101 (xbt_datadesc_type_t) xbt_set_get_by_id(xbt_datadesc_set_local,
105 if (e.category != not_found_error)
114 * Create a new scalar and give a pointer to it
117 xbt_datadesc_scalar(const char *name,
118 xbt_ddt_scalar_type_t type,
119 enum e_xbt_dd_scalar_encoding encoding)
122 xbt_datadesc_type_t res;
126 res = xbt_datadesc_by_name_or_null(name);
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);
137 res = xbt_ddt_new(name);
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]);
146 res->category_code = e_xbt_datadesc_type_cat_scalar;
147 res->category.scalar_data.encoding = encoding;
148 res->category.scalar_data.type = type;
155 /** Frees one struct or union field */
156 void xbt_dd_cat_field_free(void *f)
158 xbt_dd_cat_field_t field = *(xbt_dd_cat_field_t *) f;
167 /** \brief Declare a new structure description */
168 xbt_datadesc_type_t xbt_datadesc_struct(const char *name)
171 xbt_datadesc_type_t res;
174 XBT_IN("(%s)", name);
175 res = xbt_datadesc_by_name_or_null(name);
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);
183 res = xbt_ddt_new(name);
185 for (arch = 0; arch < xbt_arch_count; arch++) {
187 res->alignment[arch] = 0;
188 res->aligned_size[arch] = 0;
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);
198 /** \brief Append a new field to a structure description */
200 xbt_datadesc_struct_append(xbt_datadesc_type_t struct_type,
202 xbt_datadesc_type_t field_type)
205 xbt_dd_cat_field_t field;
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) {
214 ("Ignoring request to add field to struct %s (closed. Redefinition?)",
219 xbt_assert(field_type->size[GRAS_THISARCH] >= 0,
220 "Cannot add a dynamically sized field in structure %s",
223 field = xbt_new(s_xbt_dd_cat_field_t, 1);
224 field->name = (char *) strdup(name);
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]);
233 for (arch = 0; arch < xbt_arch_count; arch++) {
234 field->offset[arch] = ddt_aligned(struct_type->size[arch],
235 field_type->alignment[arch]);
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
244 field->type = field_type;
248 xbt_dynar_push(struct_type->category.struct_data.fields, &field);
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]);
264 /** \brief Close a structure description
266 * No new field can be added afterward, and it is mandatory to close the structure before using it.
268 void xbt_datadesc_struct_close(xbt_datadesc_type_t struct_type)
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];
276 XBT_DEBUG("structure %s closed. size=%ld,align=%lu,asize=%lu",
278 struct_type->size[GRAS_THISARCH],
279 struct_type->alignment[GRAS_THISARCH],
280 struct_type->aligned_size[GRAS_THISARCH]);
284 * xbt_datadesc_cycle_set:
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.
289 * Note that setting this option when not needed have a rather bad effect
290 * on the performance (several times slower on big data).
292 void xbt_datadesc_cycle_set(xbt_datadesc_type_t ddt)
298 * xbt_datadesc_cycle_unset:
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.
304 void xbt_datadesc_cycle_unset(xbt_datadesc_type_t ddt)
309 /** \brief Declare a new union description */
311 xbt_datadesc_union(const char *name, xbt_datadesc_type_cb_int_t selector)
314 xbt_datadesc_type_t res;
317 XBT_IN("(%s)", name);
319 "Attempt to creat an union without field_count function");
321 res = xbt_datadesc_by_name_or_null(name);
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);
332 res = xbt_ddt_new(name);
334 for (arch = 0; arch < xbt_arch_count; arch++) {
336 res->alignment[arch] = 0;
337 res->aligned_size[arch] = 0;
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;
348 /** \brief Append a new field to an union description */
349 void xbt_datadesc_union_append(xbt_datadesc_type_t union_type,
351 xbt_datadesc_type_t field_type)
354 xbt_dd_cat_field_t field;
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",
362 if (union_type->category.union_data.closed) {
363 XBT_VERB("Ignoring request to add field to union %s (closed)",
368 field = xbt_new0(s_xbt_dd_cat_field_t, 1);
370 field->name = (char *) strdup(name);
371 field->type = field_type;
372 /* All offset are left to 0 in an union */
374 xbt_dynar_push(union_type->category.union_data.fields, &field);
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
388 /** \brief Close an union description
390 * No new field can be added afterward, and it is mandatory to close the union before using it.
392 void xbt_datadesc_union_close(xbt_datadesc_type_t union_type)
394 union_type->category.union_data.closed = 1;
397 /** \brief Copy a type under another name
399 * This may reveal useful to circumvent parsing macro limitations
402 xbt_datadesc_copy(const char *name, xbt_datadesc_type_t copied)
405 xbt_datadesc_type_t res = xbt_ddt_new(name);
406 char *name_cpy = res->name;
408 memcpy(res, copied, sizeof(s_xbt_datadesc_type_t));
409 res->name = name_cpy;
413 /** \brief Declare a new type being a reference to the one passed in arg */
415 xbt_datadesc_ref(const char *name, xbt_datadesc_type_t referenced_type)
418 xbt_datadesc_type_t res;
419 xbt_datadesc_type_t pointer_type =
420 xbt_datadesc_by_name("data pointer");
423 XBT_IN("(%s)", name);
424 res = xbt_datadesc_by_name_or_null(name);
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);
436 res = xbt_ddt_new(name);
438 xbt_assert(pointer_type, "Cannot get the description of data pointer");
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];
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;
453 /** \brief Declare a new type being a generic reference.
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.
460 xbt_datadesc_ref_generic(const char *name,
461 xbt_datadesc_selector_t selector)
464 xbt_datadesc_type_t res;
465 xbt_datadesc_type_t pointer_type =
466 xbt_datadesc_by_name("data pointer");
469 XBT_IN("(%s)", name);
470 res = xbt_datadesc_by_name_or_null(name);
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);
482 res = xbt_ddt_new(name);
484 xbt_assert(pointer_type, "Cannot get the description of data pointer");
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];
492 res->category_code = e_xbt_datadesc_type_cat_ref;
494 res->category.ref_data.type = NULL;
495 res->category.ref_data.selector = selector;
500 /** \brief Declare a new type being an array of fixed size and content */
502 xbt_datadesc_array_fixed(const char *name,
503 xbt_datadesc_type_t element_type,
507 xbt_datadesc_type_t res;
510 XBT_IN("(%s)", name);
511 res = xbt_datadesc_by_name_or_null(name);
513 xbt_assert(res->category_code == e_xbt_datadesc_type_cat_array,
514 "Redefinition of type %s does not match", name);
516 if (res->category.array_data.type != element_type) {
518 ("Redefinition of type %s does not match: array elements differ",
520 xbt_datadesc_type_dump(res->category.array_data.type);
521 xbt_datadesc_type_dump(element_type);
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);
532 res = xbt_ddt_new(name);
534 xbt_assert(fixed_size >= 0, "'%s' is a array of negative fixed size",
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];
542 res->category_code = e_xbt_datadesc_type_cat_array;
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;
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,
555 xbt_datadesc_type_cb_int_t
559 xbt_datadesc_type_t res;
562 XBT_IN("(%s)", name);
563 xbt_assert(dynamic_size,
564 "'%s' is a dynamic array without size discriminant", name);
566 res = xbt_datadesc_by_name_or_null(name);
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);
581 res = xbt_ddt_new(name);
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 */
589 res->category_code = e_xbt_datadesc_type_cat_array;
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;
598 /** \brief Declare a new type being an array which size can be found with \ref xbt_cbps_i_pop
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
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.
608 * The name of the created datatype will be the name of the element type, with
609 * '[]*' appended to it.
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
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.
621 xbt_datadesc_ref_pop_arr(xbt_datadesc_type_t element_type)
624 xbt_datadesc_type_t res, ddt2;
625 char *name = (char *) xbt_malloc(strlen(element_type->name) + 4);
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);
633 name = bprintf("%s[]_%d", element_type->name, cpt++);
634 ddt2 = xbt_datadesc_by_name_or_null(name);
637 res = xbt_datadesc_array_dyn(name, element_type, xbt_datadesc_cb_pop);
639 sprintf(name, "%s[]*", element_type->name);
641 ddt2 = xbt_datadesc_by_name_or_null(name);
644 name = bprintf("%s[]*_%d", element_type->name, cpt++);
645 ddt2 = xbt_datadesc_by_name_or_null(name);
648 res = xbt_datadesc_ref(name, res);
657 *## Constructor of container datatypes
661 static void xbt_datadesc_dynar_cb(xbt_datadesc_type_t typedesc,
662 xbt_cbps_t vars, void *data)
664 xbt_datadesc_type_t subtype;
665 xbt_dynar_t dynar = (xbt_dynar_t) data;
667 memcpy(&dynar->free_f, &typedesc->extra, sizeof(dynar->free_f));
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;
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;
676 XBT_DEBUG("subtype is %s", subtype->name);
678 dynar->elmsize = subtype->size[GRAS_THISARCH];
679 dynar->size = dynar->used;
683 /** \brief Declare a new type being a dynar in which each elements are of the given type
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".
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
693 xbt_datadesc_dynar(xbt_datadesc_type_t elm_t, void_f_pvoid_t free_func)
697 xbt_datadesc_type_t res;
699 buffname = bprintf("s_xbt_dynar_of_%s", elm_t->name);
701 res = xbt_datadesc_struct(buffname);
703 xbt_datadesc_struct_append(res, "size",
704 xbt_datadesc_by_name("unsigned long int"));
706 xbt_datadesc_struct_append(res, "used",
707 xbt_datadesc_by_name("unsigned long int"));
709 xbt_datadesc_struct_append(res, "elmsize",
710 xbt_datadesc_by_name("unsigned long int"));
712 xbt_datadesc_struct_append(res, "data",
713 xbt_datadesc_ref_pop_arr(elm_t));
715 xbt_datadesc_struct_append(res, "free_f",
716 xbt_datadesc_by_name("function pointer"));
717 memcpy(res->extra, &free_func, sizeof(free_func));
719 xbt_datadesc_struct_append(res, "mutex",
720 xbt_datadesc_by_name("data pointer"));
722 xbt_datadesc_struct_close(res);
724 xbt_datadesc_cb_field_push(res, "used");
725 xbt_datadesc_cb_recv(res, &xbt_datadesc_dynar_cb);
727 /* build a ref to it */
729 buffname = bprintf("xbt_dynar_of_%s", elm_t->name);
730 res = xbt_datadesc_ref(buffname, res);
735 #include "xbt/matrix.h"
736 static void xbt_datadesc_matrix_cb(xbt_datadesc_type_t typedesc,
737 xbt_cbps_t vars, void *data)
739 xbt_datadesc_type_t subtype;
740 xbt_matrix_t matrix = (xbt_matrix_t) data;
742 memcpy(&matrix->free_f, &typedesc->extra, sizeof(matrix->free_f));
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;
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;
751 XBT_DEBUG("subtype is %s", subtype->name);
753 matrix->elmsize = subtype->size[GRAS_THISARCH];
757 xbt_datadesc_matrix(xbt_datadesc_type_t elm_t,
758 void_f_pvoid_t const free_f)
761 xbt_datadesc_type_t res;
763 buffname = bprintf("s_xbt_matrix_t(%s)", elm_t->name);
764 res = xbt_datadesc_struct(buffname);
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"));
771 xbt_datadesc_struct_append(res, "elmsize",
772 xbt_datadesc_by_name("unsigned long int"));
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);
780 xbt_datadesc_cb_field_push(res, "lines");
781 xbt_datadesc_cb_field_push_multiplier(res, "rows");
783 xbt_datadesc_cb_recv(res, &xbt_datadesc_matrix_cb);
784 memcpy(res->extra, &free_f, sizeof(free_f));
786 /* build a ref to it */
788 buffname = bprintf("xbt_matrix_t(%s)", elm_t->name);
789 res = xbt_datadesc_ref(buffname, res);
795 xbt_datadesc_import_nws(const char *name,
796 const DataDescriptor * desc,
797 unsigned long howmany)
803 * (useful to push the sizes of the upcoming arrays, for example)
805 void xbt_datadesc_cb_send(xbt_datadesc_type_t type,
806 xbt_datadesc_type_cb_void_t send)
812 * (useful to put the function pointers to the rigth value, for example)
814 void xbt_datadesc_cb_recv(xbt_datadesc_type_t type,
815 xbt_datadesc_type_cb_void_t recv)
823 * Returns the type descriptor of the given field. Abort on error.
825 static xbt_dd_cat_field_t
826 xbt_dd_find_field(xbt_datadesc_type_t type, const char *field_name)
828 xbt_dynar_t field_array;
830 xbt_dd_cat_field_t field = NULL;
831 unsigned int field_num;
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;
838 XBT_ERROR("%s (%p) is not a struct nor an union. There is no field.",
839 type->name, (void *) type);
842 xbt_dynar_foreach(field_array, field_num, field) {
843 if (!strcmp(field_name, field->name)) {
847 XBT_ERROR("No field named '%s' in '%s'", field_name, type->name);
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)
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)
861 xbt_dd_cat_field_t field = xbt_dd_find_field(type, field_name);
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.
871 void xbt_datadesc_cb_field_push(xbt_datadesc_type_t type,
872 const char *field_name)
875 xbt_dd_cat_field_t field = xbt_dd_find_field(type, field_name);
876 xbt_datadesc_type_t sub_type = field->type;
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;
890 ("Field %s is not an int, unsigned int, long int neither unsigned long int",
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
902 * The field must be an int, unsigned int, long int or unsigned long int.
904 void xbt_datadesc_cb_field_push_multiplier(xbt_datadesc_type_t type,
905 const char *field_name)
908 xbt_dd_cat_field_t field = xbt_dd_find_field(type, field_name);
909 xbt_datadesc_type_t sub_type = field->type;
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;
923 ("Field %s is not an int, unsigned int, long int neither unsigned long int",
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)
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)
938 xbt_dd_cat_field_t field = xbt_dd_find_field(type, field_name);
943 * Free a datadesc. Should only be called at xbt_exit.
945 void xbt_datadesc_free(xbt_datadesc_type_t * type)
948 XBT_DEBUG("Let's free ddt %s", (*type)->name);
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 */
957 case e_xbt_datadesc_type_cat_struct:
958 xbt_dynar_free(&((*type)->category.struct_data.fields));
961 case e_xbt_datadesc_type_cat_union:
962 xbt_dynar_free(&((*type)->category.union_data.fields));
966 /* datadesc was invalid. Killing it is like euthanasy, I guess */
975 * xbt_datadesc_type_cmp:
977 * Compares two datadesc types with the same semantic than strcmp.
979 * This comparison does not take the set headers into account (name and ID),
980 * but only the payload (actual type description).
982 int xbt_datadesc_type_cmp(const xbt_datadesc_type_t d1,
983 const xbt_datadesc_type_t d2)
987 xbt_dd_cat_field_t field1, field2;
988 xbt_datadesc_type_t field_desc_1, field_desc_2;
991 return 0; /* easy optimization */
994 XBT_DEBUG("ddt_cmp: !d1 && d2 => 1");
998 XBT_DEBUG("ddt_cmp: !d1 && !d2 => 0");
1002 XBT_DEBUG("ddt_cmp: d1 && !d2 => -1");
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;
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;
1021 if (d1->aligned_size[cpt] != d2->aligned_size[cpt]) {
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;
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;
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 */
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 */
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;
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));
1064 return xbt_dynar_length(d1->category.struct_data.fields) >
1065 xbt_dynar_length(d2->category.struct_data.fields) ? 1 : -1;
1067 xbt_dynar_foreach(d1->category.struct_data.fields, cpt, field1) {
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);
1076 XBT_DEBUG("%s->field[%u]=%s != %s->field[%u]=%s",
1077 d1->name, cpt, field1->name, d2->name, cpt, field2->name);
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 */
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;
1094 xbt_dynar_foreach(d1->category.union_data.fields, cpt, field1) {
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);
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 */
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;
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;
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;
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 */
1135 /* two stupidly created ddt are equally stupid ;) */