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 "gras/DataDesc/datadesc_private.h"
13 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(ddt_create,datadesc,"Creating new datadescriptions");
18 * gime that memory back, dude. I mean it.
20 void gras_ddt_freev(void *ddt) {
21 gras_datadesc_type_t *type= (gras_datadesc_type_t *)ddt;
24 gras_datadesc_free(type);
29 gras_ddt_new(const char *name,
30 gras_datadesc_type_t **dst) {
32 gras_datadesc_type_t *res;
34 GRAS_IN1("(%s)",name);
35 res=gras_new0(gras_datadesc_type_t,1);
39 res->name = (char*)strdup(name);
40 res->name_len = strlen(name);
43 TRY(gras_set_add(gras_datadesc_set_local,
44 (gras_set_elm_t*)res,&gras_ddt_freev));
50 * gras_datadesc_by_name:
52 * Retrieve a type from its name
54 gras_datadesc_type_t *gras_datadesc_by_name(const char *name) {
56 gras_datadesc_type_t *type;
58 GRAS_IN1("(%s)",name);
59 if (gras_set_get_by_name(gras_datadesc_set_local,
60 name,(gras_set_elm_t**)&type) == no_error) {
70 * gras_datadesc_by_id:
72 * Retrieve a type from its code
74 gras_error_t gras_datadesc_by_id(long int code,
75 gras_datadesc_type_t **type) {
77 return gras_set_get_by_id(gras_datadesc_set_local,
78 code,(gras_set_elm_t**)type);
82 * gras_datadesc_scalar:
84 * Create a new scalar and give a pointer to it
87 gras_datadesc_scalar(const char *name,
88 gras_ddt_scalar_type_t type,
89 enum e_gras_dd_scalar_encoding encoding,
90 gras_datadesc_type_t **dst) {
93 gras_datadesc_type_t *res;
97 res = gras_datadesc_by_name(name);
99 gras_assert1(res->category_code == e_gras_datadesc_type_cat_scalar,
100 "Redefinition of type %s does not match", name);
101 gras_assert1(res->category.scalar_data.encoding == encoding,
102 "Redefinition of type %s does not match", name);
103 gras_assert1(res->category.scalar_data.type == type,
104 "Redefinition of type %s does not match", name);
105 VERB1("Discarding redefinition of %s",name);
109 TRY(gras_ddt_new(name,dst));
112 for (arch = 0; arch < gras_arch_count; arch ++) {
115 res->size[arch] = gras_arches[arch].sizeofs[type];
116 res->alignment[arch] = gras_arches[arch].boundaries[type];
117 res->aligned_size[arch] = aligned(res->size[arch], res->alignment[arch]);
119 sz = res->size[arch];
122 /* just in case you wonder, x>>1 == x/2 on all architectures when x>=0
123 and a size is always>=0 */
125 /* make sure mask have all the bits under the biggest one of size set to 1
126 Example: size=000100101 => mask=0000111111 */
131 if (res->size[arch] & (mask >> 1)) { /* if size have bits to one beside its biggest */
132 /* size is not a power of 2 */
133 /* alignment= next power of 2 after size */
134 res->alignment[arch] = (res->size[arch] & ~(mask >> 1)) << 1;
135 gras_assert0(res->alignment[arch] != 0,
136 "scalar type too large");
138 res->aligned_size[arch] = aligned(res->size[arch], res->alignment[arch]);
139 gras_assert0 (res->aligned_size[arch] >= 0,
140 "scalar type too large");
143 /* size is a power of 2, life is great */
144 res->alignment[arch] = res->size[arch];
145 res->aligned_size[arch] = res->size[arch];
150 res->category_code = e_gras_datadesc_type_cat_scalar;
151 res->category.scalar_data.encoding = encoding;
152 res->category.scalar_data.type = type;
159 * gras_dd_cat_field_free:
161 * Frees one struct or union field
163 void gras_dd_cat_field_free(void *f) {
164 gras_dd_cat_field_t *field = *(gras_dd_cat_field_t **)f;
168 gras_free(field->name);
175 * gras_datadesc_struct:
177 * Create a new struct and give a pointer to it
180 gras_datadesc_struct(const char *name,
181 gras_datadesc_type_t **dst) {
183 gras_error_t errcode;
184 gras_datadesc_type_t *res;
187 GRAS_IN1("(%s)",name);
188 res = gras_datadesc_by_name(name);
190 /* FIXME: Check that field redefinition matches */
191 gras_assert1(res->category_code == e_gras_datadesc_type_cat_struct,
192 "Redefinition of type %s does not match", name);
193 VERB1("Discarding redefinition of %s",name);
197 TRY(gras_ddt_new(name,dst));
200 for (arch=0; arch<gras_arch_count; arch ++) {
202 res->alignment[arch] = 0;
203 res->aligned_size[arch] = 0;
205 res->category_code = e_gras_datadesc_type_cat_struct;
206 TRY(gras_dynar_new(&(res->category.struct_data.fields),
207 sizeof(gras_dd_cat_field_t*),
208 &gras_dd_cat_field_free));
215 * gras_datadesc_struct_append:
217 * Append a field to the struct
220 gras_datadesc_struct_append(gras_datadesc_type_t *struct_type,
222 gras_datadesc_type_t *field_type) {
224 gras_error_t errcode;
225 gras_dd_cat_field_t *field;
228 GRAS_IN3("(%s %s.%s;)",field_type->name,struct_type->name,name);
229 if (struct_type->category.struct_data.closed) {
230 VERB1("Ignoring request to add field to struct %s (closed. Redefinition?)",
235 gras_assert1(field_type->size != 0,
236 "Cannot add a dynamically sized field in structure %s",
239 field=gras_new(gras_dd_cat_field_t,1);
243 field->name = (char*)strdup(name);
245 DEBUG0("----------------");
246 DEBUG3("PRE s={size=%ld,align=%ld,asize=%ld}",
247 struct_type->size[GRAS_THISARCH],
248 struct_type->alignment[GRAS_THISARCH],
249 struct_type->aligned_size[GRAS_THISARCH]);
252 for (arch=0; arch<gras_arch_count; arch ++) {
253 field->offset[arch] = aligned(struct_type->size[arch],
254 field_type->alignment[arch]);
256 struct_type->size[arch] = field->offset[arch] + field_type->size[arch];
257 struct_type->alignment[arch] = max(struct_type->alignment[arch],
258 field_type->alignment[arch]);
259 struct_type->aligned_size[arch] = aligned(struct_type->size[arch],
260 struct_type->alignment[arch]);
262 field->type = field_type;
266 TRY(gras_dynar_push(struct_type->category.struct_data.fields, &field));
268 DEBUG3("Push a %s into %s at offset %ld.",
269 field_type->name, struct_type->name,field->offset[GRAS_THISARCH]);
270 DEBUG3(" f={size=%ld,align=%ld,asize=%ld}",
271 field_type->size[GRAS_THISARCH],
272 field_type->alignment[GRAS_THISARCH],
273 field_type->aligned_size[GRAS_THISARCH]);
274 DEBUG3(" s={size=%ld,align=%ld,asize=%ld}",
275 struct_type->size[GRAS_THISARCH],
276 struct_type->alignment[GRAS_THISARCH],
277 struct_type->aligned_size[GRAS_THISARCH]);
282 gras_datadesc_struct_close(gras_datadesc_type_t *struct_type) {
284 struct_type->category.struct_data.closed = 1;
288 * gras_datadesc_union:
290 * Create a new union and give a pointer to it
293 gras_datadesc_union(const char *name,
294 gras_datadesc_type_cb_int_t selector,
295 gras_datadesc_type_t **dst) {
297 gras_error_t errcode;
298 gras_datadesc_type_t *res;
301 GRAS_IN1("(%s)",name);
302 gras_assert0(selector,
303 "Attempt to creat an union without field_count function");
305 res = gras_datadesc_by_name(name);
307 /* FIXME: Check that field redefinition matches */
308 gras_assert1(res->category_code == e_gras_datadesc_type_cat_union,
309 "Redefinition of type %s does not match", name);
310 gras_assert1(res->category.union_data.selector == selector,
311 "Redefinition of type %s does not match", name);
312 VERB1("Discarding redefinition of %s",name);
317 TRY(gras_ddt_new(name,dst));
320 for (arch=0; arch<gras_arch_count; arch ++) {
322 res->alignment[arch] = 0;
323 res->aligned_size[arch] = 0;
326 res->category_code = e_gras_datadesc_type_cat_union;
327 TRY(gras_dynar_new(&(res->category.union_data.fields),
328 sizeof(gras_dd_cat_field_t*),
329 &gras_dd_cat_field_free));
330 res->category.union_data.selector = selector;
336 * gras_datadesc_union_append:
338 * Append a field to the union
341 gras_datadesc_union_append(gras_datadesc_type_t *union_type,
343 gras_datadesc_type_t *field_type) {
345 gras_error_t errcode;
346 gras_dd_cat_field_t *field;
349 GRAS_IN3("(%s %s.%s;)",field_type->name,union_type->name,name);
350 gras_assert1(field_type->size != 0,
351 "Cannot add a dynamically sized field in union %s",
354 if (union_type->category.union_data.closed) {
355 VERB1("Ignoring request to add field to union %s (closed)",
360 field=gras_new0(gras_dd_cat_field_t,1);
364 field->name = (char*)strdup(name);
365 field->type = field_type;
366 /* All offset are left to 0 in an union */
368 TRY(gras_dynar_push(union_type->category.union_data.fields, &field));
370 for (arch=0; arch<gras_arch_count; arch ++) {
371 union_type->size[arch] = max(union_type->size[arch],
372 field_type->size[arch]);
373 union_type->alignment[arch] = max(union_type->alignment[arch],
374 field_type->alignment[arch]);
375 union_type->aligned_size[arch] = aligned(union_type->size[arch],
376 union_type->alignment[arch]);
382 gras_datadesc_union_close(gras_datadesc_type_t *union_type) {
383 union_type->category.union_data.closed = 1;
388 * Create a new ref to a fixed type and give a pointer to it
391 gras_datadesc_ref(const char *name,
392 gras_datadesc_type_t *referenced_type,
393 gras_datadesc_type_t **dst) {
395 gras_error_t errcode;
396 gras_datadesc_type_t *res;
397 gras_datadesc_type_t *pointer_type = gras_datadesc_by_name("data pointer");
400 GRAS_IN1("(%s)",name);
401 res = gras_datadesc_by_name(name);
403 gras_assert1(res->category_code == e_gras_datadesc_type_cat_ref,
404 "Redefinition of %s does not match",name);
405 gras_assert1(res->category.ref_data.type == referenced_type,
406 "Redefinition of %s does not match",name);
407 gras_assert1(res->category.ref_data.selector == NULL,
408 "Redefinition of %s does not match",name);
409 VERB1("Discarding redefinition of %s",name);
414 TRY(gras_ddt_new(name,dst));
417 gras_assert0(pointer_type, "Cannot get the description of data pointer");
419 for (arch=0; arch<gras_arch_count; arch ++){
420 res->size[arch] = pointer_type->size[arch];
421 res->alignment[arch] = pointer_type->alignment[arch];
422 res->aligned_size[arch] = pointer_type->aligned_size[arch];
425 res->category_code = e_gras_datadesc_type_cat_ref;
426 res->category.ref_data.type = referenced_type;
427 res->category.ref_data.selector = NULL;
432 * gras_datadesc_ref_generic:
434 * Create a new ref to a type given at use time, and give a pointer to it
437 gras_datadesc_ref_generic(const char *name,
438 gras_datadesc_selector_t selector,
439 gras_datadesc_type_t **dst) {
441 gras_error_t errcode;
442 gras_datadesc_type_t *res;
443 gras_datadesc_type_t *pointer_type = gras_datadesc_by_name("data pointer");
446 GRAS_IN1("(%s)",name);
447 res = gras_datadesc_by_name(name);
449 gras_assert1(res->category_code == e_gras_datadesc_type_cat_ref,
450 "Redefinition of type %s does not match", name);
451 gras_assert1(res->category.ref_data.type == NULL,
452 "Redefinition of type %s does not match", name);
453 gras_assert1(res->category.ref_data.selector == selector,
454 "Redefinition of type %s does not match", name);
455 VERB1("Discarding redefinition of %s",name);
459 TRY(gras_ddt_new(name,dst));
462 gras_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,
487 gras_datadesc_type_t **dst) {
489 gras_error_t errcode;
490 gras_datadesc_type_t *res;
493 GRAS_IN1("(%s)",name);
494 res = gras_datadesc_by_name(name);
496 gras_assert1(res->category_code == e_gras_datadesc_type_cat_array,
497 "Redefinition of type %s does not match", name);
498 gras_assert1(res->category.array_data.type == element_type,
499 "Redefinition of type %s does not match", name);
500 gras_assert1(res->category.array_data.fixed_size == fixed_size,
501 "Redefinition of type %s does not match", name);
502 gras_assert1(res->category.array_data.dynamic_size == NULL,
503 "Redefinition of type %s does not match", name);
504 VERB1("Discarding redefinition of %s",name);
509 TRY(gras_ddt_new(name,dst));
512 gras_assert1(fixed_size > 0, "'%s' is a array of null fixed size",name);
513 for (arch=0; arch<gras_arch_count; arch ++) {
514 res->size[arch] = fixed_size * element_type->aligned_size[arch];
515 res->alignment[arch] = element_type->alignment[arch];
516 res->aligned_size[arch] = res->size[arch];
519 res->category_code = e_gras_datadesc_type_cat_array;
521 res->category.array_data.type = element_type;
522 res->category.array_data.fixed_size = fixed_size;
523 res->category.array_data.dynamic_size = NULL;
528 * gras_datadesc_array_dyn:
530 * Create a new array and give a pointer to it
533 gras_datadesc_array_dyn(const char *name,
534 gras_datadesc_type_t *element_type,
535 gras_datadesc_type_cb_int_t dynamic_size,
536 gras_datadesc_type_t **dst) {
538 gras_error_t errcode;
539 gras_datadesc_type_t *res;
542 GRAS_IN1("(%s)",name);
543 gras_assert1(dynamic_size,
544 "'%s' is a dynamic array without size discriminant",
547 res = gras_datadesc_by_name(name);
549 gras_assert1(res->category_code == e_gras_datadesc_type_cat_array,
550 "Redefinition of type %s does not match", name);
551 gras_assert1(res->category.array_data.type == element_type,
552 "Redefinition of type %s does not match", name);
553 gras_assert1(res->category.array_data.fixed_size == 0,
554 "Redefinition of type %s does not match", name);
555 gras_assert1(res->category.array_data.dynamic_size == dynamic_size,
556 "Redefinition of type %s does not match", name);
557 VERB1("Discarding redefinition of %s",name);
563 TRY(gras_ddt_new(name,dst));
566 for (arch=0; arch<gras_arch_count; arch ++) {
567 res->size[arch] = 0; /* make sure it indicates "dynamic" */
568 res->alignment[arch] = element_type->alignment[arch];
569 res->aligned_size[arch] = 0; /*FIXME: That was so in GS, but looks stupid*/
572 res->category_code = e_gras_datadesc_type_cat_array;
574 res->category.array_data.type = element_type;
575 res->category.array_data.fixed_size = 0;
576 res->category.array_data.dynamic_size = dynamic_size;
582 * gras_datadesc_ref_pop_arr:
584 * Most of the time, you want to include a reference in your structure which
585 * is a pointer to a dynamic array whose size is fixed by another field of
588 * This case pops up so often that this function was created to take care of
589 * this case. It creates a dynamic array type whose size is poped from the
590 * current cbps, and then create a reference to it.
592 * The name of the created datatype will be the name of the element type, with
593 * '[]*' appended to it.
595 * Then to use it, you just have to make sure that your structure pre-callback
596 * does push the size of the array in the cbps (using #gras_cbps_i_push), and
599 * But be remember that this is a stack. If you have two different pop_arr, you
600 * should push the second one first, so that the first one is on the top of the
601 * list when the first field gets transfered.
605 gras_datadesc_ref_pop_arr(gras_datadesc_type_t *element_type,
606 gras_datadesc_type_t **dst) {
607 gras_error_t errcode;
608 char *name=(char*)gras_malloc(strlen(element_type->name) + 4);
610 sprintf(name,"%s[]",element_type->name);
612 TRY(gras_datadesc_array_dyn(name,element_type,
613 gras_datadesc_cb_pop, dst));
615 sprintf(name,"%s[]*",element_type->name);
616 TRY(gras_datadesc_ref(name,*dst,dst));
623 gras_datadesc_import_nws(const char *name,
624 const DataDescriptor *desc,
626 gras_datadesc_type_t **dst) {
631 * gras_datadesc_cb_send:
633 * Add a pre-send callback to this datadesc.
634 * (useful to push the sizes of the upcoming arrays, for example)
636 void gras_datadesc_cb_send (gras_datadesc_type_t *type,
637 gras_datadesc_type_cb_void_t send) {
641 * gras_datadesc_cb_recv:
643 * Add a post-receive callback to this datadesc.
644 * (useful to put the function pointers to the rigth value, for example)
646 void gras_datadesc_cb_recv(gras_datadesc_type_t *type,
647 gras_datadesc_type_cb_void_t recv) {
651 * gras_dd_find_field:
653 * Returns the type descriptor of the given field. Abort on error.
655 static gras_datadesc_type_t *
656 gras_dd_find_field(gras_datadesc_type_t *type,
657 const char *field_name) {
658 gras_datadesc_type_t *sub_type=NULL;
659 gras_dynar_t *field_array;
661 gras_dd_cat_field_t *field=NULL;
664 if (type->category_code == e_gras_datadesc_type_cat_union) {
665 field_array = type->category.union_data.fields;
666 } else if (type->category_code == e_gras_datadesc_type_cat_struct) {
667 field_array = type->category.struct_data.fields;
669 ERROR2("%s (%p) is not a struct nor an union. There is no field.", type->name,(void*)type);
672 gras_dynar_foreach(field_array,field_num,field) {
673 if (!strcmp(field_name,field->name)) {
677 ERROR2("No field nammed %s in %s",field_name,type->name);
683 * gras_datadesc_cb_field_send:
685 * Add a pre-send callback to the given field of the datadesc (which must be a struct or union).
686 * (useful to push the sizes of the upcoming arrays, for example)
688 void gras_datadesc_cb_field_send (gras_datadesc_type_t *type,
689 const char *field_name,
690 gras_datadesc_type_cb_void_t send) {
692 gras_datadesc_type_t *sub_type=gras_dd_find_field(type,field_name);
693 sub_type->send = send;
697 * gras_datadesc_cb_field_push:
699 * Add a pre-send callback to the given field resulting in its value to be pushed to
700 * the stack of sizes. It must be a int, unsigned int, long int or unsigned long int.
702 void gras_datadesc_cb_field_push (gras_datadesc_type_t *type,
703 const char *field_name) {
705 gras_datadesc_type_t *sub_type=gras_dd_find_field(type,field_name);
706 if (!strcmp("int",sub_type->name)) {
707 sub_type->send = gras_datadesc_cb_push_int;
708 } else if (!strcmp("unsigned int",sub_type->name)) {
709 sub_type->send = gras_datadesc_cb_push_uint;
710 } else if (!strcmp("long int",sub_type->name)) {
711 sub_type->send = gras_datadesc_cb_push_lint;
712 } else if (!strcmp("unsigned long int",sub_type->name)) {
713 sub_type->send = gras_datadesc_cb_push_ulint;
715 ERROR1("Field %s is not an int, unsigned int, long int neither unsigned long int",
721 * gras_datadesc_cb_field_recv:
723 * Add a post-receive callback to the given field of the datadesc (which must be a struct or union).
724 * (useful to put the function pointers to the right value, for example)
726 void gras_datadesc_cb_field_recv(gras_datadesc_type_t *type,
727 const char *field_name,
728 gras_datadesc_type_cb_void_t recv) {
730 gras_datadesc_type_t *sub_type=gras_dd_find_field(type,field_name);
731 sub_type->recv = recv;
735 * gras_datadesc_free:
737 * Free a datadesc. Should only be called at gras_exit.
739 void gras_datadesc_free(gras_datadesc_type_t *type) {
741 DEBUG1("Let's free ddt %s",type->name);
743 switch (type->category_code) {
744 case e_gras_datadesc_type_cat_scalar:
745 case e_gras_datadesc_type_cat_ref:
746 case e_gras_datadesc_type_cat_array:
747 /* nothing to free in there */
750 case e_gras_datadesc_type_cat_ignored:
751 if (type->category.ignored_data.free_func) {
752 type->category.ignored_data.free_func
753 (type->category.ignored_data.default_value);
757 case e_gras_datadesc_type_cat_struct:
758 gras_dynar_free(type->category.struct_data.fields);
761 case e_gras_datadesc_type_cat_union:
762 gras_dynar_free(type->category.union_data.fields);
766 /* datadesc was invalid. Killing it is like euthanasy, I guess */
769 gras_free(type->name);