Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
61b371b40ce54f2db01b5c9617523116957b3979
[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_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);
57 }
58
59 /**
60  * gras_datadesc_by_id:
61  *
62  * Retrieve a type from its code
63  */
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);
68 }
69
70 /**
71  * gras_datadesc_declare_scalar:
72  *
73  * Create a new scalar and give a pointer to it 
74  */
75 gras_error_t 
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) {
81
82   gras_error_t errcode;
83   gras_datadesc_type_t *res;
84   long int arch;
85
86   TRY(gras_ddt_new(name,dst));
87   res=*dst;
88
89   for (arch = 0; arch < gras_arch_count; arch ++) {
90     long int sz;
91     long int mask;
92
93     res->size[arch] = gras_arch_sizes[arch].sizeof_scalars[type];
94     
95     sz = res->size[arch];
96     mask = sz;
97     
98     /* just in case you wonder, x>>1 == x/2 on all architectures when x>=0 and a size is always>=0 */
99
100     /* make sure mask have all the bits under the biggest one of size set to 1
101        Example: size=000100101 => mask=0000111111 */
102     while ((sz >>= 1)) {
103       mask |= sz;
104     }
105     
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");
112       
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");
116       
117     } else {
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];
121     }
122
123     /* FIXME size < 0 sometimes? 
124        } else {
125        res->alignment    = 0;
126        res->aligned_size = 0;
127        }
128     */
129   }
130
131   res->category_code                 = e_gras_datadesc_type_cat_scalar;
132   res->category.scalar_data.encoding = encoding;
133
134   res->pre = cb;
135   return no_error;
136 }
137
138
139 /**
140  * gras_dd_cat_field_free:
141  *
142  * Frees one struct or union field
143  */
144 void gras_dd_cat_field_free(void *f) {
145   gras_dd_cat_field_t *field = (gras_dd_cat_field_t *)f;
146   if (field) {
147     if (field->name) 
148       free(field->name);
149     free(field);
150   }
151 }
152
153 /**
154  * gras_datadesc_declare_struct:
155  *
156  * Create a new struct and give a pointer to it 
157  */
158 gras_error_t 
159 gras_datadesc_declare_struct(const char                      *name,
160                         gras_datadesc_type_t           **dst) {
161
162   gras_error_t errcode;
163   gras_datadesc_type_t *res;
164   long int arch;
165
166   TRY(gras_ddt_new(name,dst));
167   res=*dst;
168
169   for (arch=0; arch<gras_arch_count; arch ++) {
170     res->size[arch]                     = 0;
171     res->alignment[arch]                = 0;
172     res->aligned_size[arch]             = 0;
173   }
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));
178
179   return no_error;
180 }
181
182 gras_error_t 
183 gras_datadesc_declare_struct_append_name(gras_datadesc_type_t *struct_type,
184                                          const char           *name,
185                                          const char          *field_type_name){
186   gras_error_t errcode;
187   gras_datadesc_type_t *field_type;
188
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);
193     return errcode;
194   }
195   return gras_datadesc_declare_struct_append(struct_type,name,field_type);
196 }
197 /**
198  * gras_datadesc_declare_struct_append:
199  *
200  * Append a field to the struct
201  */
202 gras_error_t 
203 gras_datadesc_declare_struct_append(gras_datadesc_type_t            *struct_type,
204                                const char                      *name,
205                                gras_datadesc_type_t            *field_type) {
206
207   gras_error_t errcode;
208   gras_dd_cat_field_t *field;
209   int arch;
210
211   gras_assert0(field_type->size >= 0,
212                "Cannot add a dynamically sized field in a structure");
213     
214   field=malloc(sizeof(gras_dd_cat_field_t));
215   if (!field)
216     RAISE_MALLOC;
217
218   field->name   = strdup(name);
219
220   for (arch=0; arch<gras_arch_count; arch ++) {
221     field->offset[arch] = aligned(struct_type->size[arch], field_type->alignment[arch]);
222   }
223   field->code   = field_type->code;
224   field->pre    = NULL;
225   field->post   = NULL;
226   
227   TRY(gras_dynar_push(struct_type->category.struct_data.fields, &field));
228
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]);
233   }
234
235   return no_error;
236 }
237
238 /**
239  * gras_datadesc_declare_union:
240  *
241  * Create a new union and give a pointer to it 
242  */
243 gras_error_t 
244 gras_datadesc_declare_union(const char                      *name,
245                    gras_datadesc_type_cb_int_t      selector,
246                    gras_datadesc_type_t           **dst) {
247
248   gras_error_t errcode;
249   gras_datadesc_type_t *res;
250   int arch;
251
252   gras_assert0(selector,
253                "Attempt to creat an union without field_count function");
254
255   TRY(gras_ddt_new(name,dst));
256   res=*dst;
257
258   for (arch=0; arch<gras_arch_count; arch ++) {
259     res->size[arch]                     = 0;
260     res->alignment[arch]                = 0;
261     res->aligned_size[arch]             = 0;
262   }
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;
268
269   return no_error;
270 }
271
272 /**
273  * gras_datadesc_declare_union_append:
274  *
275  * Append a field to the union
276  */
277 gras_error_t 
278 gras_datadesc_declare_union_append(gras_datadesc_type_t            *union_type,
279                           const char                      *name,
280                           gras_datadesc_type_t            *field_type) {
281
282   gras_error_t errcode;
283   gras_dd_cat_field_t *field;
284   int arch;
285
286   gras_assert0(field_type->size >= 0,
287                "Cannot add a dynamically sized field in an union");
288     
289   field=malloc(sizeof(gras_dd_cat_field_t));
290   if (!field)
291     RAISE_MALLOC;
292
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 ;) */
296   }
297   field->code   = field_type->code;
298   field->pre    = NULL;
299   field->post   = NULL;
300   
301   TRY(gras_dynar_push(union_type->category.union_data.fields, &field));
302
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]);
307   }
308
309   return no_error;
310 }
311
312 /**
313  * gras_datadesc_declare_ref:
314  *
315  * Create a new ref to a fixed type and give a pointer to it 
316  */
317 gras_error_t 
318 gras_datadesc_declare_ref(const char             *name,
319                           gras_datadesc_type_t   *referenced_type,
320                           gras_datadesc_type_t  **dst) {
321
322   gras_error_t errcode;
323   gras_datadesc_type_t *res;
324   gras_datadesc_type_t *pointer_type;
325   int arch;
326
327   TRY(gras_ddt_new(name,dst));
328   res=*dst;
329
330   TRY(gras_datadesc_by_name("data pointer", &pointer_type));
331       
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];
336   }
337
338   res->category_code            = e_gras_datadesc_type_cat_ref;
339
340   res->category.ref_data.code     = referenced_type->code;
341   res->category.ref_data.selector = NULL;
342
343   return no_error;
344 }
345 /**
346  * gras_datadesc_declare_ref_generic:
347  *
348  * Create a new ref to a type given at use time, and give a pointer to it 
349  */
350 gras_error_t 
351 gras_datadesc_declare_ref_generic(const char                      *name,
352                          gras_datadesc_type_cb_int_t      selector,
353                          gras_datadesc_type_t           **dst) {
354
355   gras_error_t errcode;
356   gras_datadesc_type_t *res;
357   gras_datadesc_type_t *pointer_type;
358   int arch;
359
360   TRY(gras_ddt_new(name,dst));
361   res=*dst;
362
363   TRY(gras_datadesc_by_name("data pointer", &pointer_type));
364       
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];
369   }
370
371   res->category_code            = e_gras_datadesc_type_cat_ref;
372
373   res->category.ref_data.code     = -1;
374   res->category.ref_data.selector = selector;
375
376   return no_error;
377 }
378
379 /**
380  * gras_datadesc_declare_array_fixed:
381  *
382  * Create a new array and give a pointer to it 
383  */
384 gras_error_t 
385 gras_datadesc_declare_array_fixed(const char                      *name,
386                          gras_datadesc_type_t            *element_type,
387                          long int                         fixed_size,
388                          gras_datadesc_type_t           **dst) {
389
390   gras_error_t errcode;
391   gras_datadesc_type_t *res;
392   int arch;
393
394   TRY(gras_ddt_new(name,dst));
395   res=*dst;
396
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*/
402   }
403   res->category_code            = e_gras_datadesc_type_cat_array;
404
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;
408
409   return no_error;
410 }
411 /**
412  * gras_datadesc_declare_array_dyn:
413  *
414  * Create a new array and give a pointer to it 
415  */
416 gras_error_t 
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) {
421
422   gras_error_t errcode;
423   gras_datadesc_type_t *res;
424   int arch;
425
426   gras_assert1(dynamic_size,
427                "'%s' is a dynamic array without size discriminant",
428                name);
429
430   TRY(gras_ddt_new(name,dst));
431   res=*dst;
432
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*/
437   }
438   res->category_code            = e_gras_datadesc_type_cat_array;
439
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;
443
444   return no_error;
445 }
446
447 /**
448  * gras_datadesc_declare_parse:
449  *
450  * Create a datadescription from the result of parsing the C type description
451  */
452 gras_error_t
453 gras_datadesc_parse(const char            *name,
454                     const char            *C_statement,
455                     gras_datadesc_type_t **dst) {
456   RAISE_UNIMPLEMENTED;
457 }
458
459
460 gras_error_t
461 gras_datadesc_import_nws(const char           *name,
462                          const DataDescriptor *desc,
463                          size_t                howmany,
464                          gras_datadesc_type_t **dst) {
465   RAISE_UNIMPLEMENTED;
466 }
467
468 /**
469  * gras_ddt_free:
470  *
471  * Frees a datadescription.
472  */
473 void gras_ddt_free(gras_datadesc_type_t **type) {
474   gras_datadesc_type_t *t;
475
476   if (type && *type) {
477     t=*type;
478
479     free(t->name);
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 */
485       break;
486
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);
490       }
491       break;
492
493     case e_gras_datadesc_type_cat_struct:
494       gras_dynar_free(t->category.struct_data.fields);
495       break;
496
497     case e_gras_datadesc_type_cat_union:
498       gras_dynar_free(t->category.union_data.fields);
499       break;
500       
501     default:
502       /* datadesc was invalid. Killing it is like euthanasy, I guess */
503       break;
504     }
505   }
506 }
507
508 /**
509  * gras_datadesc_cb_set_pre:
510  *
511  * Add a pre-send callback to this datadexc
512  */
513 void gras_datadesc_cb_set_pre (gras_datadesc_type_t         *type,
514                                gras_datadesc_type_cb_void_t  pre) {
515   type->pre = pre;
516 }
517 /**
518  * gras_datadesc_cb_set_post:
519  *
520  * Add a post-send callback to this datadexc
521  */
522 void gras_datadesc_cb_set_post(gras_datadesc_type_t         *type,
523                                gras_datadesc_type_cb_void_t  post) {
524   type->post = post;
525 }