Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
c720f9c76990588493bad63c9f6a91b56536d3c6
[simgrid.git] / src / gras_simix / DataDesc / gras_simix_ddt_create.c
1 /* $Id$ */
2
3 /* ddt_new - creation/deletion of datatypes structs (private to this module)*/
4
5 /* Copyright (c) 2003 Olivier Aumage.                                       */
6 /* Copyright (c) 2003, 2004 Martin Quinson.                                 */
7 /* All rights reserved.                                                     */
8
9 /* This program is free software; you can redistribute it and/or modify it
10  * under the terms of the license (GNU LGPL) which comes with this package. */
11
12 #include "xbt/misc.h" /* min()/max() */
13 #include "xbt/ex.h"
14 #include "gras/DataDesc/datadesc_private.h"
15
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_ddt_create,gras_ddt,"Creating new datadescriptions");
17
18 /*** prototypes ***/
19 static gras_dd_cat_field_t
20   gras_dd_find_field(gras_datadesc_type_t  type,
21                      const char           *field_name);
22 /**
23  * gras_ddt_freev:
24  *
25  * gime that memory back, dude. I mean it.
26  */
27 void gras_ddt_freev(void *ddt) {
28   gras_datadesc_type_t type= (gras_datadesc_type_t)ddt;
29   
30   if (type) {
31     gras_datadesc_free(&type);
32   }
33 }
34
35 static gras_datadesc_type_t gras_ddt_new(const char *name) {
36   gras_datadesc_type_t res;
37
38   XBT_IN1("(%s)",name);
39   res=xbt_new0(s_gras_datadesc_type_t,1);
40
41   res->name = (char*)strdup(name);
42   res->name_len = strlen(name);
43   res->cycle = 0;
44       
45   xbt_set_add(gras_datadesc_set_local,
46                (xbt_set_elm_t)res,&gras_ddt_freev);
47   XBT_OUT;
48   return res;
49 }
50
51 /** @brief retrieve an existing message type from its name (or NULL if it does not exist). */
52 gras_datadesc_type_t gras_datadesc_by_name_or_null (const char *name) {
53   xbt_ex_t e;
54   gras_datadesc_type_t res = NULL;
55    
56   TRY {
57      res = gras_datadesc_by_name(name);
58   } CATCH(e) {
59      res = NULL;
60      xbt_ex_free(e);
61   }
62   return res;
63 }
64 /**
65  * Search the given datadesc (or raises an exception if it can't be found)
66  */
67 gras_datadesc_type_t gras_datadesc_by_name(const char *name) {
68   xbt_ex_t e;
69   gras_datadesc_type_t res = NULL;
70   volatile int found = 0;
71   TRY {
72     res = (gras_datadesc_type_t)xbt_set_get_by_name(gras_datadesc_set_local,name);
73     found = 1;
74   } CATCH(e) {
75     if (e.category != not_found_error)
76       RETHROW;
77     xbt_ex_free(e);
78   }
79   if (!found)
80     THROW1(not_found_error,0,"No registred datatype of that name: %s",name);     
81    
82   return res;
83 }
84
85 /**
86  * Retrieve a type from its code (or NULL if not found)
87  */
88 gras_datadesc_type_t gras_datadesc_by_id(long int code) {
89   xbt_ex_t e;
90   gras_datadesc_type_t res=NULL;
91   TRY {
92     res = (gras_datadesc_type_t)xbt_set_get_by_id(gras_datadesc_set_local,code);
93   } CATCH(e) {
94     if (e.category != not_found_error)
95       RETHROW;
96     xbt_ex_free(e);
97     res = NULL;
98   }
99   return res;
100 }
101
102 /**
103  * Create a new scalar and give a pointer to it 
104  */
105 gras_datadesc_type_t 
106   gras_datadesc_scalar(const char                      *name,
107                        gras_ddt_scalar_type_t           type,
108                        enum e_gras_dd_scalar_encoding   encoding) {
109
110   gras_datadesc_type_t res;
111   long int arch;
112
113   XBT_IN;
114   res = gras_datadesc_by_name_or_null(name);
115   if (res) {
116     xbt_assert1(res->category_code == e_gras_datadesc_type_cat_scalar,
117                  "Redefinition of type %s does not match", name);
118     xbt_assert1(res->category.scalar_data.encoding == encoding,
119                  "Redefinition of type %s does not match", name);
120     xbt_assert1(res->category.scalar_data.type == type,
121                  "Redefinition of type %s does not match", name);
122     VERB1("Discarding redefinition of %s",name);
123     return res;
124   } 
125   res = gras_ddt_new(name);
126
127   for (arch = 0; arch < gras_arch_count; arch ++) {
128     res->size[arch]         = gras_arches[arch].sizeofs[type];
129     res->alignment[arch]    = gras_arches[arch].boundaries[type];
130     res->aligned_size[arch] = ddt_aligned(res->size[arch], res->alignment[arch]);
131   }
132
133   res->category_code                 = e_gras_datadesc_type_cat_scalar;
134   res->category.scalar_data.encoding = encoding;
135   res->category.scalar_data.type     = type;
136   XBT_OUT;
137   
138   return res;
139 }
140
141
142 /** Frees one struct or union field */
143 void gras_dd_cat_field_free(void *f) {
144   gras_dd_cat_field_t field = *(gras_dd_cat_field_t *)f;
145   XBT_IN;
146   if (field) {
147     if (field->name) 
148       free(field->name);
149     free(field);
150   }
151   XBT_OUT;
152 }
153
154 /** \brief Declare a new structure description */
155 gras_datadesc_type_t 
156   gras_datadesc_struct(const char            *name) {
157
158   gras_datadesc_type_t res;
159   long int arch;
160   
161   XBT_IN1("(%s)",name);
162   res = gras_datadesc_by_name_or_null(name);
163   if (res) {
164     /* FIXME: Check that field redefinition matches */
165     xbt_assert1(res->category_code == e_gras_datadesc_type_cat_struct,
166                  "Redefinition of type %s does not match", name);
167     VERB1("Discarding redefinition of %s",name);
168     return res;
169   }
170   res = gras_ddt_new(name);
171
172   for (arch=0; arch<gras_arch_count; arch ++) {
173     res->size[arch] = 0;
174     res->alignment[arch] = 0;
175     res->aligned_size[arch] = 0;
176   }
177   res->category_code = e_gras_datadesc_type_cat_struct;
178   res->category.struct_data.fields = 
179        xbt_dynar_new(sizeof(gras_dd_cat_field_t),
180                       &gras_dd_cat_field_free);
181
182   XBT_OUT;
183   return res;
184 }
185
186 /** \brief Append a new field to a structure description */
187 void
188 gras_datadesc_struct_append(gras_datadesc_type_t struct_type,
189                             const char          *name,
190                             gras_datadesc_type_t field_type) {
191
192   gras_dd_cat_field_t field;
193   int arch;
194
195   xbt_assert2(field_type,
196                "Cannot add the field '%s' into struct '%s': its type is NULL",
197                name,struct_type->name);
198   XBT_IN3("(%s %s.%s;)",field_type->name,struct_type->name,name);
199   if (struct_type->category.struct_data.closed) {
200     VERB1("Ignoring request to add field to struct %s (closed. Redefinition?)",
201           struct_type->name);
202     return;
203   }
204
205   xbt_assert1(field_type->size != 0,
206                "Cannot add a dynamically sized field in structure %s",
207                struct_type->name);
208     
209   field=xbt_new(s_gras_dd_cat_field_t,1);
210   field->name   = (char*)strdup(name);
211
212   DEBUG0("----------------");
213   DEBUG3("PRE s={size=%ld,align=%ld,asize=%ld}",
214          struct_type->size[GRAS_THISARCH], 
215          struct_type->alignment[GRAS_THISARCH], 
216          struct_type->aligned_size[GRAS_THISARCH]);
217      
218      
219   for (arch=0; arch<gras_arch_count; arch ++) {
220     field->offset[arch] = ddt_aligned(struct_type->size[arch],
221                                       field_type->alignment[arch]);
222
223     struct_type->size[arch] = field->offset[arch] + field_type->size[arch];
224     struct_type->alignment[arch] = max(struct_type->alignment[arch],
225                                        field_type->alignment[arch]);
226     struct_type->aligned_size[arch] = ddt_aligned(struct_type->size[arch],
227                                                   struct_type->alignment[arch]);
228   }
229   field->type   = field_type;
230   field->send   = NULL;
231   field->recv   = NULL;
232   
233   xbt_dynar_push(struct_type->category.struct_data.fields, &field);
234
235   DEBUG3("Push a %s into %s at offset %ld.",
236          field_type->name, struct_type->name,field->offset[GRAS_THISARCH]);
237   DEBUG3("  f={size=%ld,align=%ld,asize=%ld}",
238          field_type->size[GRAS_THISARCH], 
239          field_type->alignment[GRAS_THISARCH], 
240          field_type->aligned_size[GRAS_THISARCH]);
241   DEBUG3("  s={size=%ld,align=%ld,asize=%ld}",
242          struct_type->size[GRAS_THISARCH], 
243          struct_type->alignment[GRAS_THISARCH], 
244          struct_type->aligned_size[GRAS_THISARCH]);
245   XBT_OUT;
246 }
247
248 /** \brief Close a structure description
249  * 
250  * No new field can be added afterward, and it is mandatory to close the structure before using it.
251  */
252 void
253 gras_datadesc_struct_close(gras_datadesc_type_t struct_type) {
254   XBT_IN;
255   struct_type->category.struct_data.closed = 1;
256   DEBUG4("structure %s closed. size=%ld,align=%ld,asize=%ld",
257          struct_type->name,
258          struct_type->size[GRAS_THISARCH], 
259          struct_type->alignment[GRAS_THISARCH], 
260          struct_type->aligned_size[GRAS_THISARCH]);
261 }
262
263 /**
264  * gras_datadesc_cycle_set:
265  * 
266  * Tell GRAS that the pointers of the type described by ddt may present
267  * some loop, and that the cycle detection mechanism is needed.
268  *
269  * Note that setting this option when not needed have a rather bad effect 
270  * on the performance (several times slower on big data).
271  */
272 void
273 gras_datadesc_cycle_set(gras_datadesc_type_t ddt) {
274   ddt->cycle = 1;
275 }
276
277 /**
278  * gras_datadesc_cycle_unset:
279  * 
280  * Tell GRAS that the pointers of the type described by ddt do not present
281  * any loop and that cycle detection mechanism are not needed.
282  * (default)
283  */
284 void
285 gras_datadesc_cycle_unset(gras_datadesc_type_t ddt) {
286   ddt->cycle = 0;
287 }
288
289 /** \brief Declare a new union description */
290 gras_datadesc_type_t 
291   gras_datadesc_union(const char                   *name,
292                       gras_datadesc_type_cb_int_t   selector) {
293
294   gras_datadesc_type_t res;
295   int arch;
296
297   XBT_IN1("(%s)",name);
298   xbt_assert0(selector,
299                "Attempt to creat an union without field_count function");
300
301   res = gras_datadesc_by_name_or_null(name);
302   if (res) {
303     /* FIXME: Check that field redefinition matches */
304     xbt_assert1(res->category_code == e_gras_datadesc_type_cat_union,
305                  "Redefinition of type %s does not match", name);
306     xbt_assert1(res->category.union_data.selector == selector,
307                  "Redefinition of type %s does not match", name);
308     VERB1("Discarding redefinition of %s",name);
309     return res;
310   }
311
312   res = gras_ddt_new(name);
313
314   for (arch=0; arch<gras_arch_count; arch ++) {
315      res->size[arch] = 0;
316      res->alignment[arch] = 0;
317      res->aligned_size[arch] = 0;
318   }
319
320   res->category_code            = e_gras_datadesc_type_cat_union;
321   res->category.union_data.fields =
322      xbt_dynar_new(sizeof(gras_dd_cat_field_t*),
323                     &gras_dd_cat_field_free);
324   res->category.union_data.selector = selector;
325
326   return res;
327 }
328
329 /** \brief Append a new field to an union description */
330 void gras_datadesc_union_append(gras_datadesc_type_t  union_type,
331                                 const char           *name,
332                                 gras_datadesc_type_t  field_type) {
333
334   gras_dd_cat_field_t field;
335   int arch;
336
337   XBT_IN3("(%s %s.%s;)",field_type->name,union_type->name,name);
338   xbt_assert1(field_type->size != 0,
339                "Cannot add a dynamically sized field in union %s",
340                union_type->name);
341
342   if (union_type->category.union_data.closed) {
343     VERB1("Ignoring request to add field to union %s (closed)",
344            union_type->name);
345     return;
346   }
347     
348   field=xbt_new0(s_gras_dd_cat_field_t,1);
349
350   field->name   = (char*)strdup(name);
351   field->type   = field_type;
352   /* All offset are left to 0 in an union */
353   
354   xbt_dynar_push(union_type->category.union_data.fields, &field);
355
356   for (arch=0; arch<gras_arch_count; arch ++) {
357     union_type->size[arch] = max(union_type->size[arch],
358                                  field_type->size[arch]);
359     union_type->alignment[arch] = max(union_type->alignment[arch],
360                                       field_type->alignment[arch]);
361     union_type->aligned_size[arch] = ddt_aligned(union_type->size[arch],
362                                                  union_type->alignment[arch]);
363   }
364 }
365
366
367 /** \brief Close an union description 
368  *
369  * No new field can be added afterward, and it is mandatory to close the union before using it.
370  */
371 void
372 gras_datadesc_union_close(gras_datadesc_type_t union_type) {
373    union_type->category.union_data.closed = 1;
374 }
375
376 /** \brief Declare a new type being a reference to the one passed in arg */
377 gras_datadesc_type_t 
378   gras_datadesc_ref(const char           *name,
379                     gras_datadesc_type_t  referenced_type) {
380
381   gras_datadesc_type_t res;
382   gras_datadesc_type_t pointer_type = gras_datadesc_by_name("data pointer");
383   int arch;
384
385   XBT_IN1("(%s)",name);
386   res = gras_datadesc_by_name_or_null(name);
387   if (res) {
388     xbt_assert1(res->category_code == e_gras_datadesc_type_cat_ref,
389                  "Redefinition of %s does not match",name);
390     xbt_assert1(res->category.ref_data.type == referenced_type,
391                  "Redefinition of %s does not match",name);
392     xbt_assert1(res->category.ref_data.selector == NULL,
393                  "Redefinition of %s does not match",name);
394     VERB1("Discarding redefinition of %s",name);
395     return res;
396   }
397
398   res = gras_ddt_new(name);
399
400   xbt_assert0(pointer_type, "Cannot get the description of data pointer");
401       
402   for (arch=0; arch<gras_arch_count; arch ++){
403     res->size[arch] = pointer_type->size[arch];
404     res->alignment[arch] = pointer_type->alignment[arch];
405     res->aligned_size[arch] = pointer_type->aligned_size[arch];
406   }
407   
408   res->category_code              = e_gras_datadesc_type_cat_ref;
409   res->category.ref_data.type     = referenced_type;
410   res->category.ref_data.selector = NULL;
411
412   return res;
413 }
414 /** \brief Declare a new type being a generic reference.
415  * 
416  * The callback passed in argument is to be used to select which type is currently used.
417  * So, when GRAS wants to send a generic reference, it passes the current data to the selector 
418  * callback and expects it to return the type description to use. 
419  */
420 gras_datadesc_type_t 
421   gras_datadesc_ref_generic(const char                *name,
422                             gras_datadesc_selector_t   selector) {
423
424   gras_datadesc_type_t res;
425   gras_datadesc_type_t pointer_type = gras_datadesc_by_name("data pointer");
426   int arch;
427
428   XBT_IN1("(%s)",name);
429   res = gras_datadesc_by_name_or_null(name);
430
431   if (res) {
432     xbt_assert1(res->category_code == e_gras_datadesc_type_cat_ref,
433                  "Redefinition of type %s does not match", name);
434     xbt_assert1(res->category.ref_data.type == NULL,
435                  "Redefinition of type %s does not match", name);
436     xbt_assert1(res->category.ref_data.selector == selector,
437                  "Redefinition of type %s does not match", name);
438     VERB1("Discarding redefinition of %s",name);
439     return res;
440   }
441   res = gras_ddt_new(name);
442
443   xbt_assert0(pointer_type, "Cannot get the description of data pointer");
444       
445   for (arch=0; arch<gras_arch_count; arch ++) {
446     res->size[arch] = pointer_type->size[arch];
447     res->alignment[arch] = pointer_type->alignment[arch];
448     res->aligned_size[arch] = pointer_type->aligned_size[arch];
449   }
450
451   res->category_code            = e_gras_datadesc_type_cat_ref;
452
453   res->category.ref_data.type     = NULL;
454   res->category.ref_data.selector = selector;
455
456   return res;
457 }
458
459 /** \brief Declare a new type being an array of fixed size and content */
460 gras_datadesc_type_t 
461   gras_datadesc_array_fixed(const char           *name,
462                             gras_datadesc_type_t  element_type,
463                             long int              fixed_size) {
464
465   gras_datadesc_type_t res;
466   int arch;
467
468   XBT_IN1("(%s)",name);
469   res = gras_datadesc_by_name_or_null(name);
470   if (res) {
471     xbt_assert1(res->category_code == e_gras_datadesc_type_cat_array,
472                  "Redefinition of type %s does not match", name);
473      
474     if (res->category.array_data.type != element_type) {
475        ERROR1("Redefinition of type %s does not match: array elements differ", name);
476        gras_datadesc_type_dump(res->category.array_data.type);
477        gras_datadesc_type_dump(element_type);
478     }
479      
480     xbt_assert1(res->category.array_data.fixed_size == fixed_size,
481                  "Redefinition of type %s does not match", name);
482     xbt_assert1(res->category.array_data.dynamic_size == NULL,
483                  "Redefinition of type %s does not match", name);
484     VERB1("Discarding redefinition of %s",name);
485
486     return res;
487   }
488   res = gras_ddt_new(name);
489
490   xbt_assert1(fixed_size > 0, "'%s' is a array of null fixed size",name);
491   for (arch=0; arch<gras_arch_count; arch ++) {
492     res->size[arch] = fixed_size * element_type->aligned_size[arch];
493     res->alignment[arch] = element_type->alignment[arch];
494     res->aligned_size[arch] = res->size[arch];
495   }  
496
497   res->category_code            = e_gras_datadesc_type_cat_array;
498
499   res->category.array_data.type         = element_type;
500   res->category.array_data.fixed_size   = fixed_size;
501   res->category.array_data.dynamic_size = NULL;
502
503   return res;
504 }
505
506 /** \brief Declare a new type being an array of fixed size, but accepting several content types. */
507 gras_datadesc_type_t gras_datadesc_array_dyn(const char                 *name,
508                                              gras_datadesc_type_t        element_type,
509                                              gras_datadesc_type_cb_int_t dynamic_size) {
510
511   gras_datadesc_type_t res;
512   int arch;
513
514   XBT_IN1("(%s)",name);
515   xbt_assert1(dynamic_size,
516                "'%s' is a dynamic array without size discriminant",
517                name);
518
519   res = gras_datadesc_by_name_or_null(name);
520   if (res) {
521     xbt_assert1(res->category_code == e_gras_datadesc_type_cat_array,
522                  "Redefinition of type %s does not match", name);
523     xbt_assert1(res->category.array_data.type == element_type,
524                  "Redefinition of type %s does not match", name);
525     xbt_assert1(res->category.array_data.fixed_size == 0,
526                  "Redefinition of type %s does not match", name);
527     xbt_assert1(res->category.array_data.dynamic_size == dynamic_size,
528                  "Redefinition of type %s does not match", name);
529     VERB1("Discarding redefinition of %s",name);
530
531     return res;
532   }
533
534   res = gras_ddt_new(name);
535
536   for (arch=0; arch<gras_arch_count; arch ++) {
537     res->size[arch] = 0; /* make sure it indicates "dynamic" */
538     res->alignment[arch] = element_type->alignment[arch];
539     res->aligned_size[arch] = 0; /*FIXME: That was so in GS, but looks stupid*/
540   }
541
542   res->category_code            = e_gras_datadesc_type_cat_array;
543
544   res->category.array_data.type         = element_type;
545   res->category.array_data.fixed_size   = 0;
546   res->category.array_data.dynamic_size = dynamic_size;
547
548   return res;
549 }
550
551 /** \brief Declare a new type being an array which size can be found with \ref gras_cbps_i_pop 
552  * 
553  * Most of the time, you want to include a reference in your structure which
554  * is a pointer to a dynamic array whose size is fixed by another field of 
555  * your structure.
556  *
557  * This case pops up so often that this function was created to take care of
558  * this case. It creates a dynamic array type whose size is poped from the 
559  * current cbps, and then create a reference to it.
560  *
561  * The name of the created datatype will be the name of the element type, with
562  * '[]*' appended to it.
563  *
564  * Then to use it, you just have to make sure that your structure pre-callback
565  * does push the size of the array in the cbps (using #gras_cbps_i_push), and 
566  * you are set. 
567  *
568  * But be remember that this is a stack. If you have two different pop_arr, you
569  * should push the second one first, so that the first one is on the top of the 
570  * list when the first field gets transfered.
571  *
572  */
573 gras_datadesc_type_t 
574   gras_datadesc_ref_pop_arr(gras_datadesc_type_t element_type) {
575
576   gras_datadesc_type_t res;
577   char *name=(char*)xbt_malloc(strlen(element_type->name) + 4);
578
579   sprintf(name,"%s[]",element_type->name);
580
581   res = gras_datadesc_array_dyn(name,element_type,
582                                 gras_datadesc_cb_pop);
583
584   sprintf(name,"%s[]*",element_type->name);
585   res = gras_datadesc_ref(name,res);
586
587   free(name);
588
589   return res;
590 }
591
592 /*
593  *##
594  *## Constructor of container datatypes
595  *##
596  */
597
598 #include "xbt/dynar_private.h"
599 static void gras_datadesc_dynar_cb(gras_datadesc_type_t typedesc, gras_cbps_t vars, void *data) {
600   gras_datadesc_type_t subtype;
601   xbt_dynar_t dynar=(xbt_dynar_t)data;
602    
603   memcpy(&dynar->free_f, &typedesc->extra, sizeof(dynar->free_f));
604
605   /* search for the elemsize in what we have. If elements are "int", typedesc got is "int[]*" */
606   subtype = gras_dd_find_field(typedesc,"data")->type;
607   
608   /* this is now a ref to array of what we're looking for */
609   subtype = subtype->category.ref_data.type;
610   subtype = subtype->category.array_data.type;
611    
612   DEBUG1("subtype is %s",subtype->name);
613
614   dynar->elmsize = subtype->size[GRAS_THISARCH];
615   dynar->size = dynar->used;
616 }
617
618 /** \brief Declare a new type being a dynar in which each elements are of the given type
619  * 
620  *  The type gets registered under the name "dynar(%s)_s", where %s is the name of the subtype.
621  *  For example, a dynar of doubles will be called "dynar(double)_s" and a dynar of dynar of 
622  *  strings will be called "dynar(dynar(string)_s)_s".
623  * 
624  *  \param elm_t: the datadesc of the elements
625  *  \param free_func: the function to use to free the elements when the dynar gets freed
626  */
627 gras_datadesc_type_t
628 gras_datadesc_dynar(gras_datadesc_type_t elm_t,
629                     void_f_pvoid_t *free_func) {
630    
631   char *buffname;
632   gras_datadesc_type_t res;
633   
634   asprintf(&buffname,"dynar(%s)_s",elm_t->name);
635    
636   res = gras_datadesc_struct(buffname);
637   
638   gras_datadesc_struct_append(res, "size",    
639                               gras_datadesc_by_name("unsigned long int"));
640    
641   gras_datadesc_struct_append(res, "used",    
642                               gras_datadesc_by_name("unsigned long int"));
643    
644   gras_datadesc_struct_append(res, "elmsize", 
645                               gras_datadesc_by_name("unsigned long int"));
646    
647   gras_datadesc_struct_append(res, "data",    
648                               gras_datadesc_ref_pop_arr (elm_t));
649
650   gras_datadesc_struct_append(res, "free_f",  
651                               gras_datadesc_by_name("function pointer"));
652   memcpy(res->extra,&free_func,sizeof(free_func));
653       
654   gras_datadesc_struct_close(res);
655    
656   gras_datadesc_cb_field_push(res, "used");
657   gras_datadesc_cb_recv(res,  &gras_datadesc_dynar_cb);
658
659   /* build a ref to it */
660   free(buffname);
661   asprintf(&buffname,"dynar(%s)",elm_t->name);
662   res=gras_datadesc_ref(buffname,res);
663   free(buffname);
664   return res;
665 }
666
667 #include "xbt/matrix.h"
668 static void gras_datadesc_matrix_cb(gras_datadesc_type_t typedesc, gras_cbps_t vars, void *data) {
669   gras_datadesc_type_t subtype;
670   xbt_matrix_t matrix=(xbt_matrix_t)data;
671    
672   memcpy(&matrix->free_f, &typedesc->extra, sizeof(matrix->free_f));
673
674   /* search for the elemsize in what we have. If elements are "int", typedesc got is "int[]*" */
675   subtype = gras_dd_find_field(typedesc,"data")->type;
676   
677   /* this is now a ref to array of what we're looking for */
678   subtype = subtype->category.ref_data.type;
679   subtype = subtype->category.array_data.type;
680    
681   DEBUG1("subtype is %s",subtype->name);
682
683   matrix->elmsize = subtype->size[GRAS_THISARCH];
684 }
685 gras_datadesc_type_t
686 gras_datadesc_matrix(gras_datadesc_type_t elm_t,
687                      void_f_pvoid_t * const free_f) {
688   char *buffname;
689   gras_datadesc_type_t res;
690
691   asprintf(&buffname,"s_xbt_matrix_t(%s)",elm_t->name);   
692   res = gras_datadesc_struct(buffname);
693   
694   gras_datadesc_struct_append(res, "lines",    
695                               gras_datadesc_by_name("unsigned int"));
696   gras_datadesc_struct_append(res, "rows",    
697                               gras_datadesc_by_name("unsigned int"));
698    
699   gras_datadesc_struct_append(res, "elmsize", 
700                               gras_datadesc_by_name("unsigned long int"));
701    
702   gras_datadesc_struct_append(res, "data",    
703                               gras_datadesc_ref_pop_arr (elm_t));
704   gras_datadesc_struct_append(res, "free_f",  
705                               gras_datadesc_by_name("function pointer"));      
706   gras_datadesc_struct_close(res);
707     
708   gras_datadesc_cb_field_push(res, "lines");
709   gras_datadesc_cb_field_push_multiplier(res, "rows");
710
711   gras_datadesc_cb_recv(res,  &gras_datadesc_matrix_cb);
712   memcpy(res->extra,&free_f,sizeof(free_f));
713
714   /* build a ref to it */
715   free(buffname);
716   asprintf(&buffname,"xbt_matrix_t(%s)",elm_t->name);
717   res=gras_datadesc_ref(buffname,res);
718   free(buffname);
719   return res;
720 }
721
722 gras_datadesc_type_t
723 gras_datadesc_import_nws(const char           *name,
724                          const DataDescriptor *desc,
725                          unsigned long         howmany) {
726   THROW_UNIMPLEMENTED;
727 }
728
729 /**
730  * (useful to push the sizes of the upcoming arrays, for example)
731  */
732 void gras_datadesc_cb_send (gras_datadesc_type_t          type,
733                             gras_datadesc_type_cb_void_t  send) {
734   type->send = send;
735 }
736 /**
737  * (useful to put the function pointers to the rigth value, for example)
738  */
739 void gras_datadesc_cb_recv(gras_datadesc_type_t          type,
740                            gras_datadesc_type_cb_void_t  recv) {
741   type->recv = recv;
742 }
743 /*
744  * gras_dd_find_field:
745  * 
746  * Returns the type descriptor of the given field. Abort on error.
747  */
748 static gras_dd_cat_field_t
749   gras_dd_find_field(gras_datadesc_type_t  type,
750                      const char           *field_name) {
751    xbt_dynar_t         field_array;
752    
753    gras_dd_cat_field_t  field=NULL;
754    int                  field_num;
755    
756    if (type->category_code == e_gras_datadesc_type_cat_union) {
757       field_array = type->category.union_data.fields;
758    } else if (type->category_code == e_gras_datadesc_type_cat_struct) {
759       field_array = type->category.struct_data.fields;
760    } else {
761       ERROR2("%s (%p) is not a struct nor an union. There is no field.", type->name,(void*)type);
762       xbt_abort();
763    }
764    xbt_dynar_foreach(field_array,field_num,field) {
765       if (!strcmp(field_name,field->name)) {
766          return field;
767       }
768    }
769    ERROR2("No field named '%s' in '%s'",field_name,type->name);
770    xbt_abort();
771
772 }
773                                   
774 /**
775  * The given datadesc must be a struct or union (abort if not).
776  * (useful to push the sizes of the upcoming arrays, for example)
777  */
778 void gras_datadesc_cb_field_send (gras_datadesc_type_t          type,
779                                   const char                   *field_name,
780                                   gras_datadesc_type_cb_void_t  send) {
781    
782    gras_dd_cat_field_t field=gras_dd_find_field(type,field_name);   
783    field->send = send;
784 }
785
786
787 /**
788  * The value, which must be an int, unsigned int, long int or unsigned long int
789  * is pushed to the stacks of sizes and can then be retrieved with 
790  * \ref gras_datadesc_ref_pop_arr or directly with \ref gras_cbps_i_pop.
791  */
792 void gras_datadesc_cb_field_push (gras_datadesc_type_t  type,
793                                   const char           *field_name) {
794    
795    gras_dd_cat_field_t  field=gras_dd_find_field(type,field_name);
796    gras_datadesc_type_t sub_type=field->type;
797    
798    DEBUG3("add a PUSHy cb to '%s' field (type '%s') of '%s'",
799           field_name,sub_type->name,type->name);
800    if (!strcmp("int",sub_type->name)) {
801       field->send = gras_datadesc_cb_push_int;
802    } else if (!strcmp("unsigned int",sub_type->name)) {
803       field->send = gras_datadesc_cb_push_uint;
804    } else if (!strcmp("long int",sub_type->name)) {
805       field->send = gras_datadesc_cb_push_lint;
806    } else if (!strcmp("unsigned long int",sub_type->name)) {
807       field->send = gras_datadesc_cb_push_ulint;
808    } else {
809       ERROR1("Field %s is not an int, unsigned int, long int neither unsigned long int",
810              sub_type->name);
811       xbt_abort();
812    }
813 }
814
815 /**
816  * Any previously pushed value is poped and the field value is multiplied to
817  * it. The result is then pushed back into the stack of sizes. It can then be
818  * retrieved with \ref gras_datadesc_ref_pop_arr or directly with \ref
819  * gras_cbps_i_pop.
820  *
821  * The field must be an int, unsigned int, long int or unsigned long int.
822  */
823 void gras_datadesc_cb_field_push_multiplier (gras_datadesc_type_t  type,
824                                              const char         *field_name) {
825    
826    gras_dd_cat_field_t  field=gras_dd_find_field(type,field_name);
827    gras_datadesc_type_t sub_type=field->type;
828    
829    DEBUG3("add a MPUSHy cb to '%s' field (type '%s') of '%s'",
830           field_name,sub_type->name,type->name);
831    if (!strcmp("int",sub_type->name)) {
832       field->send = gras_datadesc_cb_push_int_mult;
833    } else if (!strcmp("unsigned int",sub_type->name)) {
834       field->send = gras_datadesc_cb_push_uint_mult;
835    } else if (!strcmp("long int",sub_type->name)) {
836       field->send = gras_datadesc_cb_push_lint_mult;
837    } else if (!strcmp("unsigned long int",sub_type->name)) {
838       field->send = gras_datadesc_cb_push_ulint_mult;
839    } else {
840       ERROR1("Field %s is not an int, unsigned int, long int neither unsigned long int",
841              sub_type->name);
842       xbt_abort();
843    }
844 }
845
846 /**
847  * The given datadesc must be a struct or union (abort if not).
848  * (useful to put the function pointers to the right value, for example)
849  */
850 void gras_datadesc_cb_field_recv(gras_datadesc_type_t          type,
851                                  const char                   *field_name,
852                                  gras_datadesc_type_cb_void_t  recv) {
853    
854    gras_dd_cat_field_t field=gras_dd_find_field(type,field_name);   
855    field->recv = recv;
856 }
857
858 /*
859  * Free a datadesc. Should only be called at xbt_exit. 
860  */
861 void gras_datadesc_free(gras_datadesc_type_t *type) {
862
863   DEBUG1("Let's free ddt %s",(*type)->name);
864   
865   switch ((*type)->category_code) {
866   case e_gras_datadesc_type_cat_scalar:
867   case e_gras_datadesc_type_cat_ref:
868   case e_gras_datadesc_type_cat_array:
869     /* nothing to free in there */
870     break;
871     
872   case e_gras_datadesc_type_cat_struct:
873     xbt_dynar_free(&( (*type)->category.struct_data.fields ));
874     break;
875     
876   case e_gras_datadesc_type_cat_union:
877     xbt_dynar_free(&( (*type)->category.union_data.fields ));
878     break;
879     
880   default:
881     /* datadesc was invalid. Killing it is like euthanasy, I guess */
882     break;
883   }
884   free((*type)->name);
885   free(*type);
886   type=NULL;
887 }
888
889 /**
890  * gras_datadesc_type_cmp:
891  *
892  * Compares two datadesc types with the same semantic than strcmp.
893  *
894  * This comparison does not take the set headers into account (name and ID), 
895  * but only the payload (actual type description).
896  */
897 int gras_datadesc_type_cmp(const gras_datadesc_type_t d1,
898                            const gras_datadesc_type_t d2) {
899   int ret,cpt;
900   gras_dd_cat_field_t field1,field2;
901   gras_datadesc_type_t field_desc_1,field_desc_2;
902
903   if (d1 == d2) return 0; /* easy optimization */
904
905   if (!d1 && d2) {
906     DEBUG0("ddt_cmp: !d1 && d2 => 1");
907     return 1;
908   }
909   if (!d1 && !d2) {
910     DEBUG0("ddt_cmp: !d1 && !d2 => 0");
911     return 0;
912   }
913   if ( d1 && !d2) {
914     DEBUG0("ddt_cmp: d1 && !d2 => -1");
915     return -1;
916   }
917
918   for (cpt=0; cpt<gras_arch_count; cpt++) {
919     if (d1->size[cpt] != d2->size[cpt]) {
920       DEBUG5("ddt_cmp: %s->size=%ld  !=  %s->size=%ld (on %s)",
921              d1->name,d1->size[cpt],d2->name,d2->size[cpt],
922              gras_arches[cpt].name);
923       return d1->size[cpt] >  d2->size[cpt] ? 1 : -1;
924     }
925
926     if (d1->alignment[cpt] != d2->alignment[cpt]) {
927       DEBUG5("ddt_cmp: %s->alignment=%ld  !=  %s->alignment=%ld (on %s)",
928              d1->name,d1->alignment[cpt],d2->name,d2->alignment[cpt],
929              gras_arches[cpt].name);
930       return d1->alignment[cpt] > d2->alignment[cpt] ? 1 : -1;
931     }
932
933     if (d1->aligned_size[cpt] != d2->aligned_size[cpt]) {
934       DEBUG5("ddt_cmp: %s->aligned_size=%ld  !=  %s->aligned_size=%ld (on %s)",
935              d1->name,d1->aligned_size[cpt],d2->name,d2->aligned_size[cpt],
936              gras_arches[cpt].name);
937       return d1->aligned_size[cpt] > d2->aligned_size[cpt] ? 1 : -1;
938     }
939   }
940
941   if (d1->category_code != d2->category_code) {
942     DEBUG4("ddt_cmp: %s->cat=%s  !=  %s->cat=%s",
943            d1->name,gras_datadesc_cat_names[d1->category_code],
944            d2->name,gras_datadesc_cat_names[d2->category_code]);
945     return d1->category_code > d2->category_code ? 1 : -1;
946   }
947
948   if (d1->send != d2->send) {
949     DEBUG4("ddt_cmp: %s->send=%p  !=  %s->send=%p",
950            d1->name,(void*)d1->send, d2->name,(void*)d2->send);
951     return 1; /* ISO C forbids ordered comparisons of pointers to functions */
952   }
953
954   if (d1->recv != d2->recv) {
955     DEBUG4("ddt_cmp: %s->recv=%p  !=  %s->recv=%p",
956            d1->name,(void*)d1->recv, d2->name,(void*)d2->recv);
957     return 1; /* ISO C forbids ordered comparisons of pointers to functions */
958   }
959
960   switch (d1->category_code) {
961   case e_gras_datadesc_type_cat_scalar:
962     if (d1->category.scalar_data.encoding != d2->category.scalar_data.encoding)
963       return d1->category.scalar_data.encoding > d2->category.scalar_data.encoding ? 1 : -1 ;
964     break;
965     
966   case e_gras_datadesc_type_cat_struct:    
967     if (xbt_dynar_length(d1->category.struct_data.fields) != 
968         xbt_dynar_length(d2->category.struct_data.fields)) {
969       DEBUG4("ddt_cmp: %s (having %lu fields) !=  %s (having %lu fields)",
970              d1->name, xbt_dynar_length(d1->category.struct_data.fields),
971              d2->name, xbt_dynar_length(d2->category.struct_data.fields));
972       
973       return xbt_dynar_length(d1->category.struct_data.fields) >
974         xbt_dynar_length(d2->category.struct_data.fields) ?
975         1 : -1;
976     }
977     xbt_dynar_foreach(d1->category.struct_data.fields, cpt, field1) {
978       
979       field2 = xbt_dynar_get_as(d2->category.struct_data.fields, cpt, gras_dd_cat_field_t);
980       field_desc_1 = field1->type;
981       field_desc_2 = field2->type;
982       ret = gras_datadesc_type_cmp(field_desc_1,field_desc_2);
983       if (ret) {
984         DEBUG6("%s->field[%d]=%s != %s->field[%d]=%s",
985                d1->name,cpt,field1->name,              
986                d2->name,cpt,field2->name);
987         return ret;
988       }
989       
990     }
991     break;
992     
993   case e_gras_datadesc_type_cat_union:
994     if (d1->category.union_data.selector != d2->category.union_data.selector) 
995       return 1;  /* ISO C forbids ordered comparisons of pointers to functions */
996     
997     if (xbt_dynar_length(d1->category.union_data.fields) != 
998         xbt_dynar_length(d2->category.union_data.fields))
999       return xbt_dynar_length(d1->category.union_data.fields) >
1000              xbt_dynar_length(d2->category.union_data.fields) ?
1001         1 : -1;
1002     
1003     xbt_dynar_foreach(d1->category.union_data.fields, cpt, field1) {
1004       
1005       field2 = xbt_dynar_get_as(d2->category.union_data.fields, cpt, gras_dd_cat_field_t);
1006       field_desc_1 = field1->type;
1007       field_desc_2 = field2->type;
1008       ret = gras_datadesc_type_cmp(field_desc_1,field_desc_2);
1009       if (ret)
1010         return ret;
1011       
1012     }
1013     break;
1014     
1015     
1016   case e_gras_datadesc_type_cat_ref:
1017     if (d1->category.ref_data.selector != d2->category.ref_data.selector) 
1018       return 1; /* ISO C forbids ordered comparisons of pointers to functions */
1019     
1020     if (d1->category.ref_data.type != d2->category.ref_data.type) 
1021       return d1->category.ref_data.type > d2->category.ref_data.type ? 1 : -1;
1022     break;
1023     
1024   case e_gras_datadesc_type_cat_array:
1025     if (d1->category.array_data.type != d2->category.array_data.type) 
1026       return d1->category.array_data.type > d2->category.array_data.type ? 1 : -1;
1027     
1028     if (d1->category.array_data.fixed_size != d2->category.array_data.fixed_size) 
1029       return d1->category.array_data.fixed_size > d2->category.array_data.fixed_size ? 1 : -1;
1030     
1031     if (d1->category.array_data.dynamic_size != d2->category.array_data.dynamic_size) 
1032       return 1; /* ISO C forbids ordered comparisons of pointers to functions */
1033     
1034     break;
1035     
1036   default:
1037     /* two stupidly created ddt are equally stupid ;) */
1038     break;
1039   }
1040   return 0;
1041   
1042 }