3 /* ddt_new - creation/deletion of datatypes structs (private to this module)*/
5 /* Authors: Olivier Aumage, Martin Quinson */
6 /* Copyright (C) 2003, 2004 the GRAS posse. */
8 /* This program is free software; you can redistribute it and/or modify it
9 under the terms of the license (GNU LGPL) which comes with this package. */
11 #include "xbt/misc.h" /* min()/max() */
12 #include "gras/DataDesc/datadesc_private.h"
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ddt_create,datadesc,"Creating new datadescriptions");
19 * gime that memory back, dude. I mean it.
21 void gras_ddt_freev(void *ddt) {
22 gras_datadesc_type_t type= (gras_datadesc_type_t)ddt;
25 gras_datadesc_free(&type);
29 static gras_datadesc_type_t gras_ddt_new(const char *name) {
30 gras_datadesc_type_t res;
33 res=xbt_new0(s_gras_datadesc_type_t,1);
35 res->name = (char*)strdup(name);
36 res->name_len = strlen(name);
39 xbt_set_add(gras_datadesc_set_local,
40 (xbt_set_elm_t)res,&gras_ddt_freev);
46 * gras_datadesc_by_name:
48 * Retrieve a type from its name
50 gras_datadesc_type_t gras_datadesc_by_name(const char *name) {
52 gras_datadesc_type_t type;
55 if (xbt_set_get_by_name(gras_datadesc_set_local,
56 name,(xbt_set_elm_t*)&type) == no_error) {
66 * gras_datadesc_by_id:
68 * Retrieve a type from its code
70 xbt_error_t gras_datadesc_by_id(long int code,
71 gras_datadesc_type_t *type) {
73 return xbt_set_get_by_id(gras_datadesc_set_local,
74 code,(xbt_set_elm_t*)type);
78 * gras_datadesc_scalar:
80 * Create a new scalar and give a pointer to it
83 gras_datadesc_scalar(const char *name,
84 gras_ddt_scalar_type_t type,
85 enum e_gras_dd_scalar_encoding encoding) {
87 gras_datadesc_type_t res;
91 res = gras_datadesc_by_name(name);
93 xbt_assert1(res->category_code == e_gras_datadesc_type_cat_scalar,
94 "Redefinition of type %s does not match", name);
95 xbt_assert1(res->category.scalar_data.encoding == encoding,
96 "Redefinition of type %s does not match", name);
97 xbt_assert1(res->category.scalar_data.type == type,
98 "Redefinition of type %s does not match", name);
99 VERB1("Discarding redefinition of %s",name);
102 res = gras_ddt_new(name);
104 for (arch = 0; arch < gras_arch_count; arch ++) {
109 res->size[arch] = gras_arches[arch].sizeofs[type];
110 res->alignment[arch] = gras_arches[arch].boundaries[type];
111 res->aligned_size[arch] = aligned(res->size[arch], res->alignment[arch]);
112 /* FIXME: kill the following after discution with Oli */
114 sz = res->size[arch];
117 /* just in case you wonder, x>>1 == x/2 on all architectures when x>=0
118 and a size is always>=0 */
120 /* make sure mask have all the bits under the biggest one of size set to 1
121 Example: size=000100101 => mask=0000111111 */
126 if (res->size[arch] & (mask >> 1)) { /* if size have bits to one beside its biggest */
127 /* size is not a power of 2 */
128 /* alignment= next power of 2 after size */
129 res->alignment[arch] = (res->size[arch] & ~(mask >> 1)) << 1;
130 xbt_assert0(res->alignment[arch] != 0,
131 "scalar type too large");
133 res->aligned_size[arch] = aligned(res->size[arch], res->alignment[arch]);
134 xbt_assert0 (res->aligned_size[arch] >= 0,
135 "scalar type too large");
138 /* size is a power of 2, life is great */
139 res->alignment[arch] = res->size[arch];
140 res->aligned_size[arch] = res->size[arch];
145 res->category_code = e_gras_datadesc_type_cat_scalar;
146 res->category.scalar_data.encoding = encoding;
147 res->category.scalar_data.type = type;
155 * gras_dd_cat_field_free:
157 * Frees one struct or union field
159 void gras_dd_cat_field_free(void *f) {
160 gras_dd_cat_field_t field = *(gras_dd_cat_field_t *)f;
164 xbt_free(field->name);
171 * gras_datadesc_struct:
173 * Create a new struct and give a pointer to it
176 gras_datadesc_struct(const char *name) {
178 gras_datadesc_type_t res;
181 XBT_IN1("(%s)",name);
182 res = gras_datadesc_by_name(name);
184 /* FIXME: Check that field redefinition matches */
185 xbt_assert1(res->category_code == e_gras_datadesc_type_cat_struct,
186 "Redefinition of type %s does not match", name);
187 VERB1("Discarding redefinition of %s",name);
190 res = gras_ddt_new(name);
192 for (arch=0; arch<gras_arch_count; arch ++) {
194 res->alignment[arch] = 0;
195 res->aligned_size[arch] = 0;
197 res->category_code = e_gras_datadesc_type_cat_struct;
198 res->category.struct_data.fields =
199 xbt_dynar_new(sizeof(gras_dd_cat_field_t),
200 &gras_dd_cat_field_free);
207 * gras_datadesc_struct_append:
209 * Append a field to the struct
212 gras_datadesc_struct_append(gras_datadesc_type_t struct_type,
214 gras_datadesc_type_t field_type) {
216 gras_dd_cat_field_t field;
219 xbt_assert2(field_type,
220 "Cannot add the field '%s' into struct '%s': its type is NULL. Typo in get_by_name?",
221 name,struct_type->name);
222 XBT_IN3("(%s %s.%s;)",field_type->name,struct_type->name,name);
223 if (struct_type->category.struct_data.closed) {
224 VERB1("Ignoring request to add field to struct %s (closed. Redefinition?)",
229 xbt_assert1(field_type->size != 0,
230 "Cannot add a dynamically sized field in structure %s",
233 field=xbt_new(s_gras_dd_cat_field_t,1);
234 field->name = (char*)strdup(name);
236 DEBUG0("----------------");
237 DEBUG3("PRE s={size=%ld,align=%ld,asize=%ld}",
238 struct_type->size[GRAS_THISARCH],
239 struct_type->alignment[GRAS_THISARCH],
240 struct_type->aligned_size[GRAS_THISARCH]);
243 for (arch=0; arch<gras_arch_count; arch ++) {
244 field->offset[arch] = aligned(struct_type->size[arch],
245 field_type->alignment[arch]);
247 struct_type->size[arch] = field->offset[arch] + field_type->size[arch];
248 struct_type->alignment[arch] = max(struct_type->alignment[arch],
249 field_type->alignment[arch]);
250 struct_type->aligned_size[arch] = aligned(struct_type->size[arch],
251 struct_type->alignment[arch]);
253 field->type = field_type;
257 xbt_dynar_push(struct_type->category.struct_data.fields, &field);
259 DEBUG3("Push a %s into %s at offset %ld.",
260 field_type->name, struct_type->name,field->offset[GRAS_THISARCH]);
261 DEBUG3(" f={size=%ld,align=%ld,asize=%ld}",
262 field_type->size[GRAS_THISARCH],
263 field_type->alignment[GRAS_THISARCH],
264 field_type->aligned_size[GRAS_THISARCH]);
265 DEBUG3(" s={size=%ld,align=%ld,asize=%ld}",
266 struct_type->size[GRAS_THISARCH],
267 struct_type->alignment[GRAS_THISARCH],
268 struct_type->aligned_size[GRAS_THISARCH]);
273 gras_datadesc_struct_close(gras_datadesc_type_t struct_type) {
275 struct_type->category.struct_data.closed = 1;
279 * gras_datadesc_cycle_set:
281 * Tell GRAS that the pointers of the type described by @ddt may present
282 * some loop, and that the cycle detection mecanism is needed.
284 * Note that setting this option when not needed have a rather bad effect
285 * on the performance (several times slower on big data).
288 gras_datadesc_cycle_set(gras_datadesc_type_t ddt) {
292 * gras_datadesc_cycle_unset:
294 * Tell GRAS that the pointers of the type described by @ddt do not present
295 * any loop and that cycle detection mecanism are not needed.
299 gras_datadesc_cycle_unset(gras_datadesc_type_t ddt) {
304 * gras_datadesc_union:
306 * Create a new union and give a pointer to it
309 gras_datadesc_union(const char *name,
310 gras_datadesc_type_cb_int_t selector) {
312 gras_datadesc_type_t res;
315 XBT_IN1("(%s)",name);
316 xbt_assert0(selector,
317 "Attempt to creat an union without field_count function");
319 res = gras_datadesc_by_name(name);
321 /* FIXME: Check that field redefinition matches */
322 xbt_assert1(res->category_code == e_gras_datadesc_type_cat_union,
323 "Redefinition of type %s does not match", name);
324 xbt_assert1(res->category.union_data.selector == selector,
325 "Redefinition of type %s does not match", name);
326 VERB1("Discarding redefinition of %s",name);
330 res = gras_ddt_new(name);
332 for (arch=0; arch<gras_arch_count; arch ++) {
334 res->alignment[arch] = 0;
335 res->aligned_size[arch] = 0;
338 res->category_code = e_gras_datadesc_type_cat_union;
339 res->category.union_data.fields =
340 xbt_dynar_new(sizeof(gras_dd_cat_field_t*),
341 &gras_dd_cat_field_free);
342 res->category.union_data.selector = selector;
348 * gras_datadesc_union_append:
350 * Append a field to the union
353 gras_datadesc_union_append(gras_datadesc_type_t union_type,
355 gras_datadesc_type_t field_type) {
357 gras_dd_cat_field_t field;
360 XBT_IN3("(%s %s.%s;)",field_type->name,union_type->name,name);
361 xbt_assert1(field_type->size != 0,
362 "Cannot add a dynamically sized field in union %s",
365 if (union_type->category.union_data.closed) {
366 VERB1("Ignoring request to add field to union %s (closed)",
371 field=xbt_new0(s_gras_dd_cat_field_t,1);
373 field->name = (char*)strdup(name);
374 field->type = field_type;
375 /* All offset are left to 0 in an union */
377 xbt_dynar_push(union_type->category.union_data.fields, &field);
379 for (arch=0; arch<gras_arch_count; arch ++) {
380 union_type->size[arch] = max(union_type->size[arch],
381 field_type->size[arch]);
382 union_type->alignment[arch] = max(union_type->alignment[arch],
383 field_type->alignment[arch]);
384 union_type->aligned_size[arch] = aligned(union_type->size[arch],
385 union_type->alignment[arch]);
390 gras_datadesc_union_close(gras_datadesc_type_t union_type) {
391 union_type->category.union_data.closed = 1;
396 * Create a new ref to a fixed type and give a pointer to it
399 gras_datadesc_ref(const char *name,
400 gras_datadesc_type_t referenced_type) {
402 gras_datadesc_type_t res;
403 gras_datadesc_type_t pointer_type = gras_datadesc_by_name("data pointer");
406 XBT_IN1("(%s)",name);
407 res = gras_datadesc_by_name(name);
409 xbt_assert1(res->category_code == e_gras_datadesc_type_cat_ref,
410 "Redefinition of %s does not match",name);
411 xbt_assert1(res->category.ref_data.type == referenced_type,
412 "Redefinition of %s does not match",name);
413 xbt_assert1(res->category.ref_data.selector == NULL,
414 "Redefinition of %s does not match",name);
415 VERB1("Discarding redefinition of %s",name);
419 res = gras_ddt_new(name);
421 xbt_assert0(pointer_type, "Cannot get the description of data pointer");
423 for (arch=0; arch<gras_arch_count; arch ++){
424 res->size[arch] = pointer_type->size[arch];
425 res->alignment[arch] = pointer_type->alignment[arch];
426 res->aligned_size[arch] = pointer_type->aligned_size[arch];
429 res->category_code = e_gras_datadesc_type_cat_ref;
430 res->category.ref_data.type = referenced_type;
431 res->category.ref_data.selector = NULL;
436 * gras_datadesc_ref_generic:
438 * Create a new ref to a type given at use time, and give a pointer to it
441 gras_datadesc_ref_generic(const char *name,
442 gras_datadesc_selector_t selector) {
444 gras_datadesc_type_t res;
445 gras_datadesc_type_t pointer_type = gras_datadesc_by_name("data pointer");
448 XBT_IN1("(%s)",name);
449 res = gras_datadesc_by_name(name);
451 xbt_assert1(res->category_code == e_gras_datadesc_type_cat_ref,
452 "Redefinition of type %s does not match", name);
453 xbt_assert1(res->category.ref_data.type == NULL,
454 "Redefinition of type %s does not match", name);
455 xbt_assert1(res->category.ref_data.selector == selector,
456 "Redefinition of type %s does not match", name);
457 VERB1("Discarding redefinition of %s",name);
460 res = gras_ddt_new(name);
462 xbt_assert0(pointer_type, "Cannot get the description of data pointer");
464 for (arch=0; arch<gras_arch_count; arch ++) {
465 res->size[arch] = pointer_type->size[arch];
466 res->alignment[arch] = pointer_type->alignment[arch];
467 res->aligned_size[arch] = pointer_type->aligned_size[arch];
470 res->category_code = e_gras_datadesc_type_cat_ref;
472 res->category.ref_data.type = NULL;
473 res->category.ref_data.selector = selector;
479 * gras_datadesc_array_fixed:
481 * Create a new array and give a pointer to it
484 gras_datadesc_array_fixed(const char *name,
485 gras_datadesc_type_t element_type,
486 long int fixed_size) {
488 gras_datadesc_type_t res;
491 XBT_IN1("(%s)",name);
492 res = gras_datadesc_by_name(name);
494 xbt_assert1(res->category_code == e_gras_datadesc_type_cat_array,
495 "Redefinition of type %s does not match", name);
496 xbt_assert1(res->category.array_data.type == element_type,
497 "Redefinition of type %s does not match", name);
498 xbt_assert1(res->category.array_data.fixed_size == fixed_size,
499 "Redefinition of type %s does not match", name);
500 xbt_assert1(res->category.array_data.dynamic_size == NULL,
501 "Redefinition of type %s does not match", name);
502 VERB1("Discarding redefinition of %s",name);
506 res = gras_ddt_new(name);
508 xbt_assert1(fixed_size > 0, "'%s' is a array of null fixed size",name);
509 for (arch=0; arch<gras_arch_count; arch ++) {
510 res->size[arch] = fixed_size * element_type->aligned_size[arch];
511 res->alignment[arch] = element_type->alignment[arch];
512 res->aligned_size[arch] = res->size[arch];
515 res->category_code = e_gras_datadesc_type_cat_array;
517 res->category.array_data.type = element_type;
518 res->category.array_data.fixed_size = fixed_size;
519 res->category.array_data.dynamic_size = NULL;
524 * gras_datadesc_array_dyn:
526 * Create a new array and give a pointer to it
529 gras_datadesc_array_dyn(const char *name,
530 gras_datadesc_type_t element_type,
531 gras_datadesc_type_cb_int_t dynamic_size) {
533 gras_datadesc_type_t res;
536 XBT_IN1("(%s)",name);
537 xbt_assert1(dynamic_size,
538 "'%s' is a dynamic array without size discriminant",
541 res = gras_datadesc_by_name(name);
543 xbt_assert1(res->category_code == e_gras_datadesc_type_cat_array,
544 "Redefinition of type %s does not match", name);
545 xbt_assert1(res->category.array_data.type == element_type,
546 "Redefinition of type %s does not match", name);
547 xbt_assert1(res->category.array_data.fixed_size == 0,
548 "Redefinition of type %s does not match", name);
549 xbt_assert1(res->category.array_data.dynamic_size == dynamic_size,
550 "Redefinition of type %s does not match", name);
551 VERB1("Discarding redefinition of %s",name);
556 res = gras_ddt_new(name);
558 for (arch=0; arch<gras_arch_count; arch ++) {
559 res->size[arch] = 0; /* make sure it indicates "dynamic" */
560 res->alignment[arch] = element_type->alignment[arch];
561 res->aligned_size[arch] = 0; /*FIXME: That was so in GS, but looks stupid*/
564 res->category_code = e_gras_datadesc_type_cat_array;
566 res->category.array_data.type = element_type;
567 res->category.array_data.fixed_size = 0;
568 res->category.array_data.dynamic_size = dynamic_size;
574 * gras_datadesc_ref_pop_arr:
576 * Most of the time, you want to include a reference in your structure which
577 * is a pointer to a dynamic array whose size is fixed by another field of
580 * This case pops up so often that this function was created to take care of
581 * this case. It creates a dynamic array type whose size is poped from the
582 * current cbps, and then create a reference to it.
584 * The name of the created datatype will be the name of the element type, with
585 * '[]*' appended to it.
587 * Then to use it, you just have to make sure that your structure pre-callback
588 * does push the size of the array in the cbps (using #gras_cbps_i_push), and
591 * But be remember that this is a stack. If you have two different pop_arr, you
592 * should push the second one first, so that the first one is on the top of the
593 * list when the first field gets transfered.
597 gras_datadesc_ref_pop_arr(gras_datadesc_type_t element_type) {
599 gras_datadesc_type_t res;
600 char *name=(char*)xbt_malloc(strlen(element_type->name) + 4);
602 sprintf(name,"%s[]",element_type->name);
604 res = gras_datadesc_array_dyn(name,element_type,
605 gras_datadesc_cb_pop);
607 sprintf(name,"%s[]*",element_type->name);
608 res = gras_datadesc_ref(name,res);
615 gras_datadesc_import_nws(const char *name,
616 const DataDescriptor *desc,
617 unsigned long howmany,
618 /* OUT */ gras_datadesc_type_t *dst) {
623 * gras_datadesc_cb_send:
625 * Add a pre-send callback to this datadesc.
626 * (useful to push the sizes of the upcoming arrays, for example)
628 void gras_datadesc_cb_send (gras_datadesc_type_t type,
629 gras_datadesc_type_cb_void_t send) {
633 * gras_datadesc_cb_recv:
635 * Add a post-receive callback to this datadesc.
636 * (useful to put the function pointers to the rigth value, for example)
638 void gras_datadesc_cb_recv(gras_datadesc_type_t type,
639 gras_datadesc_type_cb_void_t recv) {
643 * gras_dd_find_field:
645 * Returns the type descriptor of the given field. Abort on error.
647 static gras_datadesc_type_t
648 gras_dd_find_field(gras_datadesc_type_t type,
649 const char *field_name) {
650 xbt_dynar_t field_array;
652 gras_dd_cat_field_t field=NULL;
655 if (type->category_code == e_gras_datadesc_type_cat_union) {
656 field_array = type->category.union_data.fields;
657 } else if (type->category_code == e_gras_datadesc_type_cat_struct) {
658 field_array = type->category.struct_data.fields;
660 ERROR2("%s (%p) is not a struct nor an union. There is no field.", type->name,(void*)type);
663 xbt_dynar_foreach(field_array,field_num,field) {
664 if (!strcmp(field_name,field->name)) {
668 ERROR2("No field nammed %s in %s",field_name,type->name);
674 * gras_datadesc_cb_field_send:
676 * Add a pre-send callback to the given field of the datadesc (which must be a struct or union).
677 * (useful to push the sizes of the upcoming arrays, for example)
679 void gras_datadesc_cb_field_send (gras_datadesc_type_t type,
680 const char *field_name,
681 gras_datadesc_type_cb_void_t send) {
683 gras_datadesc_type_t sub_type=gras_dd_find_field(type,field_name);
684 sub_type->send = send;
688 * gras_datadesc_cb_field_push:
690 * Add a pre-send callback to the given field resulting in its value to be pushed to
691 * the stack of sizes. It must be a int, unsigned int, long int or unsigned long int.
693 void gras_datadesc_cb_field_push (gras_datadesc_type_t type,
694 const char *field_name) {
696 gras_datadesc_type_t sub_type=gras_dd_find_field(type,field_name);
697 if (!strcmp("int",sub_type->name)) {
698 sub_type->send = gras_datadesc_cb_push_int;
699 } else if (!strcmp("unsigned int",sub_type->name)) {
700 sub_type->send = gras_datadesc_cb_push_uint;
701 } else if (!strcmp("long int",sub_type->name)) {
702 sub_type->send = gras_datadesc_cb_push_lint;
703 } else if (!strcmp("unsigned long int",sub_type->name)) {
704 sub_type->send = gras_datadesc_cb_push_ulint;
706 ERROR1("Field %s is not an int, unsigned int, long int neither unsigned long int",
712 * gras_datadesc_cb_field_recv:
714 * Add a post-receive callback to the given field of the datadesc (which must be a struct or union).
715 * (useful to put the function pointers to the right value, for example)
717 void gras_datadesc_cb_field_recv(gras_datadesc_type_t type,
718 const char *field_name,
719 gras_datadesc_type_cb_void_t recv) {
721 gras_datadesc_type_t sub_type=gras_dd_find_field(type,field_name);
722 sub_type->recv = recv;
726 * gras_datadesc_free:
728 * Free a datadesc. Should only be called at xbt_exit.
730 void gras_datadesc_free(gras_datadesc_type_t *type) {
732 DEBUG1("Let's free ddt %s",(*type)->name);
734 switch ((*type)->category_code) {
735 case e_gras_datadesc_type_cat_scalar:
736 case e_gras_datadesc_type_cat_ref:
737 case e_gras_datadesc_type_cat_array:
738 /* nothing to free in there */
741 case e_gras_datadesc_type_cat_ignored:
742 if ((*type)->category.ignored_data.free_func) {
743 (*type)->category.ignored_data.free_func
744 ((*type)->category.ignored_data.default_value);
748 case e_gras_datadesc_type_cat_struct:
749 xbt_dynar_free(&( (*type)->category.struct_data.fields ));
752 case e_gras_datadesc_type_cat_union:
753 xbt_dynar_free(&( (*type)->category.union_data.fields ));
757 /* datadesc was invalid. Killing it is like euthanasy, I guess */
760 xbt_free((*type)->name);