3 /* datadesc - data description in order to send/recv it in GRAS */
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(DataDesc,GRAS);
14 gras_set_t *gras_datadesc_set_local=NULL;
19 * Initialize the datadesc module
22 gras_datadesc_init(void) {
25 VERB0("Initializing DataDesc");
26 errcode = gras_set_new(&gras_datadesc_set_local);
27 gras_assert0(errcode==no_error,
28 "Impossible to create the data set containg locally known types");
34 * Finalize the datadesc module
37 gras_datadesc_exit(void) {
38 VERB0("Exiting DataDesc");
39 gras_set_free(&gras_datadesc_set_local);
43 *** CREATION FUNCTIONS: private to this file
46 gras_ddt_new(const char *name,
47 gras_datadesc_type_t **dst) {
49 gras_datadesc_type_t *res=malloc(sizeof(gras_datadesc_type_t));
53 memset(res, 0, sizeof(res));
54 res->name = strdup(name);
61 * gras_ddt_new_scalar:
63 * Create a new scalar and give a pointer to it
66 gras_ddt_new_scalar(const char *name,
68 enum e_gras_dd_scalar_encoding encoding,
69 gras_datadesc_type_cb_void_t cb,
70 gras_datadesc_type_t **dst) {
73 gras_datadesc_type_t *res;
75 TRY(gras_ddt_new(name,dst));
78 res->size = size>0 ? size : 0;
80 if (size>0) { /* Black magic from Oli FIXME: documentation ;)*/
88 if (size & (mask >> 1)) {
89 res->alignment = (size & ~(mask >> 1)) << 1;
90 gras_assert0(res->alignment != 0,
91 "scalar type too large");
93 res->aligned_size = aligned(size, res->alignment);
94 gras_assert0 (res->aligned_size >= 0,
95 "scalar type too large");
98 res->alignment = size & ~(mask >> 1);;
99 res->aligned_size = aligned(size, res->alignment);
104 res->aligned_size = 0;
107 res->category_code = e_gras_datadesc_type_cat_scalar;
108 res->category.scalar_data.encoding = encoding;
116 * gras_dd_cat_field_free:
118 * Frees one struct or union field
120 void gras_dd_cat_field_free(void *f) {
121 gras_dd_cat_field_t *field = (gras_dd_cat_field_t *)f;
130 * gras_ddt_new_struct:
132 * Create a new struct and give a pointer to it
135 gras_ddt_new_struct(const char *name,
136 gras_datadesc_type_cb_void_t pre,
137 gras_datadesc_type_cb_void_t post,
138 gras_datadesc_type_t **dst) {
140 gras_error_t errcode;
141 gras_datadesc_type_t *res;
143 TRY(gras_ddt_new(name,dst));
148 res->aligned_size = 0;
149 res->category_code = e_gras_datadesc_type_cat_struct;
150 TRY(gras_dynar_new(&(res->category.struct_data.fields),
151 sizeof(gras_dd_cat_field_t*),
152 &gras_dd_cat_field_free));
160 * gras_ddt_new_struct_append:
162 * Append a field to the struct
165 gras_ddt_new_struct_append(gras_datadesc_type_t *struct_type,
167 gras_datadesc_type_t *field_type,
168 gras_datadesc_type_cb_void_t pre,
169 gras_datadesc_type_cb_void_t post) {
171 gras_error_t errcode;
172 gras_dd_cat_field_t *field;
174 gras_assert0(field_type->size >= 0,
175 "Cannot add a dynamically sized field in a structure");
177 field=malloc(sizeof(gras_dd_cat_field_t));
181 field->name = strdup(name);
182 field->offset = aligned(struct_type->size, field_type->alignment);
183 field->code = field_type->code;
187 TRY(gras_dynar_push(struct_type->category.struct_data.fields, field));
189 struct_type->size = field->offset + field_type->size;
190 struct_type->alignment = max(struct_type->alignment, field_type->alignment);
191 struct_type->aligned_size = aligned(struct_type->size, struct_type->alignment);
197 * gras_ddt_new_union:
199 * Create a new union and give a pointer to it
202 gras_ddt_new_union(const char *name,
203 gras_datadesc_type_cb_int_t field_count,
204 gras_datadesc_type_cb_void_t post,
205 gras_datadesc_type_t **dst) {
207 gras_error_t errcode;
208 gras_datadesc_type_t *res;
210 gras_assert0(field_count,
211 "Attempt to creat an union without field_count function");
213 TRY(gras_ddt_new(name,dst));
218 res->aligned_size = 0;
219 res->category_code = e_gras_datadesc_type_cat_union;
220 TRY(gras_dynar_new(&(res->category.union_data.fields),
221 sizeof(gras_dd_cat_field_t*),
222 &gras_dd_cat_field_free));
223 res->category.union_data.field_count = field_count;
231 * gras_ddt_new_union_append:
233 * Append a field to the union
236 gras_ddt_new_union_append(gras_datadesc_type_t *union_type,
238 gras_datadesc_type_t *field_type,
239 gras_datadesc_type_cb_void_t pre,
240 gras_datadesc_type_cb_void_t post) {
242 gras_error_t errcode;
243 gras_dd_cat_field_t *field;
245 gras_assert0(field_type->size >= 0,
246 "Cannot add a dynamically sized field in an union");
248 field=malloc(sizeof(gras_dd_cat_field_t));
252 field->name = strdup(name);
253 field->offset = 0; /* that's the purpose of union ;) */
254 field->code = field_type->code;
258 TRY(gras_dynar_push(union_type->category.union_data.fields, field));
260 union_type->size = max(union_type->size, field_type->size);
261 union_type->alignment = max(union_type->alignment, field_type->alignment);
262 union_type->aligned_size = aligned(union_type->size, union_type->alignment);
270 * Create a new ref and give a pointer to it
273 gras_ddt_new_ref(const char *name,
274 gras_datadesc_type_t *referenced_type,
275 gras_datadesc_type_cb_int_t discriminant,
276 gras_datadesc_type_cb_void_t post,
277 gras_datadesc_type_t **dst) {
279 gras_error_t errcode;
280 gras_datadesc_type_t *res;
282 gras_assert0(discriminant || referenced_type,
283 "Attempt to create a generic reference without discriminant");
285 TRY(gras_ddt_new(name,dst));
288 /* FIXME: Size from bootstraping */
291 res->aligned_size = 0;
292 res->category_code = e_gras_datadesc_type_cat_ref;
294 res->category.ref_data.code = referenced_type ? referenced_type->code : -1;
295 res->category.ref_data.discriminant = discriminant;
303 * gras_ddt_new_array:
305 * Create a new array and give a pointer to it
308 gras_ddt_new_array(const char *name,
309 gras_datadesc_type_t *element_type,
311 gras_datadesc_type_cb_int_t dynamic_size,
312 gras_datadesc_type_cb_void_t post,
313 gras_datadesc_type_t **dst) {
315 gras_error_t errcode;
316 gras_datadesc_type_t *res;
318 gras_assert0(dynamic_size || fixed_size>0,
319 "Attempt to create a dynamic array without size discriminant");
321 TRY(gras_ddt_new(name,dst));
324 if (fixed_size <= 0) {
325 res->size = fixed_size;
327 res->size = fixed_size * element_type->aligned_size;
329 res->alignment = element_type->alignment;
330 res->aligned_size = fixed_size; /*FIXME: That was so in GS, but looks stupid*/
331 res->category_code = e_gras_datadesc_type_cat_array;
333 res->category.array_data.code = element_type->code;
334 res->category.array_data.fixed_size = fixed_size;
335 res->category.array_data.dynamic_size = dynamic_size;
344 *** BOOTSTRAPING FUNCTIONS: known within GRAS only
348 *** DECLARATION FUNCTIONS: public functions
351 gras_datadesc_declare_struct_cb(const char *name,
352 gras_datadesc_type_cb_void_t pre,
353 gras_datadesc_type_cb_void_t post,
355 gras_error_t errcode;
356 gras_datadesc_type_t *type;
357 TRY(gras_ddt_new_struct(name, pre, post, &type));
358 TRY(gras_ddt_register(gras_datadesc_set_local, type));
364 gras_datadesc_declare_struct_add_name_cb(long int struct_code,
365 const char *field_name,
366 const char *field_type_name,
367 gras_datadesc_type_cb_void_t pre_cb,
368 gras_datadesc_type_cb_void_t post_cb) {
370 gras_error_t errcode;
371 gras_datadesc_type_t *struct_type;
372 gras_datadesc_type_t *field_type;
374 TRY(gras_set_get_by_id(gras_datadesc_set_local,struct_code,
375 (gras_set_elm_t**)struct_type));
376 TRY(gras_set_get_by_name(gras_datadesc_set_local,field_type_name,
377 (gras_set_elm_t**)field_type));
379 TRY(gras_ddt_new_struct_append(struct_type,
380 field_name, field_type,
386 gras_datadesc_declare_struct_add_code_cb(long int struct_code,
387 const char *field_name,
389 gras_datadesc_type_cb_void_t pre_cb,
390 gras_datadesc_type_cb_void_t post_cb) {
391 gras_error_t errcode;
392 gras_datadesc_type_t *struct_type;
393 gras_datadesc_type_t *field_type;
395 TRY(gras_set_get_by_id(gras_datadesc_set_local,struct_code,
396 (gras_set_elm_t**)struct_type));
397 TRY(gras_set_get_by_id(gras_datadesc_set_local,field_code,
398 (gras_set_elm_t**)field_type));
400 TRY(gras_ddt_new_struct_append(struct_type,
401 field_name, field_type,
408 gras_datadesc_declare_union_cb(const char *name,
409 gras_datadesc_type_cb_int_t field_count,
410 gras_datadesc_type_cb_void_t post,
412 gras_error_t errcode;
413 gras_datadesc_type_t *type;
414 TRY(gras_ddt_new_union(name, field_count, post, &type));
415 TRY(gras_ddt_register(gras_datadesc_set_local, type));
421 gras_datadesc_declare_union_add_name_cb(long int union_code,
422 const char *field_name,
423 const char *field_type_name,
424 gras_datadesc_type_cb_void_t pre_cb,
425 gras_datadesc_type_cb_void_t post_cb) {
426 gras_error_t errcode;
427 gras_datadesc_type_t *union_type;
428 gras_datadesc_type_t *field_type;
430 TRY(gras_set_get_by_id(gras_datadesc_set_local,union_code,
431 (gras_set_elm_t**)union_type));
432 TRY(gras_set_get_by_name(gras_datadesc_set_local,field_type_name,
433 (gras_set_elm_t**)field_type));
435 TRY(gras_ddt_new_union_append(union_type,
436 field_name, field_type,
442 gras_datadesc_declare_union_add_code_cb(long int union_code,
443 const char *field_name,
445 gras_datadesc_type_cb_void_t pre_cb,
446 gras_datadesc_type_cb_void_t post_cb) {
447 gras_error_t errcode;
448 gras_datadesc_type_t *union_type;
449 gras_datadesc_type_t *field_type;
451 TRY(gras_set_get_by_id(gras_datadesc_set_local,union_code,
452 (gras_set_elm_t**)union_type));
453 TRY(gras_set_get_by_id(gras_datadesc_set_local,field_code,
454 (gras_set_elm_t**)field_type));
456 TRY(gras_ddt_new_union_append(union_type,
457 field_name, field_type,
464 gras_datadesc_declare_ref_cb(const char *name,
465 gras_datadesc_type_t *referenced_type,
466 gras_datadesc_type_cb_int_t discriminant,
467 gras_datadesc_type_cb_void_t post,
469 gras_error_t errcode;
470 gras_datadesc_type_t *type;
471 TRY(gras_ddt_new_ref(name, referenced_type,discriminant,post, &type));
472 TRY(gras_ddt_register(gras_datadesc_set_local, type));
478 gras_datadesc_declare_array_cb(const char *name,
479 gras_datadesc_type_t *element_type,
481 gras_datadesc_type_cb_int_t dynamic_size,
482 gras_datadesc_type_cb_void_t post,
484 gras_error_t errcode;
485 gras_datadesc_type_t *type;
486 TRY(gras_ddt_new_array(name, element_type, fixed_size, dynamic_size, post, &type));
487 TRY(gras_ddt_register(gras_datadesc_set_local, type));