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 static void gras_ddt_freev(void *ddt) {
21 gras_datadesc_type_t *type= (gras_datadesc_type_t *)ddt;
29 gras_ddt_new(const char *name,
30 gras_datadesc_type_t **dst) {
33 gras_datadesc_type_t *res=malloc(sizeof(gras_datadesc_type_t));
37 memset(res, 0, sizeof(gras_datadesc_type_t));
38 res->name = strdup(name);
39 res->name_len = strlen(name);
41 TRY(gras_set_add(gras_datadesc_set_local,
42 (gras_set_elm_t*)res,&gras_ddt_freev));
49 * gras_datadesc_by_name:
51 * Retrieve a type from its name
53 gras_datadesc_type_t *gras_datadesc_by_name(const char *name) {
55 gras_datadesc_type_t *type;
57 if (gras_set_get_by_name(gras_datadesc_set_local,
58 name,(gras_set_elm_t**)&type) == no_error) {
66 * gras_datadesc_by_id:
68 * Retrieve a type from its code
70 gras_error_t gras_datadesc_by_id(long int code,
71 gras_datadesc_type_t **type) {
72 return gras_set_get_by_id(gras_datadesc_set_local,
73 code,(gras_set_elm_t**)type);
77 * gras_datadesc_declare_scalar:
79 * Create a new scalar and give a pointer to it
82 gras_datadesc_declare_scalar(const char *name,
83 gras_ddt_scalar_type_t type,
84 enum e_gras_dd_scalar_encoding encoding,
85 gras_datadesc_type_cb_void_t cb,
86 gras_datadesc_type_t **dst) {
89 gras_datadesc_type_t *res;
92 TRY(gras_ddt_new(name,dst));
95 for (arch = 0; arch < gras_arch_count; arch ++) {
99 res->size[arch] = gras_arch_sizes[arch].sizeof_scalars[type];
101 sz = res->size[arch];
104 /* just in case you wonder, x>>1 == x/2 on all architectures when x>=0 and a size is always>=0 */
106 /* make sure mask have all the bits under the biggest one of size set to 1
107 Example: size=000100101 => mask=0000111111 */
112 if (res->size[arch] & (mask >> 1)) { /* if size have bits to one beside its biggest */
113 /* size is not a power of 2 */
114 /* alignment= next power of 2 after size */
115 res->alignment[arch] = (res->size[arch] & ~(mask >> 1)) << 1;
116 gras_assert0(res->alignment[arch] != 0,
117 "scalar type too large");
119 res->aligned_size[arch] = aligned(res->size[arch], res->alignment[arch]);
120 gras_assert0 (res->aligned_size[arch] >= 0,
121 "scalar type too large");
124 /* size is a power of 2, life is great */
125 res->alignment[arch] = res->size[arch];
126 res->aligned_size[arch] = res->size[arch];
129 /* FIXME size < 0 sometimes?
132 res->aligned_size = 0;
137 res->category_code = e_gras_datadesc_type_cat_scalar;
138 res->category.scalar_data.encoding = encoding;
146 * gras_dd_cat_field_free:
148 * Frees one struct or union field
150 void gras_dd_cat_field_free(void *f) {
151 gras_dd_cat_field_t *field = (gras_dd_cat_field_t *)f;
160 * gras_datadesc_declare_struct:
162 * Create a new struct and give a pointer to it
165 gras_datadesc_declare_struct(const char *name,
166 gras_datadesc_type_t **dst) {
168 gras_error_t errcode;
169 gras_datadesc_type_t *res;
172 TRY(gras_ddt_new(name,dst));
175 for (arch=0; arch<gras_arch_count; arch ++) {
177 res->alignment[arch] = 0;
178 res->aligned_size[arch] = 0;
180 res->category_code = e_gras_datadesc_type_cat_struct;
181 TRY(gras_dynar_new(&(res->category.struct_data.fields),
182 sizeof(gras_dd_cat_field_t*),
183 &gras_dd_cat_field_free));
189 * gras_datadesc_declare_struct_append:
191 * Append a field to the struct
194 gras_datadesc_declare_struct_append(gras_datadesc_type_t *struct_type,
196 gras_datadesc_type_t *field_type) {
198 gras_error_t errcode;
199 gras_dd_cat_field_t *field;
202 gras_assert0(field_type->size >= 0,
203 "Cannot add a dynamically sized field in a structure");
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], field_type->alignment[arch]);
214 field->code = field_type->code;
218 TRY(gras_dynar_push(struct_type->category.struct_data.fields, &field));
220 for (arch=0; arch<gras_arch_count; arch ++) {
221 struct_type->size[arch] = field->offset[arch] + field_type->size[arch];
222 struct_type->alignment[arch] = max(struct_type->alignment[arch], field_type->alignment[arch]);
223 struct_type->aligned_size[arch] = aligned(struct_type->size[arch], struct_type->alignment[arch]);
230 * gras_datadesc_declare_union:
232 * Create a new union and give a pointer to it
235 gras_datadesc_declare_union(const char *name,
236 gras_datadesc_type_cb_int_t selector,
237 gras_datadesc_type_t **dst) {
239 gras_error_t errcode;
240 gras_datadesc_type_t *res;
243 gras_assert0(selector,
244 "Attempt to creat an union without field_count function");
246 TRY(gras_ddt_new(name,dst));
249 for (arch=0; arch<gras_arch_count; arch ++) {
251 res->alignment[arch] = 0;
252 res->aligned_size[arch] = 0;
254 res->category_code = e_gras_datadesc_type_cat_union;
255 TRY(gras_dynar_new(&(res->category.union_data.fields),
256 sizeof(gras_dd_cat_field_t*),
257 &gras_dd_cat_field_free));
258 res->category.union_data.selector = selector;
264 * gras_datadesc_declare_union_append:
266 * Append a field to the union
269 gras_datadesc_declare_union_append(gras_datadesc_type_t *union_type,
271 gras_datadesc_type_t *field_type) {
273 gras_error_t errcode;
274 gras_dd_cat_field_t *field;
277 gras_assert0(field_type->size >= 0,
278 "Cannot add a dynamically sized field in an union");
280 field=malloc(sizeof(gras_dd_cat_field_t));
284 field->name = strdup(name);
285 for (arch=0; arch<gras_arch_count; arch ++) {
286 field->offset[arch] = 0; /* that's the purpose of union ;) */
288 field->code = field_type->code;
292 TRY(gras_dynar_push(union_type->category.union_data.fields, &field));
294 for (arch=0; arch<gras_arch_count; arch ++) {
295 union_type->size[arch] = max(union_type->size[arch], field_type->size[arch]);
296 union_type->alignment[arch] = max(union_type->alignment[arch], field_type->alignment[arch]);
297 union_type->aligned_size[arch] = aligned(union_type->size[arch], union_type->alignment[arch]);
304 * gras_datadesc_declare_ref:
306 * Create a new ref to a fixed type and give a pointer to it
309 gras_datadesc_declare_ref(const char *name,
310 gras_datadesc_type_t *referenced_type,
311 gras_datadesc_type_t **dst) {
313 gras_error_t errcode;
314 gras_datadesc_type_t *res;
315 gras_datadesc_type_t *pointer_type = gras_datadesc_by_name("data pointer");
318 TRY(gras_ddt_new(name,dst));
321 gras_assert0(pointer_type, "Cannot get the description of data pointer");
323 for (arch=0; arch<gras_arch_count; arch ++) {
324 res->size[arch] = pointer_type->size[arch];
325 res->alignment[arch] = pointer_type->alignment[arch];
326 res->aligned_size[arch] = pointer_type->aligned_size[arch];
329 res->category_code = e_gras_datadesc_type_cat_ref;
331 res->category.ref_data.code = referenced_type->code;
332 res->category.ref_data.selector = NULL;
337 * gras_datadesc_declare_ref_generic:
339 * Create a new ref to a type given at use time, and give a pointer to it
342 gras_datadesc_declare_ref_generic(const char *name,
343 gras_datadesc_type_cb_int_t selector,
344 gras_datadesc_type_t **dst) {
346 gras_error_t errcode;
347 gras_datadesc_type_t *res;
348 gras_datadesc_type_t *pointer_type = gras_datadesc_by_name("data pointer");
351 TRY(gras_ddt_new(name,dst));
354 gras_assert0(pointer_type, "Cannot get the description of data pointer");
356 for (arch=0; arch<gras_arch_count; arch ++) {
357 res->size[arch] = pointer_type->size[arch];
358 res->alignment[arch] = pointer_type->alignment[arch];
359 res->aligned_size[arch] = pointer_type->aligned_size[arch];
362 res->category_code = e_gras_datadesc_type_cat_ref;
364 res->category.ref_data.code = -1;
365 res->category.ref_data.selector = selector;
371 * gras_datadesc_declare_array_fixed:
373 * Create a new array and give a pointer to it
376 gras_datadesc_declare_array_fixed(const char *name,
377 gras_datadesc_type_t *element_type,
379 gras_datadesc_type_t **dst) {
381 gras_error_t errcode;
382 gras_datadesc_type_t *res;
385 TRY(gras_ddt_new(name,dst));
388 gras_assert1(fixed_size > 0, "'%s' is a array of negative fixed size",name);
389 for (arch=0; arch<gras_arch_count; arch ++) {
390 res->size[arch] = fixed_size * element_type->aligned_size[arch];
391 res->alignment[arch] = element_type->alignment[arch];
392 res->aligned_size[arch] = fixed_size; /*FIXME: That was so in GS, but looks stupid*/
394 res->category_code = e_gras_datadesc_type_cat_array;
396 res->category.array_data.code = element_type->code;
397 res->category.array_data.fixed_size = fixed_size;
398 res->category.array_data.dynamic_size = NULL;
403 * gras_datadesc_declare_array_dyn:
405 * Create a new array and give a pointer to it
408 gras_datadesc_declare_array_dyn(const char *name,
409 gras_datadesc_type_t *element_type,
410 gras_datadesc_type_cb_int_t dynamic_size,
411 gras_datadesc_type_t **dst) {
413 gras_error_t errcode;
414 gras_datadesc_type_t *res;
417 gras_assert1(dynamic_size,
418 "'%s' is a dynamic array without size discriminant",
421 TRY(gras_ddt_new(name,dst));
424 for (arch=0; arch<gras_arch_count; arch ++) {
425 res->size[arch] = -1; /* make sure it indicates "dynamic" */
426 res->alignment[arch] = element_type->alignment[arch];
427 res->aligned_size[arch] = -1; /*FIXME: That was so in GS, but looks stupid*/
429 res->category_code = e_gras_datadesc_type_cat_array;
431 res->category.array_data.code = element_type->code;
432 res->category.array_data.fixed_size = -1;
433 res->category.array_data.dynamic_size = dynamic_size;
439 * gras_datadesc_declare_parse:
441 * Create a datadescription from the result of parsing the C type description
444 gras_datadesc_parse(const char *name,
445 const char *C_statement,
446 gras_datadesc_type_t **dst) {
452 gras_datadesc_import_nws(const char *name,
453 const DataDescriptor *desc,
455 gras_datadesc_type_t **dst) {
462 * Frees a datadescription.
464 void gras_ddt_free(gras_datadesc_type_t **type) {
465 gras_datadesc_type_t *t;
471 switch (t->category_code) {
472 case e_gras_datadesc_type_cat_scalar:
473 case e_gras_datadesc_type_cat_ref:
474 case e_gras_datadesc_type_cat_array:
475 /* nothing to free in there */
478 case e_gras_datadesc_type_cat_ignored:
479 if (t->category.ignored_data.free_func) {
480 t->category.ignored_data.free_func(t->category.ignored_data.default_value);
484 case e_gras_datadesc_type_cat_struct:
485 gras_dynar_free(t->category.struct_data.fields);
488 case e_gras_datadesc_type_cat_union:
489 gras_dynar_free(t->category.union_data.fields);
493 /* datadesc was invalid. Killing it is like euthanasy, I guess */
500 * gras_datadesc_cb_set_pre:
502 * Add a pre-send callback to this datadexc
504 void gras_datadesc_cb_set_pre (gras_datadesc_type_t *type,
505 gras_datadesc_type_cb_void_t pre) {
509 * gras_datadesc_cb_set_post:
511 * Add a post-send callback to this datadexc
513 void gras_datadesc_cb_set_post(gras_datadesc_type_t *type,
514 gras_datadesc_type_cb_void_t post) {