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(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_unref(type);
29 gras_ddt_new(const char *name,
30 gras_datadesc_type_t **dst) {
33 gras_datadesc_type_t *res;
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));
52 * gras_datadesc_by_name:
54 * Retrieve a type from its name
56 gras_datadesc_type_t *gras_datadesc_by_name(const char *name) {
58 gras_datadesc_type_t *type;
60 if (gras_set_get_by_name(gras_datadesc_set_local,
61 name,(gras_set_elm_t**)&type) == no_error) {
69 * gras_datadesc_by_id:
71 * Retrieve a type from its code
73 gras_error_t gras_datadesc_by_id(long int code,
74 gras_datadesc_type_t **type) {
75 return gras_set_get_by_id(gras_datadesc_set_local,
76 code,(gras_set_elm_t**)type);
80 * gras_datadesc_scalar:
82 * Create a new scalar and give a pointer to it
85 gras_datadesc_scalar(const char *name,
86 gras_ddt_scalar_type_t type,
87 enum e_gras_dd_scalar_encoding encoding,
88 gras_datadesc_type_t **dst) {
91 gras_datadesc_type_t *res;
94 TRY(gras_ddt_new(name,dst));
97 for (arch = 0; arch < gras_arch_count; arch ++) {
100 res->size[arch] = gras_arches[arch].sizeof_scalars[type];
102 sz = res->size[arch];
105 /* just in case you wonder, x>>1 == x/2 on all architectures when x>=0
106 and a size is always>=0 */
108 /* make sure mask have all the bits under the biggest one of size set to 1
109 Example: size=000100101 => mask=0000111111 */
114 if (res->size[arch] & (mask >> 1)) { /* if size have bits to one beside its biggest */
115 /* size is not a power of 2 */
116 /* alignment= next power of 2 after size */
117 res->alignment[arch] = (res->size[arch] & ~(mask >> 1)) << 1;
118 gras_assert0(res->alignment[arch] != 0,
119 "scalar type too large");
121 res->aligned_size[arch] = aligned(res->size[arch], res->alignment[arch]);
122 gras_assert0 (res->aligned_size[arch] >= 0,
123 "scalar type too large");
126 /* size is a power of 2, life is great */
127 res->alignment[arch] = res->size[arch];
128 res->aligned_size[arch] = res->size[arch];
132 res->category_code = e_gras_datadesc_type_cat_scalar;
133 res->category.scalar_data.encoding = encoding;
140 * gras_dd_cat_field_free:
142 * Frees one struct or union field
144 void gras_dd_cat_field_free(void *f) {
145 gras_dd_cat_field_t *field = *(gras_dd_cat_field_t **)f;
154 * gras_datadesc_struct:
156 * Create a new struct and give a pointer to it
159 gras_datadesc_struct(const char *name,
160 gras_datadesc_type_t **dst) {
162 gras_error_t errcode;
163 gras_datadesc_type_t *res;
166 TRY(gras_ddt_new(name,dst));
169 for (arch=0; arch<gras_arch_count; arch ++) {
171 res->alignment[arch] = 0;
172 res->aligned_size[arch] = 0;
174 res->category_code = e_gras_datadesc_type_cat_struct;
175 TRY(gras_dynar_new(&(res->category.struct_data.fields),
176 sizeof(gras_dd_cat_field_t*),
177 &gras_dd_cat_field_free));
183 * gras_datadesc_struct_append:
185 * Append a field to the struct
188 gras_datadesc_struct_append(gras_datadesc_type_t *struct_type,
190 gras_datadesc_type_t *field_type) {
192 gras_error_t errcode;
193 gras_dd_cat_field_t *field;
196 gras_assert1(!struct_type->category.struct_data.closed,
197 "Cannot add anything to the already closed struct %s",
199 gras_assert1(field_type->size >= 0,
200 "Cannot add a dynamically sized field in structure %s",
203 field=malloc(sizeof(gras_dd_cat_field_t));
207 field->name = strdup(name);
209 DEBUG0("----------------");
210 DEBUG4("PRE s={size=%ld,align=%ld,asize=%ld} struct_boundary=%d",
211 struct_type->size[GRAS_THISARCH],
212 struct_type->alignment[GRAS_THISARCH],
213 struct_type->aligned_size[GRAS_THISARCH],
214 gras_arches[GRAS_THISARCH].struct_boundary);
217 for (arch=0; arch<gras_arch_count; arch ++) {
218 field->offset[arch] = aligned(struct_type->size[arch],
219 min(field_type->alignment[arch],
220 gras_arches[arch].struct_boundary));
222 struct_type->size[arch] = field->offset[arch] + field_type->size[arch];
223 struct_type->alignment[arch] = max(struct_type->alignment[arch],
224 field_type->alignment[arch]);
225 struct_type->aligned_size[arch] = aligned(struct_type->size[arch],
226 struct_type->alignment[arch]);
228 field->code = field_type->code;
232 TRY(gras_dynar_push(struct_type->category.struct_data.fields, &field));
234 DEBUG3("Push a %s into %s at offset %ld.",
235 field_type->name, struct_type->name,field->offset[GRAS_THISARCH]);
236 DEBUG3(" f={size=%ld,align=%ld,asize=%ld}",
237 field_type->size[GRAS_THISARCH],
238 field_type->alignment[GRAS_THISARCH],
239 field_type->aligned_size[GRAS_THISARCH]);
240 DEBUG3(" s={size=%ld,align=%ld,asize=%ld}",
241 struct_type->size[GRAS_THISARCH],
242 struct_type->alignment[GRAS_THISARCH],
243 struct_type->aligned_size[GRAS_THISARCH]);
247 gras_datadesc_struct_close(gras_datadesc_type_t *struct_type) {
248 struct_type->category.struct_data.closed = 1;
249 // INFO0("FIXME: Do something in gras_datadesc_struct_close");
253 * gras_datadesc_union:
255 * Create a new union and give a pointer to it
258 gras_datadesc_union(const char *name,
259 gras_datadesc_type_cb_int_t selector,
260 gras_datadesc_type_t **dst) {
262 gras_error_t errcode;
263 gras_datadesc_type_t *res;
266 gras_assert0(selector,
267 "Attempt to creat an union without field_count function");
269 TRY(gras_ddt_new(name,dst));
272 for (arch=0; arch<gras_arch_count; arch ++) {
274 res->alignment[arch] = 0;
275 res->aligned_size[arch] = 0;
278 res->category_code = e_gras_datadesc_type_cat_union;
279 TRY(gras_dynar_new(&(res->category.union_data.fields),
280 sizeof(gras_dd_cat_field_t*),
281 &gras_dd_cat_field_free));
282 res->category.union_data.selector = selector;
288 * gras_datadesc_union_append:
290 * Append a field to the union
293 gras_datadesc_union_append(gras_datadesc_type_t *union_type,
295 gras_datadesc_type_t *field_type) {
297 gras_error_t errcode;
298 gras_dd_cat_field_t *field;
301 gras_assert1(!union_type->category.union_data.closed,
302 "Cannot add anything to the already closed union %s",
304 gras_assert1(field_type->size >= 0,
305 "Cannot add a dynamically sized field in union %s",
308 field=malloc(sizeof(gras_dd_cat_field_t));
312 field->name = strdup(name);
313 for (arch=0; arch<gras_arch_count; arch ++) {
314 field->offset[arch] = 0; /* that's the purpose of union ;) */
316 field->code = field_type->code;
320 TRY(gras_dynar_push(union_type->category.union_data.fields, &field));
322 for (arch=0; arch<gras_arch_count; arch ++) {
323 union_type->size[arch] = max(union_type->size[arch],
324 field_type->size[arch]);
325 union_type->alignment[arch] = max(union_type->alignment[arch],
326 field_type->alignment[arch]);
327 union_type->aligned_size[arch] = aligned(union_type->size[arch],
328 union_type->alignment[arch]);
334 gras_datadesc_union_close(gras_datadesc_type_t *union_type) {
335 union_type->category.union_data.closed = 1;
336 // INFO0("FIXME: Do something in gras_datadesc_union_close");
341 * Create a new ref to a fixed type and give a pointer to it
344 gras_datadesc_ref(const char *name,
345 gras_datadesc_type_t *referenced_type,
346 gras_datadesc_type_t **dst) {
348 gras_error_t errcode;
349 gras_datadesc_type_t *res;
350 gras_datadesc_type_t *pointer_type = gras_datadesc_by_name("data pointer");
353 TRY(gras_ddt_new(name,dst));
356 gras_assert0(pointer_type, "Cannot get the description of data pointer");
358 for (arch=0; arch<gras_arch_count; arch ++){
359 res->size[arch] = pointer_type->size[arch];
360 res->alignment[arch] = pointer_type->alignment[arch];
361 res->aligned_size[arch] = pointer_type->aligned_size[arch];
364 res->category_code = e_gras_datadesc_type_cat_ref;
366 res->category.ref_data.code = referenced_type->code;
367 res->category.ref_data.selector = NULL;
372 * gras_datadesc_ref_generic:
374 * Create a new ref to a type given at use time, and give a pointer to it
377 gras_datadesc_ref_generic(const char *name,
378 gras_datadesc_type_cb_int_t selector,
379 gras_datadesc_type_t **dst) {
381 gras_error_t errcode;
382 gras_datadesc_type_t *res;
383 gras_datadesc_type_t *pointer_type = gras_datadesc_by_name("data pointer");
386 TRY(gras_ddt_new(name,dst));
389 gras_assert0(pointer_type, "Cannot get the description of data pointer");
391 for (arch=0; arch<gras_arch_count; arch ++) {
392 res->size[arch] = pointer_type->size[arch];
393 res->alignment[arch] = pointer_type->alignment[arch];
394 res->aligned_size[arch] = pointer_type->aligned_size[arch];
397 res->category_code = e_gras_datadesc_type_cat_ref;
399 res->category.ref_data.code = -1;
400 res->category.ref_data.selector = selector;
406 * gras_datadesc_array_fixed:
408 * Create a new array and give a pointer to it
411 gras_datadesc_array_fixed(const char *name,
412 gras_datadesc_type_t *element_type,
414 gras_datadesc_type_t **dst) {
416 gras_error_t errcode;
417 gras_datadesc_type_t *res;
420 TRY(gras_ddt_new(name,dst));
423 gras_assert1(fixed_size > 0, "'%s' is a array of negative fixed size",name);
424 for (arch=0; arch<gras_arch_count; arch ++) {
425 res->size[arch] = fixed_size * element_type->aligned_size[arch];
426 res->alignment[arch] = element_type->alignment[arch];
427 res->aligned_size[arch] = res->size[arch];
430 res->category_code = e_gras_datadesc_type_cat_array;
432 res->category.array_data.code = element_type->code;
433 res->category.array_data.fixed_size = fixed_size;
434 res->category.array_data.dynamic_size = NULL;
439 * gras_datadesc_array_dyn:
441 * Create a new array and give a pointer to it
444 gras_datadesc_array_dyn(const char *name,
445 gras_datadesc_type_t *element_type,
446 gras_datadesc_type_cb_int_t dynamic_size,
447 gras_datadesc_type_t **dst) {
449 gras_error_t errcode;
450 gras_datadesc_type_t *res;
453 gras_assert1(dynamic_size,
454 "'%s' is a dynamic array without size discriminant",
457 TRY(gras_ddt_new(name,dst));
460 for (arch=0; arch<gras_arch_count; arch ++) {
461 res->size[arch] = -1; /* make sure it indicates "dynamic" */
462 res->alignment[arch] = element_type->alignment[arch];
463 res->aligned_size[arch] = -1; /*FIXME: That was so in GS, but looks stupid*/
466 res->category_code = e_gras_datadesc_type_cat_array;
468 res->category.array_data.code = element_type->code;
469 res->category.array_data.fixed_size = -1;
470 res->category.array_data.dynamic_size = dynamic_size;
476 * gras_datadesc_ref_pop_arr:
478 * Most of the time, you want to include a reference in your structure which
479 * is a pointer to a dynamic array whose size is fixed by another field of
482 * This case pops up so often that this function was created to take care of
483 * this case. It creates a dynamic array type whose size is poped from the
484 * current cbps, and then create a reference to it.
486 * The name of the created datatype will be the name of the element type, with
487 * '[]*' appended to it.
489 * Then to use it, you just have to make sure that your structure pre-callback
490 * does push the size of the array in the cbps (using #gras_cbps_i_push), and
493 * But be remember that this is a stack. If you have two different pop_arr, you
494 * should push the second one first, so that the first one is on the top of the
495 * list when the first field gets transfered.
499 gras_datadesc_ref_pop_arr(gras_datadesc_type_t *element_type,
500 gras_datadesc_type_t **dst) {
501 gras_error_t errcode;
502 char *name=malloc(strlen(element_type->name) + 4);
504 sprintf(name,"%s[]",element_type->name);
506 TRY(gras_datadesc_array_dyn(name,element_type,
507 gras_datadesc_cb_pop, dst));
509 sprintf(name,"%s[]*",element_type->name);
510 TRY(gras_datadesc_ref(name,*dst,dst));
517 gras_datadesc_import_nws(const char *name,
518 const DataDescriptor *desc,
520 gras_datadesc_type_t **dst) {
525 * gras_datadesc_cb_send:
527 * Add a pre-send callback to this datadexc.
528 * (useful to push the sizes of the upcoming arrays, for example)
530 void gras_datadesc_cb_send (gras_datadesc_type_t *type,
531 gras_datadesc_type_cb_void_t send) {
535 * gras_datadesc_cb_recv:
537 * Add a post-receive callback to this datadesc.
538 * (useful to put the function pointers to the rigth value, for example)
540 void gras_datadesc_cb_recv(gras_datadesc_type_t *type,
541 gras_datadesc_type_cb_void_t recv) {
546 * gras_datadesc_addref:
548 * Adds a reference to the datastruct.
549 * ddt will be freed only when the refcount becomes 0.
551 void gras_datadesc_addref(gras_datadesc_type_t *type) {
556 * gras_datadesc_unref:
558 * Removes a reference to the datastruct.
559 * ddt will be freed only when the refcount becomes 0.
561 void gras_datadesc_unref(gras_datadesc_type_t *type) {
563 if (!type->refcounter) {
564 /* even the set of ddt released that type. Let's free it */
565 DEBUG1("Let's free ddt %s",type->name);
568 switch (type->category_code) {
569 case e_gras_datadesc_type_cat_scalar:
570 case e_gras_datadesc_type_cat_ref:
571 case e_gras_datadesc_type_cat_array:
572 /* nothing to free in there */
575 case e_gras_datadesc_type_cat_ignored:
576 if (type->category.ignored_data.free_func) {
577 type->category.ignored_data.free_func
578 (type->category.ignored_data.default_value);
582 case e_gras_datadesc_type_cat_struct:
583 gras_dynar_free(type->category.struct_data.fields);
586 case e_gras_datadesc_type_cat_union:
587 gras_dynar_free(type->category.union_data.fields);
591 /* datadesc was invalid. Killing it is like euthanasy, I guess */