Logo AND Algorithmique Numérique Distribuée

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