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 "DataDesc/datadesc_private.h"
13 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(ddt_create,datadesc);
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=malloc(sizeof(gras_datadesc_type_t));
39 memset(res, 0, sizeof(gras_datadesc_type_t));
40 res->name = strdup(name);
41 res->name_len = strlen(name);
44 TRY(gras_set_add(gras_datadesc_set_local,
45 (gras_set_elm_t*)res,&gras_ddt_freev));
51 * gras_datadesc_by_name:
53 * Retrieve a type from its name
55 gras_datadesc_type_t *gras_datadesc_by_name(const char *name) {
57 gras_datadesc_type_t *type;
59 GRAS_IN1("(%s)",name);
60 if (gras_set_get_by_name(gras_datadesc_set_local,
61 name,(gras_set_elm_t**)&type) == no_error) {
71 * gras_datadesc_by_id:
73 * Retrieve a type from its code
75 gras_error_t gras_datadesc_by_id(long int code,
76 gras_datadesc_type_t **type) {
78 return gras_set_get_by_id(gras_datadesc_set_local,
79 code,(gras_set_elm_t**)type);
83 * gras_datadesc_scalar:
85 * Create a new scalar and give a pointer to it
88 gras_datadesc_scalar(const char *name,
89 gras_ddt_scalar_type_t type,
90 enum e_gras_dd_scalar_encoding encoding,
91 gras_datadesc_type_t **dst) {
94 gras_datadesc_type_t *res;
98 res = gras_datadesc_by_name(name);
100 gras_assert1(res->category_code == e_gras_datadesc_type_cat_scalar,
101 "Redefinition of type %s does not match", name);
102 gras_assert1(res->category.scalar_data.encoding == encoding,
103 "Redefinition of type %s does not match", name);
104 gras_assert1(res->category.scalar_data.type == type,
105 "Redefinition of type %s does not match", name);
106 VERB1("Discarding redefinition of %s",name);
110 TRY(gras_ddt_new(name,dst));
113 for (arch = 0; arch < gras_arch_count; arch ++) {
116 res->size[arch] = gras_arches[arch].sizeof_scalars[type];
118 sz = res->size[arch];
121 /* just in case you wonder, x>>1 == x/2 on all architectures when x>=0
122 and a size is always>=0 */
124 /* make sure mask have all the bits under the biggest one of size set to 1
125 Example: size=000100101 => mask=0000111111 */
130 if (res->size[arch] & (mask >> 1)) { /* if size have bits to one beside its biggest */
131 /* size is not a power of 2 */
132 /* alignment= next power of 2 after size */
133 res->alignment[arch] = (res->size[arch] & ~(mask >> 1)) << 1;
134 gras_assert0(res->alignment[arch] != 0,
135 "scalar type too large");
137 res->aligned_size[arch] = aligned(res->size[arch], res->alignment[arch]);
138 gras_assert0 (res->aligned_size[arch] >= 0,
139 "scalar type too large");
142 /* size is a power of 2, life is great */
143 res->alignment[arch] = res->size[arch];
144 res->aligned_size[arch] = res->size[arch];
148 res->category_code = e_gras_datadesc_type_cat_scalar;
149 res->category.scalar_data.encoding = encoding;
150 res->category.scalar_data.type = type;
157 * gras_dd_cat_field_free:
159 * Frees one struct or union field
161 void gras_dd_cat_field_free(void *f) {
162 gras_dd_cat_field_t *field = *(gras_dd_cat_field_t **)f;
173 * gras_datadesc_struct:
175 * Create a new struct and give a pointer to it
178 gras_datadesc_struct(const char *name,
179 gras_datadesc_type_t **dst) {
181 gras_error_t errcode;
182 gras_datadesc_type_t *res;
185 GRAS_IN1("(%s)",name);
186 res = gras_datadesc_by_name(name);
188 /* FIXME: Check that field redefinition matches */
189 gras_assert1(res->category_code == e_gras_datadesc_type_cat_struct,
190 "Redefinition of type %s does not match", name);
191 VERB1("Discarding redefinition of %s",name);
195 TRY(gras_ddt_new(name,dst));
198 for (arch=0; arch<gras_arch_count; arch ++) {
200 res->alignment[arch] = 0;
201 res->aligned_size[arch] = 0;
203 res->category_code = e_gras_datadesc_type_cat_struct;
204 TRY(gras_dynar_new(&(res->category.struct_data.fields),
205 sizeof(gras_dd_cat_field_t*),
206 &gras_dd_cat_field_free));
213 * gras_datadesc_struct_append:
215 * Append a field to the struct
218 gras_datadesc_struct_append(gras_datadesc_type_t *struct_type,
220 gras_datadesc_type_t *field_type) {
222 gras_error_t errcode;
223 gras_dd_cat_field_t *field;
226 GRAS_IN3("(%s %s.%s;)",field_type->name,struct_type->name,name);
227 if (struct_type->category.struct_data.closed) {
228 VERB1("Ignoring request to add field to struct %s (closed. Redefinition?)",
233 gras_assert1(field_type->size >= 0,
234 "Cannot add a dynamically sized field in structure %s",
237 field=malloc(sizeof(gras_dd_cat_field_t));
241 field->name = strdup(name);
243 DEBUG0("----------------");
244 DEBUG4("PRE s={size=%ld,align=%ld,asize=%ld} struct_boundary=%d",
245 struct_type->size[GRAS_THISARCH],
246 struct_type->alignment[GRAS_THISARCH],
247 struct_type->aligned_size[GRAS_THISARCH],
248 gras_arches[GRAS_THISARCH].struct_boundary);
251 for (arch=0; arch<gras_arch_count; arch ++) {
252 field->offset[arch] = aligned(struct_type->size[arch],
253 min(field_type->alignment[arch],
254 gras_arches[arch].struct_boundary));
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=malloc(sizeof(gras_dd_cat_field_t));
364 field->name = strdup(name);
365 for (arch=0; arch<gras_arch_count; arch ++) {
366 field->offset[arch] = 0; /* that's the purpose of union ;) */
368 field->type = field_type;
372 TRY(gras_dynar_push(union_type->category.union_data.fields, &field));
374 for (arch=0; arch<gras_arch_count; arch ++) {
375 union_type->size[arch] = max(union_type->size[arch],
376 field_type->size[arch]);
377 union_type->alignment[arch] = max(union_type->alignment[arch],
378 field_type->alignment[arch]);
379 union_type->aligned_size[arch] = aligned(union_type->size[arch],
380 union_type->alignment[arch]);
386 gras_datadesc_union_close(gras_datadesc_type_t *union_type) {
387 union_type->category.union_data.closed = 1;
392 * Create a new ref to a fixed type and give a pointer to it
395 gras_datadesc_ref(const char *name,
396 gras_datadesc_type_t *referenced_type,
397 gras_datadesc_type_t **dst) {
399 gras_error_t errcode;
400 gras_datadesc_type_t *res;
401 gras_datadesc_type_t *pointer_type = gras_datadesc_by_name("data pointer");
404 GRAS_IN1("(%s)",name);
405 res = gras_datadesc_by_name(name);
407 gras_assert1(res->category_code == e_gras_datadesc_type_cat_ref,
408 "Redefinition of %s does not match",name);
409 gras_assert1(res->category.ref_data.type == referenced_type,
410 "Redefinition of %s does not match",name);
411 gras_assert1(res->category.ref_data.selector == NULL,
412 "Redefinition of %s does not match",name);
413 VERB1("Discarding redefinition of %s",name);
418 TRY(gras_ddt_new(name,dst));
421 gras_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,
443 gras_datadesc_type_t **dst) {
445 gras_error_t errcode;
446 gras_datadesc_type_t *res;
447 gras_datadesc_type_t *pointer_type = gras_datadesc_by_name("data pointer");
450 GRAS_IN1("(%s)",name);
451 res = gras_datadesc_by_name(name);
453 gras_assert1(res->category_code == e_gras_datadesc_type_cat_ref,
454 "Redefinition of type %s does not match", name);
455 gras_assert1(res->category.ref_data.type == NULL,
456 "Redefinition of type %s does not match", name);
457 gras_assert1(res->category.ref_data.selector == selector,
458 "Redefinition of type %s does not match", name);
459 VERB1("Discarding redefinition of %s",name);
463 TRY(gras_ddt_new(name,dst));
466 gras_assert0(pointer_type, "Cannot get the description of data pointer");
468 for (arch=0; arch<gras_arch_count; arch ++) {
469 res->size[arch] = pointer_type->size[arch];
470 res->alignment[arch] = pointer_type->alignment[arch];
471 res->aligned_size[arch] = pointer_type->aligned_size[arch];
474 res->category_code = e_gras_datadesc_type_cat_ref;
476 res->category.ref_data.type = NULL;
477 res->category.ref_data.selector = selector;
483 * gras_datadesc_array_fixed:
485 * Create a new array and give a pointer to it
488 gras_datadesc_array_fixed(const char *name,
489 gras_datadesc_type_t *element_type,
491 gras_datadesc_type_t **dst) {
493 gras_error_t errcode;
494 gras_datadesc_type_t *res;
497 GRAS_IN1("(%s)",name);
498 res = gras_datadesc_by_name(name);
500 gras_assert1(res->category_code == e_gras_datadesc_type_cat_array,
501 "Redefinition of type %s does not match", name);
502 gras_assert1(res->category.array_data.type == element_type,
503 "Redefinition of type %s does not match", name);
504 gras_assert1(res->category.array_data.fixed_size == fixed_size,
505 "Redefinition of type %s does not match", name);
506 gras_assert1(res->category.array_data.dynamic_size == NULL,
507 "Redefinition of type %s does not match", name);
508 VERB1("Discarding redefinition of %s",name);
513 TRY(gras_ddt_new(name,dst));
516 gras_assert1(fixed_size > 0, "'%s' is a array of negative fixed size",name);
517 for (arch=0; arch<gras_arch_count; arch ++) {
518 res->size[arch] = fixed_size * element_type->aligned_size[arch];
519 res->alignment[arch] = element_type->alignment[arch];
520 res->aligned_size[arch] = res->size[arch];
523 res->category_code = e_gras_datadesc_type_cat_array;
525 res->category.array_data.type = element_type;
526 res->category.array_data.fixed_size = fixed_size;
527 res->category.array_data.dynamic_size = NULL;
532 * gras_datadesc_array_dyn:
534 * Create a new array and give a pointer to it
537 gras_datadesc_array_dyn(const char *name,
538 gras_datadesc_type_t *element_type,
539 gras_datadesc_type_cb_int_t dynamic_size,
540 gras_datadesc_type_t **dst) {
542 gras_error_t errcode;
543 gras_datadesc_type_t *res;
546 GRAS_IN1("(%s)",name);
547 gras_assert1(dynamic_size,
548 "'%s' is a dynamic array without size discriminant",
551 res = gras_datadesc_by_name(name);
553 gras_assert1(res->category_code == e_gras_datadesc_type_cat_array,
554 "Redefinition of type %s does not match", name);
555 gras_assert1(res->category.array_data.type == element_type,
556 "Redefinition of type %s does not match", name);
557 gras_assert1(res->category.array_data.fixed_size == -1,
558 "Redefinition of type %s does not match", name);
559 gras_assert1(res->category.array_data.dynamic_size == dynamic_size,
560 "Redefinition of type %s does not match", name);
561 VERB1("Discarding redefinition of %s",name);
567 TRY(gras_ddt_new(name,dst));
570 for (arch=0; arch<gras_arch_count; arch ++) {
571 res->size[arch] = -1; /* make sure it indicates "dynamic" */
572 res->alignment[arch] = element_type->alignment[arch];
573 res->aligned_size[arch] = -1; /*FIXME: That was so in GS, but looks stupid*/
576 res->category_code = e_gras_datadesc_type_cat_array;
578 res->category.array_data.type = element_type;
579 res->category.array_data.fixed_size = -1;
580 res->category.array_data.dynamic_size = dynamic_size;
586 * gras_datadesc_ref_pop_arr:
588 * Most of the time, you want to include a reference in your structure which
589 * is a pointer to a dynamic array whose size is fixed by another field of
592 * This case pops up so often that this function was created to take care of
593 * this case. It creates a dynamic array type whose size is poped from the
594 * current cbps, and then create a reference to it.
596 * The name of the created datatype will be the name of the element type, with
597 * '[]*' appended to it.
599 * Then to use it, you just have to make sure that your structure pre-callback
600 * does push the size of the array in the cbps (using #gras_cbps_i_push), and
603 * But be remember that this is a stack. If you have two different pop_arr, you
604 * should push the second one first, so that the first one is on the top of the
605 * list when the first field gets transfered.
609 gras_datadesc_ref_pop_arr(gras_datadesc_type_t *element_type,
610 gras_datadesc_type_t **dst) {
611 gras_error_t errcode;
612 char *name=malloc(strlen(element_type->name) + 4);
614 sprintf(name,"%s[]",element_type->name);
616 TRY(gras_datadesc_array_dyn(name,element_type,
617 gras_datadesc_cb_pop, dst));
619 sprintf(name,"%s[]*",element_type->name);
620 TRY(gras_datadesc_ref(name,*dst,dst));
627 gras_datadesc_import_nws(const char *name,
628 const DataDescriptor *desc,
630 gras_datadesc_type_t **dst) {
635 * gras_datadesc_cb_send:
637 * Add a pre-send callback to this datadesc.
638 * (useful to push the sizes of the upcoming arrays, for example)
640 void gras_datadesc_cb_send (gras_datadesc_type_t *type,
641 gras_datadesc_type_cb_void_t send) {
645 * gras_datadesc_cb_recv:
647 * Add a post-receive callback to this datadesc.
648 * (useful to put the function pointers to the rigth value, for example)
650 void gras_datadesc_cb_recv(gras_datadesc_type_t *type,
651 gras_datadesc_type_cb_void_t recv) {
655 * gras_dd_find_field:
657 * Returns the type descriptor of the given field. Abort on error.
659 static gras_datadesc_type_t *
660 gras_dd_find_field(gras_datadesc_type_t *type,
661 const char *field_name) {
662 gras_datadesc_type_t *sub_type=NULL;
663 gras_dynar_t *field_array;
665 gras_dd_cat_field_t *field=NULL;
668 if (type->category_code == e_gras_datadesc_type_cat_union) {
669 field_array = type->category.union_data.fields;
670 } else if (type->category_code == e_gras_datadesc_type_cat_struct) {
671 field_array = type->category.struct_data.fields;
673 ERROR2("%s (%p) is not a struct nor an union. There is no field.", type->name,type);
676 gras_dynar_foreach(field_array,field_num,field) {
677 if (!strcmp(field_name,field->name)) {
681 ERROR2("No field nammed %s in %s",field_name,type->name);
687 * gras_datadesc_cb_field_send:
689 * Add a pre-send callback to the given field of the datadesc (which must be a struct or union).
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_datadesc_type_t *sub_type=gras_dd_find_field(type,field_name);
697 sub_type->send = send;
701 * gras_datadesc_cb_field_push:
703 * Add a pre-send callback to the given field resulting in its value to be pushed to
704 * the stack of sizes. It must be a int, unsigned int, long int or unsigned long int.
706 void gras_datadesc_cb_field_push (gras_datadesc_type_t *type,
707 const char *field_name) {
709 gras_datadesc_type_t *sub_type=gras_dd_find_field(type,field_name);
710 if (!strcmp("int",sub_type->name)) {
711 sub_type->send = gras_datadesc_cb_push_int;
712 } else if (!strcmp("unsigned int",sub_type->name)) {
713 sub_type->send = gras_datadesc_cb_push_uint;
714 } else if (!strcmp("long int",sub_type->name)) {
715 sub_type->send = gras_datadesc_cb_push_lint;
716 } else if (!strcmp("unsigned long int",sub_type->name)) {
717 sub_type->send = gras_datadesc_cb_push_ulint;
719 ERROR1("Field %s is not an int, unsigned int, long int neither unsigned long int",
725 * gras_datadesc_cb_field_recv:
727 * Add a post-receive callback to the given field of the datadesc (which must be a struct or union).
728 * (useful to put the function pointers to the right value, for example)
730 void gras_datadesc_cb_field_recv(gras_datadesc_type_t *type,
731 const char *field_name,
732 gras_datadesc_type_cb_void_t recv) {
734 gras_datadesc_type_t *sub_type=gras_dd_find_field(type,field_name);
735 sub_type->recv = recv;
739 * gras_datadesc_free:
741 * Free a datadesc. Should only be called at gras_exit.
743 void gras_datadesc_free(gras_datadesc_type_t *type) {
745 DEBUG1("Let's free ddt %s",type->name);
747 switch (type->category_code) {
748 case e_gras_datadesc_type_cat_scalar:
749 case e_gras_datadesc_type_cat_ref:
750 case e_gras_datadesc_type_cat_array:
751 /* nothing to free in there */
754 case e_gras_datadesc_type_cat_ignored:
755 if (type->category.ignored_data.free_func) {
756 type->category.ignored_data.free_func
757 (type->category.ignored_data.default_value);
761 case e_gras_datadesc_type_cat_struct:
762 gras_dynar_free(type->category.struct_data.fields);
765 case e_gras_datadesc_type_cat_union:
766 gras_dynar_free(type->category.union_data.fields);
770 /* datadesc was invalid. Killing it is like euthanasy, I guess */