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);
37 res->name = (char*)strdup(name);
38 res->name_len = strlen(name);
41 gras_set_add(gras_datadesc_set_local,
42 (gras_set_elm_t*)res,&gras_ddt_freev);
47 * gras_datadesc_by_name:
49 * Retrieve a type from its name
51 gras_datadesc_type_t *gras_datadesc_by_name(const char *name) {
53 gras_datadesc_type_t *type;
55 GRAS_IN1("(%s)",name);
56 if (gras_set_get_by_name(gras_datadesc_set_local,
57 name,(gras_set_elm_t**)&type) == no_error) {
67 * gras_datadesc_by_id:
69 * Retrieve a type from its code
71 gras_error_t gras_datadesc_by_id(long int code,
72 gras_datadesc_type_t **type) {
74 return gras_set_get_by_id(gras_datadesc_set_local,
75 code,(gras_set_elm_t**)type);
79 * gras_datadesc_scalar:
81 * Create a new scalar and give a pointer to it
83 gras_datadesc_type_t *
84 gras_datadesc_scalar(const char *name,
85 gras_ddt_scalar_type_t type,
86 enum e_gras_dd_scalar_encoding encoding) {
88 gras_datadesc_type_t *res;
92 res = gras_datadesc_by_name(name);
94 gras_assert1(res->category_code == e_gras_datadesc_type_cat_scalar,
95 "Redefinition of type %s does not match", name);
96 gras_assert1(res->category.scalar_data.encoding == encoding,
97 "Redefinition of type %s does not match", name);
98 gras_assert1(res->category.scalar_data.type == type,
99 "Redefinition of type %s does not match", name);
100 VERB1("Discarding redefinition of %s",name);
103 gras_ddt_new(name,&res);
105 for (arch = 0; arch < gras_arch_count; arch ++) {
108 res->size[arch] = gras_arches[arch].sizeofs[type];
109 res->alignment[arch] = gras_arches[arch].boundaries[type];
110 res->aligned_size[arch] = aligned(res->size[arch], res->alignment[arch]);
111 /* FIXME: kill the following after discution with Oli */
113 sz = res->size[arch];
116 /* just in case you wonder, x>>1 == x/2 on all architectures when x>=0
117 and a size is always>=0 */
119 /* make sure mask have all the bits under the biggest one of size set to 1
120 Example: size=000100101 => mask=0000111111 */
125 if (res->size[arch] & (mask >> 1)) { /* if size have bits to one beside its biggest */
126 /* size is not a power of 2 */
127 /* alignment= next power of 2 after size */
128 res->alignment[arch] = (res->size[arch] & ~(mask >> 1)) << 1;
129 gras_assert0(res->alignment[arch] != 0,
130 "scalar type too large");
132 res->aligned_size[arch] = aligned(res->size[arch], res->alignment[arch]);
133 gras_assert0 (res->aligned_size[arch] >= 0,
134 "scalar type too large");
137 /* size is a power of 2, life is great */
138 res->alignment[arch] = res->size[arch];
139 res->aligned_size[arch] = res->size[arch];
144 res->category_code = e_gras_datadesc_type_cat_scalar;
145 res->category.scalar_data.encoding = encoding;
146 res->category.scalar_data.type = type;
154 * gras_dd_cat_field_free:
156 * Frees one struct or union field
158 void gras_dd_cat_field_free(void *f) {
159 gras_dd_cat_field_t *field = *(gras_dd_cat_field_t **)f;
163 gras_free(field->name);
170 * gras_datadesc_struct:
172 * Create a new struct and give a pointer to it
174 gras_datadesc_type_t *
175 gras_datadesc_struct(const char *name) {
177 gras_error_t errcode;
178 gras_datadesc_type_t *res;
181 GRAS_IN1("(%s)",name);
182 res = gras_datadesc_by_name(name);
184 /* FIXME: Check that field redefinition matches */
185 gras_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 gras_ddt_new(name,&res);
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 gras_dynar_new(&(res->category.struct_data.fields),
199 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_error_t errcode;
217 gras_dd_cat_field_t *field;
220 GRAS_IN3("(%s %s.%s;)",field_type->name,struct_type->name,name);
221 if (struct_type->category.struct_data.closed) {
222 VERB1("Ignoring request to add field to struct %s (closed. Redefinition?)",
227 gras_assert1(field_type->size != 0,
228 "Cannot add a dynamically sized field in structure %s",
231 field=gras_new(gras_dd_cat_field_t,1);
232 field->name = (char*)strdup(name);
234 DEBUG0("----------------");
235 DEBUG3("PRE s={size=%ld,align=%ld,asize=%ld}",
236 struct_type->size[GRAS_THISARCH],
237 struct_type->alignment[GRAS_THISARCH],
238 struct_type->aligned_size[GRAS_THISARCH]);
241 for (arch=0; arch<gras_arch_count; arch ++) {
242 field->offset[arch] = aligned(struct_type->size[arch],
243 field_type->alignment[arch]);
245 struct_type->size[arch] = field->offset[arch] + field_type->size[arch];
246 struct_type->alignment[arch] = max(struct_type->alignment[arch],
247 field_type->alignment[arch]);
248 struct_type->aligned_size[arch] = aligned(struct_type->size[arch],
249 struct_type->alignment[arch]);
251 field->type = field_type;
255 gras_dynar_push(struct_type->category.struct_data.fields, &field);
257 DEBUG3("Push a %s into %s at offset %ld.",
258 field_type->name, struct_type->name,field->offset[GRAS_THISARCH]);
259 DEBUG3(" f={size=%ld,align=%ld,asize=%ld}",
260 field_type->size[GRAS_THISARCH],
261 field_type->alignment[GRAS_THISARCH],
262 field_type->aligned_size[GRAS_THISARCH]);
263 DEBUG3(" s={size=%ld,align=%ld,asize=%ld}",
264 struct_type->size[GRAS_THISARCH],
265 struct_type->alignment[GRAS_THISARCH],
266 struct_type->aligned_size[GRAS_THISARCH]);
271 gras_datadesc_struct_close(gras_datadesc_type_t *struct_type) {
273 struct_type->category.struct_data.closed = 1;
277 * gras_datadesc_union:
279 * Create a new union and give a pointer to it
281 gras_datadesc_type_t *
282 gras_datadesc_union(const char *name,
283 gras_datadesc_type_cb_int_t selector) {
285 gras_error_t errcode;
286 gras_datadesc_type_t *res;
289 GRAS_IN1("(%s)",name);
290 gras_assert0(selector,
291 "Attempt to creat an union without field_count function");
293 res = gras_datadesc_by_name(name);
295 /* FIXME: Check that field redefinition matches */
296 gras_assert1(res->category_code == e_gras_datadesc_type_cat_union,
297 "Redefinition of type %s does not match", name);
298 gras_assert1(res->category.union_data.selector == selector,
299 "Redefinition of type %s does not match", name);
300 VERB1("Discarding redefinition of %s",name);
304 gras_ddt_new(name,&res);
306 for (arch=0; arch<gras_arch_count; arch ++) {
308 res->alignment[arch] = 0;
309 res->aligned_size[arch] = 0;
312 res->category_code = e_gras_datadesc_type_cat_union;
313 gras_dynar_new(&(res->category.union_data.fields),
314 sizeof(gras_dd_cat_field_t*),
315 &gras_dd_cat_field_free);
316 res->category.union_data.selector = selector;
322 * gras_datadesc_union_append:
324 * Append a field to the union
327 gras_datadesc_union_append(gras_datadesc_type_t *union_type,
329 gras_datadesc_type_t *field_type) {
331 gras_dd_cat_field_t *field;
334 GRAS_IN3("(%s %s.%s;)",field_type->name,union_type->name,name);
335 gras_assert1(field_type->size != 0,
336 "Cannot add a dynamically sized field in union %s",
339 if (union_type->category.union_data.closed) {
340 VERB1("Ignoring request to add field to union %s (closed)",
345 field=gras_new0(gras_dd_cat_field_t,1);
347 field->name = (char*)strdup(name);
348 field->type = field_type;
349 /* All offset are left to 0 in an union */
351 gras_dynar_push(union_type->category.union_data.fields, &field);
353 for (arch=0; arch<gras_arch_count; arch ++) {
354 union_type->size[arch] = max(union_type->size[arch],
355 field_type->size[arch]);
356 union_type->alignment[arch] = max(union_type->alignment[arch],
357 field_type->alignment[arch]);
358 union_type->aligned_size[arch] = aligned(union_type->size[arch],
359 union_type->alignment[arch]);
364 gras_datadesc_union_close(gras_datadesc_type_t *union_type) {
365 union_type->category.union_data.closed = 1;
370 * Create a new ref to a fixed type and give a pointer to it
372 gras_datadesc_type_t *
373 gras_datadesc_ref(const char *name,
374 gras_datadesc_type_t *referenced_type) {
376 gras_error_t errcode;
377 gras_datadesc_type_t *res;
378 gras_datadesc_type_t *pointer_type = gras_datadesc_by_name("data pointer");
381 GRAS_IN1("(%s)",name);
382 res = gras_datadesc_by_name(name);
384 gras_assert1(res->category_code == e_gras_datadesc_type_cat_ref,
385 "Redefinition of %s does not match",name);
386 gras_assert1(res->category.ref_data.type == referenced_type,
387 "Redefinition of %s does not match",name);
388 gras_assert1(res->category.ref_data.selector == NULL,
389 "Redefinition of %s does not match",name);
390 VERB1("Discarding redefinition of %s",name);
394 gras_ddt_new(name,&res);
396 gras_assert0(pointer_type, "Cannot get the description of data pointer");
398 for (arch=0; arch<gras_arch_count; arch ++){
399 res->size[arch] = pointer_type->size[arch];
400 res->alignment[arch] = pointer_type->alignment[arch];
401 res->aligned_size[arch] = pointer_type->aligned_size[arch];
404 res->category_code = e_gras_datadesc_type_cat_ref;
405 res->category.ref_data.type = referenced_type;
406 res->category.ref_data.selector = NULL;
411 * gras_datadesc_ref_generic:
413 * Create a new ref to a type given at use time, and give a pointer to it
415 gras_datadesc_type_t *
416 gras_datadesc_ref_generic(const char *name,
417 gras_datadesc_selector_t selector) {
419 gras_datadesc_type_t *res;
420 gras_datadesc_type_t *pointer_type = gras_datadesc_by_name("data pointer");
423 GRAS_IN1("(%s)",name);
424 res = gras_datadesc_by_name(name);
426 gras_assert1(res->category_code == e_gras_datadesc_type_cat_ref,
427 "Redefinition of type %s does not match", name);
428 gras_assert1(res->category.ref_data.type == NULL,
429 "Redefinition of type %s does not match", name);
430 gras_assert1(res->category.ref_data.selector == selector,
431 "Redefinition of type %s does not match", name);
432 VERB1("Discarding redefinition of %s",name);
435 gras_ddt_new(name,&res);
437 gras_assert0(pointer_type, "Cannot get the description of data pointer");
439 for (arch=0; arch<gras_arch_count; arch ++) {
440 res->size[arch] = pointer_type->size[arch];
441 res->alignment[arch] = pointer_type->alignment[arch];
442 res->aligned_size[arch] = pointer_type->aligned_size[arch];
445 res->category_code = e_gras_datadesc_type_cat_ref;
447 res->category.ref_data.type = NULL;
448 res->category.ref_data.selector = selector;
454 * gras_datadesc_array_fixed:
456 * Create a new array and give a pointer to it
458 gras_datadesc_type_t *
459 gras_datadesc_array_fixed(const char *name,
460 gras_datadesc_type_t *element_type,
461 long int fixed_size) {
463 gras_datadesc_type_t *res;
466 GRAS_IN1("(%s)",name);
467 res = gras_datadesc_by_name(name);
469 gras_assert1(res->category_code == e_gras_datadesc_type_cat_array,
470 "Redefinition of type %s does not match", name);
471 gras_assert1(res->category.array_data.type == element_type,
472 "Redefinition of type %s does not match", name);
473 gras_assert1(res->category.array_data.fixed_size == fixed_size,
474 "Redefinition of type %s does not match", name);
475 gras_assert1(res->category.array_data.dynamic_size == NULL,
476 "Redefinition of type %s does not match", name);
477 VERB1("Discarding redefinition of %s",name);
481 gras_ddt_new(name,&res);
483 gras_assert1(fixed_size > 0, "'%s' is a array of null fixed size",name);
484 for (arch=0; arch<gras_arch_count; arch ++) {
485 res->size[arch] = fixed_size * element_type->aligned_size[arch];
486 res->alignment[arch] = element_type->alignment[arch];
487 res->aligned_size[arch] = res->size[arch];
490 res->category_code = e_gras_datadesc_type_cat_array;
492 res->category.array_data.type = element_type;
493 res->category.array_data.fixed_size = fixed_size;
494 res->category.array_data.dynamic_size = NULL;
499 * gras_datadesc_array_dyn:
501 * Create a new array and give a pointer to it
503 gras_datadesc_type_t *
504 gras_datadesc_array_dyn(const char *name,
505 gras_datadesc_type_t *element_type,
506 gras_datadesc_type_cb_int_t dynamic_size) {
508 gras_datadesc_type_t *res;
511 GRAS_IN1("(%s)",name);
512 gras_assert1(dynamic_size,
513 "'%s' is a dynamic array without size discriminant",
516 res = gras_datadesc_by_name(name);
518 gras_assert1(res->category_code == e_gras_datadesc_type_cat_array,
519 "Redefinition of type %s does not match", name);
520 gras_assert1(res->category.array_data.type == element_type,
521 "Redefinition of type %s does not match", name);
522 gras_assert1(res->category.array_data.fixed_size == 0,
523 "Redefinition of type %s does not match", name);
524 gras_assert1(res->category.array_data.dynamic_size == dynamic_size,
525 "Redefinition of type %s does not match", name);
526 VERB1("Discarding redefinition of %s",name);
531 gras_ddt_new(name,&res);
533 for (arch=0; arch<gras_arch_count; arch ++) {
534 res->size[arch] = 0; /* make sure it indicates "dynamic" */
535 res->alignment[arch] = element_type->alignment[arch];
536 res->aligned_size[arch] = 0; /*FIXME: That was so in GS, but looks stupid*/
539 res->category_code = e_gras_datadesc_type_cat_array;
541 res->category.array_data.type = element_type;
542 res->category.array_data.fixed_size = 0;
543 res->category.array_data.dynamic_size = dynamic_size;
549 * gras_datadesc_ref_pop_arr:
551 * Most of the time, you want to include a reference in your structure which
552 * is a pointer to a dynamic array whose size is fixed by another field of
555 * This case pops up so often that this function was created to take care of
556 * this case. It creates a dynamic array type whose size is poped from the
557 * current cbps, and then create a reference to it.
559 * The name of the created datatype will be the name of the element type, with
560 * '[]*' appended to it.
562 * Then to use it, you just have to make sure that your structure pre-callback
563 * does push the size of the array in the cbps (using #gras_cbps_i_push), and
566 * But be remember that this is a stack. If you have two different pop_arr, you
567 * should push the second one first, so that the first one is on the top of the
568 * list when the first field gets transfered.
571 gras_datadesc_type_t *
572 gras_datadesc_ref_pop_arr(gras_datadesc_type_t *element_type) {
574 gras_datadesc_type_t *res;
575 char *name=(char*)gras_malloc(strlen(element_type->name) + 4);
577 sprintf(name,"%s[]",element_type->name);
579 res = gras_datadesc_array_dyn(name,element_type,
580 gras_datadesc_cb_pop);
582 sprintf(name,"%s[]*",element_type->name);
583 res = gras_datadesc_ref(name,res);
590 gras_datadesc_import_nws(const char *name,
591 const DataDescriptor *desc,
593 gras_datadesc_type_t **dst) {
598 * gras_datadesc_cb_send:
600 * Add a pre-send callback to this datadesc.
601 * (useful to push the sizes of the upcoming arrays, for example)
603 void gras_datadesc_cb_send (gras_datadesc_type_t *type,
604 gras_datadesc_type_cb_void_t send) {
608 * gras_datadesc_cb_recv:
610 * Add a post-receive callback to this datadesc.
611 * (useful to put the function pointers to the rigth value, for example)
613 void gras_datadesc_cb_recv(gras_datadesc_type_t *type,
614 gras_datadesc_type_cb_void_t recv) {
618 * gras_dd_find_field:
620 * Returns the type descriptor of the given field. Abort on error.
622 static gras_datadesc_type_t *
623 gras_dd_find_field(gras_datadesc_type_t *type,
624 const char *field_name) {
625 gras_datadesc_type_t *sub_type=NULL;
626 gras_dynar_t *field_array;
628 gras_dd_cat_field_t *field=NULL;
631 if (type->category_code == e_gras_datadesc_type_cat_union) {
632 field_array = type->category.union_data.fields;
633 } else if (type->category_code == e_gras_datadesc_type_cat_struct) {
634 field_array = type->category.struct_data.fields;
636 ERROR2("%s (%p) is not a struct nor an union. There is no field.", type->name,(void*)type);
639 gras_dynar_foreach(field_array,field_num,field) {
640 if (!strcmp(field_name,field->name)) {
644 ERROR2("No field nammed %s in %s",field_name,type->name);
650 * gras_datadesc_cb_field_send:
652 * Add a pre-send callback to the given field of the datadesc (which must be a struct or union).
653 * (useful to push the sizes of the upcoming arrays, for example)
655 void gras_datadesc_cb_field_send (gras_datadesc_type_t *type,
656 const char *field_name,
657 gras_datadesc_type_cb_void_t send) {
659 gras_datadesc_type_t *sub_type=gras_dd_find_field(type,field_name);
660 sub_type->send = send;
664 * gras_datadesc_cb_field_push:
666 * Add a pre-send callback to the given field resulting in its value to be pushed to
667 * the stack of sizes. It must be a int, unsigned int, long int or unsigned long int.
669 void gras_datadesc_cb_field_push (gras_datadesc_type_t *type,
670 const char *field_name) {
672 gras_datadesc_type_t *sub_type=gras_dd_find_field(type,field_name);
673 if (!strcmp("int",sub_type->name)) {
674 sub_type->send = gras_datadesc_cb_push_int;
675 } else if (!strcmp("unsigned int",sub_type->name)) {
676 sub_type->send = gras_datadesc_cb_push_uint;
677 } else if (!strcmp("long int",sub_type->name)) {
678 sub_type->send = gras_datadesc_cb_push_lint;
679 } else if (!strcmp("unsigned long int",sub_type->name)) {
680 sub_type->send = gras_datadesc_cb_push_ulint;
682 ERROR1("Field %s is not an int, unsigned int, long int neither unsigned long int",
688 * gras_datadesc_cb_field_recv:
690 * Add a post-receive callback to the given field of the datadesc (which must be a struct or union).
691 * (useful to put the function pointers to the right value, for example)
693 void gras_datadesc_cb_field_recv(gras_datadesc_type_t *type,
694 const char *field_name,
695 gras_datadesc_type_cb_void_t recv) {
697 gras_datadesc_type_t *sub_type=gras_dd_find_field(type,field_name);
698 sub_type->recv = recv;
702 * gras_datadesc_free:
704 * Free a datadesc. Should only be called at gras_exit.
706 void gras_datadesc_free(gras_datadesc_type_t *type) {
708 DEBUG1("Let's free ddt %s",type->name);
710 switch (type->category_code) {
711 case e_gras_datadesc_type_cat_scalar:
712 case e_gras_datadesc_type_cat_ref:
713 case e_gras_datadesc_type_cat_array:
714 /* nothing to free in there */
717 case e_gras_datadesc_type_cat_ignored:
718 if (type->category.ignored_data.free_func) {
719 type->category.ignored_data.free_func
720 (type->category.ignored_data.default_value);
724 case e_gras_datadesc_type_cat_struct:
725 gras_dynar_free(type->category.struct_data.fields);
728 case e_gras_datadesc_type_cat_union:
729 gras_dynar_free(type->category.union_data.fields);
733 /* datadesc was invalid. Killing it is like euthanasy, I guess */
736 gras_free(type->name);