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(new,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_declare_scalar:
82 * Create a new scalar and give a pointer to it
85 gras_datadesc_declare_scalar(const char *name,
86 gras_ddt_scalar_type_t type,
87 enum e_gras_dd_scalar_encoding encoding,
88 gras_datadesc_type_cb_void_t cb,
89 gras_datadesc_type_t **dst) {
92 gras_datadesc_type_t *res;
95 TRY(gras_ddt_new(name,dst));
98 for (arch = 0; arch < gras_arch_count; arch ++) {
101 res->size[arch] = gras_arches[arch].sizeof_scalars[type];
103 sz = res->size[arch];
106 /* just in case you wonder, x>>1 == x/2 on all architectures when x>=0
107 and a size is always>=0 */
109 /* make sure mask have all the bits under the biggest one of size set to 1
110 Example: size=000100101 => mask=0000111111 */
115 if (res->size[arch] & (mask >> 1)) { /* if size have bits to one beside its biggest */
116 /* size is not a power of 2 */
117 /* alignment= next power of 2 after size */
118 res->alignment[arch] = (res->size[arch] & ~(mask >> 1)) << 1;
119 gras_assert0(res->alignment[arch] != 0,
120 "scalar type too large");
122 res->aligned_size[arch] = aligned(res->size[arch], res->alignment[arch]);
123 gras_assert0 (res->aligned_size[arch] >= 0,
124 "scalar type too large");
127 /* size is a power of 2, life is great */
128 res->alignment[arch] = res->size[arch];
129 res->aligned_size[arch] = res->size[arch];
133 res->category_code = e_gras_datadesc_type_cat_scalar;
134 res->category.scalar_data.encoding = encoding;
142 * gras_dd_cat_field_free:
144 * Frees one struct or union field
146 void gras_dd_cat_field_free(void *f) {
147 gras_dd_cat_field_t *field = *(gras_dd_cat_field_t **)f;
156 * gras_datadesc_declare_struct:
158 * Create a new struct and give a pointer to it
161 gras_datadesc_declare_struct(const char *name,
162 gras_datadesc_type_t **dst) {
164 gras_error_t errcode;
165 gras_datadesc_type_t *res;
168 TRY(gras_ddt_new(name,dst));
171 for (arch=0; arch<gras_arch_count; arch ++) {
173 res->alignment[arch] = 0;
174 res->aligned_size[arch] = 0;
176 res->category_code = e_gras_datadesc_type_cat_struct;
177 TRY(gras_dynar_new(&(res->category.struct_data.fields),
178 sizeof(gras_dd_cat_field_t*),
179 &gras_dd_cat_field_free));
185 * gras_datadesc_declare_struct_append:
187 * Append a field to the struct
190 gras_datadesc_declare_struct_append(gras_datadesc_type_t *struct_type,
192 gras_datadesc_type_t *field_type) {
194 gras_error_t errcode;
195 gras_dd_cat_field_t *field;
198 gras_assert1(!struct_type->category.struct_data.closed,
199 "Cannot add anything to the already closed struct %s",
201 gras_assert1(field_type->size >= 0,
202 "Cannot add a dynamically sized field in structure %s",
205 field=malloc(sizeof(gras_dd_cat_field_t));
209 field->name = strdup(name);
211 for (arch=0; arch<gras_arch_count; arch ++) {
212 field->offset[arch] = aligned(struct_type->size[arch],
213 field_type->alignment[arch]);
215 field->code = field_type->code;
219 TRY(gras_dynar_push(struct_type->category.struct_data.fields, &field));
221 for (arch=0; arch<gras_arch_count; arch ++) {
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]);
229 DEBUG3("Push a %s into %s at offset %d.",
230 field_type->name, struct_type->name,field->offset[GRAS_THISARCH]);
231 DEBUG3(" f={size=%d,align=%d,asize=%d}",
232 field_type->size[GRAS_THISARCH],
233 field_type->alignment, field_type->aligned_size);
234 DEBUG3(" s={size=%d,align=%d,asize=%d}",
235 struct_type->size[GRAS_THISARCH],
236 struct_type->alignment, struct_type->aligned_size);
240 gras_datadesc_declare_struct_close(gras_datadesc_type_t *struct_type) {
241 struct_type->category.struct_data.closed = 1;
242 INFO0("FIXME: Do something in gras_datadesc_declare_struct_close");
246 * gras_datadesc_declare_union:
248 * Create a new union and give a pointer to it
251 gras_datadesc_declare_union(const char *name,
252 gras_datadesc_type_cb_int_t selector,
253 gras_datadesc_type_t **dst) {
255 gras_error_t errcode;
256 gras_datadesc_type_t *res;
259 gras_assert0(selector,
260 "Attempt to creat an union without field_count function");
262 TRY(gras_ddt_new(name,dst));
265 for (arch=0; arch<gras_arch_count; arch ++) {
267 res->alignment[arch] = 0;
268 res->aligned_size[arch] = 0;
271 res->category_code = e_gras_datadesc_type_cat_union;
272 TRY(gras_dynar_new(&(res->category.union_data.fields),
273 sizeof(gras_dd_cat_field_t*),
274 &gras_dd_cat_field_free));
275 res->category.union_data.selector = selector;
281 * gras_datadesc_declare_union_append:
283 * Append a field to the union
286 gras_datadesc_declare_union_append(gras_datadesc_type_t *union_type,
288 gras_datadesc_type_t *field_type) {
290 gras_error_t errcode;
291 gras_dd_cat_field_t *field;
294 gras_assert1(!union_type->category.union_data.closed,
295 "Cannot add anything to the already closed union %s",
297 gras_assert1(field_type->size >= 0,
298 "Cannot add a dynamically sized field in union %s",
301 field=malloc(sizeof(gras_dd_cat_field_t));
305 field->name = strdup(name);
306 for (arch=0; arch<gras_arch_count; arch ++) {
307 field->offset[arch] = 0; /* that's the purpose of union ;) */
309 field->code = field_type->code;
313 TRY(gras_dynar_push(union_type->category.union_data.fields, &field));
315 for (arch=0; arch<gras_arch_count; arch ++) {
316 union_type->size[arch] = max(union_type->size[arch],
317 field_type->size[arch]);
318 union_type->alignment[arch] = max(union_type->alignment[arch],
319 field_type->alignment[arch]);
320 union_type->aligned_size[arch] = aligned(union_type->size[arch],
321 union_type->alignment[arch]);
327 gras_datadesc_declare_union_close(gras_datadesc_type_t *union_type) {
328 union_type->category.union_data.closed = 1;
329 INFO0("FIXME: Do something in gras_datadesc_declare_array_close");
332 * gras_datadesc_declare_ref:
334 * Create a new ref to a fixed type and give a pointer to it
337 gras_datadesc_declare_ref(const char *name,
338 gras_datadesc_type_t *referenced_type,
339 gras_datadesc_type_t **dst) {
341 gras_error_t errcode;
342 gras_datadesc_type_t *res;
343 gras_datadesc_type_t *pointer_type = gras_datadesc_by_name("data pointer");
346 TRY(gras_ddt_new(name,dst));
349 gras_assert0(pointer_type, "Cannot get the description of data pointer");
351 for (arch=0; arch<gras_arch_count; arch ++){
352 res->size[arch] = pointer_type->size[arch];
353 res->alignment[arch] = pointer_type->alignment[arch];
354 res->aligned_size[arch] = pointer_type->aligned_size[arch];
357 res->category_code = e_gras_datadesc_type_cat_ref;
359 res->category.ref_data.code = referenced_type->code;
360 res->category.ref_data.selector = NULL;
365 * gras_datadesc_declare_ref_generic:
367 * Create a new ref to a type given at use time, and give a pointer to it
370 gras_datadesc_declare_ref_generic(const char *name,
371 gras_datadesc_type_cb_int_t selector,
372 gras_datadesc_type_t **dst) {
374 gras_error_t errcode;
375 gras_datadesc_type_t *res;
376 gras_datadesc_type_t *pointer_type = gras_datadesc_by_name("data pointer");
379 TRY(gras_ddt_new(name,dst));
382 gras_assert0(pointer_type, "Cannot get the description of data pointer");
384 for (arch=0; arch<gras_arch_count; arch ++) {
385 res->size[arch] = pointer_type->size[arch];
386 res->alignment[arch] = pointer_type->alignment[arch];
387 res->aligned_size[arch] = pointer_type->aligned_size[arch];
390 res->category_code = e_gras_datadesc_type_cat_ref;
392 res->category.ref_data.code = -1;
393 res->category.ref_data.selector = selector;
399 * gras_datadesc_declare_array_fixed:
401 * Create a new array and give a pointer to it
404 gras_datadesc_declare_array_fixed(const char *name,
405 gras_datadesc_type_t *element_type,
407 gras_datadesc_type_t **dst) {
409 gras_error_t errcode;
410 gras_datadesc_type_t *res;
413 TRY(gras_ddt_new(name,dst));
416 gras_assert1(fixed_size > 0, "'%s' is a array of negative fixed size",name);
417 for (arch=0; arch<gras_arch_count; arch ++) {
418 res->size[arch] = fixed_size * element_type->aligned_size[arch];
419 res->alignment[arch] = element_type->alignment[arch];
420 res->aligned_size[arch] = res->size[arch];
423 res->category_code = e_gras_datadesc_type_cat_array;
425 res->category.array_data.code = element_type->code;
426 res->category.array_data.fixed_size = fixed_size;
427 res->category.array_data.dynamic_size = NULL;
432 * gras_datadesc_declare_array_dyn:
434 * Create a new array and give a pointer to it
437 gras_datadesc_declare_array_dyn(const char *name,
438 gras_datadesc_type_t *element_type,
439 gras_datadesc_type_cb_int_t dynamic_size,
440 gras_datadesc_type_t **dst) {
442 gras_error_t errcode;
443 gras_datadesc_type_t *res;
446 gras_assert1(dynamic_size,
447 "'%s' is a dynamic array without size discriminant",
450 TRY(gras_ddt_new(name,dst));
453 for (arch=0; arch<gras_arch_count; arch ++) {
454 res->size[arch] = -1; /* make sure it indicates "dynamic" */
455 res->alignment[arch] = element_type->alignment[arch];
456 res->aligned_size[arch] = -1; /*FIXME: That was so in GS, but looks stupid*/
459 res->category_code = e_gras_datadesc_type_cat_array;
461 res->category.array_data.code = element_type->code;
462 res->category.array_data.fixed_size = -1;
463 res->category.array_data.dynamic_size = dynamic_size;
469 gras_datadesc_import_nws(const char *name,
470 const DataDescriptor *desc,
472 gras_datadesc_type_t **dst) {
477 * gras_datadesc_cb_set_pre:
479 * Add a pre-send callback to this datadexc
481 void gras_datadesc_cb_set_pre (gras_datadesc_type_t *type,
482 gras_datadesc_type_cb_void_t pre) {
486 * gras_datadesc_cb_set_post:
488 * Add a post-send callback to this datadexc
490 void gras_datadesc_cb_set_post(gras_datadesc_type_t *type,
491 gras_datadesc_type_cb_void_t post) {
498 * Adds a reference to the datastruct.
499 * ddt will be freed only when the refcount becomes 0.
501 void gras_datadesc_ref(gras_datadesc_type_t *type) {
506 * gras_datadesc_unref:
508 * Adds a reference to the datastruct.
509 * ddt will be freed only when the refcount becomes 0.
511 void gras_datadesc_unref(gras_datadesc_type_t *type) {
513 if (!type->refcounter) {
514 /* even the set of ddt released that type. Let's free it */
515 DEBUG1("Let's free ddt %s",type->name);
518 switch (type->category_code) {
519 case e_gras_datadesc_type_cat_scalar:
520 case e_gras_datadesc_type_cat_ref:
521 case e_gras_datadesc_type_cat_array:
522 /* nothing to free in there */
525 case e_gras_datadesc_type_cat_ignored:
526 if (type->category.ignored_data.free_func) {
527 type->category.ignored_data.free_func
528 (type->category.ignored_data.default_value);
532 case e_gras_datadesc_type_cat_struct:
533 gras_dynar_free(type->category.struct_data.fields);
536 case e_gras_datadesc_type_cat_union:
537 gras_dynar_free(type->category.union_data.fields);
541 /* datadesc was invalid. Killing it is like euthanasy, I guess */