Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Interface revolution: do not try to survive to malloc failure
[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 "gras/DataDesc/datadesc_private.h"
12
13 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(ddt_create,datadesc,"Creating new datadescriptions");
14
15 /**
16  * gras_ddt_freev:
17  *
18  * gime that memory back, dude. I mean it.
19  */
20 void gras_ddt_freev(void *ddt) {
21   gras_datadesc_type_t *type= (gras_datadesc_type_t *)ddt;
22   
23   if (type) {
24     gras_datadesc_free(type);
25   }
26 }
27
28 static void
29 gras_ddt_new(const char            *name,
30              gras_datadesc_type_t **dst) {
31   gras_error_t errcode;
32   gras_datadesc_type_t *res;
33
34   GRAS_IN1("(%s)",name);
35   res=gras_new0(gras_datadesc_type_t,1);
36
37   res->name = (char*)strdup(name);
38   res->name_len = strlen(name);
39       
40   *dst=res;
41   gras_set_add(gras_datadesc_set_local,
42                (gras_set_elm_t*)res,&gras_ddt_freev);
43   GRAS_OUT;
44 }
45
46 /**
47  * gras_datadesc_by_name:
48  *
49  * Retrieve a type from its name
50  */
51 gras_datadesc_type_t *gras_datadesc_by_name(const char *name) {
52
53   gras_datadesc_type_t *type;
54
55   GRAS_IN1("(%s)",name);
56   if (gras_set_get_by_name(gras_datadesc_set_local,
57                            name,(gras_set_elm_t**)&type) == no_error) {
58     GRAS_OUT;
59     return type;
60   } else { 
61     GRAS_OUT;
62     return NULL;
63   }
64 }
65
66 /**
67  * gras_datadesc_by_id:
68  *
69  * Retrieve a type from its code
70  */
71 gras_error_t gras_datadesc_by_id(long int               code,
72                                  gras_datadesc_type_t **type) {
73   GRAS_IN;
74   return gras_set_get_by_id(gras_datadesc_set_local,
75                             code,(gras_set_elm_t**)type);
76 }
77
78 /**
79  * gras_datadesc_scalar:
80  *
81  * Create a new scalar and give a pointer to it 
82  */
83 gras_datadesc_type_t *
84   gras_datadesc_scalar(const char                      *name,
85                        gras_ddt_scalar_type_t           type,
86                        enum e_gras_dd_scalar_encoding   encoding) {
87
88   gras_datadesc_type_t *res;
89   long int arch;
90
91   GRAS_IN;
92   res = gras_datadesc_by_name(name);
93   if (res) {
94     gras_assert1(res->category_code == e_gras_datadesc_type_cat_scalar,
95                  "Redefinition of type %s does not match", name);
96     gras_assert1(res->category.scalar_data.encoding == encoding,
97                  "Redefinition of type %s does not match", name);
98     gras_assert1(res->category.scalar_data.type == type,
99                  "Redefinition of type %s does not match", name);
100     VERB1("Discarding redefinition of %s",name);
101     return res;
102   }
103   gras_ddt_new(name,&res);
104
105   for (arch = 0; arch < gras_arch_count; arch ++) {
106     long int sz;
107     long int mask;
108     res->size[arch]         = gras_arches[arch].sizeofs[type];
109     res->alignment[arch]    = gras_arches[arch].boundaries[type];
110     res->aligned_size[arch] = aligned(res->size[arch], res->alignment[arch]);
111 /* FIXME: kill the following after discution with Oli */
112 #if 0
113     sz = res->size[arch];
114     mask = sz;
115     
116     /* just in case you wonder, x>>1 == x/2 on all architectures when x>=0
117        and a size is always>=0 */
118     
119     /* make sure mask have all the bits under the biggest one of size set to 1
120        Example: size=000100101 => mask=0000111111 */
121     while ((sz >>= 1)) {
122       mask |= sz;
123     } 
124     
125     if (res->size[arch] & (mask >> 1)) { /* if size have bits to one beside its biggest */
126       /* size is not a power of 2 */
127       /* alignment= next power of 2 after size */
128       res->alignment[arch] = (res->size[arch] & ~(mask >> 1)) << 1;
129       gras_assert0(res->alignment[arch] != 0,
130                    "scalar type too large");
131       
132       res->aligned_size[arch] = aligned(res->size[arch], res->alignment[arch]);
133       gras_assert0 (res->aligned_size[arch] >= 0,
134                     "scalar type too large");
135       
136     } else {
137       /* size is a power of 2, life is great */
138       res->alignment[arch]       = res->size[arch];
139       res->aligned_size[arch]    = res->size[arch];
140     }
141 #endif     
142   }
143
144   res->category_code                 = e_gras_datadesc_type_cat_scalar;
145   res->category.scalar_data.encoding = encoding;
146   res->category.scalar_data.type     = type;
147   GRAS_OUT;
148   
149   return res;
150 }
151
152
153 /**
154  * gras_dd_cat_field_free:
155  *
156  * Frees one struct or union field
157  */
158 void gras_dd_cat_field_free(void *f) {
159   gras_dd_cat_field_t *field = *(gras_dd_cat_field_t **)f;
160   GRAS_IN;
161   if (field) {
162     if (field->name) 
163       gras_free(field->name);
164     gras_free(field);
165   }
166   GRAS_OUT;
167 }
168
169 /**
170  * gras_datadesc_struct:
171  *
172  * Create a new struct and give a pointer to it 
173  */
174 gras_datadesc_type_t *
175   gras_datadesc_struct(const char            *name) {
176
177   gras_error_t errcode;
178   gras_datadesc_type_t *res;
179   long int arch;
180   
181   GRAS_IN1("(%s)",name);
182   res = gras_datadesc_by_name(name);
183   if (res) {
184     /* FIXME: Check that field redefinition matches */
185     gras_assert1(res->category_code == e_gras_datadesc_type_cat_struct,
186                  "Redefinition of type %s does not match", name);
187     VERB1("Discarding redefinition of %s",name);
188     return res;
189   }
190   gras_ddt_new(name,&res);
191
192   for (arch=0; arch<gras_arch_count; arch ++) {
193     res->size[arch] = 0;
194     res->alignment[arch] = 0;
195     res->aligned_size[arch] = 0;
196   }
197   res->category_code = e_gras_datadesc_type_cat_struct;
198   gras_dynar_new(&(res->category.struct_data.fields),
199                  sizeof(gras_dd_cat_field_t*),
200                  &gras_dd_cat_field_free);
201
202   GRAS_OUT;
203   return res;
204 }
205
206 /**
207  * gras_datadesc_struct_append:
208  *
209  * Append a field to the struct
210  */
211 void
212 gras_datadesc_struct_append(gras_datadesc_type_t  *struct_type,
213                             const char            *name,
214                             gras_datadesc_type_t  *field_type) {
215
216   gras_error_t errcode;
217   gras_dd_cat_field_t *field;
218   int arch;
219
220   GRAS_IN3("(%s %s.%s;)",field_type->name,struct_type->name,name);
221   if (struct_type->category.struct_data.closed) {
222     VERB1("Ignoring request to add field to struct %s (closed. Redefinition?)",
223           struct_type->name);
224     return;
225   }
226
227   gras_assert1(field_type->size != 0,
228                "Cannot add a dynamically sized field in structure %s",
229                struct_type->name);
230     
231   field=gras_new(gras_dd_cat_field_t,1);
232   field->name   = (char*)strdup(name);
233
234   DEBUG0("----------------");
235   DEBUG3("PRE s={size=%ld,align=%ld,asize=%ld}",
236          struct_type->size[GRAS_THISARCH], 
237          struct_type->alignment[GRAS_THISARCH], 
238          struct_type->aligned_size[GRAS_THISARCH]);
239      
240      
241   for (arch=0; arch<gras_arch_count; arch ++) {
242     field->offset[arch] = aligned(struct_type->size[arch],
243                                   field_type->alignment[arch]);
244
245     struct_type->size[arch] = field->offset[arch] + field_type->size[arch];
246     struct_type->alignment[arch] = max(struct_type->alignment[arch],
247                                        field_type->alignment[arch]);
248     struct_type->aligned_size[arch] = aligned(struct_type->size[arch],
249                                               struct_type->alignment[arch]);
250   }
251   field->type   = field_type;
252   field->pre    = NULL;
253   field->post   = NULL;
254   
255   gras_dynar_push(struct_type->category.struct_data.fields, &field);
256
257   DEBUG3("Push a %s into %s at offset %ld.",
258          field_type->name, struct_type->name,field->offset[GRAS_THISARCH]);
259   DEBUG3("  f={size=%ld,align=%ld,asize=%ld}",
260          field_type->size[GRAS_THISARCH], 
261          field_type->alignment[GRAS_THISARCH], 
262          field_type->aligned_size[GRAS_THISARCH]);
263   DEBUG3("  s={size=%ld,align=%ld,asize=%ld}",
264          struct_type->size[GRAS_THISARCH], 
265          struct_type->alignment[GRAS_THISARCH], 
266          struct_type->aligned_size[GRAS_THISARCH]);
267   GRAS_OUT;
268 }
269
270 void
271 gras_datadesc_struct_close(gras_datadesc_type_t  *struct_type) {
272   GRAS_IN;
273   struct_type->category.struct_data.closed = 1;
274 }
275
276 /**
277  * gras_datadesc_union:
278  *
279  * Create a new union and give a pointer to it 
280  */
281 gras_datadesc_type_t *
282 gras_datadesc_union(const char                   *name,
283                     gras_datadesc_type_cb_int_t   selector) {
284
285   gras_error_t errcode;
286   gras_datadesc_type_t *res;
287   int arch;
288
289   GRAS_IN1("(%s)",name);
290   gras_assert0(selector,
291                "Attempt to creat an union without field_count function");
292
293   res = gras_datadesc_by_name(name);
294   if (res) {
295     /* FIXME: Check that field redefinition matches */
296     gras_assert1(res->category_code == e_gras_datadesc_type_cat_union,
297                  "Redefinition of type %s does not match", name);
298     gras_assert1(res->category.union_data.selector == selector,
299                  "Redefinition of type %s does not match", name);
300     VERB1("Discarding redefinition of %s",name);
301     return res;
302   }
303
304   gras_ddt_new(name,&res);
305
306   for (arch=0; arch<gras_arch_count; arch ++) {
307      res->size[arch] = 0;
308      res->alignment[arch] = 0;
309      res->aligned_size[arch] = 0;
310   }
311
312   res->category_code            = e_gras_datadesc_type_cat_union;
313   gras_dynar_new(&(res->category.union_data.fields),
314                  sizeof(gras_dd_cat_field_t*),
315                  &gras_dd_cat_field_free);
316   res->category.union_data.selector = selector;
317
318   return res;
319 }
320
321 /**
322  * gras_datadesc_union_append:
323  *
324  * Append a field to the union
325  */
326 void
327 gras_datadesc_union_append(gras_datadesc_type_t  *union_type,
328                            const char            *name,
329                            gras_datadesc_type_t  *field_type) {
330
331   gras_dd_cat_field_t *field;
332   int arch;
333
334   GRAS_IN3("(%s %s.%s;)",field_type->name,union_type->name,name);
335   gras_assert1(field_type->size != 0,
336                "Cannot add a dynamically sized field in union %s",
337                union_type->name);
338
339   if (union_type->category.union_data.closed) {
340     VERB1("Ignoring request to add field to union %s (closed)",
341            union_type->name);
342     return;
343   }
344     
345   field=gras_new0(gras_dd_cat_field_t,1);
346
347   field->name   = (char*)strdup(name);
348   field->type   = field_type;
349   /* All offset are left to 0 in an union */
350   
351   gras_dynar_push(union_type->category.union_data.fields, &field);
352
353   for (arch=0; arch<gras_arch_count; arch ++) {
354     union_type->size[arch] = max(union_type->size[arch],
355                                  field_type->size[arch]);
356     union_type->alignment[arch] = max(union_type->alignment[arch],
357                                       field_type->alignment[arch]);
358     union_type->aligned_size[arch] = aligned(union_type->size[arch],
359                                              union_type->alignment[arch]);
360   }
361 }
362
363 void
364 gras_datadesc_union_close(gras_datadesc_type_t  *union_type) {
365    union_type->category.union_data.closed = 1;
366 }
367 /**
368  * gras_datadesc_ref:
369  *
370  * Create a new ref to a fixed type and give a pointer to it 
371  */
372 gras_datadesc_type_t *
373   gras_datadesc_ref(const char             *name,
374                     gras_datadesc_type_t   *referenced_type) {
375
376   gras_error_t errcode;
377   gras_datadesc_type_t *res;
378   gras_datadesc_type_t *pointer_type = gras_datadesc_by_name("data pointer");
379   int arch;
380
381   GRAS_IN1("(%s)",name);
382   res = gras_datadesc_by_name(name);
383   if (res) {
384     gras_assert1(res->category_code == e_gras_datadesc_type_cat_ref,
385                  "Redefinition of %s does not match",name);
386     gras_assert1(res->category.ref_data.type == referenced_type,
387                  "Redefinition of %s does not match",name);
388     gras_assert1(res->category.ref_data.selector == NULL,
389                  "Redefinition of %s does not match",name);
390     VERB1("Discarding redefinition of %s",name);
391     return res;
392   }
393
394   gras_ddt_new(name,&res);
395
396   gras_assert0(pointer_type, "Cannot get the description of data pointer");
397       
398   for (arch=0; arch<gras_arch_count; arch ++){
399     res->size[arch] = pointer_type->size[arch];
400     res->alignment[arch] = pointer_type->alignment[arch];
401     res->aligned_size[arch] = pointer_type->aligned_size[arch];
402   }
403   
404   res->category_code              = e_gras_datadesc_type_cat_ref;
405   res->category.ref_data.type     = referenced_type;
406   res->category.ref_data.selector = NULL;
407
408   return res;
409 }
410 /**
411  * gras_datadesc_ref_generic:
412  *
413  * Create a new ref to a type given at use time, and give a pointer to it 
414  */
415 gras_datadesc_type_t *
416   gras_datadesc_ref_generic(const char                *name,
417                             gras_datadesc_selector_t   selector) {
418
419   gras_datadesc_type_t *res;
420   gras_datadesc_type_t *pointer_type = gras_datadesc_by_name("data pointer");
421   int arch;
422
423   GRAS_IN1("(%s)",name);
424   res = gras_datadesc_by_name(name);
425   if (res) {
426     gras_assert1(res->category_code == e_gras_datadesc_type_cat_ref,
427                  "Redefinition of type %s does not match", name);
428     gras_assert1(res->category.ref_data.type == NULL,
429                  "Redefinition of type %s does not match", name);
430     gras_assert1(res->category.ref_data.selector == selector,
431                  "Redefinition of type %s does not match", name);
432     VERB1("Discarding redefinition of %s",name);
433     return res;
434   }
435   gras_ddt_new(name,&res);
436
437   gras_assert0(pointer_type, "Cannot get the description of data pointer");
438       
439   for (arch=0; arch<gras_arch_count; arch ++) {
440     res->size[arch] = pointer_type->size[arch];
441     res->alignment[arch] = pointer_type->alignment[arch];
442     res->aligned_size[arch] = pointer_type->aligned_size[arch];
443   }
444
445   res->category_code            = e_gras_datadesc_type_cat_ref;
446
447   res->category.ref_data.type     = NULL;
448   res->category.ref_data.selector = selector;
449
450   return res;
451 }
452
453 /**
454  * gras_datadesc_array_fixed:
455  *
456  * Create a new array and give a pointer to it 
457  */
458 gras_datadesc_type_t *
459   gras_datadesc_array_fixed(const char              *name,
460                             gras_datadesc_type_t    *element_type,
461                             long int                 fixed_size) {
462
463   gras_datadesc_type_t *res;
464   int arch;
465
466   GRAS_IN1("(%s)",name);
467   res = gras_datadesc_by_name(name);
468   if (res) {
469     gras_assert1(res->category_code == e_gras_datadesc_type_cat_array,
470                  "Redefinition of type %s does not match", name);
471     gras_assert1(res->category.array_data.type == element_type,
472                  "Redefinition of type %s does not match", name);
473     gras_assert1(res->category.array_data.fixed_size == fixed_size,
474                  "Redefinition of type %s does not match", name);
475     gras_assert1(res->category.array_data.dynamic_size == NULL,
476                  "Redefinition of type %s does not match", name);
477     VERB1("Discarding redefinition of %s",name);
478
479     return res;
480   }
481   gras_ddt_new(name,&res);
482
483   gras_assert1(fixed_size > 0, "'%s' is a array of null fixed size",name);
484   for (arch=0; arch<gras_arch_count; arch ++) {
485     res->size[arch] = fixed_size * element_type->aligned_size[arch];
486     res->alignment[arch] = element_type->alignment[arch];
487     res->aligned_size[arch] = res->size[arch];
488   }  
489
490   res->category_code            = e_gras_datadesc_type_cat_array;
491
492   res->category.array_data.type         = element_type;
493   res->category.array_data.fixed_size   = fixed_size;
494   res->category.array_data.dynamic_size = NULL;
495
496   return res;
497 }
498 /**
499  * gras_datadesc_array_dyn:
500  *
501  * Create a new array and give a pointer to it 
502  */
503 gras_datadesc_type_t *
504   gras_datadesc_array_dyn(const char                      *name,
505                           gras_datadesc_type_t            *element_type,
506                           gras_datadesc_type_cb_int_t      dynamic_size) {
507
508   gras_datadesc_type_t *res;
509   int arch;
510
511   GRAS_IN1("(%s)",name);
512   gras_assert1(dynamic_size,
513                "'%s' is a dynamic array without size discriminant",
514                name);
515
516   res = gras_datadesc_by_name(name);
517   if (res) {
518     gras_assert1(res->category_code == e_gras_datadesc_type_cat_array,
519                  "Redefinition of type %s does not match", name);
520     gras_assert1(res->category.array_data.type == element_type,
521                  "Redefinition of type %s does not match", name);
522     gras_assert1(res->category.array_data.fixed_size == 0,
523                  "Redefinition of type %s does not match", name);
524     gras_assert1(res->category.array_data.dynamic_size == dynamic_size,
525                  "Redefinition of type %s does not match", name);
526     VERB1("Discarding redefinition of %s",name);
527
528     return res;
529   }
530
531   gras_ddt_new(name,&res);
532
533   for (arch=0; arch<gras_arch_count; arch ++) {
534     res->size[arch] = 0; /* make sure it indicates "dynamic" */
535     res->alignment[arch] = element_type->alignment[arch];
536     res->aligned_size[arch] = 0; /*FIXME: That was so in GS, but looks stupid*/
537   }
538
539   res->category_code            = e_gras_datadesc_type_cat_array;
540
541   res->category.array_data.type         = element_type;
542   res->category.array_data.fixed_size   = 0;
543   res->category.array_data.dynamic_size = dynamic_size;
544
545   return res;
546 }
547
548 /**
549  * gras_datadesc_ref_pop_arr:
550  *
551  * Most of the time, you want to include a reference in your structure which
552  * is a pointer to a dynamic array whose size is fixed by another field of 
553  * your structure.
554  *
555  * This case pops up so often that this function was created to take care of
556  * this case. It creates a dynamic array type whose size is poped from the 
557  * current cbps, and then create a reference to it.
558  *
559  * The name of the created datatype will be the name of the element type, with
560  * '[]*' appended to it.
561  *
562  * Then to use it, you just have to make sure that your structure pre-callback
563  * does push the size of the array in the cbps (using #gras_cbps_i_push), and 
564  * you are set. 
565  *
566  * But be remember that this is a stack. If you have two different pop_arr, you
567  * should push the second one first, so that the first one is on the top of the 
568  * list when the first field gets transfered.
569  *
570  */
571 gras_datadesc_type_t *
572   gras_datadesc_ref_pop_arr(gras_datadesc_type_t  *element_type) {
573
574   gras_datadesc_type_t *res;
575   char *name=(char*)gras_malloc(strlen(element_type->name) + 4);
576
577   sprintf(name,"%s[]",element_type->name);
578
579   res = gras_datadesc_array_dyn(name,element_type,
580                                 gras_datadesc_cb_pop);
581
582   sprintf(name,"%s[]*",element_type->name);
583   res = gras_datadesc_ref(name,res);
584
585   gras_free(name);
586
587   return res;
588 }
589 gras_error_t
590 gras_datadesc_import_nws(const char           *name,
591                          const DataDescriptor *desc,
592                          size_t                howmany,
593                          gras_datadesc_type_t **dst) {
594   RAISE_UNIMPLEMENTED;
595 }
596
597 /**
598  * gras_datadesc_cb_send:
599  *
600  * Add a pre-send callback to this datadesc.
601  * (useful to push the sizes of the upcoming arrays, for example)
602  */
603 void gras_datadesc_cb_send (gras_datadesc_type_t         *type,
604                             gras_datadesc_type_cb_void_t  send) {
605   type->send = send;
606 }
607 /**
608  * gras_datadesc_cb_recv:
609  *
610  * Add a post-receive callback to this datadesc.
611  * (useful to put the function pointers to the rigth value, for example)
612  */
613 void gras_datadesc_cb_recv(gras_datadesc_type_t         *type,
614                            gras_datadesc_type_cb_void_t  recv) {
615   type->recv = recv;
616 }
617 /**
618  * gras_dd_find_field:
619  * 
620  * Returns the type descriptor of the given field. Abort on error.
621  */
622 static gras_datadesc_type_t *
623   gras_dd_find_field(gras_datadesc_type_t         *type,
624                      const char                   *field_name) {
625    gras_datadesc_type_t *sub_type=NULL;
626    gras_dynar_t         *field_array;
627    
628    gras_dd_cat_field_t  *field=NULL;
629    int                   field_num;
630    
631    if (type->category_code == e_gras_datadesc_type_cat_union) {
632       field_array = type->category.union_data.fields;
633    } else if (type->category_code == e_gras_datadesc_type_cat_struct) {
634       field_array = type->category.struct_data.fields;
635    } else {
636       ERROR2("%s (%p) is not a struct nor an union. There is no field.", type->name,(void*)type);
637       gras_abort();
638    }
639    gras_dynar_foreach(field_array,field_num,field) {
640       if (!strcmp(field_name,field->name)) {
641          return field->type;
642       }
643    }
644    ERROR2("No field nammed %s in %s",field_name,type->name);
645    gras_abort();
646
647 }
648                                   
649 /**
650  * gras_datadesc_cb_field_send:
651  *
652  * Add a pre-send callback to the given field of the datadesc (which must be a struct or union).
653  * (useful to push the sizes of the upcoming arrays, for example)
654  */
655 void gras_datadesc_cb_field_send (gras_datadesc_type_t         *type,
656                                   const char                   *field_name,
657                                   gras_datadesc_type_cb_void_t  send) {
658    
659    gras_datadesc_type_t *sub_type=gras_dd_find_field(type,field_name);   
660    sub_type->send = send;
661 }
662
663 /**
664  * gras_datadesc_cb_field_push:
665  *
666  * Add a pre-send callback to the given field resulting in its value to be pushed to
667  * the stack of sizes. It must be a int, unsigned int, long int or unsigned long int.
668  */
669 void gras_datadesc_cb_field_push (gras_datadesc_type_t         *type,
670                                   const char                   *field_name) {
671    
672    gras_datadesc_type_t *sub_type=gras_dd_find_field(type,field_name);
673    if (!strcmp("int",sub_type->name)) {
674       sub_type->send = gras_datadesc_cb_push_int;
675    } else if (!strcmp("unsigned int",sub_type->name)) {
676       sub_type->send = gras_datadesc_cb_push_uint;
677    } else if (!strcmp("long int",sub_type->name)) {
678       sub_type->send = gras_datadesc_cb_push_lint;
679    } else if (!strcmp("unsigned long int",sub_type->name)) {
680       sub_type->send = gras_datadesc_cb_push_ulint;
681    } else {
682       ERROR1("Field %s is not an int, unsigned int, long int neither unsigned long int",
683              sub_type->name);
684       gras_abort();
685    }
686 }
687 /**
688  * gras_datadesc_cb_field_recv:
689  *
690  * Add a post-receive callback to the given field of the datadesc (which must be a struct or union).
691  * (useful to put the function pointers to the right value, for example)
692  */
693 void gras_datadesc_cb_field_recv(gras_datadesc_type_t         *type,
694                                  const char                   *field_name,
695                                  gras_datadesc_type_cb_void_t  recv) {
696    
697    gras_datadesc_type_t *sub_type=gras_dd_find_field(type,field_name);   
698    sub_type->recv = recv;
699 }
700
701 /**
702  * gras_datadesc_free:
703  *
704  * Free a datadesc. Should only be called at gras_exit. 
705  */
706 void gras_datadesc_free(gras_datadesc_type_t *type) {
707
708   DEBUG1("Let's free ddt %s",type->name);
709   
710   switch (type->category_code) {
711   case e_gras_datadesc_type_cat_scalar:
712   case e_gras_datadesc_type_cat_ref:
713   case e_gras_datadesc_type_cat_array:
714     /* nothing to free in there */
715     break;
716     
717   case e_gras_datadesc_type_cat_ignored:
718     if (type->category.ignored_data.free_func) {
719       type->category.ignored_data.free_func
720         (type->category.ignored_data.default_value);
721     }
722     break;
723     
724   case e_gras_datadesc_type_cat_struct:
725     gras_dynar_free(type->category.struct_data.fields);
726     break;
727     
728   case e_gras_datadesc_type_cat_union:
729     gras_dynar_free(type->category.union_data.fields);
730     break;
731     
732   default:
733     /* datadesc was invalid. Killing it is like euthanasy, I guess */
734     break;
735   }
736   gras_free(type->name);
737   gras_free(type);
738 }