Logo AND Algorithmique Numérique Distribuée

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