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_error_t gras_datadesc_by_name(const char *name,
54 gras_datadesc_type_t **type) {
55 return gras_set_get_by_name(gras_datadesc_set_local,
56 name,(gras_set_elm_t**)type);
60 * gras_datadesc_by_id:
62 * Retrieve a type from its code
64 gras_error_t gras_datadesc_by_id(long int code,
65 gras_datadesc_type_t **type) {
66 return gras_set_get_by_id(gras_datadesc_set_local,
67 code,(gras_set_elm_t**)type);
71 * gras_datadesc_declare_scalar:
73 * Create a new scalar and give a pointer to it
76 gras_datadesc_declare_scalar(const char *name,
77 gras_ddt_scalar_type_t type,
78 enum e_gras_dd_scalar_encoding encoding,
79 gras_datadesc_type_cb_void_t cb,
80 gras_datadesc_type_t **dst) {
83 gras_datadesc_type_t *res;
86 TRY(gras_ddt_new(name,dst));
89 for (arch = 0; arch < gras_arch_count; arch ++) {
93 res->size[arch] = gras_arch_sizes[arch].sizeof_scalars[type];
98 /* just in case you wonder, x>>1 == x/2 on all architectures when x>=0 and a size is always>=0 */
100 /* make sure mask have all the bits under the biggest one of size set to 1
101 Example: size=000100101 => mask=0000111111 */
106 if (res->size[arch] & (mask >> 1)) { /* if size have bits to one beside its biggest */
107 /* size is not a power of 2 */
108 /* alignment= next power of 2 after size */
109 res->alignment[arch] = (res->size[arch] & ~(mask >> 1)) << 1;
110 gras_assert0(res->alignment[arch] != 0,
111 "scalar type too large");
113 res->aligned_size[arch] = aligned(res->size[arch], res->alignment[arch]);
114 gras_assert0 (res->aligned_size[arch] >= 0,
115 "scalar type too large");
118 /* size is a power of 2, life is great */
119 res->alignment[arch] = res->size[arch];
120 res->aligned_size[arch] = res->size[arch];
123 /* FIXME size < 0 sometimes?
126 res->aligned_size = 0;
131 res->category_code = e_gras_datadesc_type_cat_scalar;
132 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_declare_struct:
156 * Create a new struct and give a pointer to it
159 gras_datadesc_declare_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_declare_struct_append_name(gras_datadesc_type_t *struct_type,
185 const char *field_type_name){
186 gras_error_t errcode;
187 gras_datadesc_type_t *field_type;
189 errcode = gras_datadesc_by_name(field_type_name, &field_type);
190 if (errcode != no_error) {
191 WARN2("Got error '%s' while looking for '%s'",
192 gras_error_name(errcode), field_type_name);
195 return gras_datadesc_declare_struct_append(struct_type,name,field_type);
198 * gras_datadesc_declare_struct_append:
200 * Append a field to the struct
203 gras_datadesc_declare_struct_append(gras_datadesc_type_t *struct_type,
205 gras_datadesc_type_t *field_type) {
207 gras_error_t errcode;
208 gras_dd_cat_field_t *field;
211 gras_assert0(field_type->size >= 0,
212 "Cannot add a dynamically sized field in a structure");
214 field=malloc(sizeof(gras_dd_cat_field_t));
218 field->name = strdup(name);
220 for (arch=0; arch<gras_arch_count; arch ++) {
221 field->offset[arch] = aligned(struct_type->size[arch], field_type->alignment[arch]);
223 field->code = field_type->code;
227 TRY(gras_dynar_push(struct_type->category.struct_data.fields, &field));
229 for (arch=0; arch<gras_arch_count; arch ++) {
230 struct_type->size[arch] = field->offset[arch] + field_type->size[arch];
231 struct_type->alignment[arch] = max(struct_type->alignment[arch], field_type->alignment[arch]);
232 struct_type->aligned_size[arch] = aligned(struct_type->size[arch], struct_type->alignment[arch]);
239 * gras_datadesc_declare_union:
241 * Create a new union and give a pointer to it
244 gras_datadesc_declare_union(const char *name,
245 gras_datadesc_type_cb_int_t selector,
246 gras_datadesc_type_t **dst) {
248 gras_error_t errcode;
249 gras_datadesc_type_t *res;
252 gras_assert0(selector,
253 "Attempt to creat an union without field_count function");
255 TRY(gras_ddt_new(name,dst));
258 for (arch=0; arch<gras_arch_count; arch ++) {
260 res->alignment[arch] = 0;
261 res->aligned_size[arch] = 0;
263 res->category_code = e_gras_datadesc_type_cat_union;
264 TRY(gras_dynar_new(&(res->category.union_data.fields),
265 sizeof(gras_dd_cat_field_t*),
266 &gras_dd_cat_field_free));
267 res->category.union_data.selector = selector;
273 * gras_datadesc_declare_union_append:
275 * Append a field to the union
278 gras_datadesc_declare_union_append(gras_datadesc_type_t *union_type,
280 gras_datadesc_type_t *field_type) {
282 gras_error_t errcode;
283 gras_dd_cat_field_t *field;
286 gras_assert0(field_type->size >= 0,
287 "Cannot add a dynamically sized field in an union");
289 field=malloc(sizeof(gras_dd_cat_field_t));
293 field->name = strdup(name);
294 for (arch=0; arch<gras_arch_count; arch ++) {
295 field->offset[arch] = 0; /* that's the purpose of union ;) */
297 field->code = field_type->code;
301 TRY(gras_dynar_push(union_type->category.union_data.fields, &field));
303 for (arch=0; arch<gras_arch_count; arch ++) {
304 union_type->size[arch] = max(union_type->size[arch], field_type->size[arch]);
305 union_type->alignment[arch] = max(union_type->alignment[arch], field_type->alignment[arch]);
306 union_type->aligned_size[arch] = aligned(union_type->size[arch], union_type->alignment[arch]);
313 * gras_datadesc_declare_ref:
315 * Create a new ref to a fixed type and give a pointer to it
318 gras_datadesc_declare_ref(const char *name,
319 gras_datadesc_type_t *referenced_type,
320 gras_datadesc_type_t **dst) {
322 gras_error_t errcode;
323 gras_datadesc_type_t *res;
324 gras_datadesc_type_t *pointer_type;
327 TRY(gras_ddt_new(name,dst));
330 TRY(gras_datadesc_by_name("data pointer", &pointer_type));
332 for (arch=0; arch<gras_arch_count; arch ++) {
333 res->size[arch] = pointer_type->size[arch];
334 res->alignment[arch] = pointer_type->alignment[arch];
335 res->aligned_size[arch] = pointer_type->aligned_size[arch];
338 res->category_code = e_gras_datadesc_type_cat_ref;
340 res->category.ref_data.code = referenced_type->code;
341 res->category.ref_data.selector = NULL;
346 * gras_datadesc_declare_ref_generic:
348 * Create a new ref to a type given at use time, and give a pointer to it
351 gras_datadesc_declare_ref_generic(const char *name,
352 gras_datadesc_type_cb_int_t selector,
353 gras_datadesc_type_t **dst) {
355 gras_error_t errcode;
356 gras_datadesc_type_t *res;
357 gras_datadesc_type_t *pointer_type;
360 TRY(gras_ddt_new(name,dst));
363 TRY(gras_datadesc_by_name("data pointer", &pointer_type));
365 for (arch=0; arch<gras_arch_count; arch ++) {
366 res->size[arch] = pointer_type->size[arch];
367 res->alignment[arch] = pointer_type->alignment[arch];
368 res->aligned_size[arch] = pointer_type->aligned_size[arch];
371 res->category_code = e_gras_datadesc_type_cat_ref;
373 res->category.ref_data.code = -1;
374 res->category.ref_data.selector = selector;
380 * gras_datadesc_declare_array_fixed:
382 * Create a new array and give a pointer to it
385 gras_datadesc_declare_array_fixed(const char *name,
386 gras_datadesc_type_t *element_type,
388 gras_datadesc_type_t **dst) {
390 gras_error_t errcode;
391 gras_datadesc_type_t *res;
394 TRY(gras_ddt_new(name,dst));
397 gras_assert1(fixed_size > 0, "'%s' is a array of negative fixed size",name);
398 for (arch=0; arch<gras_arch_count; arch ++) {
399 res->size[arch] = fixed_size * element_type->aligned_size[arch];
400 res->alignment[arch] = element_type->alignment[arch];
401 res->aligned_size[arch] = fixed_size; /*FIXME: That was so in GS, but looks stupid*/
403 res->category_code = e_gras_datadesc_type_cat_array;
405 res->category.array_data.code = element_type->code;
406 res->category.array_data.fixed_size = fixed_size;
407 res->category.array_data.dynamic_size = NULL;
412 * gras_datadesc_declare_array_dyn:
414 * Create a new array and give a pointer to it
417 gras_datadesc_declare_array_dyn(const char *name,
418 gras_datadesc_type_t *element_type,
419 gras_datadesc_type_cb_int_t dynamic_size,
420 gras_datadesc_type_t **dst) {
422 gras_error_t errcode;
423 gras_datadesc_type_t *res;
426 gras_assert1(dynamic_size,
427 "'%s' is a dynamic array without size discriminant",
430 TRY(gras_ddt_new(name,dst));
433 for (arch=0; arch<gras_arch_count; arch ++) {
434 res->size[arch] = -1; /* make sure it indicates "dynamic" */
435 res->alignment[arch] = element_type->alignment[arch];
436 res->aligned_size[arch] = -1; /*FIXME: That was so in GS, but looks stupid*/
438 res->category_code = e_gras_datadesc_type_cat_array;
440 res->category.array_data.code = element_type->code;
441 res->category.array_data.fixed_size = -1;
442 res->category.array_data.dynamic_size = dynamic_size;
448 * gras_datadesc_declare_parse:
450 * Create a datadescription from the result of parsing the C type description
453 gras_datadesc_parse(const char *name,
454 const char *C_statement,
455 gras_datadesc_type_t **dst) {
461 gras_datadesc_import_nws(const char *name,
462 const DataDescriptor *desc,
464 gras_datadesc_type_t **dst) {
471 * Frees a datadescription.
473 void gras_ddt_free(gras_datadesc_type_t **type) {
474 gras_datadesc_type_t *t;
480 switch (t->category_code) {
481 case e_gras_datadesc_type_cat_scalar:
482 case e_gras_datadesc_type_cat_ref:
483 case e_gras_datadesc_type_cat_array:
484 /* nothing to free in there */
487 case e_gras_datadesc_type_cat_ignored:
488 if (t->category.ignored_data.free_func) {
489 t->category.ignored_data.free_func(t->category.ignored_data.default_value);
493 case e_gras_datadesc_type_cat_struct:
494 gras_dynar_free(t->category.struct_data.fields);
497 case e_gras_datadesc_type_cat_union:
498 gras_dynar_free(t->category.union_data.fields);
502 /* datadesc was invalid. Killing it is like euthanasy, I guess */
509 * gras_datadesc_cb_set_pre:
511 * Add a pre-send callback to this datadexc
513 void gras_datadesc_cb_set_pre (gras_datadesc_type_t *type,
514 gras_datadesc_type_cb_void_t pre) {
518 * gras_datadesc_cb_set_post:
520 * Add a post-send callback to this datadexc
522 void gras_datadesc_cb_set_post(gras_datadesc_type_t *type,
523 gras_datadesc_type_cb_void_t post) {