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 = 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];
117 sz = res->size[arch];
120 /* just in case you wonder, x>>1 == x/2 on all architectures when x>=0
121 and a size is always>=0 */
123 /* make sure mask have all the bits under the biggest one of size set to 1
124 Example: size=000100101 => mask=0000111111 */
129 if (res->size[arch] & (mask >> 1)) { /* if size have bits to one beside its biggest */
130 /* size is not a power of 2 */
131 /* alignment= next power of 2 after size */
132 res->alignment[arch] = (res->size[arch] & ~(mask >> 1)) << 1;
133 gras_assert0(res->alignment[arch] != 0,
134 "scalar type too large");
136 res->aligned_size[arch] = aligned(res->size[arch], res->alignment[arch]);
137 gras_assert0 (res->aligned_size[arch] >= 0,
138 "scalar type too large");
141 /* size is a power of 2, life is great */
142 res->alignment[arch] = res->size[arch];
143 res->aligned_size[arch] = res->size[arch];
147 res->category_code = e_gras_datadesc_type_cat_scalar;
148 res->category.scalar_data.encoding = encoding;
149 res->category.scalar_data.type = type;
156 * gras_dd_cat_field_free:
158 * Frees one struct or union field
160 void gras_dd_cat_field_free(void *f) {
161 gras_dd_cat_field_t *field = *(gras_dd_cat_field_t **)f;
165 gras_free(field->name);
172 * gras_datadesc_struct:
174 * Create a new struct and give a pointer to it
177 gras_datadesc_struct(const char *name,
178 gras_datadesc_type_t **dst) {
180 gras_error_t errcode;
181 gras_datadesc_type_t *res;
184 GRAS_IN1("(%s)",name);
185 res = gras_datadesc_by_name(name);
187 /* FIXME: Check that field redefinition matches */
188 gras_assert1(res->category_code == e_gras_datadesc_type_cat_struct,
189 "Redefinition of type %s does not match", name);
190 VERB1("Discarding redefinition of %s",name);
194 TRY(gras_ddt_new(name,dst));
197 for (arch=0; arch<gras_arch_count; arch ++) {
199 res->alignment[arch] = 0;
200 res->aligned_size[arch] = 0;
202 res->category_code = e_gras_datadesc_type_cat_struct;
203 TRY(gras_dynar_new(&(res->category.struct_data.fields),
204 sizeof(gras_dd_cat_field_t*),
205 &gras_dd_cat_field_free));
212 * gras_datadesc_struct_append:
214 * Append a field to the struct
217 gras_datadesc_struct_append(gras_datadesc_type_t *struct_type,
219 gras_datadesc_type_t *field_type) {
221 gras_error_t errcode;
222 gras_dd_cat_field_t *field;
225 GRAS_IN3("(%s %s.%s;)",field_type->name,struct_type->name,name);
226 if (struct_type->category.struct_data.closed) {
227 VERB1("Ignoring request to add field to struct %s (closed. Redefinition?)",
232 gras_assert1(field_type->size >= 0,
233 "Cannot add a dynamically sized field in structure %s",
236 field=gras_new(gras_dd_cat_field_t,1);
240 field->name = strdup(name);
242 DEBUG0("----------------");
243 DEBUG3("PRE s={size=%ld,align=%ld,asize=%ld}",
244 struct_type->size[GRAS_THISARCH],
245 struct_type->alignment[GRAS_THISARCH],
246 struct_type->aligned_size[GRAS_THISARCH]);
249 for (arch=0; arch<gras_arch_count; arch ++) {
250 field->offset[arch] = aligned(struct_type->size[arch],
251 min(field_type->alignment[arch],
252 gras_arches[arch].boundaries));
254 struct_type->size[arch] = field->offset[arch] + field_type->size[arch];
255 struct_type->alignment[arch] = max(struct_type->alignment[arch],
256 field_type->alignment[arch]);
257 struct_type->aligned_size[arch] = aligned(struct_type->size[arch],
258 struct_type->alignment[arch]);
260 field->type = field_type;
264 TRY(gras_dynar_push(struct_type->category.struct_data.fields, &field));
266 DEBUG3("Push a %s into %s at offset %ld.",
267 field_type->name, struct_type->name,field->offset[GRAS_THISARCH]);
268 DEBUG3(" f={size=%ld,align=%ld,asize=%ld}",
269 field_type->size[GRAS_THISARCH],
270 field_type->alignment[GRAS_THISARCH],
271 field_type->aligned_size[GRAS_THISARCH]);
272 DEBUG3(" s={size=%ld,align=%ld,asize=%ld}",
273 struct_type->size[GRAS_THISARCH],
274 struct_type->alignment[GRAS_THISARCH],
275 struct_type->aligned_size[GRAS_THISARCH]);
280 gras_datadesc_struct_close(gras_datadesc_type_t *struct_type) {
282 struct_type->category.struct_data.closed = 1;
286 * gras_datadesc_union:
288 * Create a new union and give a pointer to it
291 gras_datadesc_union(const char *name,
292 gras_datadesc_type_cb_int_t selector,
293 gras_datadesc_type_t **dst) {
295 gras_error_t errcode;
296 gras_datadesc_type_t *res;
299 GRAS_IN1("(%s)",name);
300 gras_assert0(selector,
301 "Attempt to creat an union without field_count function");
303 res = gras_datadesc_by_name(name);
305 /* FIXME: Check that field redefinition matches */
306 gras_assert1(res->category_code == e_gras_datadesc_type_cat_union,
307 "Redefinition of type %s does not match", name);
308 gras_assert1(res->category.union_data.selector == selector,
309 "Redefinition of type %s does not match", name);
310 VERB1("Discarding redefinition of %s",name);
315 TRY(gras_ddt_new(name,dst));
318 for (arch=0; arch<gras_arch_count; arch ++) {
320 res->alignment[arch] = 0;
321 res->aligned_size[arch] = 0;
324 res->category_code = e_gras_datadesc_type_cat_union;
325 TRY(gras_dynar_new(&(res->category.union_data.fields),
326 sizeof(gras_dd_cat_field_t*),
327 &gras_dd_cat_field_free));
328 res->category.union_data.selector = selector;
334 * gras_datadesc_union_append:
336 * Append a field to the union
339 gras_datadesc_union_append(gras_datadesc_type_t *union_type,
341 gras_datadesc_type_t *field_type) {
343 gras_error_t errcode;
344 gras_dd_cat_field_t *field;
347 GRAS_IN3("(%s %s.%s;)",field_type->name,union_type->name,name);
348 gras_assert1(field_type->size >= 0,
349 "Cannot add a dynamically sized field in union %s",
352 if (union_type->category.union_data.closed) {
353 VERB1("Ignoring request to add field to union %s (closed)",
358 field=gras_new0(gras_dd_cat_field_t,1);
362 field->name = strdup(name);
363 field->type = field_type;
364 /* All offset are left to 0 in an union */
366 TRY(gras_dynar_push(union_type->category.union_data.fields, &field));
368 for (arch=0; arch<gras_arch_count; arch ++) {
369 union_type->size[arch] = max(union_type->size[arch],
370 field_type->size[arch]);
371 union_type->alignment[arch] = max(union_type->alignment[arch],
372 field_type->alignment[arch]);
373 union_type->aligned_size[arch] = aligned(union_type->size[arch],
374 union_type->alignment[arch]);
380 gras_datadesc_union_close(gras_datadesc_type_t *union_type) {
381 union_type->category.union_data.closed = 1;
386 * Create a new ref to a fixed type and give a pointer to it
389 gras_datadesc_ref(const char *name,
390 gras_datadesc_type_t *referenced_type,
391 gras_datadesc_type_t **dst) {
393 gras_error_t errcode;
394 gras_datadesc_type_t *res;
395 gras_datadesc_type_t *pointer_type = gras_datadesc_by_name("data pointer");
398 GRAS_IN1("(%s)",name);
399 res = gras_datadesc_by_name(name);
401 gras_assert1(res->category_code == e_gras_datadesc_type_cat_ref,
402 "Redefinition of %s does not match",name);
403 gras_assert1(res->category.ref_data.type == referenced_type,
404 "Redefinition of %s does not match",name);
405 gras_assert1(res->category.ref_data.selector == NULL,
406 "Redefinition of %s does not match",name);
407 VERB1("Discarding redefinition of %s",name);
412 TRY(gras_ddt_new(name,dst));
415 gras_assert0(pointer_type, "Cannot get the description of data pointer");
417 for (arch=0; arch<gras_arch_count; arch ++){
418 res->size[arch] = pointer_type->size[arch];
419 res->alignment[arch] = pointer_type->alignment[arch];
420 res->aligned_size[arch] = pointer_type->aligned_size[arch];
423 res->category_code = e_gras_datadesc_type_cat_ref;
424 res->category.ref_data.type = referenced_type;
425 res->category.ref_data.selector = NULL;
430 * gras_datadesc_ref_generic:
432 * Create a new ref to a type given at use time, and give a pointer to it
435 gras_datadesc_ref_generic(const char *name,
436 gras_datadesc_selector_t selector,
437 gras_datadesc_type_t **dst) {
439 gras_error_t errcode;
440 gras_datadesc_type_t *res;
441 gras_datadesc_type_t *pointer_type = gras_datadesc_by_name("data pointer");
444 GRAS_IN1("(%s)",name);
445 res = gras_datadesc_by_name(name);
447 gras_assert1(res->category_code == e_gras_datadesc_type_cat_ref,
448 "Redefinition of type %s does not match", name);
449 gras_assert1(res->category.ref_data.type == NULL,
450 "Redefinition of type %s does not match", name);
451 gras_assert1(res->category.ref_data.selector == selector,
452 "Redefinition of type %s does not match", name);
453 VERB1("Discarding redefinition of %s",name);
457 TRY(gras_ddt_new(name,dst));
460 gras_assert0(pointer_type, "Cannot get the description of data pointer");
462 for (arch=0; arch<gras_arch_count; arch ++) {
463 res->size[arch] = pointer_type->size[arch];
464 res->alignment[arch] = pointer_type->alignment[arch];
465 res->aligned_size[arch] = pointer_type->aligned_size[arch];
468 res->category_code = e_gras_datadesc_type_cat_ref;
470 res->category.ref_data.type = NULL;
471 res->category.ref_data.selector = selector;
477 * gras_datadesc_array_fixed:
479 * Create a new array and give a pointer to it
482 gras_datadesc_array_fixed(const char *name,
483 gras_datadesc_type_t *element_type,
485 gras_datadesc_type_t **dst) {
487 gras_error_t errcode;
488 gras_datadesc_type_t *res;
491 GRAS_IN1("(%s)",name);
492 res = gras_datadesc_by_name(name);
494 gras_assert1(res->category_code == e_gras_datadesc_type_cat_array,
495 "Redefinition of type %s does not match", name);
496 gras_assert1(res->category.array_data.type == element_type,
497 "Redefinition of type %s does not match", name);
498 gras_assert1(res->category.array_data.fixed_size == fixed_size,
499 "Redefinition of type %s does not match", name);
500 gras_assert1(res->category.array_data.dynamic_size == NULL,
501 "Redefinition of type %s does not match", name);
502 VERB1("Discarding redefinition of %s",name);
507 TRY(gras_ddt_new(name,dst));
510 gras_assert1(fixed_size > 0, "'%s' is a array of negative fixed size",name);
511 for (arch=0; arch<gras_arch_count; arch ++) {
512 res->size[arch] = fixed_size * element_type->aligned_size[arch];
513 res->alignment[arch] = element_type->alignment[arch];
514 res->aligned_size[arch] = res->size[arch];
517 res->category_code = e_gras_datadesc_type_cat_array;
519 res->category.array_data.type = element_type;
520 res->category.array_data.fixed_size = fixed_size;
521 res->category.array_data.dynamic_size = NULL;
526 * gras_datadesc_array_dyn:
528 * Create a new array and give a pointer to it
531 gras_datadesc_array_dyn(const char *name,
532 gras_datadesc_type_t *element_type,
533 gras_datadesc_type_cb_int_t dynamic_size,
534 gras_datadesc_type_t **dst) {
536 gras_error_t errcode;
537 gras_datadesc_type_t *res;
540 GRAS_IN1("(%s)",name);
541 gras_assert1(dynamic_size,
542 "'%s' is a dynamic array without size discriminant",
545 res = gras_datadesc_by_name(name);
547 gras_assert1(res->category_code == e_gras_datadesc_type_cat_array,
548 "Redefinition of type %s does not match", name);
549 gras_assert1(res->category.array_data.type == element_type,
550 "Redefinition of type %s does not match", name);
551 gras_assert1(res->category.array_data.fixed_size == -1,
552 "Redefinition of type %s does not match", name);
553 gras_assert1(res->category.array_data.dynamic_size == dynamic_size,
554 "Redefinition of type %s does not match", name);
555 VERB1("Discarding redefinition of %s",name);
561 TRY(gras_ddt_new(name,dst));
564 for (arch=0; arch<gras_arch_count; arch ++) {
565 res->size[arch] = -1; /* make sure it indicates "dynamic" */
566 res->alignment[arch] = element_type->alignment[arch];
567 res->aligned_size[arch] = -1; /*FIXME: That was so in GS, but looks stupid*/
570 res->category_code = e_gras_datadesc_type_cat_array;
572 res->category.array_data.type = element_type;
573 res->category.array_data.fixed_size = -1;
574 res->category.array_data.dynamic_size = dynamic_size;
580 * gras_datadesc_ref_pop_arr:
582 * Most of the time, you want to include a reference in your structure which
583 * is a pointer to a dynamic array whose size is fixed by another field of
586 * This case pops up so often that this function was created to take care of
587 * this case. It creates a dynamic array type whose size is poped from the
588 * current cbps, and then create a reference to it.
590 * The name of the created datatype will be the name of the element type, with
591 * '[]*' appended to it.
593 * Then to use it, you just have to make sure that your structure pre-callback
594 * does push the size of the array in the cbps (using #gras_cbps_i_push), and
597 * But be remember that this is a stack. If you have two different pop_arr, you
598 * should push the second one first, so that the first one is on the top of the
599 * list when the first field gets transfered.
603 gras_datadesc_ref_pop_arr(gras_datadesc_type_t *element_type,
604 gras_datadesc_type_t **dst) {
605 gras_error_t errcode;
606 char *name=(char*)gras_malloc(strlen(element_type->name) + 4);
608 sprintf(name,"%s[]",element_type->name);
610 TRY(gras_datadesc_array_dyn(name,element_type,
611 gras_datadesc_cb_pop, dst));
613 sprintf(name,"%s[]*",element_type->name);
614 TRY(gras_datadesc_ref(name,*dst,dst));
621 gras_datadesc_import_nws(const char *name,
622 const DataDescriptor *desc,
624 gras_datadesc_type_t **dst) {
629 * gras_datadesc_cb_send:
631 * Add a pre-send callback to this datadesc.
632 * (useful to push the sizes of the upcoming arrays, for example)
634 void gras_datadesc_cb_send (gras_datadesc_type_t *type,
635 gras_datadesc_type_cb_void_t send) {
639 * gras_datadesc_cb_recv:
641 * Add a post-receive callback to this datadesc.
642 * (useful to put the function pointers to the rigth value, for example)
644 void gras_datadesc_cb_recv(gras_datadesc_type_t *type,
645 gras_datadesc_type_cb_void_t recv) {
649 * gras_dd_find_field:
651 * Returns the type descriptor of the given field. Abort on error.
653 static gras_datadesc_type_t *
654 gras_dd_find_field(gras_datadesc_type_t *type,
655 const char *field_name) {
656 gras_datadesc_type_t *sub_type=NULL;
657 gras_dynar_t *field_array;
659 gras_dd_cat_field_t *field=NULL;
662 if (type->category_code == e_gras_datadesc_type_cat_union) {
663 field_array = type->category.union_data.fields;
664 } else if (type->category_code == e_gras_datadesc_type_cat_struct) {
665 field_array = type->category.struct_data.fields;
667 ERROR2("%s (%p) is not a struct nor an union. There is no field.", type->name,type);
670 gras_dynar_foreach(field_array,field_num,field) {
671 if (!strcmp(field_name,field->name)) {
675 ERROR2("No field nammed %s in %s",field_name,type->name);
681 * gras_datadesc_cb_field_send:
683 * Add a pre-send callback to the given field of the datadesc (which must be a struct or union).
684 * (useful to push the sizes of the upcoming arrays, for example)
686 void gras_datadesc_cb_field_send (gras_datadesc_type_t *type,
687 const char *field_name,
688 gras_datadesc_type_cb_void_t send) {
690 gras_datadesc_type_t *sub_type=gras_dd_find_field(type,field_name);
691 sub_type->send = send;
695 * gras_datadesc_cb_field_push:
697 * Add a pre-send callback to the given field resulting in its value to be pushed to
698 * the stack of sizes. It must be a int, unsigned int, long int or unsigned long int.
700 void gras_datadesc_cb_field_push (gras_datadesc_type_t *type,
701 const char *field_name) {
703 gras_datadesc_type_t *sub_type=gras_dd_find_field(type,field_name);
704 if (!strcmp("int",sub_type->name)) {
705 sub_type->send = gras_datadesc_cb_push_int;
706 } else if (!strcmp("unsigned int",sub_type->name)) {
707 sub_type->send = gras_datadesc_cb_push_uint;
708 } else if (!strcmp("long int",sub_type->name)) {
709 sub_type->send = gras_datadesc_cb_push_lint;
710 } else if (!strcmp("unsigned long int",sub_type->name)) {
711 sub_type->send = gras_datadesc_cb_push_ulint;
713 ERROR1("Field %s is not an int, unsigned int, long int neither unsigned long int",
719 * gras_datadesc_cb_field_recv:
721 * Add a post-receive callback to the given field of the datadesc (which must be a struct or union).
722 * (useful to put the function pointers to the right value, for example)
724 void gras_datadesc_cb_field_recv(gras_datadesc_type_t *type,
725 const char *field_name,
726 gras_datadesc_type_cb_void_t recv) {
728 gras_datadesc_type_t *sub_type=gras_dd_find_field(type,field_name);
729 sub_type->recv = recv;
733 * gras_datadesc_free:
735 * Free a datadesc. Should only be called at gras_exit.
737 void gras_datadesc_free(gras_datadesc_type_t *type) {
739 DEBUG1("Let's free ddt %s",type->name);
741 switch (type->category_code) {
742 case e_gras_datadesc_type_cat_scalar:
743 case e_gras_datadesc_type_cat_ref:
744 case e_gras_datadesc_type_cat_array:
745 /* nothing to free in there */
748 case e_gras_datadesc_type_cat_ignored:
749 if (type->category.ignored_data.free_func) {
750 type->category.ignored_data.free_func
751 (type->category.ignored_data.default_value);
755 case e_gras_datadesc_type_cat_struct:
756 gras_dynar_free(type->category.struct_data.fields);
759 case e_gras_datadesc_type_cat_union:
760 gras_dynar_free(type->category.union_data.fields);
764 /* datadesc was invalid. Killing it is like euthanasy, I guess */
767 gras_free(type->name);