Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[Data description]
[simgrid.git] / src / gras / DataDesc / ddt_create.c
1 /* $Id$ */
2
3 /* ddt_new - creation/deletion of datatypes structs (private to this module)*/
4
5 /* Authors: Olivier Aumage, Martin Quinson                                  */
6 /* Copyright (C) 2003, 2004 the GRAS posse.                                 */
7
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. */
10
11 #include "DataDesc/datadesc_private.h"
12
13 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(new,DataDesc);
14
15 /**
16  * gras_ddt_freev:
17  *
18  * gime that memory back, dude. I mean it.
19  */
20 static void gras_ddt_freev(void *ddt) {
21   gras_datadesc_type_t *type= (gras_datadesc_type_t *)ddt;
22   
23   if (type) {
24     gras_ddt_free(&type);
25   }
26 }
27
28 static gras_error_t
29 gras_ddt_new(const char            *name,
30              gras_datadesc_type_t **dst) {
31
32   gras_error_t errcode;
33   gras_datadesc_type_t *res=malloc(sizeof(gras_datadesc_type_t));
34   if (!res) 
35     RAISE_MALLOC;
36
37   memset(res, 0, sizeof(gras_datadesc_type_t));
38   res->name = strdup(name);
39   res->name_len = strlen(name);
40   
41   TRY(gras_set_add(gras_datadesc_set_local,
42                    (gras_set_elm_t*)res,&gras_ddt_freev));
43
44   *dst=res;
45   return no_error;
46 }
47
48 /**
49  * gras_datadesc_by_name:
50  *
51  * Retrieve a type from its name
52  */
53 gras_datadesc_type_t *gras_datadesc_by_name(const char *name) {
54
55   gras_datadesc_type_t *type;
56
57   if (gras_set_get_by_name(gras_datadesc_set_local,
58                            name,(gras_set_elm_t**)&type) == no_error) {
59     return type;
60   } else { 
61     return NULL;
62   }
63 }
64
65 /**
66  * gras_datadesc_by_id:
67  *
68  * Retrieve a type from its code
69  */
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);
74 }
75
76 /**
77  * gras_datadesc_declare_scalar:
78  *
79  * Create a new scalar and give a pointer to it 
80  */
81 gras_error_t 
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) {
87
88   gras_error_t errcode;
89   gras_datadesc_type_t *res;
90   long int arch;
91
92   TRY(gras_ddt_new(name,dst));
93   res=*dst;
94
95   for (arch = 0; arch < gras_arch_count; arch ++) {
96     long int sz;
97     long int mask;
98
99     res->size[arch] = gras_arch_sizes[arch].sizeof_scalars[type];
100     
101     sz = res->size[arch];
102     mask = sz;
103     
104     /* just in case you wonder, x>>1 == x/2 on all architectures when x>=0 and a size is always>=0 */
105
106     /* make sure mask have all the bits under the biggest one of size set to 1
107        Example: size=000100101 => mask=0000111111 */
108     while ((sz >>= 1)) {
109       mask |= sz;
110     }
111     
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");
118       
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");
122       
123     } else {
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];
127     }
128
129     /* FIXME size < 0 sometimes? 
130        } else {
131        res->alignment    = 0;
132        res->aligned_size = 0;
133        }
134     */
135   }
136
137   res->category_code                 = e_gras_datadesc_type_cat_scalar;
138   res->category.scalar_data.encoding = encoding;
139
140   res->pre = cb;
141   return no_error;
142 }
143
144
145 /**
146  * gras_dd_cat_field_free:
147  *
148  * Frees one struct or union field
149  */
150 void gras_dd_cat_field_free(void *f) {
151   gras_dd_cat_field_t *field = (gras_dd_cat_field_t *)f;
152   if (field) {
153     if (field->name) 
154       free(field->name);
155     free(field);
156   }
157 }
158
159 /**
160  * gras_datadesc_declare_struct:
161  *
162  * Create a new struct and give a pointer to it 
163  */
164 gras_error_t 
165 gras_datadesc_declare_struct(const char                      *name,
166                         gras_datadesc_type_t           **dst) {
167
168   gras_error_t errcode;
169   gras_datadesc_type_t *res;
170   long int arch;
171
172   TRY(gras_ddt_new(name,dst));
173   res=*dst;
174
175   for (arch=0; arch<gras_arch_count; arch ++) {
176     res->size[arch]                     = 0;
177     res->alignment[arch]                = 0;
178     res->aligned_size[arch]             = 0;
179   }
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));
184
185   return no_error;
186 }
187
188 /**
189  * gras_datadesc_declare_struct_append:
190  *
191  * Append a field to the struct
192  */
193 gras_error_t 
194 gras_datadesc_declare_struct_append(gras_datadesc_type_t            *struct_type,
195                                const char                      *name,
196                                gras_datadesc_type_t            *field_type) {
197
198   gras_error_t errcode;
199   gras_dd_cat_field_t *field;
200   int arch;
201
202   gras_assert0(field_type->size >= 0,
203                "Cannot add a dynamically sized field in a structure");
204     
205   field=malloc(sizeof(gras_dd_cat_field_t));
206   if (!field)
207     RAISE_MALLOC;
208
209   field->name   = strdup(name);
210
211   for (arch=0; arch<gras_arch_count; arch ++) {
212     field->offset[arch] = aligned(struct_type->size[arch], field_type->alignment[arch]);
213   }
214   field->code   = field_type->code;
215   field->pre    = NULL;
216   field->post   = NULL;
217   
218   TRY(gras_dynar_push(struct_type->category.struct_data.fields, &field));
219
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]);
224   }
225
226   return no_error;
227 }
228
229 /**
230  * gras_datadesc_declare_union:
231  *
232  * Create a new union and give a pointer to it 
233  */
234 gras_error_t 
235 gras_datadesc_declare_union(const char                      *name,
236                    gras_datadesc_type_cb_int_t      selector,
237                    gras_datadesc_type_t           **dst) {
238
239   gras_error_t errcode;
240   gras_datadesc_type_t *res;
241   int arch;
242
243   gras_assert0(selector,
244                "Attempt to creat an union without field_count function");
245
246   TRY(gras_ddt_new(name,dst));
247   res=*dst;
248
249   for (arch=0; arch<gras_arch_count; arch ++) {
250     res->size[arch]                     = 0;
251     res->alignment[arch]                = 0;
252     res->aligned_size[arch]             = 0;
253   }
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;
259
260   return no_error;
261 }
262
263 /**
264  * gras_datadesc_declare_union_append:
265  *
266  * Append a field to the union
267  */
268 gras_error_t 
269 gras_datadesc_declare_union_append(gras_datadesc_type_t            *union_type,
270                           const char                      *name,
271                           gras_datadesc_type_t            *field_type) {
272
273   gras_error_t errcode;
274   gras_dd_cat_field_t *field;
275   int arch;
276
277   gras_assert0(field_type->size >= 0,
278                "Cannot add a dynamically sized field in an union");
279     
280   field=malloc(sizeof(gras_dd_cat_field_t));
281   if (!field)
282     RAISE_MALLOC;
283
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 ;) */
287   }
288   field->code   = field_type->code;
289   field->pre    = NULL;
290   field->post   = NULL;
291   
292   TRY(gras_dynar_push(union_type->category.union_data.fields, &field));
293
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]);
298   }
299
300   return no_error;
301 }
302
303 /**
304  * gras_datadesc_declare_ref:
305  *
306  * Create a new ref to a fixed type and give a pointer to it 
307  */
308 gras_error_t 
309 gras_datadesc_declare_ref(const char             *name,
310                           gras_datadesc_type_t   *referenced_type,
311                           gras_datadesc_type_t  **dst) {
312
313   gras_error_t errcode;
314   gras_datadesc_type_t *res;
315   gras_datadesc_type_t *pointer_type = gras_datadesc_by_name("data pointer");
316   int arch;
317
318   TRY(gras_ddt_new(name,dst));
319   res=*dst;
320
321   gras_assert0(pointer_type, "Cannot get the description of data pointer");
322       
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];
327   }
328
329   res->category_code            = e_gras_datadesc_type_cat_ref;
330
331   res->category.ref_data.code     = referenced_type->code;
332   res->category.ref_data.selector = NULL;
333
334   return no_error;
335 }
336 /**
337  * gras_datadesc_declare_ref_generic:
338  *
339  * Create a new ref to a type given at use time, and give a pointer to it 
340  */
341 gras_error_t 
342 gras_datadesc_declare_ref_generic(const char                      *name,
343                          gras_datadesc_type_cb_int_t      selector,
344                          gras_datadesc_type_t           **dst) {
345
346   gras_error_t errcode;
347   gras_datadesc_type_t *res;
348   gras_datadesc_type_t *pointer_type = gras_datadesc_by_name("data pointer");
349   int arch;
350
351   TRY(gras_ddt_new(name,dst));
352   res=*dst;
353
354   gras_assert0(pointer_type, "Cannot get the description of data pointer");
355       
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];
360   }
361
362   res->category_code            = e_gras_datadesc_type_cat_ref;
363
364   res->category.ref_data.code     = -1;
365   res->category.ref_data.selector = selector;
366
367   return no_error;
368 }
369
370 /**
371  * gras_datadesc_declare_array_fixed:
372  *
373  * Create a new array and give a pointer to it 
374  */
375 gras_error_t 
376 gras_datadesc_declare_array_fixed(const char                      *name,
377                          gras_datadesc_type_t            *element_type,
378                          long int                         fixed_size,
379                          gras_datadesc_type_t           **dst) {
380
381   gras_error_t errcode;
382   gras_datadesc_type_t *res;
383   int arch;
384
385   TRY(gras_ddt_new(name,dst));
386   res=*dst;
387
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*/
393   }
394   res->category_code            = e_gras_datadesc_type_cat_array;
395
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;
399
400   return no_error;
401 }
402 /**
403  * gras_datadesc_declare_array_dyn:
404  *
405  * Create a new array and give a pointer to it 
406  */
407 gras_error_t 
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) {
412
413   gras_error_t errcode;
414   gras_datadesc_type_t *res;
415   int arch;
416
417   gras_assert1(dynamic_size,
418                "'%s' is a dynamic array without size discriminant",
419                name);
420
421   TRY(gras_ddt_new(name,dst));
422   res=*dst;
423
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*/
428   }
429   res->category_code            = e_gras_datadesc_type_cat_array;
430
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;
434
435   return no_error;
436 }
437
438 /**
439  * gras_datadesc_declare_parse:
440  *
441  * Create a datadescription from the result of parsing the C type description
442  */
443 gras_error_t
444 gras_datadesc_parse(const char            *name,
445                     const char            *C_statement,
446                     gras_datadesc_type_t **dst) {
447   RAISE_UNIMPLEMENTED;
448 }
449
450
451 gras_error_t
452 gras_datadesc_import_nws(const char           *name,
453                          const DataDescriptor *desc,
454                          size_t                howmany,
455                          gras_datadesc_type_t **dst) {
456   RAISE_UNIMPLEMENTED;
457 }
458
459 /**
460  * gras_ddt_free:
461  *
462  * Frees a datadescription.
463  */
464 void gras_ddt_free(gras_datadesc_type_t **type) {
465   gras_datadesc_type_t *t;
466
467   if (type && *type) {
468     t=*type;
469
470     free(t->name);
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 */
476       break;
477
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);
481       }
482       break;
483
484     case e_gras_datadesc_type_cat_struct:
485       gras_dynar_free(t->category.struct_data.fields);
486       break;
487
488     case e_gras_datadesc_type_cat_union:
489       gras_dynar_free(t->category.union_data.fields);
490       break;
491       
492     default:
493       /* datadesc was invalid. Killing it is like euthanasy, I guess */
494       break;
495     }
496   }
497 }
498
499 /**
500  * gras_datadesc_cb_set_pre:
501  *
502  * Add a pre-send callback to this datadexc
503  */
504 void gras_datadesc_cb_set_pre (gras_datadesc_type_t         *type,
505                                gras_datadesc_type_cb_void_t  pre) {
506   type->pre = pre;
507 }
508 /**
509  * gras_datadesc_cb_set_post:
510  *
511  * Add a post-send callback to this datadexc
512  */
513 void gras_datadesc_cb_set_post(gras_datadesc_type_t         *type,
514                                gras_datadesc_type_cb_void_t  post) {
515   type->post = post;
516 }