3 /* ddt_new - creation/deletion of datatypes structs (private to this module)*/
5 /* Copyright (c) 2003 Olivier Aumage. */
6 /* Copyright (c) 2003, 2004 Martin Quinson. */
7 /* All rights reserved. */
9 /* This program is free software; you can redistribute it and/or modify it
10 * under the terms of the license (GNU LGPL) which comes with this package. */
12 #include "xbt/misc.h" /* min()/max() */
14 #include "gras/DataDesc/datadesc_private.h"
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ddt_create,datadesc,"Creating new datadescriptions");
19 static gras_dd_cat_field_t
20 gras_dd_find_field(gras_datadesc_type_t type,
21 const char *field_name);
25 * gime that memory back, dude. I mean it.
27 void gras_ddt_freev(void *ddt) {
28 gras_datadesc_type_t type= (gras_datadesc_type_t)ddt;
31 gras_datadesc_free(&type);
35 static gras_datadesc_type_t gras_ddt_new(const char *name) {
36 gras_datadesc_type_t res;
39 res=xbt_new0(s_gras_datadesc_type_t,1);
41 res->name = (char*)strdup(name);
42 res->name_len = strlen(name);
45 xbt_set_add(gras_datadesc_set_local,
46 (xbt_set_elm_t)res,&gras_ddt_freev);
52 * This returns NULL no type of this name can be found instead of throwing exceptions which would complicate the GRAS_DEFINE_TYPE macro
54 gras_datadesc_type_t gras_datadesc_by_name(const char *name) {
56 gras_datadesc_type_t res = NULL;
58 res = (gras_datadesc_type_t)xbt_set_get_by_name(gras_datadesc_set_local,name);
60 if (e.category != not_found_error)
69 * Retrieve a type from its code (or NULL if not found)
71 gras_datadesc_type_t gras_datadesc_by_id(long int code) {
73 gras_datadesc_type_t res=NULL;
75 res = (gras_datadesc_type_t)xbt_set_get_by_id(gras_datadesc_set_local,code);
77 if (e.category != not_found_error)
86 * Create a new scalar and give a pointer to it
89 gras_datadesc_scalar(const char *name,
90 gras_ddt_scalar_type_t type,
91 enum e_gras_dd_scalar_encoding encoding) {
93 gras_datadesc_type_t res;
97 res = gras_datadesc_by_name(name);
99 xbt_assert1(res->category_code == e_gras_datadesc_type_cat_scalar,
100 "Redefinition of type %s does not match", name);
101 xbt_assert1(res->category.scalar_data.encoding == encoding,
102 "Redefinition of type %s does not match", name);
103 xbt_assert1(res->category.scalar_data.type == type,
104 "Redefinition of type %s does not match", name);
105 VERB1("Discarding redefinition of %s",name);
108 res = gras_ddt_new(name);
110 for (arch = 0; arch < gras_arch_count; arch ++) {
111 res->size[arch] = gras_arches[arch].sizeofs[type];
112 res->alignment[arch] = gras_arches[arch].boundaries[type];
113 res->aligned_size[arch] = aligned(res->size[arch], res->alignment[arch]);
116 res->category_code = e_gras_datadesc_type_cat_scalar;
117 res->category.scalar_data.encoding = encoding;
118 res->category.scalar_data.type = type;
125 /** Frees one struct or union field */
126 void gras_dd_cat_field_free(void *f) {
127 gras_dd_cat_field_t field = *(gras_dd_cat_field_t *)f;
137 /** \brief Declare a new structure description */
139 gras_datadesc_struct(const char *name) {
141 gras_datadesc_type_t res;
144 XBT_IN1("(%s)",name);
145 res = gras_datadesc_by_name(name);
147 /* FIXME: Check that field redefinition matches */
148 xbt_assert1(res->category_code == e_gras_datadesc_type_cat_struct,
149 "Redefinition of type %s does not match", name);
150 VERB1("Discarding redefinition of %s",name);
153 res = gras_ddt_new(name);
155 for (arch=0; arch<gras_arch_count; arch ++) {
157 res->alignment[arch] = 0;
158 res->aligned_size[arch] = 0;
160 res->category_code = e_gras_datadesc_type_cat_struct;
161 res->category.struct_data.fields =
162 xbt_dynar_new(sizeof(gras_dd_cat_field_t),
163 &gras_dd_cat_field_free);
169 /** \brief Append a new field to a structure description */
171 gras_datadesc_struct_append(gras_datadesc_type_t struct_type,
173 gras_datadesc_type_t field_type) {
175 gras_dd_cat_field_t field;
178 xbt_assert2(field_type,
179 "Cannot add the field '%s' into struct '%s': its type is NULL. Typo in get_by_name?",
180 name,struct_type->name);
181 XBT_IN3("(%s %s.%s;)",field_type->name,struct_type->name,name);
182 if (struct_type->category.struct_data.closed) {
183 VERB1("Ignoring request to add field to struct %s (closed. Redefinition?)",
188 xbt_assert1(field_type->size != 0,
189 "Cannot add a dynamically sized field in structure %s",
192 field=xbt_new(s_gras_dd_cat_field_t,1);
193 field->name = (char*)strdup(name);
195 DEBUG0("----------------");
196 DEBUG3("PRE s={size=%ld,align=%ld,asize=%ld}",
197 struct_type->size[GRAS_THISARCH],
198 struct_type->alignment[GRAS_THISARCH],
199 struct_type->aligned_size[GRAS_THISARCH]);
202 for (arch=0; arch<gras_arch_count; arch ++) {
203 field->offset[arch] = aligned(struct_type->size[arch],
204 field_type->alignment[arch]);
206 struct_type->size[arch] = field->offset[arch] + field_type->size[arch];
207 struct_type->alignment[arch] = max(struct_type->alignment[arch],
208 field_type->alignment[arch]);
209 struct_type->aligned_size[arch] = aligned(struct_type->size[arch],
210 struct_type->alignment[arch]);
212 field->type = field_type;
216 xbt_dynar_push(struct_type->category.struct_data.fields, &field);
218 DEBUG3("Push a %s into %s at offset %ld.",
219 field_type->name, struct_type->name,field->offset[GRAS_THISARCH]);
220 DEBUG3(" f={size=%ld,align=%ld,asize=%ld}",
221 field_type->size[GRAS_THISARCH],
222 field_type->alignment[GRAS_THISARCH],
223 field_type->aligned_size[GRAS_THISARCH]);
224 DEBUG3(" s={size=%ld,align=%ld,asize=%ld}",
225 struct_type->size[GRAS_THISARCH],
226 struct_type->alignment[GRAS_THISARCH],
227 struct_type->aligned_size[GRAS_THISARCH]);
231 /** \brief Close a structure description
233 * No new field can be added afterward, and it is mandatory to close the structure before using it.
236 gras_datadesc_struct_close(gras_datadesc_type_t struct_type) {
238 struct_type->category.struct_data.closed = 1;
239 DEBUG4("structure %s closed. size=%ld,align=%ld,asize=%ld",
241 struct_type->size[GRAS_THISARCH],
242 struct_type->alignment[GRAS_THISARCH],
243 struct_type->aligned_size[GRAS_THISARCH]);
247 * gras_datadesc_cycle_set:
249 * Tell GRAS that the pointers of the type described by ddt may present
250 * some loop, and that the cycle detection mechanism is needed.
252 * Note that setting this option when not needed have a rather bad effect
253 * on the performance (several times slower on big data).
256 gras_datadesc_cycle_set(gras_datadesc_type_t ddt) {
261 * gras_datadesc_cycle_unset:
263 * Tell GRAS that the pointers of the type described by ddt do not present
264 * any loop and that cycle detection mechanism are not needed.
268 gras_datadesc_cycle_unset(gras_datadesc_type_t ddt) {
272 /** \brief Declare a new union description */
274 gras_datadesc_union(const char *name,
275 gras_datadesc_type_cb_int_t selector) {
277 gras_datadesc_type_t res;
280 XBT_IN1("(%s)",name);
281 xbt_assert0(selector,
282 "Attempt to creat an union without field_count function");
284 res = gras_datadesc_by_name(name);
286 /* FIXME: Check that field redefinition matches */
287 xbt_assert1(res->category_code == e_gras_datadesc_type_cat_union,
288 "Redefinition of type %s does not match", name);
289 xbt_assert1(res->category.union_data.selector == selector,
290 "Redefinition of type %s does not match", name);
291 VERB1("Discarding redefinition of %s",name);
295 res = gras_ddt_new(name);
297 for (arch=0; arch<gras_arch_count; arch ++) {
299 res->alignment[arch] = 0;
300 res->aligned_size[arch] = 0;
303 res->category_code = e_gras_datadesc_type_cat_union;
304 res->category.union_data.fields =
305 xbt_dynar_new(sizeof(gras_dd_cat_field_t*),
306 &gras_dd_cat_field_free);
307 res->category.union_data.selector = selector;
312 /** \brief Append a new field to an union description */
313 void gras_datadesc_union_append(gras_datadesc_type_t union_type,
315 gras_datadesc_type_t field_type) {
317 gras_dd_cat_field_t field;
320 XBT_IN3("(%s %s.%s;)",field_type->name,union_type->name,name);
321 xbt_assert1(field_type->size != 0,
322 "Cannot add a dynamically sized field in union %s",
325 if (union_type->category.union_data.closed) {
326 VERB1("Ignoring request to add field to union %s (closed)",
331 field=xbt_new0(s_gras_dd_cat_field_t,1);
333 field->name = (char*)strdup(name);
334 field->type = field_type;
335 /* All offset are left to 0 in an union */
337 xbt_dynar_push(union_type->category.union_data.fields, &field);
339 for (arch=0; arch<gras_arch_count; arch ++) {
340 union_type->size[arch] = max(union_type->size[arch],
341 field_type->size[arch]);
342 union_type->alignment[arch] = max(union_type->alignment[arch],
343 field_type->alignment[arch]);
344 union_type->aligned_size[arch] = aligned(union_type->size[arch],
345 union_type->alignment[arch]);
350 /** \brief Close an union description
352 * No new field can be added afterward, and it is mandatory to close the union before using it.
355 gras_datadesc_union_close(gras_datadesc_type_t union_type) {
356 union_type->category.union_data.closed = 1;
359 /** \brief Declare a new type being a reference to the one passed in arg */
361 gras_datadesc_ref(const char *name,
362 gras_datadesc_type_t referenced_type) {
364 gras_datadesc_type_t res;
365 gras_datadesc_type_t pointer_type = gras_datadesc_by_name("data pointer");
368 XBT_IN1("(%s)",name);
369 res = gras_datadesc_by_name(name);
371 xbt_assert1(res->category_code == e_gras_datadesc_type_cat_ref,
372 "Redefinition of %s does not match",name);
373 xbt_assert1(res->category.ref_data.type == referenced_type,
374 "Redefinition of %s does not match",name);
375 xbt_assert1(res->category.ref_data.selector == NULL,
376 "Redefinition of %s does not match",name);
377 VERB1("Discarding redefinition of %s",name);
381 res = gras_ddt_new(name);
383 xbt_assert0(pointer_type, "Cannot get the description of data pointer");
385 for (arch=0; arch<gras_arch_count; arch ++){
386 res->size[arch] = pointer_type->size[arch];
387 res->alignment[arch] = pointer_type->alignment[arch];
388 res->aligned_size[arch] = pointer_type->aligned_size[arch];
391 res->category_code = e_gras_datadesc_type_cat_ref;
392 res->category.ref_data.type = referenced_type;
393 res->category.ref_data.selector = NULL;
397 /** \brief Declare a new type being a generic reference.
399 * The callback passed in argument is to be used to select which type is currently used.
400 * So, when GRAS wants to send a generic reference, it passes the current data to the selector
401 * callback and expects it to return the type description to use.
404 gras_datadesc_ref_generic(const char *name,
405 gras_datadesc_selector_t selector) {
407 gras_datadesc_type_t res;
408 gras_datadesc_type_t pointer_type = gras_datadesc_by_name("data pointer");
411 XBT_IN1("(%s)",name);
412 res = gras_datadesc_by_name(name);
415 xbt_assert1(res->category_code == e_gras_datadesc_type_cat_ref,
416 "Redefinition of type %s does not match", name);
417 xbt_assert1(res->category.ref_data.type == NULL,
418 "Redefinition of type %s does not match", name);
419 xbt_assert1(res->category.ref_data.selector == selector,
420 "Redefinition of type %s does not match", name);
421 VERB1("Discarding redefinition of %s",name);
424 res = gras_ddt_new(name);
426 xbt_assert0(pointer_type, "Cannot get the description of data pointer");
428 for (arch=0; arch<gras_arch_count; arch ++) {
429 res->size[arch] = pointer_type->size[arch];
430 res->alignment[arch] = pointer_type->alignment[arch];
431 res->aligned_size[arch] = pointer_type->aligned_size[arch];
434 res->category_code = e_gras_datadesc_type_cat_ref;
436 res->category.ref_data.type = NULL;
437 res->category.ref_data.selector = selector;
442 /** \brief Declare a new type being an array of fixed size and content */
444 gras_datadesc_array_fixed(const char *name,
445 gras_datadesc_type_t element_type,
446 long int fixed_size) {
448 gras_datadesc_type_t res;
451 XBT_IN1("(%s)",name);
452 res = gras_datadesc_by_name(name);
454 xbt_assert1(res->category_code == e_gras_datadesc_type_cat_array,
455 "Redefinition of type %s does not match", name);
457 if (res->category.array_data.type != element_type) {
458 ERROR1("Redefinition of type %s does not match: array elements differ", name);
459 gras_datadesc_type_dump(res->category.array_data.type);
460 gras_datadesc_type_dump(element_type);
463 xbt_assert1(res->category.array_data.fixed_size == fixed_size,
464 "Redefinition of type %s does not match", name);
465 xbt_assert1(res->category.array_data.dynamic_size == NULL,
466 "Redefinition of type %s does not match", name);
467 VERB1("Discarding redefinition of %s",name);
471 res = gras_ddt_new(name);
473 xbt_assert1(fixed_size > 0, "'%s' is a array of null fixed size",name);
474 for (arch=0; arch<gras_arch_count; arch ++) {
475 res->size[arch] = fixed_size * element_type->aligned_size[arch];
476 res->alignment[arch] = element_type->alignment[arch];
477 res->aligned_size[arch] = res->size[arch];
480 res->category_code = e_gras_datadesc_type_cat_array;
482 res->category.array_data.type = element_type;
483 res->category.array_data.fixed_size = fixed_size;
484 res->category.array_data.dynamic_size = NULL;
489 /** \brief Declare a new type being an array of fixed size, but accepting several content types. */
490 gras_datadesc_type_t gras_datadesc_array_dyn(const char *name,
491 gras_datadesc_type_t element_type,
492 gras_datadesc_type_cb_int_t dynamic_size) {
494 gras_datadesc_type_t res;
497 XBT_IN1("(%s)",name);
498 xbt_assert1(dynamic_size,
499 "'%s' is a dynamic array without size discriminant",
502 res = gras_datadesc_by_name(name);
504 xbt_assert1(res->category_code == e_gras_datadesc_type_cat_array,
505 "Redefinition of type %s does not match", name);
506 xbt_assert1(res->category.array_data.type == element_type,
507 "Redefinition of type %s does not match", name);
508 xbt_assert1(res->category.array_data.fixed_size == 0,
509 "Redefinition of type %s does not match", name);
510 xbt_assert1(res->category.array_data.dynamic_size == dynamic_size,
511 "Redefinition of type %s does not match", name);
512 VERB1("Discarding redefinition of %s",name);
517 res = gras_ddt_new(name);
519 for (arch=0; arch<gras_arch_count; arch ++) {
520 res->size[arch] = 0; /* make sure it indicates "dynamic" */
521 res->alignment[arch] = element_type->alignment[arch];
522 res->aligned_size[arch] = 0; /*FIXME: That was so in GS, but looks stupid*/
525 res->category_code = e_gras_datadesc_type_cat_array;
527 res->category.array_data.type = element_type;
528 res->category.array_data.fixed_size = 0;
529 res->category.array_data.dynamic_size = dynamic_size;
534 /** \brief Declare a new type being an array which size can be found with \ref gras_cbps_i_pop
536 * Most of the time, you want to include a reference in your structure which
537 * is a pointer to a dynamic array whose size is fixed by another field of
540 * This case pops up so often that this function was created to take care of
541 * this case. It creates a dynamic array type whose size is poped from the
542 * current cbps, and then create a reference to it.
544 * The name of the created datatype will be the name of the element type, with
545 * '[]*' appended to it.
547 * Then to use it, you just have to make sure that your structure pre-callback
548 * does push the size of the array in the cbps (using #gras_cbps_i_push), and
551 * But be remember that this is a stack. If you have two different pop_arr, you
552 * should push the second one first, so that the first one is on the top of the
553 * list when the first field gets transfered.
557 gras_datadesc_ref_pop_arr(gras_datadesc_type_t element_type) {
559 gras_datadesc_type_t res;
560 char *name=(char*)xbt_malloc(strlen(element_type->name) + 4);
562 sprintf(name,"%s[]",element_type->name);
564 res = gras_datadesc_array_dyn(name,element_type,
565 gras_datadesc_cb_pop);
567 sprintf(name,"%s[]*",element_type->name);
568 res = gras_datadesc_ref(name,res);
575 #include "xbt/dynar_private.h"
576 static void gras_datadesc_dynar_cb(gras_datadesc_type_t typedesc, gras_cbps_t vars, void *data) {
577 gras_datadesc_type_t subtype;
578 xbt_dynar_t dynar=(xbt_dynar_t)data;
580 memcpy(&dynar->free_f, &typedesc->extra, sizeof(dynar->free_f));
582 /* search for the elemsize in what we have. If elements are "int", typedesc got is "int[]*" */
583 subtype = gras_dd_find_field(typedesc,"data")->type;
585 /* this is now a ref to array of what we're looking for */
586 subtype = subtype->category.ref_data.type;
587 subtype = subtype->category.array_data.type;
589 DEBUG1("subtype is %s",subtype->name);
591 dynar->elmsize = subtype->size[GRAS_THISARCH];
592 dynar->size = dynar->used;
595 /** \brief Declare a new type being a dynar in which each elements are of the given type
597 * \param elm_t: the datadesc of the elements
598 * \param free_func: the function to use to free the elements when the dynar gets freed
601 gras_datadesc_dynar(gras_datadesc_type_t elm_t,
602 void_f_pvoid_t *free_func) {
605 gras_datadesc_type_t res;
607 buffname=xbt_new0(char, strlen(elm_t->name)+10);
608 sprintf(buffname,"dynar(%s)_s",elm_t->name);
610 res = gras_datadesc_struct(buffname);
612 gras_datadesc_struct_append(res, "size", gras_datadesc_by_name("unsigned long int"));
614 gras_datadesc_struct_append(res, "used", gras_datadesc_by_name("unsigned long int"));
615 gras_datadesc_cb_field_push(res, "used");
617 gras_datadesc_struct_append(res, "elmsize", gras_datadesc_by_name("unsigned long int"));
619 gras_datadesc_struct_append(res, "data", gras_datadesc_ref_pop_arr (elm_t));
621 gras_datadesc_struct_append(res, "free_f", gras_datadesc_by_name("function pointer"));
622 memcpy(res->extra,&free_func,sizeof(free_func));
624 gras_datadesc_struct_close(res);
626 gras_datadesc_cb_recv(res, &gras_datadesc_dynar_cb);
628 /* build a ref to it */
629 sprintf(buffname,"dynar(%s)",elm_t->name);
630 res=gras_datadesc_ref(buffname,res);
637 gras_datadesc_import_nws(const char *name,
638 const DataDescriptor *desc,
639 unsigned long howmany) {
644 * (useful to push the sizes of the upcoming arrays, for example)
646 void gras_datadesc_cb_send (gras_datadesc_type_t type,
647 gras_datadesc_type_cb_void_t send) {
651 * (useful to put the function pointers to the rigth value, for example)
653 void gras_datadesc_cb_recv(gras_datadesc_type_t type,
654 gras_datadesc_type_cb_void_t recv) {
658 * gras_dd_find_field:
660 * Returns the type descriptor of the given field. Abort on error.
662 static gras_dd_cat_field_t
663 gras_dd_find_field(gras_datadesc_type_t type,
664 const char *field_name) {
665 xbt_dynar_t field_array;
667 gras_dd_cat_field_t field=NULL;
670 if (type->category_code == e_gras_datadesc_type_cat_union) {
671 field_array = type->category.union_data.fields;
672 } else if (type->category_code == e_gras_datadesc_type_cat_struct) {
673 field_array = type->category.struct_data.fields;
675 ERROR2("%s (%p) is not a struct nor an union. There is no field.", type->name,(void*)type);
678 xbt_dynar_foreach(field_array,field_num,field) {
679 if (!strcmp(field_name,field->name)) {
683 ERROR2("No field nammed %s in %s",field_name,type->name);
689 * The given datadesc must be a struct or union (abort if not).
690 * (useful to push the sizes of the upcoming arrays, for example)
692 void gras_datadesc_cb_field_send (gras_datadesc_type_t type,
693 const char *field_name,
694 gras_datadesc_type_cb_void_t send) {
696 gras_dd_cat_field_t field=gras_dd_find_field(type,field_name);
702 * The value, which must be an int, unsigned int, long int or unsigned long int
703 * is pushed to the stacks of sizes and can then be retrieved with
704 * \ref gras_datadesc_ref_pop_arr or directly with \ref gras_cbps_i_pop.
706 void gras_datadesc_cb_field_push (gras_datadesc_type_t type,
707 const char *field_name) {
709 gras_dd_cat_field_t field=gras_dd_find_field(type,field_name);
710 gras_datadesc_type_t sub_type=field->type;
712 DEBUG3("add a PUSHy cb to '%s' field (type '%s') of '%s'",
713 field_name,sub_type->name,type->name);
714 if (!strcmp("int",sub_type->name)) {
715 field->send = gras_datadesc_cb_push_int;
716 } else if (!strcmp("unsigned int",sub_type->name)) {
717 field->send = gras_datadesc_cb_push_uint;
718 } else if (!strcmp("long int",sub_type->name)) {
719 field->send = gras_datadesc_cb_push_lint;
720 } else if (!strcmp("unsigned long int",sub_type->name)) {
721 field->send = gras_datadesc_cb_push_ulint;
723 ERROR1("Field %s is not an int, unsigned int, long int neither unsigned long int",
730 * Any previously pushed value is poped and the field value is multiplied to
731 * it. The result is then pushed back into the stack of sizes. It can then be
732 * retrieved with \ref gras_datadesc_ref_pop_arr or directly with \ref
735 * The field must be an int, unsigned int, long int or unsigned long int.
737 void gras_datadesc_cb_field_push_multiplier (gras_datadesc_type_t type,
738 const char *field_name) {
740 gras_dd_cat_field_t field=gras_dd_find_field(type,field_name);
741 gras_datadesc_type_t sub_type=field->type;
743 DEBUG3("add a MPUSHy cb to '%s' field (type '%s') of '%s'",
744 field_name,sub_type->name,type->name);
745 if (!strcmp("int",sub_type->name)) {
746 field->send = gras_datadesc_cb_push_int_mult;
747 } else if (!strcmp("unsigned int",sub_type->name)) {
748 field->send = gras_datadesc_cb_push_uint_mult;
749 } else if (!strcmp("long int",sub_type->name)) {
750 field->send = gras_datadesc_cb_push_lint_mult;
751 } else if (!strcmp("unsigned long int",sub_type->name)) {
752 field->send = gras_datadesc_cb_push_ulint_mult;
754 ERROR1("Field %s is not an int, unsigned int, long int neither unsigned long int",
761 * The given datadesc must be a struct or union (abort if not).
762 * (useful to put the function pointers to the right value, for example)
764 void gras_datadesc_cb_field_recv(gras_datadesc_type_t type,
765 const char *field_name,
766 gras_datadesc_type_cb_void_t recv) {
768 gras_dd_cat_field_t field=gras_dd_find_field(type,field_name);
773 * Free a datadesc. Should only be called at xbt_exit.
775 void gras_datadesc_free(gras_datadesc_type_t *type) {
777 DEBUG1("Let's free ddt %s",(*type)->name);
779 switch ((*type)->category_code) {
780 case e_gras_datadesc_type_cat_scalar:
781 case e_gras_datadesc_type_cat_ref:
782 case e_gras_datadesc_type_cat_array:
783 /* nothing to free in there */
786 case e_gras_datadesc_type_cat_struct:
787 xbt_dynar_free(&( (*type)->category.struct_data.fields ));
790 case e_gras_datadesc_type_cat_union:
791 xbt_dynar_free(&( (*type)->category.union_data.fields ));
795 /* datadesc was invalid. Killing it is like euthanasy, I guess */
804 * gras_datadesc_type_cmp:
806 * Compares two datadesc types with the same semantic than strcmp.
808 * This comparison does not take the set headers into account (name and ID),
809 * but only the payload (actual type description).
811 int gras_datadesc_type_cmp(const gras_datadesc_type_t d1,
812 const gras_datadesc_type_t d2) {
814 gras_dd_cat_field_t field1,field2;
815 gras_datadesc_type_t field_desc_1,field_desc_2;
817 if (d1 == d2) return 0; /* easy optimization */
820 DEBUG0("ddt_cmp: !d1 && d2 => 1");
824 DEBUG0("ddt_cmp: !d1 && !d2 => 0");
828 DEBUG0("ddt_cmp: d1 && !d2 => -1");
832 for (cpt=0; cpt<gras_arch_count; cpt++) {
833 if (d1->size[cpt] != d2->size[cpt]) {
834 DEBUG5("ddt_cmp: %s->size=%ld != %s->size=%ld (on %s)",
835 d1->name,d1->size[cpt],d2->name,d2->size[cpt],
836 gras_arches[cpt].name);
837 return d1->size[cpt] > d2->size[cpt] ? 1 : -1;
840 if (d1->alignment[cpt] != d2->alignment[cpt]) {
841 DEBUG5("ddt_cmp: %s->alignment=%ld != %s->alignment=%ld (on %s)",
842 d1->name,d1->alignment[cpt],d2->name,d2->alignment[cpt],
843 gras_arches[cpt].name);
844 return d1->alignment[cpt] > d2->alignment[cpt] ? 1 : -1;
847 if (d1->aligned_size[cpt] != d2->aligned_size[cpt]) {
848 DEBUG5("ddt_cmp: %s->aligned_size=%ld != %s->aligned_size=%ld (on %s)",
849 d1->name,d1->aligned_size[cpt],d2->name,d2->aligned_size[cpt],
850 gras_arches[cpt].name);
851 return d1->aligned_size[cpt] > d2->aligned_size[cpt] ? 1 : -1;
855 if (d1->category_code != d2->category_code) {
856 DEBUG4("ddt_cmp: %s->cat=%s != %s->cat=%s",
857 d1->name,gras_datadesc_cat_names[d1->category_code],
858 d2->name,gras_datadesc_cat_names[d2->category_code]);
859 return d1->category_code > d2->category_code ? 1 : -1;
862 if (d1->send != d2->send) {
863 DEBUG4("ddt_cmp: %s->send=%p != %s->send=%p",
864 d1->name,(void*)d1->send, d2->name,(void*)d2->send);
865 return 1; /* ISO C forbids ordered comparisons of pointers to functions */
868 if (d1->recv != d2->recv) {
869 DEBUG4("ddt_cmp: %s->recv=%p != %s->recv=%p",
870 d1->name,(void*)d1->recv, d2->name,(void*)d2->recv);
871 return 1; /* ISO C forbids ordered comparisons of pointers to functions */
874 switch (d1->category_code) {
875 case e_gras_datadesc_type_cat_scalar:
876 if (d1->category.scalar_data.encoding != d2->category.scalar_data.encoding)
877 return d1->category.scalar_data.encoding > d2->category.scalar_data.encoding ? 1 : -1 ;
880 case e_gras_datadesc_type_cat_struct:
881 if (xbt_dynar_length(d1->category.struct_data.fields) !=
882 xbt_dynar_length(d2->category.struct_data.fields)) {
883 DEBUG4("ddt_cmp: %s (having %lu fields) != %s (having %lu fields)",
884 d1->name, xbt_dynar_length(d1->category.struct_data.fields),
885 d2->name, xbt_dynar_length(d2->category.struct_data.fields));
887 return xbt_dynar_length(d1->category.struct_data.fields) >
888 xbt_dynar_length(d2->category.struct_data.fields) ?
891 xbt_dynar_foreach(d1->category.struct_data.fields, cpt, field1) {
893 field2 = xbt_dynar_get_as(d2->category.struct_data.fields, cpt, gras_dd_cat_field_t);
894 field_desc_1 = field1->type;
895 field_desc_2 = field2->type;
896 ret = gras_datadesc_type_cmp(field_desc_1,field_desc_2);
898 DEBUG6("%s->field[%d]=%s != %s->field[%d]=%s",
899 d1->name,cpt,field1->name,
900 d2->name,cpt,field2->name);
907 case e_gras_datadesc_type_cat_union:
908 if (d1->category.union_data.selector != d2->category.union_data.selector)
909 return 1; /* ISO C forbids ordered comparisons of pointers to functions */
911 if (xbt_dynar_length(d1->category.union_data.fields) !=
912 xbt_dynar_length(d2->category.union_data.fields))
913 return xbt_dynar_length(d1->category.union_data.fields) >
914 xbt_dynar_length(d2->category.union_data.fields) ?
917 xbt_dynar_foreach(d1->category.union_data.fields, cpt, field1) {
919 field2 = xbt_dynar_get_as(d2->category.union_data.fields, cpt, gras_dd_cat_field_t);
920 field_desc_1 = field1->type;
921 field_desc_2 = field2->type;
922 ret = gras_datadesc_type_cmp(field_desc_1,field_desc_2);
930 case e_gras_datadesc_type_cat_ref:
931 if (d1->category.ref_data.selector != d2->category.ref_data.selector)
932 return 1; /* ISO C forbids ordered comparisons of pointers to functions */
934 if (d1->category.ref_data.type != d2->category.ref_data.type)
935 return d1->category.ref_data.type > d2->category.ref_data.type ? 1 : -1;
938 case e_gras_datadesc_type_cat_array:
939 if (d1->category.array_data.type != d2->category.array_data.type)
940 return d1->category.array_data.type > d2->category.array_data.type ? 1 : -1;
942 if (d1->category.array_data.fixed_size != d2->category.array_data.fixed_size)
943 return d1->category.array_data.fixed_size > d2->category.array_data.fixed_size ? 1 : -1;
945 if (d1->category.array_data.dynamic_size != d2->category.array_data.dynamic_size)
946 return 1; /* ISO C forbids ordered comparisons of pointers to functions */
951 /* two stupidly created ddt are equally stupid ;) */