Logo AND Algorithmique Numérique Distribuée

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