Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
4e4e51059351f07bf3d3d6b565c71aa4a52e2fd4
[simgrid.git] / src / gras / DataDesc / ddt_exchange.c
1 /* ddt_exchange - send/recv data described                                  */
2
3 /* Copyright (c) 2004, 2005, 2006, 2007, 2009, 2010. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #include "xbt/ex.h"
10 #include "gras/DataDesc/datadesc_private.h"
11 #include "gras/Transport/transport_interface.h" /* gras_trp_send/recv */
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_ddt_exchange, gras_ddt,
14                                 "Sending data over the network");
15 const char *gras_datadesc_cat_names[9] = {
16   "undefined",
17   "scalar", "struct", "union", "ref", "array", "ignored",
18   "invalid"
19 };
20
21 static gras_datadesc_type_t int_type = NULL;
22 static gras_datadesc_type_t pointer_type = NULL;
23
24 static XBT_INLINE void
25 gras_dd_send_int(gras_socket_t sock, int *i, int stable)
26 {
27
28   if (!int_type) {
29     int_type = gras_datadesc_by_name("int");
30     xbt_assert(int_type);
31   }
32
33   XBT_DEBUG("send_int(%u)", *i);
34   gras_trp_send(sock, (char *) i, int_type->size[GRAS_THISARCH], stable);
35 }
36
37 static XBT_INLINE void
38 gras_dd_recv_int(gras_socket_t sock, int r_arch, int *i)
39 {
40
41   if (!int_type) {
42     int_type = gras_datadesc_by_name("int");
43     xbt_assert(int_type);
44   }
45
46   if (int_type->size[GRAS_THISARCH] >= int_type->size[r_arch]) {
47     gras_trp_recv(sock, (char *) i, int_type->size[r_arch]);
48     if (r_arch != GRAS_THISARCH)
49       gras_dd_convert_elm(int_type, 1, r_arch, i, i);
50   } else {
51     void *ptr = xbt_malloc(int_type->size[r_arch]);
52
53     gras_trp_recv(sock, (char *) ptr, int_type->size[r_arch]);
54     if (r_arch != GRAS_THISARCH)
55       gras_dd_convert_elm(int_type, 1, r_arch, ptr, i);
56     free(ptr);
57   }
58   XBT_DEBUG("recv_int(%u)", *i);
59 }
60
61 /*
62  * Note: here we suppose that the remote NULL is a sequence
63  *       of 'length' bytes set to 0.
64  * FIXME: Check in configure?
65  */
66 static XBT_INLINE int gras_dd_is_r_null(char **r_ptr, long int length)
67 {
68   int i;
69
70   for (i = 0; i < length; i++) {
71     if (((unsigned char *) r_ptr)[i]) {
72       return 0;
73     }
74   }
75
76   return 1;
77 }
78
79 static XBT_INLINE void gras_dd_alloc_ref(xbt_dict_t refs, long int size, char **r_ref, long int r_len,  /* pointer_type->size[r_arch] */
80                                          char **l_ref, int detect_cycle)
81 {
82   char *l_data = NULL;
83
84   xbt_assert1(size > 0, "Cannot allocate %ld bytes!", size);
85   l_data = xbt_malloc((size_t) size);
86
87   *l_ref = l_data;
88   XBT_DEBUG
89       ("alloc_ref: l_data=%p, &l_data=%p; r_ref=%p; *r_ref=%p, r_len=%ld",
90        (void *) l_data, (void *) &l_data, (void *) r_ref,
91        (void *) (r_ref ? *r_ref : NULL), r_len);
92   if (detect_cycle && r_ref && !gras_dd_is_r_null(r_ref, r_len)) {
93     void *ptr = xbt_malloc(sizeof(void *));
94
95     memcpy(ptr, l_ref, sizeof(void *));
96
97     XBT_DEBUG("Insert l_ref=%p under r_ref=%p", *(void **) ptr,
98            *(void **) r_ref);
99
100     if (detect_cycle)
101       xbt_dict_set_ext(refs, (const char *) r_ref, r_len, ptr, xbt_free_f);
102   }
103 }
104
105 static int
106 gras_datadesc_memcpy_rec(gras_cbps_t state,
107                          xbt_dict_t refs,
108                          gras_datadesc_type_t type,
109                          char *src, char *dst, int subsize,
110                          int detect_cycle)
111 {
112
113
114   unsigned int cpt;
115   gras_datadesc_type_t sub_type;        /* type on which we recurse */
116   int count = 0;
117
118   XBT_VERB("Copy a %s (%s) from %p to %p (local sizeof=%ld)",
119         type->name, gras_datadesc_cat_names[type->category_code],
120         src, dst, type->size[GRAS_THISARCH]);
121
122   if (type->send) {
123     type->send(type, state, src);
124   }
125
126   switch (type->category_code) {
127   case e_gras_datadesc_type_cat_scalar:
128     memcpy(dst, src, type->size[GRAS_THISARCH]);
129     count += type->size[GRAS_THISARCH];
130     break;
131
132   case e_gras_datadesc_type_cat_struct:{
133       gras_dd_cat_struct_t struct_data;
134       gras_dd_cat_field_t field;
135       char *field_src;
136       char *field_dst;
137
138       struct_data = type->category.struct_data;
139       xbt_assert1(struct_data.closed,
140                   "Please call gras_datadesc_declare_struct_close on %s before copying it",
141                   type->name);
142       XBT_VERB(">> Copy all fields of the structure %s", type->name);
143       xbt_dynar_foreach(struct_data.fields, cpt, field) {
144         field_src = src + field->offset[GRAS_THISARCH];
145         field_dst = dst + field->offset[GRAS_THISARCH];
146
147         sub_type = field->type;
148
149         if (field->send)
150           field->send(type, state, field_src);
151
152         XBT_DEBUG("Copy field %s", field->name);
153         count +=
154             gras_datadesc_memcpy_rec(state, refs, sub_type, field_src,
155                                      field_dst, 0, detect_cycle
156                                      || sub_type->cycle);
157
158         if (XBT_LOG_ISENABLED(gras_ddt_exchange, xbt_log_priority_verbose)) {
159           if (sub_type == gras_datadesc_by_name("unsigned int")) {
160             XBT_VERB("Copied value for field '%s': %d (type: unsigned int)",
161                   field->name, *(unsigned int *) field_dst);
162           } else if (sub_type == gras_datadesc_by_name("int")) {
163             XBT_VERB("Copied value for field '%s': %d (type: int)",
164                   field->name, *(int *) field_dst);
165
166           } else if (sub_type ==
167                      gras_datadesc_by_name("unsigned long int")) {
168             XBT_VERB
169                 ("Copied value for field '%s': %ld (type: unsigned long int)",
170                  field->name, *(unsigned long int *) field_dst);
171           } else if (sub_type == gras_datadesc_by_name("long int")) {
172             XBT_VERB("Copied value for field '%s': %ld (type: long int)",
173                   field->name, *(long int *) field_dst);
174
175           } else if (sub_type == gras_datadesc_by_name("string")) {
176             XBT_VERB("Copied value for field '%s': '%s' (type: string)",
177                   field->name, *(char **) field_dst);
178           } else {
179             XBT_VERB("Copied a value for field '%s' (type not scalar?)",
180                   field->name);
181           }
182         }
183
184       }
185       XBT_VERB("<< Copied all fields of the structure %s", type->name);
186
187       break;
188     }
189
190   case e_gras_datadesc_type_cat_union:{
191       gras_dd_cat_union_t union_data;
192       gras_dd_cat_field_t field = NULL;
193       unsigned int field_num;
194
195       union_data = type->category.union_data;
196
197       xbt_assert1(union_data.closed,
198                   "Please call gras_datadesc_declare_union_close on %s before copying it",
199                   type->name);
200       /* retrieve the field number */
201       field_num = union_data.selector(type, state, src);
202
203       xbt_assert1(field_num > 0,
204                   "union field selector of %s gave a negative value",
205                   type->name);
206
207       xbt_assert3(field_num < xbt_dynar_length(union_data.fields),
208                   "union field selector of %s returned %d but there is only %lu fields",
209                   type->name, field_num,
210                   xbt_dynar_length(union_data.fields));
211
212       /* Copy the content */
213       field =
214           xbt_dynar_get_as(union_data.fields, field_num,
215                            gras_dd_cat_field_t);
216       sub_type = field->type;
217
218       if (field->send)
219         field->send(type, state, src);
220
221       count += gras_datadesc_memcpy_rec(state, refs, sub_type, src, dst, 0,
222                                         detect_cycle || sub_type->cycle);
223
224       break;
225     }
226
227   case e_gras_datadesc_type_cat_ref:{
228       gras_dd_cat_ref_t ref_data;
229       char **o_ref = NULL;
230       char **n_ref = NULL;
231       int reference_is_to_cpy;
232
233       ref_data = type->category.ref_data;
234
235       /* Detect the referenced type */
236       sub_type = ref_data.type;
237       if (sub_type == NULL) {
238         sub_type = (*ref_data.selector) (type, state, src);
239       }
240
241       /* Send the pointed data only if not already sent */
242       if (*(void **) src == NULL) {
243         XBT_VERB("Not copying NULL referenced data");
244         *(void **) dst = NULL;
245         break;
246       }
247       o_ref = (char **) src;
248
249       reference_is_to_cpy = 1;
250       if (detect_cycle &&
251           (n_ref =
252            xbt_dict_get_or_null_ext(refs, (char *) o_ref,
253                                     sizeof(char *)))) {
254         /* already known, no need to copy it */
255         //XBT_INFO("Cycle detected");
256         reference_is_to_cpy = 0;
257       }
258
259       if (reference_is_to_cpy) {
260         int subsubcount = -1;
261         void *l_referenced = NULL;
262         XBT_VERB("Copy a ref to '%s' referenced at %p", sub_type->name,
263               (void *) *o_ref);
264
265         if (!pointer_type) {
266           pointer_type = gras_datadesc_by_name("data pointer");
267           xbt_assert(pointer_type);
268         }
269
270         if (sub_type->category_code == e_gras_datadesc_type_cat_array) {
271           /* Damn. Reference to a dynamic array. Allocating the space for it is more complicated */
272           gras_dd_cat_array_t array_data = sub_type->category.array_data;
273           gras_datadesc_type_t subsub_type;
274
275           subsub_type = array_data.type;
276           subsubcount = array_data.fixed_size;
277           if (subsubcount == -1)
278             subsubcount =
279                 array_data.dynamic_size(subsub_type, state, *o_ref);
280
281           if (subsubcount != 0)
282             gras_dd_alloc_ref(refs,
283                               subsub_type->size[GRAS_THISARCH] *
284                               subsubcount, o_ref,
285                               pointer_type->size[GRAS_THISARCH],
286                               (char **) &l_referenced, detect_cycle);
287         } else {
288           gras_dd_alloc_ref(refs, sub_type->size[GRAS_THISARCH],
289                             o_ref, pointer_type->size[GRAS_THISARCH],
290                             (char **) &l_referenced, detect_cycle);
291         }
292
293         count += gras_datadesc_memcpy_rec(state, refs, sub_type,
294                                           *o_ref, (char *) l_referenced,
295                                           subsubcount, detect_cycle
296                                           || sub_type->cycle);
297
298         *(void **) dst = l_referenced;
299         XBT_VERB("'%s' previously referenced at %p now at %p",
300               sub_type->name, *(void **) o_ref, l_referenced);
301
302       } else {
303         XBT_VERB
304             ("NOT copying data previously referenced @%p (already done, @%p now)",
305              *(void **) o_ref, *(void **) n_ref);
306
307         *(void **) dst = *n_ref;
308
309       }
310       break;
311     }
312
313   case e_gras_datadesc_type_cat_array:{
314       gras_dd_cat_array_t array_data;
315       unsigned long int array_count;
316       char *src_ptr = src;
317       char *dst_ptr = dst;
318       long int elm_size;
319
320       array_data = type->category.array_data;
321
322       /* determine and send the element count */
323       array_count = array_data.fixed_size;
324       if (array_count == -1)
325         array_count = subsize;
326       if (array_count == -1) {
327         array_count = array_data.dynamic_size(type, state, src);
328         xbt_assert1(array_count >= 0,
329                     "Invalid (negative) array size for type %s",
330                     type->name);
331       }
332
333       /* send the content */
334       sub_type = array_data.type;
335       elm_size = sub_type->aligned_size[GRAS_THISARCH];
336       if (sub_type->category_code == e_gras_datadesc_type_cat_scalar) {
337         XBT_VERB("Array of %ld scalars, copy it in one shot", array_count);
338         memcpy(dst, src,
339                sub_type->aligned_size[GRAS_THISARCH] * array_count);
340         count += sub_type->aligned_size[GRAS_THISARCH] * array_count;
341       } else if (sub_type->category_code == e_gras_datadesc_type_cat_array
342                  && sub_type->category.array_data.fixed_size > 0
343                  && sub_type->category.array_data.type->category_code ==
344                  e_gras_datadesc_type_cat_scalar) {
345
346         XBT_VERB("Array of %ld fixed array of scalars, copy it in one shot",
347               array_count);
348         memcpy(dst, src,
349                sub_type->category.array_data.
350                type->aligned_size[GRAS_THISARCH]
351                * array_count * sub_type->category.array_data.fixed_size);
352         count +=
353             sub_type->category.array_data.type->aligned_size[GRAS_THISARCH]
354             * array_count * sub_type->category.array_data.fixed_size;
355
356       } else {
357         XBT_VERB("Array of %ld stuff, copy it in one after the other",
358               array_count);
359         for (cpt = 0; cpt < array_count; cpt++) {
360           XBT_VERB("Copy the %dth stuff out of %ld", cpt, array_count);
361           count +=
362               gras_datadesc_memcpy_rec(state, refs, sub_type, src_ptr,
363                                        dst_ptr, 0, detect_cycle
364                                        || sub_type->cycle);
365           src_ptr += elm_size;
366           dst_ptr += elm_size;
367         }
368       }
369       break;
370     }
371
372   default:
373     xbt_die("Invalid type");
374   }
375
376   return count;
377 }
378
379 /**
380  * gras_datadesc_memcpy:
381  *
382  * Copy the data pointed by src and described by type
383  * to a new location, and store a pointer to it in dst.
384  *
385  */
386 int gras_datadesc_memcpy(gras_datadesc_type_t type, void *src, void *dst)
387 {
388   xbt_ex_t e;
389   static gras_cbps_t state = NULL;
390   static xbt_dict_t refs = NULL;        /* all references already sent */
391   int size = 0;
392
393   xbt_assert0(type, "called with NULL type descriptor");
394
395   XBT_DEBUG("Memcopy a %s from %p to %p", gras_datadesc_get_name(type), src,
396          dst);
397   if (!state) {
398     state = gras_cbps_new();
399     refs = xbt_dict_new();
400   }
401
402   TRY {
403     size =
404         gras_datadesc_memcpy_rec(state, refs, type, (char *) src,
405                                  (char *) dst, 0, type->cycle);
406   } TRY_CLEANUP {
407     xbt_dict_reset(refs);
408     gras_cbps_reset(state);
409   } CATCH(e) {
410     RETHROW;
411   }
412   return size;
413 }
414
415 /***
416  *** Direct use functions
417  ***/
418
419 static void
420 gras_datadesc_send_rec(gras_socket_t sock,
421                        gras_cbps_t state,
422                        xbt_dict_t refs,
423                        gras_datadesc_type_t type,
424                        char *data, int detect_cycle)
425 {
426
427   unsigned int cpt;
428   gras_datadesc_type_t sub_type;        /* type on which we recurse */
429
430   XBT_VERB("Send a %s (%s)",
431         type->name, gras_datadesc_cat_names[type->category_code]);
432
433   if (!strcmp(type->name, "string"))
434     XBT_VERB("value: '%s'", *(char **) data);
435
436   if (type->send) {
437     type->send(type, state, data);
438     XBT_DEBUG("Run the emission callback");
439   }
440
441   switch (type->category_code) {
442   case e_gras_datadesc_type_cat_scalar:
443     gras_trp_send(sock, data, type->size[GRAS_THISARCH], 1);
444     break;
445
446   case e_gras_datadesc_type_cat_struct:{
447       gras_dd_cat_struct_t struct_data;
448       gras_dd_cat_field_t field;
449       char *field_data;
450
451       struct_data = type->category.struct_data;
452       xbt_assert1(struct_data.closed,
453                   "Please call gras_datadesc_declare_struct_close on %s before sending it",
454                   type->name);
455       XBT_VERB(">> Send all fields of the structure %s", type->name);
456       xbt_dynar_foreach(struct_data.fields, cpt, field) {
457         field_data = data;
458         field_data += field->offset[GRAS_THISARCH];
459
460         sub_type = field->type;
461
462         if (field->send) {
463           XBT_DEBUG("Run the emission callback of field %s", field->name);
464           field->send(type, state, field_data);
465         }
466
467         XBT_VERB("Send field %s", field->name);
468         gras_datadesc_send_rec(sock, state, refs, sub_type, field_data,
469                                detect_cycle || sub_type->cycle);
470
471       }
472       XBT_VERB("<< Sent all fields of the structure %s", type->name);
473
474       break;
475     }
476
477   case e_gras_datadesc_type_cat_union:{
478       gras_dd_cat_union_t union_data;
479       gras_dd_cat_field_t field = NULL;
480       int field_num;
481
482       union_data = type->category.union_data;
483
484       xbt_assert1(union_data.closed,
485                   "Please call gras_datadesc_declare_union_close on %s before sending it",
486                   type->name);
487       /* retrieve the field number */
488       field_num = union_data.selector(type, state, data);
489
490       xbt_assert1(field_num > 0,
491                   "union field selector of %s gave a negative value",
492                   type->name);
493
494       xbt_assert3(field_num < xbt_dynar_length(union_data.fields),
495                   "union field selector of %s returned %d but there is only %lu fields",
496                   type->name, field_num,
497                   xbt_dynar_length(union_data.fields));
498
499       /* Send the field number */
500       gras_dd_send_int(sock, &field_num, 0 /* not stable */ );
501
502       /* Send the content */
503       field =
504           xbt_dynar_get_as(union_data.fields, field_num,
505                            gras_dd_cat_field_t);
506       sub_type = field->type;
507
508       if (field->send)
509         field->send(type, state, data);
510
511       gras_datadesc_send_rec(sock, state, refs, sub_type, data,
512                              detect_cycle || sub_type->cycle);
513
514       break;
515     }
516
517   case e_gras_datadesc_type_cat_ref:{
518       gras_dd_cat_ref_t ref_data;
519       void **ref = (void **) data;
520       int reference_is_to_send;
521
522       ref_data = type->category.ref_data;
523
524       /* Detect the referenced type and send it to peer if needed */
525       sub_type = ref_data.type;
526       if (sub_type == NULL) {
527         sub_type = (*ref_data.selector) (type, state, data);
528         gras_dd_send_int(sock, &(sub_type->code), 1 /*stable */ );
529       }
530
531       /* Send the actual value of the pointer for cycle handling */
532       if (!pointer_type) {
533         pointer_type = gras_datadesc_by_name("data pointer");
534         xbt_assert(pointer_type);
535       }
536
537       gras_trp_send(sock, (char *) data,
538                     pointer_type->size[GRAS_THISARCH], 1 /*stable */ );
539
540       /* Send the pointed data only if not already sent */
541       if (*(void **) data == NULL) {
542         XBT_VERB("Not sending NULL referenced data");
543         break;
544       }
545
546       reference_is_to_send = 1;
547       /* return ignored. Just checking whether it's known or not */
548       if (detect_cycle
549           && xbt_dict_get_or_null_ext(refs, (char *) ref,
550                                       sizeof(char *))) {
551         //XBT_INFO("Cycle detected");
552         reference_is_to_send = 0;
553       }
554
555       if (reference_is_to_send) {
556         XBT_VERB("Sending data referenced at %p", (void *) *ref);
557         if (detect_cycle)
558           xbt_dict_set_ext(refs, (char *) ref, sizeof(void *), ref, NULL);
559         gras_datadesc_send_rec(sock, state, refs, sub_type, *ref,
560                                detect_cycle || sub_type->cycle);
561
562       } else {
563         XBT_VERB("Not sending data referenced at %p (already done)",
564               (void *) *ref);
565       }
566
567       break;
568     }
569
570   case e_gras_datadesc_type_cat_array:{
571       gras_dd_cat_array_t array_data;
572       int count;
573       char *ptr = data;
574       long int elm_size;
575
576       array_data = type->category.array_data;
577
578       /* determine and send the element count */
579       count = array_data.fixed_size;
580       if (count == -1) {
581         count = array_data.dynamic_size(type, state, data);
582         xbt_assert1(count >= 0,
583                     "Invalid (negative) array size for type %s",
584                     type->name);
585         gras_dd_send_int(sock, &count, 0 /*non-stable */ );
586       }
587
588       /* send the content */
589       sub_type = array_data.type;
590       elm_size = sub_type->aligned_size[GRAS_THISARCH];
591       if (sub_type->category_code == e_gras_datadesc_type_cat_scalar) {
592         XBT_VERB("Array of %d scalars, send it in one shot", count);
593         gras_trp_send(sock, data,
594                       sub_type->aligned_size[GRAS_THISARCH] * count,
595                       0 /* not stable */ );
596       } else if (sub_type->category_code == e_gras_datadesc_type_cat_array
597                  && sub_type->category.array_data.fixed_size > 0
598                  && sub_type->category.array_data.type->category_code ==
599                  e_gras_datadesc_type_cat_scalar) {
600
601         XBT_VERB("Array of %d fixed array of scalars, send it in one shot",
602               count);
603         gras_trp_send(sock, data,
604                       sub_type->category.array_data.
605                       type->aligned_size[GRAS_THISARCH]
606                       * count * sub_type->category.array_data.fixed_size,
607                       0 /* not stable */ );
608
609       } else {
610         for (cpt = 0; cpt < count; cpt++) {
611           gras_datadesc_send_rec(sock, state, refs, sub_type, ptr,
612                                  detect_cycle || sub_type->cycle);
613           ptr += elm_size;
614         }
615       }
616       break;
617     }
618
619   default:
620     xbt_die("Invalid type");
621   }
622 }
623
624 /**
625  * gras_datadesc_send:
626  *
627  * Copy the data pointed by src and described by type to the socket
628  *
629  */
630 void gras_datadesc_send(gras_socket_t sock,
631                         gras_datadesc_type_t type, void *src)
632 {
633
634   xbt_ex_t e;
635   static gras_cbps_t state = NULL;
636   static xbt_dict_t refs = NULL;        /* all references already sent */
637
638   xbt_assert0(type, "called with NULL type descriptor");
639
640   if (!state) {
641     state = gras_cbps_new();
642     refs = xbt_dict_new();
643   }
644
645   TRY {
646     gras_datadesc_send_rec(sock, state, refs, type, (char *) src,
647                            type->cycle);
648   } TRY_CLEANUP {
649     xbt_dict_reset(refs);
650     gras_cbps_reset(state);
651   } CATCH(e) {
652     RETHROW;
653   }
654 }
655
656 /**
657  * gras_datadesc_recv_rec:
658  *
659  * Do the data reception job recursively.
660  *
661  * subsize used only to deal with vicious case of reference to dynamic array.
662  *  This size is needed at the reference reception level (to allocate enough
663  * space) and at the array reception level (to fill enough room).
664  *
665  * Having this size passed as an argument of the recursive function is a crude
666  * hack, but I was told that working code is sometimes better than neat one ;)
667  */
668 static void
669 gras_datadesc_recv_rec(gras_socket_t sock,
670                        gras_cbps_t state,
671                        xbt_dict_t refs,
672                        gras_datadesc_type_t type,
673                        int r_arch,
674                        char **r_data,
675                        long int r_lgr,
676                        char *l_data, int subsize, int detect_cycle)
677 {
678
679   unsigned int cpt;
680   gras_datadesc_type_t sub_type;
681
682   XBT_VERB("Recv a %s @%p", type->name, (void *) l_data);
683   xbt_assert(l_data);
684
685   switch (type->category_code) {
686   case e_gras_datadesc_type_cat_scalar:
687     if (type->size[GRAS_THISARCH] == type->size[r_arch]) {
688       gras_trp_recv(sock, (char *) l_data, type->size[r_arch]);
689       if (r_arch != GRAS_THISARCH)
690         gras_dd_convert_elm(type, 1, r_arch, l_data, l_data);
691     } else {
692       void *ptr = xbt_malloc(type->size[r_arch]);
693
694       gras_trp_recv(sock, (char *) ptr, type->size[r_arch]);
695       if (r_arch != GRAS_THISARCH)
696         gras_dd_convert_elm(type, 1, r_arch, ptr, l_data);
697       free(ptr);
698     }
699     break;
700
701   case e_gras_datadesc_type_cat_struct:{
702       gras_dd_cat_struct_t struct_data;
703       gras_dd_cat_field_t field;
704
705       struct_data = type->category.struct_data;
706
707       xbt_assert1(struct_data.closed,
708                   "Please call gras_datadesc_declare_struct_close on %s before receiving it",
709                   type->name);
710       XBT_VERB(">> Receive all fields of the structure %s", type->name);
711       xbt_dynar_foreach(struct_data.fields, cpt, field) {
712         char *field_data = l_data + field->offset[GRAS_THISARCH];
713
714         sub_type = field->type;
715
716         gras_datadesc_recv_rec(sock, state, refs, sub_type,
717                                r_arch, NULL, 0,
718                                field_data, -1,
719                                detect_cycle || sub_type->cycle);
720
721         if (field->recv) {
722           XBT_DEBUG("Run the reception callback of field %s", field->name);
723           field->recv(type, state, (void *) l_data);
724         }
725
726       }
727       XBT_VERB("<< Received all fields of the structure %s", type->name);
728
729       break;
730     }
731
732   case e_gras_datadesc_type_cat_union:{
733       gras_dd_cat_union_t union_data;
734       gras_dd_cat_field_t field = NULL;
735       int field_num;
736
737       union_data = type->category.union_data;
738
739       xbt_assert1(union_data.closed,
740                   "Please call gras_datadesc_declare_union_close on %s before receiving it",
741                   type->name);
742       /* retrieve the field number */
743       gras_dd_recv_int(sock, r_arch, &field_num);
744       if (field_num < 0)
745         THROWF(mismatch_error, 0,
746                "Received union field for %s is negative", type->name);
747       if (field_num > xbt_dynar_length(union_data.fields))
748         THROWF(mismatch_error, 0,
749                "Received union field for %s is said to be #%d but there is only %lu fields",
750                type->name, field_num, xbt_dynar_length(union_data.fields));
751
752       /* Recv the content */
753       field =
754           xbt_dynar_get_as(union_data.fields, field_num,
755                            gras_dd_cat_field_t);
756       sub_type = field->type;
757
758       gras_datadesc_recv_rec(sock, state, refs, sub_type,
759                              r_arch, NULL, 0,
760                              l_data, -1, detect_cycle || sub_type->cycle);
761       if (field->recv)
762         field->recv(type, state, l_data);
763
764       break;
765     }
766
767   case e_gras_datadesc_type_cat_ref:{
768       char **r_ref = NULL;
769       char **l_ref = NULL;
770       gras_dd_cat_ref_t ref_data;
771       int reference_is_to_recv = 0;
772
773       ref_data = type->category.ref_data;
774
775       /* Get the referenced type locally or from peer */
776       sub_type = ref_data.type;
777       if (sub_type == NULL) {
778         int ref_code;
779         gras_dd_recv_int(sock, r_arch, &ref_code);
780         sub_type = gras_datadesc_by_id(ref_code);
781       }
782
783       /* Get the actual value of the pointer for cycle handling */
784       if (!pointer_type) {
785         pointer_type = gras_datadesc_by_name("data pointer");
786         xbt_assert(pointer_type);
787       }
788
789       r_ref = xbt_malloc(pointer_type->size[r_arch]);
790
791       gras_trp_recv(sock, (char *) r_ref, pointer_type->size[r_arch]);
792
793       /* Receive the pointed data only if not already sent */
794       if (gras_dd_is_r_null(r_ref, pointer_type->size[r_arch])) {
795         XBT_VERB("Not receiving data remotely referenced @%p since it's NULL",
796               *(void **) r_ref);
797         *(void **) l_data = NULL;
798         free(r_ref);
799         break;
800       }
801
802       reference_is_to_recv = 1;
803       if (detect_cycle && (l_ref =
804                            xbt_dict_get_or_null_ext(refs, (char *) r_ref,
805                                                     pointer_type->size
806                                                     [r_arch]))) {
807         reference_is_to_recv = 0;
808         //XBT_INFO("Cycle detected");
809       }
810
811       if (reference_is_to_recv) {
812         int subsubcount = -1;
813         void *l_referenced = NULL;
814
815         XBT_VERB("Receiving a ref to '%s', remotely @%p",
816               sub_type->name, *(void **) r_ref);
817         if (sub_type->category_code == e_gras_datadesc_type_cat_array) {
818           /* Damn. Reference to a dynamic array. Allocating the space for it is more complicated */
819           gras_dd_cat_array_t array_data = sub_type->category.array_data;
820           gras_datadesc_type_t subsub_type;
821
822           subsubcount = array_data.fixed_size;
823           if (subsubcount == -1)
824             gras_dd_recv_int(sock, r_arch, &subsubcount);
825
826           subsub_type = array_data.type;
827
828           if (subsubcount != 0)
829             gras_dd_alloc_ref(refs,
830                               subsub_type->size[GRAS_THISARCH] *
831                               subsubcount, r_ref,
832                               pointer_type->size[r_arch],
833                               (char **) &l_referenced, detect_cycle);
834           else
835             l_referenced = NULL;
836         } else {
837           gras_dd_alloc_ref(refs, sub_type->size[GRAS_THISARCH],
838                             r_ref, pointer_type->size[r_arch],
839                             (char **) &l_referenced, detect_cycle);
840         }
841
842         if (l_referenced != NULL)
843           gras_datadesc_recv_rec(sock, state, refs, sub_type,
844                                  r_arch, r_ref, pointer_type->size[r_arch],
845                                  (char *) l_referenced, subsubcount,
846                                  detect_cycle || sub_type->cycle);
847
848         *(void **) l_data = l_referenced;
849         XBT_VERB("'%s' remotely referenced at %p locally at %p",
850               sub_type->name, *(void **) r_ref, l_referenced);
851
852       } else {
853         XBT_VERB
854             ("NOT receiving data remotely referenced @%p (already done, @%p here)",
855              *(void **) r_ref, *(void **) l_ref);
856
857         *(void **) l_data = *l_ref;
858
859       }
860       free(r_ref);
861       break;
862     }
863
864   case e_gras_datadesc_type_cat_array:{
865       gras_dd_cat_array_t array_data;
866       int count;
867       char *ptr;
868       long int elm_size;
869
870       array_data = type->category.array_data;
871       /* determine element count locally, or from caller, or from peer */
872       count = array_data.fixed_size;
873       if (count == -1)
874         count = subsize;
875       if (count == -1)
876         gras_dd_recv_int(sock, r_arch, &count);
877       if (count == -1)
878         THROWF(mismatch_error, 0,
879                "Invalid (=-1) array size for type %s", type->name);
880
881       /* receive the content */
882       sub_type = array_data.type;
883       if (sub_type->category_code == e_gras_datadesc_type_cat_scalar) {
884         XBT_VERB("Array of %d scalars, get it in one shoot", count);
885         if (sub_type->aligned_size[GRAS_THISARCH] >=
886             sub_type->aligned_size[r_arch]) {
887           gras_trp_recv(sock, (char *) l_data,
888                         sub_type->aligned_size[r_arch] * count);
889           if (r_arch != GRAS_THISARCH)
890             gras_dd_convert_elm(sub_type, count, r_arch, l_data, l_data);
891         } else {
892           ptr = xbt_malloc(sub_type->aligned_size[r_arch] * count);
893
894           gras_trp_recv(sock, (char *) ptr,
895                         sub_type->size[r_arch] * count);
896           if (r_arch != GRAS_THISARCH)
897             gras_dd_convert_elm(sub_type, count, r_arch, ptr, l_data);
898           free(ptr);
899         }
900       } else if (sub_type->category_code == e_gras_datadesc_type_cat_array
901                  && sub_type->category.array_data.fixed_size >= 0
902                  && sub_type->category.array_data.type->category_code ==
903                  e_gras_datadesc_type_cat_scalar) {
904         gras_datadesc_type_t subsub_type;
905         array_data = sub_type->category.array_data;
906         subsub_type = array_data.type;
907
908         XBT_VERB("Array of %d fixed array of scalars, get it in one shot",
909               count);
910         if (subsub_type->aligned_size[GRAS_THISARCH] >=
911             subsub_type->aligned_size[r_arch]) {
912           gras_trp_recv(sock, (char *) l_data,
913                         subsub_type->aligned_size[r_arch] * count *
914                         array_data.fixed_size);
915           if (r_arch != GRAS_THISARCH)
916             gras_dd_convert_elm(subsub_type, count * array_data.fixed_size,
917                                 r_arch, l_data, l_data);
918         } else {
919           ptr =
920               xbt_malloc(subsub_type->aligned_size[r_arch] * count *
921                          array_data.fixed_size);
922
923           gras_trp_recv(sock, (char *) ptr,
924                         subsub_type->size[r_arch] * count *
925                         array_data.fixed_size);
926           if (r_arch != GRAS_THISARCH)
927             gras_dd_convert_elm(subsub_type, count * array_data.fixed_size,
928                                 r_arch, ptr, l_data);
929           free(ptr);
930         }
931
932
933       } else {
934         /* not scalar content, get it recursively (may contain pointers) */
935         elm_size = sub_type->aligned_size[GRAS_THISARCH];
936         XBT_VERB("Receive a %d-long array of %s", count, sub_type->name);
937
938         ptr = l_data;
939         for (cpt = 0; cpt < count; cpt++) {
940           gras_datadesc_recv_rec(sock, state, refs, sub_type,
941                                  r_arch, NULL, 0, ptr, -1,
942                                  detect_cycle || sub_type->cycle);
943
944           ptr += elm_size;
945         }
946       }
947       break;
948     }
949
950   default:
951     xbt_die("Invalid type");
952   }
953
954   if (type->recv)
955     type->recv(type, state, l_data);
956
957   if (!strcmp(type->name, "string"))
958     XBT_VERB("value: '%s'", *(char **) l_data);
959
960 }
961
962 /**
963  * gras_datadesc_recv:
964  *
965  * Get an instance of the datatype described by @type from the @socket,
966  * and store a pointer to it in @dst
967  *
968  */
969 void
970 gras_datadesc_recv(gras_socket_t sock,
971                    gras_datadesc_type_t type, int r_arch, void *dst)
972 {
973
974   xbt_ex_t e;
975   static gras_cbps_t state = NULL;      /* callback persistent state */
976   static xbt_dict_t refs = NULL;        /* all references already sent */
977
978   if (!state) {
979     state = gras_cbps_new();
980     refs = xbt_dict_new();
981   }
982
983   xbt_assert0(type, "called with NULL type descriptor");
984   TRY {
985     gras_datadesc_recv_rec(sock, state, refs, type,
986                            r_arch, NULL, 0, (char *) dst, -1, type->cycle);
987   } TRY_CLEANUP {
988     xbt_dict_reset(refs);
989     gras_cbps_reset(state);
990   } CATCH(e) {
991     RETHROW;
992   }
993 }