Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
remove gras from the main documentation
[simgrid.git] / src / xbt / 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 "datadesc_private.h"
11 #include "xbt/socket.h" /* xbt_trp_send/recv */
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_ddt_exchange, xbt_ddt,
14                                 "Sending data over the network");
15 const char *xbt_datadesc_cat_names[9] = {
16   "undefined",
17   "scalar", "struct", "union", "ref", "array", "ignored",
18   "invalid"
19 };
20
21 static xbt_datadesc_type_t int_type = NULL;
22 static xbt_datadesc_type_t pointer_type = NULL;
23
24 static XBT_INLINE void
25 xbt_dd_send_int(xbt_socket_t sock, int *i, int stable)
26 {
27
28   if (!int_type) {
29     int_type = xbt_datadesc_by_name("int");
30     xbt_assert(int_type);
31   }
32
33   XBT_DEBUG("send_int(%d)", *i);
34   xbt_trp_send(sock, (char *) i, int_type->size[GRAS_THISARCH], stable);
35 }
36
37 static XBT_INLINE void
38 xbt_dd_recv_int(xbt_socket_t sock, int r_arch, int *i)
39 {
40
41   if (!int_type) {
42     int_type = xbt_datadesc_by_name("int");
43     xbt_assert(int_type);
44   }
45
46   if (int_type->size[GRAS_THISARCH] >= int_type->size[r_arch]) {
47     xbt_trp_recv(sock, (char *) i, int_type->size[r_arch]);
48     if (r_arch != GRAS_THISARCH)
49       xbt_dd_convert_elm(int_type, 1, r_arch, i, i);
50   } else {
51     void *ptr = xbt_malloc(int_type->size[r_arch]);
52
53     xbt_trp_recv(sock, (char *) ptr, int_type->size[r_arch]);
54     if (r_arch != GRAS_THISARCH)
55       xbt_dd_convert_elm(int_type, 1, r_arch, ptr, i);
56     free(ptr);
57   }
58   XBT_DEBUG("recv_int(%d)", *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 xbt_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 xbt_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_assert(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 && !xbt_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, NULL);
102   }
103 }
104
105 static int
106 xbt_datadesc_memcpy_rec(xbt_cbps_t state,
107                          xbt_dict_t refs,
108                          xbt_datadesc_type_t type,
109                          char *src, char *dst, int subsize,
110                          int detect_cycle)
111 {
112
113
114   unsigned int cpt;
115   xbt_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, xbt_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_xbt_datadesc_type_cat_scalar:
128     memcpy(dst, src, type->size[GRAS_THISARCH]);
129     count += type->size[GRAS_THISARCH];
130     break;
131
132   case e_xbt_datadesc_type_cat_struct:{
133       xbt_dd_cat_struct_t struct_data;
134       xbt_dd_cat_field_t field;
135       char *field_src;
136       char *field_dst;
137
138       struct_data = type->category.struct_data;
139       xbt_assert(struct_data.closed,
140                   "Please call xbt_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             xbt_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(xbt_ddt_exchange, xbt_log_priority_verbose)) {
159           if (sub_type == xbt_datadesc_by_name("unsigned int")) {
160             XBT_VERB("Copied value for field '%s': %u (type: unsigned int)",
161                   field->name, *(unsigned int *) field_dst);
162           } else if (sub_type == xbt_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                      xbt_datadesc_by_name("unsigned long int")) {
168             XBT_VERB
169                 ("Copied value for field '%s': %lu (type: unsigned long int)",
170                  field->name, *(unsigned long int *) field_dst);
171           } else if (sub_type == xbt_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 == xbt_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_xbt_datadesc_type_cat_union:{
191       xbt_dd_cat_union_t union_data;
192       xbt_dd_cat_field_t field = NULL;
193       unsigned int field_num;
194
195       union_data = type->category.union_data;
196
197       xbt_assert(union_data.closed,
198                   "Please call xbt_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_assert(field_num > 0,
204                   "union field selector of %s gave a negative value",
205                   type->name);
206
207       xbt_assert(field_num < xbt_dynar_length(union_data.fields),
208                   "union field selector of %s returned %u 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                            xbt_dd_cat_field_t);
216       sub_type = field->type;
217
218       if (field->send)
219         field->send(type, state, src);
220
221       count += xbt_datadesc_memcpy_rec(state, refs, sub_type, src, dst, 0,
222                                         detect_cycle || sub_type->cycle);
223
224       break;
225     }
226
227   case e_xbt_datadesc_type_cat_ref:{
228       xbt_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 = xbt_datadesc_by_name("data pointer");
267           xbt_assert(pointer_type);
268         }
269
270         if (sub_type->category_code == e_xbt_datadesc_type_cat_array) {
271           /* Damn. Reference to a dynamic array. Allocating the space for it is more complicated */
272           xbt_dd_cat_array_t array_data = sub_type->category.array_data;
273           xbt_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             xbt_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           xbt_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 += xbt_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_xbt_datadesc_type_cat_array:{
314       xbt_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
329       /* send the content */
330       sub_type = array_data.type;
331       elm_size = sub_type->aligned_size[GRAS_THISARCH];
332       if (sub_type->category_code == e_xbt_datadesc_type_cat_scalar) {
333         XBT_VERB("Array of %lu scalars, copy it in one shot", array_count);
334         memcpy(dst, src,
335                sub_type->aligned_size[GRAS_THISARCH] * array_count);
336         count += sub_type->aligned_size[GRAS_THISARCH] * array_count;
337       } else if (sub_type->category_code == e_xbt_datadesc_type_cat_array
338                  && sub_type->category.array_data.fixed_size > 0
339                  && sub_type->category.array_data.type->category_code ==
340                  e_xbt_datadesc_type_cat_scalar) {
341
342         XBT_VERB("Array of %lu fixed array of scalars, copy it in one shot",
343               array_count);
344         memcpy(dst, src,
345                sub_type->category.array_data.
346                type->aligned_size[GRAS_THISARCH]
347                * array_count * sub_type->category.array_data.fixed_size);
348         count +=
349             sub_type->category.array_data.type->aligned_size[GRAS_THISARCH]
350             * array_count * sub_type->category.array_data.fixed_size;
351
352       } else {
353         XBT_VERB("Array of %lu stuff, copy it in one after the other",
354               array_count);
355         for (cpt = 0; cpt < array_count; cpt++) {
356           XBT_VERB("Copy the %uth stuff out of %lu", cpt, array_count);
357           count +=
358               xbt_datadesc_memcpy_rec(state, refs, sub_type, src_ptr,
359                                        dst_ptr, 0, detect_cycle
360                                        || sub_type->cycle);
361           src_ptr += elm_size;
362           dst_ptr += elm_size;
363         }
364       }
365       break;
366     }
367
368   default:
369     xbt_die("Invalid type");
370   }
371
372   return count;
373 }
374
375 /**
376  * xbt_datadesc_memcpy:
377  *
378  * Copy the data pointed by src and described by type
379  * to a new location, and store a pointer to it in dst.
380  *
381  */
382 int xbt_datadesc_memcpy(xbt_datadesc_type_t type, void *src, void *dst)
383 {
384   static xbt_cbps_t state = NULL;
385   static xbt_dict_t refs = NULL;        /* all references already sent */
386   int size = 0;
387
388   xbt_assert(type, "called with NULL type descriptor");
389
390   XBT_DEBUG("Memcopy a %s from %p to %p", xbt_datadesc_get_name(type), src,
391          dst);
392   if (!state) {
393     state = xbt_cbps_new();
394     refs = xbt_dict_new_homogeneous(xbt_free_f);
395   }
396
397   TRY {
398     size =
399         xbt_datadesc_memcpy_rec(state, refs, type, (char *) src,
400                                  (char *) dst, 0, type->cycle);
401   }
402   TRY_CLEANUP {
403     xbt_dict_reset(refs);
404     xbt_cbps_reset(state);
405   }
406   CATCH_ANONYMOUS {
407     RETHROW;
408   }
409   return size;
410 }
411
412 /***
413  *** Direct use functions
414  ***/
415
416 static void
417 xbt_datadesc_send_rec(xbt_socket_t sock,
418                        xbt_cbps_t state,
419                        xbt_dict_t refs,
420                        xbt_datadesc_type_t type,
421                        char *data, int detect_cycle)
422 {
423
424   unsigned int cpt;
425   xbt_datadesc_type_t sub_type;        /* type on which we recurse */
426
427   XBT_VERB("Send a %s (%s)",
428         type->name, xbt_datadesc_cat_names[type->category_code]);
429
430   if (!strcmp(type->name, "string"))
431     XBT_VERB("value: '%s'", *(char **) data);
432
433   if (type->send) {
434     type->send(type, state, data);
435     XBT_DEBUG("Run the emission callback");
436   }
437
438   switch (type->category_code) {
439   case e_xbt_datadesc_type_cat_scalar:
440     xbt_trp_send(sock, data, type->size[GRAS_THISARCH], 1);
441     break;
442
443   case e_xbt_datadesc_type_cat_struct:{
444       xbt_dd_cat_struct_t struct_data;
445       xbt_dd_cat_field_t field;
446       char *field_data;
447
448       struct_data = type->category.struct_data;
449       xbt_assert(struct_data.closed,
450                   "Please call xbt_datadesc_declare_struct_close on %s before sending it",
451                   type->name);
452       XBT_VERB(">> Send all fields of the structure %s", type->name);
453       xbt_dynar_foreach(struct_data.fields, cpt, field) {
454         field_data = data;
455         field_data += field->offset[GRAS_THISARCH];
456
457         sub_type = field->type;
458
459         if (field->send) {
460           XBT_DEBUG("Run the emission callback of field %s", field->name);
461           field->send(type, state, field_data);
462         }
463
464         XBT_VERB("Send field %s", field->name);
465         xbt_datadesc_send_rec(sock, state, refs, sub_type, field_data,
466                                detect_cycle || sub_type->cycle);
467
468       }
469       XBT_VERB("<< Sent all fields of the structure %s", type->name);
470
471       break;
472     }
473
474   case e_xbt_datadesc_type_cat_union:{
475       xbt_dd_cat_union_t union_data;
476       xbt_dd_cat_field_t field = NULL;
477       int field_num;
478
479       union_data = type->category.union_data;
480
481       xbt_assert(union_data.closed,
482                   "Please call xbt_datadesc_declare_union_close on %s before sending it",
483                   type->name);
484       /* retrieve the field number */
485       field_num = union_data.selector(type, state, data);
486
487       xbt_assert(field_num > 0,
488                   "union field selector of %s gave a negative value",
489                   type->name);
490
491       xbt_assert(field_num < xbt_dynar_length(union_data.fields),
492                   "union field selector of %s returned %d but there is only %lu fields",
493                   type->name, field_num,
494                   xbt_dynar_length(union_data.fields));
495
496       /* Send the field number */
497       xbt_dd_send_int(sock, &field_num, 0 /* not stable */ );
498
499       /* Send the content */
500       field =
501           xbt_dynar_get_as(union_data.fields, field_num,
502                            xbt_dd_cat_field_t);
503       sub_type = field->type;
504
505       if (field->send)
506         field->send(type, state, data);
507
508       xbt_datadesc_send_rec(sock, state, refs, sub_type, data,
509                              detect_cycle || sub_type->cycle);
510
511       break;
512     }
513
514   case e_xbt_datadesc_type_cat_ref:{
515       xbt_dd_cat_ref_t ref_data;
516       void **ref = (void **) data;
517       int reference_is_to_send;
518
519       ref_data = type->category.ref_data;
520
521       /* Detect the referenced type and send it to peer if needed */
522       sub_type = ref_data.type;
523       if (sub_type == NULL) {
524         sub_type = ref_data.selector(type, state, data);
525         xbt_dd_send_int(sock, &(sub_type->code), 1 /*stable */ );
526       }
527
528       /* Send the actual value of the pointer for cycle handling */
529       if (!pointer_type) {
530         pointer_type = xbt_datadesc_by_name("data pointer");
531         xbt_assert(pointer_type);
532       }
533
534       xbt_trp_send(sock, (char *) data,
535                     pointer_type->size[GRAS_THISARCH], 1 /*stable */ );
536
537       /* Send the pointed data only if not already sent */
538       if (*(void **) data == NULL) {
539         XBT_VERB("Not sending NULL referenced data");
540         break;
541       }
542
543       reference_is_to_send = 1;
544       /* return ignored. Just checking whether it's known or not */
545       if (detect_cycle
546           && xbt_dict_get_or_null_ext(refs, (char *) ref,
547                                       sizeof(char *))) {
548         //XBT_INFO("Cycle detected");
549         reference_is_to_send = 0;
550       }
551
552       if (reference_is_to_send) {
553         XBT_VERB("Sending data referenced at %p", (void *) *ref);
554         if (detect_cycle)
555           xbt_dict_set_ext(refs, (char *) ref, sizeof(void *), ref, NULL);
556         xbt_datadesc_send_rec(sock, state, refs, sub_type, *ref,
557                                detect_cycle || sub_type->cycle);
558
559       } else {
560         XBT_VERB("Not sending data referenced at %p (already done)",
561               (void *) *ref);
562       }
563
564       break;
565     }
566
567   case e_xbt_datadesc_type_cat_array:{
568       xbt_dd_cat_array_t array_data;
569       int count;
570       char *ptr = data;
571       long int elm_size;
572
573       array_data = type->category.array_data;
574
575       /* determine and send the element count */
576       count = array_data.fixed_size;
577       if (count == -1) {
578         count = array_data.dynamic_size(type, state, data);
579         xbt_assert(count >= 0,
580                     "Invalid (negative) array size for type %s",
581                     type->name);
582         xbt_dd_send_int(sock, &count, 0 /*non-stable */ );
583       }
584
585       /* send the content */
586       sub_type = array_data.type;
587       elm_size = sub_type->aligned_size[GRAS_THISARCH];
588       if (sub_type->category_code == e_xbt_datadesc_type_cat_scalar) {
589         XBT_VERB("Array of %d scalars, send it in one shot", count);
590         xbt_trp_send(sock, data,
591                       sub_type->aligned_size[GRAS_THISARCH] * count,
592                       0 /* not stable */ );
593       } else if (sub_type->category_code == e_xbt_datadesc_type_cat_array
594                  && sub_type->category.array_data.fixed_size > 0
595                  && sub_type->category.array_data.type->category_code ==
596                  e_xbt_datadesc_type_cat_scalar) {
597
598         XBT_VERB("Array of %d fixed array of scalars, send it in one shot",
599               count);
600         xbt_trp_send(sock, data,
601                       sub_type->category.array_data.
602                       type->aligned_size[GRAS_THISARCH]
603                       * count * sub_type->category.array_data.fixed_size,
604                       0 /* not stable */ );
605
606       } else {
607         for (cpt = 0; cpt < count; cpt++) {
608           xbt_datadesc_send_rec(sock, state, refs, sub_type, ptr,
609                                  detect_cycle || sub_type->cycle);
610           ptr += elm_size;
611         }
612       }
613       break;
614     }
615
616   default:
617     xbt_die("Invalid type");
618   }
619 }
620
621 /**
622  * xbt_datadesc_send:
623  *
624  * Copy the data pointed by src and described by type to the socket
625  *
626  */
627 void xbt_datadesc_send(xbt_socket_t sock,
628                         xbt_datadesc_type_t type, void *src)
629 {
630   static xbt_cbps_t state = NULL;
631   static xbt_dict_t refs = NULL;        /* all references already sent */
632
633   xbt_assert(type, "called with NULL type descriptor");
634
635   if (!state) {
636     state = xbt_cbps_new();
637     refs = xbt_dict_new_homogeneous(NULL);
638   }
639
640   TRY {
641     xbt_datadesc_send_rec(sock, state, refs, type, (char *) src,
642                            type->cycle);
643   }
644   TRY_CLEANUP {
645     xbt_dict_reset(refs);
646     xbt_cbps_reset(state);
647   }
648   CATCH_ANONYMOUS {
649     RETHROW;
650   }
651 }
652
653 /**
654  * xbt_datadesc_recv_rec:
655  *
656  * Do the data reception job recursively.
657  *
658  * subsize used only to deal with vicious case of reference to dynamic array.
659  *  This size is needed at the reference reception level (to allocate enough
660  * space) and at the array reception level (to fill enough room).
661  *
662  * Having this size passed as an argument of the recursive function is a crude
663  * hack, but I was told that working code is sometimes better than neat one ;)
664  */
665 static void
666 xbt_datadesc_recv_rec(xbt_socket_t sock,
667                        xbt_cbps_t state,
668                        xbt_dict_t refs,
669                        xbt_datadesc_type_t type,
670                        int r_arch,
671                        char **r_data,
672                        long int r_lgr,
673                        char *l_data, int subsize, int detect_cycle)
674 {
675
676   unsigned int cpt;
677   xbt_datadesc_type_t sub_type;
678
679   XBT_VERB("Recv a %s @%p", type->name, (void *) l_data);
680   xbt_assert(l_data);
681
682   switch (type->category_code) {
683   case e_xbt_datadesc_type_cat_scalar:
684     if (type->size[GRAS_THISARCH] == type->size[r_arch]) {
685       xbt_trp_recv(sock, (char *) l_data, type->size[r_arch]);
686       if (r_arch != GRAS_THISARCH)
687         xbt_dd_convert_elm(type, 1, r_arch, l_data, l_data);
688     } else {
689       void *ptr = xbt_malloc(type->size[r_arch]);
690
691       xbt_trp_recv(sock, (char *) ptr, type->size[r_arch]);
692       if (r_arch != GRAS_THISARCH)
693         xbt_dd_convert_elm(type, 1, r_arch, ptr, l_data);
694       free(ptr);
695     }
696     break;
697
698   case e_xbt_datadesc_type_cat_struct:{
699       xbt_dd_cat_struct_t struct_data;
700       xbt_dd_cat_field_t field;
701
702       struct_data = type->category.struct_data;
703
704       xbt_assert(struct_data.closed,
705                   "Please call xbt_datadesc_declare_struct_close on %s before receiving it",
706                   type->name);
707       XBT_VERB(">> Receive all fields of the structure %s", type->name);
708       xbt_dynar_foreach(struct_data.fields, cpt, field) {
709         char *field_data = l_data + field->offset[GRAS_THISARCH];
710
711         sub_type = field->type;
712
713         xbt_datadesc_recv_rec(sock, state, refs, sub_type,
714                                r_arch, NULL, 0,
715                                field_data, -1,
716                                detect_cycle || sub_type->cycle);
717
718         if (field->recv) {
719           XBT_DEBUG("Run the reception callback of field %s", field->name);
720           field->recv(type, state, (void *) l_data);
721         }
722
723       }
724       XBT_VERB("<< Received all fields of the structure %s", type->name);
725
726       break;
727     }
728
729   case e_xbt_datadesc_type_cat_union:{
730       xbt_dd_cat_union_t union_data;
731       xbt_dd_cat_field_t field = NULL;
732       int field_num;
733
734       union_data = type->category.union_data;
735
736       xbt_assert(union_data.closed,
737                   "Please call xbt_datadesc_declare_union_close on %s before receiving it",
738                   type->name);
739       /* retrieve the field number */
740       xbt_dd_recv_int(sock, r_arch, &field_num);
741       if (field_num < 0)
742         THROWF(mismatch_error, 0,
743                "Received union field for %s is negative", type->name);
744       if (field_num > xbt_dynar_length(union_data.fields))
745         THROWF(mismatch_error, 0,
746                "Received union field for %s is said to be #%d but there is only %lu fields",
747                type->name, field_num, xbt_dynar_length(union_data.fields));
748
749       /* Recv the content */
750       field =
751           xbt_dynar_get_as(union_data.fields, field_num,
752                            xbt_dd_cat_field_t);
753       sub_type = field->type;
754
755       xbt_datadesc_recv_rec(sock, state, refs, sub_type,
756                              r_arch, NULL, 0,
757                              l_data, -1, detect_cycle || sub_type->cycle);
758       if (field->recv)
759         field->recv(type, state, l_data);
760
761       break;
762     }
763
764   case e_xbt_datadesc_type_cat_ref:{
765       char **r_ref = NULL;
766       char **l_ref = NULL;
767       xbt_dd_cat_ref_t ref_data;
768       int reference_is_to_recv = 0;
769
770       ref_data = type->category.ref_data;
771
772       /* Get the referenced type locally or from peer */
773       sub_type = ref_data.type;
774       if (sub_type == NULL) {
775         int ref_code;
776         xbt_dd_recv_int(sock, r_arch, &ref_code);
777         sub_type = xbt_datadesc_by_id(ref_code);
778       }
779
780       /* Get the actual value of the pointer for cycle handling */
781       if (!pointer_type) {
782         pointer_type = xbt_datadesc_by_name("data pointer");
783         xbt_assert(pointer_type);
784       }
785
786       r_ref = xbt_malloc(pointer_type->size[r_arch]);
787
788       xbt_trp_recv(sock, (char *) r_ref, pointer_type->size[r_arch]);
789
790       /* Receive the pointed data only if not already sent */
791       if (xbt_dd_is_r_null(r_ref, pointer_type->size[r_arch])) {
792         XBT_VERB("Not receiving data remotely referenced @%p since it's NULL",
793               *(void **) r_ref);
794         *(void **) l_data = NULL;
795         free(r_ref);
796         break;
797       }
798
799       reference_is_to_recv = 1;
800       if (detect_cycle && (l_ref =
801                            xbt_dict_get_or_null_ext(refs, (char *) r_ref,
802                                                     pointer_type->size
803                                                     [r_arch]))) {
804         reference_is_to_recv = 0;
805         //XBT_INFO("Cycle detected");
806       }
807
808       if (reference_is_to_recv) {
809         int subsubcount = -1;
810         void *l_referenced = NULL;
811
812         XBT_VERB("Receiving a ref to '%s', remotely @%p",
813               sub_type->name, *(void **) r_ref);
814         if (sub_type->category_code == e_xbt_datadesc_type_cat_array) {
815           /* Damn. Reference to a dynamic array. Allocating the space for it is more complicated */
816           xbt_dd_cat_array_t array_data = sub_type->category.array_data;
817           xbt_datadesc_type_t subsub_type;
818
819           subsubcount = array_data.fixed_size;
820           if (subsubcount == -1)
821             xbt_dd_recv_int(sock, r_arch, &subsubcount);
822
823           subsub_type = array_data.type;
824
825           if (subsubcount != 0)
826             xbt_dd_alloc_ref(refs,
827                               subsub_type->size[GRAS_THISARCH] *
828                               subsubcount, r_ref,
829                               pointer_type->size[r_arch],
830                               (char **) &l_referenced, detect_cycle);
831           else
832             l_referenced = NULL;
833         } else {
834           xbt_dd_alloc_ref(refs, sub_type->size[GRAS_THISARCH],
835                             r_ref, pointer_type->size[r_arch],
836                             (char **) &l_referenced, detect_cycle);
837         }
838
839         if (l_referenced != NULL)
840           xbt_datadesc_recv_rec(sock, state, refs, sub_type,
841                                  r_arch, r_ref, pointer_type->size[r_arch],
842                                  (char *) l_referenced, subsubcount,
843                                  detect_cycle || sub_type->cycle);
844
845         *(void **) l_data = l_referenced;
846         XBT_VERB("'%s' remotely referenced at %p locally at %p",
847               sub_type->name, *(void **) r_ref, l_referenced);
848
849       } else {
850         XBT_VERB
851             ("NOT receiving data remotely referenced @%p (already done, @%p here)",
852              *(void **) r_ref, *(void **) l_ref);
853
854         *(void **) l_data = *l_ref;
855
856       }
857       free(r_ref);
858       break;
859     }
860
861   case e_xbt_datadesc_type_cat_array:{
862       xbt_dd_cat_array_t array_data;
863       int count;
864       char *ptr;
865       long int elm_size;
866
867       array_data = type->category.array_data;
868       /* determine element count locally, or from caller, or from peer */
869       count = array_data.fixed_size;
870       if (count == -1)
871         count = subsize;
872       if (count == -1)
873         xbt_dd_recv_int(sock, r_arch, &count);
874       if (count == -1)
875         THROWF(mismatch_error, 0,
876                "Invalid (=-1) array size for type %s", type->name);
877
878       /* receive the content */
879       sub_type = array_data.type;
880       if (sub_type->category_code == e_xbt_datadesc_type_cat_scalar) {
881         XBT_VERB("Array of %d scalars, get it in one shoot", count);
882         if (sub_type->aligned_size[GRAS_THISARCH] >=
883             sub_type->aligned_size[r_arch]) {
884           xbt_trp_recv(sock, (char *) l_data,
885                         sub_type->aligned_size[r_arch] * count);
886           if (r_arch != GRAS_THISARCH)
887             xbt_dd_convert_elm(sub_type, count, r_arch, l_data, l_data);
888         } else {
889           ptr = xbt_malloc(sub_type->aligned_size[r_arch] * count);
890
891           xbt_trp_recv(sock, (char *) ptr,
892                         sub_type->size[r_arch] * count);
893           if (r_arch != GRAS_THISARCH)
894             xbt_dd_convert_elm(sub_type, count, r_arch, ptr, l_data);
895           free(ptr);
896         }
897       } else if (sub_type->category_code == e_xbt_datadesc_type_cat_array
898                  && sub_type->category.array_data.fixed_size >= 0
899                  && sub_type->category.array_data.type->category_code ==
900                  e_xbt_datadesc_type_cat_scalar) {
901         xbt_datadesc_type_t subsub_type;
902         array_data = sub_type->category.array_data;
903         subsub_type = array_data.type;
904
905         XBT_VERB("Array of %d fixed array of scalars, get it in one shot",
906               count);
907         if (subsub_type->aligned_size[GRAS_THISARCH] >=
908             subsub_type->aligned_size[r_arch]) {
909           xbt_trp_recv(sock, (char *) l_data,
910                         subsub_type->aligned_size[r_arch] * count *
911                         array_data.fixed_size);
912           if (r_arch != GRAS_THISARCH)
913             xbt_dd_convert_elm(subsub_type, count * array_data.fixed_size,
914                                 r_arch, l_data, l_data);
915         } else {
916           ptr =
917               xbt_malloc(subsub_type->aligned_size[r_arch] * count *
918                          array_data.fixed_size);
919
920           xbt_trp_recv(sock, (char *) ptr,
921                         subsub_type->size[r_arch] * count *
922                         array_data.fixed_size);
923           if (r_arch != GRAS_THISARCH)
924             xbt_dd_convert_elm(subsub_type, count * array_data.fixed_size,
925                                 r_arch, ptr, l_data);
926           free(ptr);
927         }
928
929
930       } else {
931         /* not scalar content, get it recursively (may contain pointers) */
932         elm_size = sub_type->aligned_size[GRAS_THISARCH];
933         XBT_VERB("Receive a %d-long array of %s", count, sub_type->name);
934
935         ptr = l_data;
936         for (cpt = 0; cpt < count; cpt++) {
937           xbt_datadesc_recv_rec(sock, state, refs, sub_type,
938                                  r_arch, NULL, 0, ptr, -1,
939                                  detect_cycle || sub_type->cycle);
940
941           ptr += elm_size;
942         }
943       }
944       break;
945     }
946
947   default:
948     xbt_die("Invalid type");
949   }
950
951   if (type->recv)
952     type->recv(type, state, l_data);
953
954   if (!strcmp(type->name, "string"))
955     XBT_VERB("value: '%s'", *(char **) l_data);
956
957 }
958
959 /**
960  * xbt_datadesc_recv:
961  *
962  * Get an instance of the datatype described by @type from the @socket,
963  * and store a pointer to it in @dst
964  *
965  */
966 void
967 xbt_datadesc_recv(xbt_socket_t sock,
968                    xbt_datadesc_type_t type, int r_arch, void *dst)
969 {
970   static xbt_cbps_t state = NULL;      /* callback persistent state */
971   static xbt_dict_t refs = NULL;        /* all references already sent */
972
973   if (!state) {
974     state = xbt_cbps_new();
975     refs = xbt_dict_new_homogeneous(xbt_free_f);
976   }
977
978   xbt_assert(type, "called with NULL type descriptor");
979   TRY {
980     xbt_datadesc_recv_rec(sock, state, refs, type,
981                            r_arch, NULL, 0, (char *) dst, -1, type->cycle);
982   }
983   TRY_CLEANUP {
984     xbt_dict_reset(refs);
985     xbt_cbps_reset(state);
986   }
987   CATCH_ANONYMOUS {
988     RETHROW;
989   }
990 }