Logo AND Algorithmique Numérique Distribuée

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