Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Simplify a bit the way the exceptions are handled
[simgrid.git] / src / gras / DataDesc / ddt_exchange.c
1 /* $Id$ */
2
3 /* ddt_exchange - send/recv data described                                  */
4
5 /* Copyright (c) 2003 Olivier Aumage.                                       */
6 /* Copyright (c) 2003, 2004, 2005 Martin Quinson.                           */
7 /* All rights reserved.                                                     */
8
9 /* This program is free software; you can redistribute it and/or modify it
10  * under the terms of the license (GNU LGPL) which comes with this package. */
11
12 #include "xbt/ex.h"
13 #include "gras/DataDesc/datadesc_private.h"
14 #include "gras/Transport/transport_interface.h" /* gras_trp_chunk_send/recv */
15
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ddt_exchange,datadesc,
17                                  "Sending data over the network");
18 const char *gras_datadesc_cat_names[9] = { 
19   "undefined", 
20   "scalar", "struct", "union", "ref", "array", "ignored",
21   "invalid"};
22
23 static gras_datadesc_type_t int_type = NULL;
24 static gras_datadesc_type_t pointer_type = NULL;    
25 static _XBT_INLINE void gras_dd_send_int(gras_socket_t sock,             int  i);
26 static _XBT_INLINE void gras_dd_recv_int(gras_socket_t sock, int r_arch, int *i);
27
28 static _XBT_INLINE xbt_error_t
29 gras_dd_alloc_ref(xbt_dict_t  refs,  long int     size,
30                   char       **r_ref, long int     r_len,
31                   char       **l_ref, int detect_cycle);
32
33 static _XBT_INLINE int
34 gras_dd_is_r_null(char **r_ptr, long int length);
35
36 static _XBT_INLINE void
37 gras_dd_send_int(gras_socket_t sock,int i) {
38
39   if (!int_type) {
40     int_type = gras_datadesc_by_name("int");
41      xbt_assert(int_type);  
42   }
43    
44   DEBUG1("send_int(%d)",i);
45   gras_trp_chunk_send(sock, (char*)&i, int_type->size[GRAS_THISARCH]);
46 }
47
48 static _XBT_INLINE void
49 gras_dd_recv_int(gras_socket_t sock, int r_arch, int *i) {
50
51   if (!int_type) {
52      int_type = gras_datadesc_by_name("int");
53      xbt_assert(int_type);
54   }
55
56   if (int_type->size[GRAS_THISARCH] >= int_type->size[r_arch]) {
57     gras_trp_chunk_recv(sock, (char*)i, int_type->size[r_arch]);
58     if (r_arch != GRAS_THISARCH)
59       gras_dd_convert_elm(int_type,1,r_arch, i,i);
60   } else {
61     void *ptr = xbt_malloc(int_type->size[r_arch]);
62
63     gras_trp_chunk_recv(sock, (char*)ptr, int_type->size[r_arch]);
64     if (r_arch != GRAS_THISARCH)
65       gras_dd_convert_elm(int_type,1,r_arch, ptr,i);
66     free(ptr);
67   }
68   DEBUG1("recv_int(%d)",*i);
69 }
70
71 /*
72  * Note: here we suppose that the remote NULL is a sequence 
73  *       of 'length' bytes set to 0.
74  * FIXME: Check in configure?
75  */
76 static _XBT_INLINE int 
77 gras_dd_is_r_null(char **r_ptr, long int length) {
78   int i;
79
80   for (i=0; i<length; i++) {
81     if ( ((unsigned char*)r_ptr) [i]) {
82       return 0;
83     }
84   }
85
86   return 1;
87 }
88
89 static _XBT_INLINE xbt_error_t
90 gras_dd_alloc_ref(xbt_dict_t  refs,
91                   long int     size,
92                   char       **r_ref,
93                   long int     r_len, /* pointer_type->size[r_arch] */
94                   char       **l_ref,
95                   int          detect_cycle) {
96   char *l_data = NULL;
97
98   xbt_assert1(size>0,"Cannot allocate %ld bytes!", size);
99   l_data = xbt_malloc((size_t)size);
100
101   *l_ref = l_data;
102   DEBUG5("alloc_ref: l_data=%p, &l_data=%p; r_ref=%p; *r_ref=%p, r_len=%ld",
103          (void*)l_data,(void*)&l_data,
104          (void*)r_ref, (void*)(r_ref?*r_ref:NULL), r_len);
105   if (detect_cycle && r_ref && !gras_dd_is_r_null( r_ref, r_len)) {
106     void *ptr = xbt_malloc(sizeof(void *));
107
108     CRITICAL0("Check for cycles");
109     memcpy(ptr,l_ref, sizeof(void *));
110
111     DEBUG2("Insert l_ref=%p under r_ref=%p",*(void**)ptr, *(void**)r_ref);
112
113     if (detect_cycle)
114        xbt_dict_set_ext(refs,(const char *) r_ref, r_len, ptr, free);
115   }
116   return no_error;
117 }
118
119 /**
120  * gras_datadesc_cpy:
121  *
122  * Copy the data pointed by src and described by type 
123  * to a new location, and store a pointer to it in dst.
124  *
125  */
126 xbt_error_t gras_datadesc_cpy(gras_datadesc_type_t type, 
127                                void *src, 
128                                void **dst) {
129   THROW_UNIMPLEMENTED;
130 }
131
132 /***
133  *** Direct use functions
134  ***/
135
136 static void
137 gras_datadesc_send_rec(gras_socket_t         sock,
138                        gras_cbps_t           state,
139                        xbt_dict_t           refs,
140                        gras_datadesc_type_t  type, 
141                        char                 *data,
142                        int                   detect_cycle) {
143
144   xbt_ex_t             e;
145   int                  cpt;
146   gras_datadesc_type_t sub_type; /* type on which we recurse */
147   
148   VERB2("Send a %s (%s)", 
149         type->name, gras_datadesc_cat_names[type->category_code]);
150
151   if (type->send) {
152     type->send(type,state,data);
153   }
154
155   switch (type->category_code) {
156   case e_gras_datadesc_type_cat_scalar:
157     gras_trp_chunk_send(sock, data, type->size[GRAS_THISARCH]);
158     break;
159
160   case e_gras_datadesc_type_cat_struct: {
161     gras_dd_cat_struct_t struct_data;
162     gras_dd_cat_field_t  field;
163     char                *field_data;
164     
165     struct_data = type->category.struct_data;
166     xbt_assert1(struct_data.closed,
167       "Please call gras_datadesc_declare_struct_close on %s before sending it",
168                 type->name);
169     VERB1(">> Send all fields of the structure %s",type->name);
170     xbt_dynar_foreach(struct_data.fields, cpt, field) {
171       field_data = data;
172       field_data += field->offset[GRAS_THISARCH];
173       
174       sub_type = field->type;
175       
176       if (field->send)
177         field->send(type,state,field_data);
178       
179       VERB1("Send field %s",field->name);
180       gras_datadesc_send_rec(sock,state,refs,sub_type, field_data, 
181                              detect_cycle || sub_type->cycle);
182       
183     }
184     VERB1("<< Sent 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     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 sending it",
198                 type->name);
199     /* retrieve the field number */
200     field_num = union_data.selector(type, state, data);
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     /* Send the field number */
211     gras_dd_send_int(sock, field_num);
212     
213     /* Send the content */
214     field = xbt_dynar_get_as(union_data.fields, field_num, gras_dd_cat_field_t);
215     sub_type = field->type;
216     
217     if (field->send)
218       field->send(type,state,data);
219     
220     gras_datadesc_send_rec(sock,state,refs, sub_type, data, 
221                            detect_cycle || sub_type->cycle);
222           
223     break;
224   }
225     
226   case e_gras_datadesc_type_cat_ref: {
227     gras_dd_cat_ref_t      ref_data;
228     void                 **ref=(void**)data;
229     int                    reference_is_to_send;
230     
231     ref_data = type->category.ref_data;
232     
233     /* Detect the referenced type and send it to peer if needed */
234     sub_type = ref_data.type;
235     if (sub_type == NULL) {
236       sub_type = (*ref_data.selector)(type,state,data);
237       gras_dd_send_int(sock, sub_type->code);
238     }
239     
240     /* Send the actual value of the pointer for cycle handling */
241     if (!pointer_type) {
242       pointer_type = gras_datadesc_by_name("data pointer");
243       xbt_assert(pointer_type);
244     }
245      
246     gras_trp_chunk_send(sock, (char*)data,
247                         pointer_type->size[GRAS_THISARCH]);
248     
249     /* Send the pointed data only if not already sent */
250     if (*(void**)data == NULL) {
251       VERB0("Not sending NULL referenced data");
252       break;
253     }
254
255     reference_is_to_send = 0;
256     TRY {
257       if (detect_cycle)
258         /* return ignored. Just checking whether it's known or not */
259         xbt_dict_get_ext(refs,(char*)ref, sizeof(void*));
260       else 
261         reference_is_to_send = 1;
262     } CATCH(e) {
263       if (e.category != mismatch_error)
264         RETHROW;
265       reference_is_to_send = 1;
266       xbt_ex_free(e);
267     }
268
269     if (reference_is_to_send) {
270        VERB1("Sending data referenced at %p", (void*)*ref);
271        if (detect_cycle)
272          xbt_dict_set_ext(refs, (char*)ref, sizeof(void*), ref, NULL);
273        gras_datadesc_send_rec(sock,state,refs, sub_type, *ref, 
274                               detect_cycle || sub_type->cycle);
275           
276     } else {
277        VERB1("Not sending data referenced at %p (already done)", (void*)*ref);
278     } 
279     
280     break;
281   }
282
283   case e_gras_datadesc_type_cat_array: {
284     gras_dd_cat_array_t    array_data;
285     long int               count;
286     char                  *ptr=data;
287     long int               elm_size;
288     
289     array_data = type->category.array_data;
290     
291     /* determine and send the element count */
292     count = array_data.fixed_size;
293     if (count == 0) {
294       count = array_data.dynamic_size(type,state,data);
295       xbt_assert1(count >=0,
296                    "Invalid (negative) array size for type %s",type->name);
297       gras_dd_send_int(sock, count);
298     }
299     
300     /* send the content */
301     sub_type = array_data.type;
302     elm_size = sub_type->aligned_size[GRAS_THISARCH];
303     if (sub_type->category_code == e_gras_datadesc_type_cat_scalar) {
304       VERB1("Array of %ld scalars, send it in one shot",count);
305       gras_trp_chunk_send(sock, data, 
306                           sub_type->aligned_size[GRAS_THISARCH] * count);
307     } else if (sub_type->category_code == e_gras_datadesc_type_cat_array &&
308                sub_type->category.array_data.fixed_size > 0 &&
309                sub_type->category.array_data.type->category_code == e_gras_datadesc_type_cat_scalar) {
310        
311       VERB1("Array of %ld fixed array of scalars, send it in one shot",count);
312       gras_trp_chunk_send(sock, data, 
313                           sub_type->category.array_data.type->aligned_size[GRAS_THISARCH] 
314                           * count * sub_type->category.array_data.fixed_size);
315        
316     } else {
317       for (cpt=0; cpt<count; cpt++) {
318         gras_datadesc_send_rec(sock,state,refs, sub_type, ptr, 
319                                detect_cycle || sub_type->cycle);
320         ptr += elm_size;
321       }
322     }
323     break;
324   }
325
326   default:
327     xbt_assert0(0, "Invalid type");
328   }
329 }
330
331 /**
332  * gras_datadesc_send:
333  *
334  * Copy the data pointed by src and described by type to the socket
335  *
336  */
337 void gras_datadesc_send(gras_socket_t        sock, 
338                         gras_datadesc_type_t type, 
339                         void *src) {
340
341   xbt_ex_t e;
342   gras_cbps_t  state;
343   xbt_dict_t  refs; /* all references already sent */
344  
345   xbt_assert0(type,"called with NULL type descriptor");
346
347   refs = xbt_dict_new();
348   state = gras_cbps_new();
349   
350   TRY {
351     gras_datadesc_send_rec(sock,state,refs,type,(char*)src, type->cycle);
352   } CLEANUP {
353     xbt_dict_free(&refs);
354     gras_cbps_free(&state);
355   } CATCH(e) {
356     RETHROW;
357   }
358 }
359
360 /**
361  * gras_datadesc_recv_rec:
362  *
363  * Do the data reception job recursively.
364  *
365  * subsize used only to deal with vicious case of reference to dynamic array.
366  *  This size is needed at the reference reception level (to allocate enough 
367  * space) and at the array reception level (to fill enough room). 
368  * 
369  * Having this size passed as an argument of the recursive function is a crude
370  * hack, but I was told that working code is sometimes better than neat one ;)
371  */
372 static void
373 gras_datadesc_recv_rec(gras_socket_t         sock, 
374                        gras_cbps_t           state,
375                        xbt_dict_t           refs,
376                        gras_datadesc_type_t  type,
377                        int                   r_arch,
378                        char                **r_data,
379                        long int              r_lgr,
380                        char                 *l_data,
381                        int                   subsize,
382                        int                   detect_cycle) {
383
384   int                  cpt;
385   gras_datadesc_type_t sub_type;
386   xbt_ex_t e;
387
388   VERB2("Recv a %s @%p", type->name, (void*)l_data);
389   xbt_assert(l_data);
390
391   switch (type->category_code) {
392   case e_gras_datadesc_type_cat_scalar:
393     if (type->size[GRAS_THISARCH] == type->size[r_arch]) {
394       gras_trp_chunk_recv(sock, (char*)l_data, type->size[r_arch]);
395       if (r_arch != GRAS_THISARCH)
396         gras_dd_convert_elm(type,1,r_arch, l_data,l_data);
397     } else {
398       void *ptr = xbt_malloc(type->size[r_arch]);
399
400       gras_trp_chunk_recv(sock, (char*)ptr, type->size[r_arch]);
401       if (r_arch != GRAS_THISARCH)
402         gras_dd_convert_elm(type,1,r_arch, ptr,l_data);
403       free(ptr);
404     }
405     break;
406
407   case e_gras_datadesc_type_cat_struct: {
408     gras_dd_cat_struct_t struct_data;
409     gras_dd_cat_field_t  field;
410
411     struct_data = type->category.struct_data;
412
413     xbt_assert1(struct_data.closed,
414                 "Please call gras_datadesc_declare_struct_close on %s before receiving it",
415                 type->name);
416     VERB1(">> Receive all fields of the structure %s",type->name);
417     xbt_dynar_foreach(struct_data.fields, cpt, field) {
418       char                 *field_data = l_data + field->offset[GRAS_THISARCH];
419
420       sub_type = field->type;
421
422       gras_datadesc_recv_rec(sock,state,refs, sub_type,
423                              r_arch,NULL,0,
424                              field_data,-1, 
425                              detect_cycle || sub_type->cycle);
426        
427       if (field->recv)
428         field->recv(type,state,(void*)l_data);
429     
430     }
431     VERB1("<< Received all fields of the structure %s", type->name);
432     
433     break;
434   }
435
436   case e_gras_datadesc_type_cat_union: {
437     gras_dd_cat_union_t union_data;
438     gras_dd_cat_field_t field=NULL;
439     int                 field_num;
440
441     union_data = type->category.union_data;
442
443     xbt_assert1(union_data.closed,
444                 "Please call gras_datadesc_declare_union_close on %s before receiving it",
445                 type->name);
446     /* retrieve the field number */
447     gras_dd_recv_int(sock, r_arch, &field_num);
448     if (field_num < 0)
449       THROW1(mismatch_error,0,
450              "Received union field for %s is negative", type->name);
451     if (field_num < xbt_dynar_length(union_data.fields)) 
452       THROW3(mismatch_error,0,
453              "Received union field for %s is said to be #%d but there is only %lu fields",
454              type->name, field_num, xbt_dynar_length(union_data.fields));
455     
456     /* Recv the content */
457     field = xbt_dynar_get_as(union_data.fields, field_num, gras_dd_cat_field_t);
458     sub_type = field->type;
459     
460     gras_datadesc_recv_rec(sock,state,refs, sub_type,
461                            r_arch,NULL,0,
462                            l_data,-1,
463                            detect_cycle || sub_type->cycle);
464     if (field->recv)
465        field->recv(type,state,l_data);
466
467     break;
468   }
469
470   case e_gras_datadesc_type_cat_ref: {
471     char             **r_ref = NULL;
472     char             **l_ref = NULL;
473     gras_dd_cat_ref_t  ref_data;
474     int reference_is_to_recv = 0;
475     
476     ref_data = type->category.ref_data;
477
478     /* Get the referenced type locally or from peer */
479     sub_type = ref_data.type;
480     if (sub_type == NULL) {
481       int ref_code;
482       gras_dd_recv_int(sock, r_arch, &ref_code);
483       sub_type = gras_datadesc_by_id(ref_code);
484     }
485
486     /* Get the actual value of the pointer for cycle handling */
487     if (!pointer_type) {
488       pointer_type = gras_datadesc_by_name("data pointer");
489       xbt_assert(pointer_type);
490     }
491
492     r_ref = xbt_malloc(pointer_type->size[r_arch]);
493
494     gras_trp_chunk_recv(sock, (char*)r_ref,
495                         pointer_type->size[r_arch]);
496
497     /* Receive the pointed data only if not already sent */
498     if (gras_dd_is_r_null(r_ref, pointer_type->size[r_arch])) {
499       VERB1("Not receiving data remotely referenced @%p since it's NULL",
500             *(void **)r_ref);
501       *(void**)l_data = NULL;
502       free(r_ref);
503       break;
504     }
505          
506     reference_is_to_recv = 0;
507     TRY {
508       if (detect_cycle)
509         l_ref = xbt_dict_get_ext(refs, (char*)r_ref, pointer_type->size[r_arch]);
510       else 
511         reference_is_to_recv = 1;
512     } CATCH(e) {
513       if (e.category != mismatch_error)
514         RETHROW;
515       reference_is_to_recv = 1;
516       xbt_ex_free(e);
517     }
518     if (reference_is_to_recv) {
519       int subsubcount = 0;
520       void *l_referenced=NULL;
521
522       VERB2("Receiving a ref to '%s', remotely @%p",
523             sub_type->name, *(void**)r_ref);
524       if (sub_type->category_code == e_gras_datadesc_type_cat_array) {
525         /* Damn. Reference to a dynamic array. Allocating the space for it 
526            is more complicated */
527         gras_dd_cat_array_t array_data = sub_type->category.array_data;
528         gras_datadesc_type_t subsub_type;
529
530         subsubcount = array_data.fixed_size;
531         if (subsubcount == 0)
532           gras_dd_recv_int(sock, r_arch, &subsubcount);
533
534         subsub_type = array_data.type;
535
536
537         gras_dd_alloc_ref(refs,
538                           subsub_type->size[GRAS_THISARCH] * subsubcount, 
539                           r_ref,pointer_type->size[r_arch], 
540                           (char**)&l_referenced,
541                           detect_cycle);
542       } else {
543         gras_dd_alloc_ref(refs,sub_type->size[GRAS_THISARCH], 
544                           r_ref,pointer_type->size[r_arch], 
545                           (char**)&l_referenced,
546                           detect_cycle);
547       }
548
549       gras_datadesc_recv_rec(sock,state,refs, sub_type,
550                              r_arch,r_ref,pointer_type->size[r_arch],
551                              (char*)l_referenced, subsubcount,
552                              detect_cycle || sub_type->cycle);
553                                
554       *(void**)l_data=l_referenced;
555       VERB3("'%s' remotely referenced at %p locally at %p",
556             sub_type->name, *(void**)r_ref, l_referenced);
557       
558     } else {
559       VERB2("NOT receiving data remotely referenced @%p (already done, @%p here)",
560             *(void**)r_ref, *(void**)l_ref);
561
562       *(void**)l_data=*l_ref;
563
564     } 
565     free(r_ref);
566     break;
567   }
568
569   case e_gras_datadesc_type_cat_array: {
570     gras_dd_cat_array_t    array_data;
571     int       count;
572     char     *ptr;
573     long int  elm_size;
574
575     array_data = type->category.array_data;
576     /* determine element count locally, or from caller, or from peer */
577     count = array_data.fixed_size;
578     if (count == 0)
579       count = subsize;
580     if (count == 0)
581       gras_dd_recv_int(sock, r_arch, &count);
582     if (count == 0)
583       THROW1(mismatch_error,0,
584              "Invalid (=0) array size for type %s",type->name);
585
586     /* receive the content */
587     sub_type = array_data.type;
588     if (sub_type->category_code == e_gras_datadesc_type_cat_scalar) {
589       VERB1("Array of %d scalars, get it in one shoot", count);
590       if (sub_type->aligned_size[GRAS_THISARCH] >= 
591           sub_type->aligned_size[r_arch]) {
592         gras_trp_chunk_recv(sock, (char*)l_data, 
593                             sub_type->aligned_size[r_arch] * count);
594         if (r_arch != GRAS_THISARCH)
595           gras_dd_convert_elm(sub_type,count,r_arch, l_data,l_data);
596       } else {
597         ptr = xbt_malloc(sub_type->aligned_size[r_arch] * count);
598
599         gras_trp_chunk_recv(sock, (char*)ptr, 
600                             sub_type->size[r_arch] * count);
601         if (r_arch != GRAS_THISARCH)
602           gras_dd_convert_elm(sub_type,count,r_arch, ptr,l_data);
603         free(ptr);
604       }
605     } else if (sub_type->category_code == e_gras_datadesc_type_cat_array &&
606                sub_type->category.array_data.fixed_size > 0 &&
607                sub_type->category.array_data.type->category_code == e_gras_datadesc_type_cat_scalar) {
608       gras_datadesc_type_t subsub_type;
609       array_data = sub_type->category.array_data;
610       subsub_type = array_data.type;
611        
612       VERB1("Array of %d fixed array of scalars, get it in one shot",count);
613       if (subsub_type->aligned_size[GRAS_THISARCH] >= 
614           subsub_type->aligned_size[r_arch]) {
615         gras_trp_chunk_recv(sock, (char*)l_data, 
616                             subsub_type->aligned_size[r_arch] * count * 
617                             array_data.fixed_size);
618         if (r_arch != GRAS_THISARCH)
619           gras_dd_convert_elm(subsub_type,count*array_data.fixed_size,r_arch, l_data,l_data);
620       } else {
621         ptr = xbt_malloc(subsub_type->aligned_size[r_arch] * count*array_data.fixed_size);
622
623         gras_trp_chunk_recv(sock, (char*)ptr, 
624                             subsub_type->size[r_arch] * count*array_data.fixed_size);
625         if (r_arch != GRAS_THISARCH)
626           gras_dd_convert_elm(subsub_type,count*array_data.fixed_size,r_arch, ptr,l_data);
627         free(ptr);
628       }
629       
630        
631     } else {
632       /* not scalar content, get it recursively (may contain pointers) */
633       elm_size = sub_type->aligned_size[GRAS_THISARCH];
634       VERB2("Receive a %d-long array of %s",count, sub_type->name);
635
636       ptr = l_data;
637       for (cpt=0; cpt<count; cpt++) {
638         gras_datadesc_recv_rec(sock,state,refs, sub_type,
639                                r_arch, NULL, 0, ptr,-1,
640                                detect_cycle || sub_type->cycle);
641                                    
642         ptr += elm_size;
643       }
644     }
645     break;
646   }
647         
648   default:
649     xbt_assert0(0, "Invalid type");
650   }
651   
652   if (type->recv)
653     type->recv(type,state,l_data);
654
655 }
656
657 /**
658  * gras_datadesc_recv:
659  *
660  * Get an instance of the datatype described by @type from the @socket, 
661  * and store a pointer to it in @dst
662  *
663  */
664 void
665 gras_datadesc_recv(gras_socket_t         sock, 
666                    gras_datadesc_type_t  type,
667                    int                   r_arch,
668                    void                 *dst) {
669
670   xbt_ex_t e;
671   gras_cbps_t  state; /* callback persistent state */
672   xbt_dict_t  refs;  /* all references already sent */
673
674   refs = xbt_dict_new();
675   state = gras_cbps_new();
676
677   xbt_assert0(type,"called with NULL type descriptor");
678   TRY {
679     gras_datadesc_recv_rec(sock, state, refs, type, 
680                            r_arch, NULL, 0,
681                            (char *) dst,-1, 
682                            type->cycle);
683   } CLEANUP {
684     xbt_dict_free(&refs);
685     gras_cbps_free(&state);
686   } CATCH(e) {
687     RETHROW;
688   }
689 }
690
691 #if 0
692 /***
693  *** IDL compiling functions
694  ***/
695
696 #define gras_datadesc_send_rec foo /* Just to make sure the copypast was ok */
697 #define gras_datadesc_send     foo /* Just to make sure the copypast was ok */
698 #define gras_datadesc_recv_rec foo /* Just to make sure the copypast was ok */
699 #define gras_datadesc_recv     foo /* Just to make sure the copypast was ok */
700
701 static xbt_error_t 
702 gras_datadesc_gen_send_rec(gras_socket_t         sock,
703                            gras_cbps_t           state,
704                            xbt_dict_t           refs,
705                            gras_datadesc_type_t  type, 
706                            char                 *data,
707                            int                   detect_cycle) {
708
709   xbt_error_t         errcode;
710   int                  cpt;
711   gras_datadesc_type_t sub_type; /* type on which we recurse */
712   
713   printf("  VERB2(\"Send a %s (%s)\");\n", 
714          type->name, gras_datadesc_cat_names[type->category_code]);
715
716   xbt_assert0(!type->send, "Callbacks not implemented in IDL compiler");
717
718   switch (type->category_code) {
719   case e_gras_datadesc_type_cat_scalar:
720     printf("  TRYOLD(gras_trp_chunk_send(sock, data, %lu));\n",type->size[GRAS_THISARCH]);
721     break;
722
723   case e_gras_datadesc_type_cat_struct: {
724     gras_dd_cat_struct_t struct_data;
725     gras_dd_cat_field_t  field;
726     char                *field_data;
727     
728     struct_data = type->category.struct_data;
729     xbt_assert1(struct_data.closed,
730       "Please call gras_datadesc_declare_struct_close on %s before sending it",
731                 type->name);
732     printf("  VERB1(\">> Send all fields of the structure %s\");\n",type->name);
733     xbt_dynar_foreach(struct_data.fields, cpt, field) {
734       field_data = data;
735       field_data += field->offset[GRAS_THISARCH];
736       
737       sub_type = field->type;
738       
739       xbt_assert0(!field->send, "Callbacks not implemented in IDL compiler");
740       
741       printf("  VERB1(\"Send field %s\");\n",field->name);
742       printf("  data += %lu;\n",field->offset[GRAS_THISARCH]);
743       TRYOLD(gras_datadesc_gen_send_rec(sock,state,refs,sub_type, field_data, 
744                                      detect_cycle || sub_type->cycle));
745       printf("  data -= %lu;\n",field->offset[GRAS_THISARCH]);
746       
747       xbt_assert0(!field->recv, "Callbacks not implemented in IDL compiler");
748     }
749     printf("  VERB1(\"<< Sent all fields of the structure %s\"", type->name);
750     
751     break;
752   }
753
754   case e_gras_datadesc_type_cat_union: {
755     gras_dd_cat_union_t union_data;
756     gras_dd_cat_field_t field=NULL;
757     int                 field_num;
758     
759     union_data = type->category.union_data;
760     
761     xbt_assert1(union_data.closed,
762                 "Please call gras_datadesc_declare_union_close on %s before sending it",
763                 type->name);
764     /* retrieve the field number */
765     printf("  field_num = union_data.selector(state, data);\n");
766     
767     printf("  xbt_assert0(field_num > 0,\n");
768     printf("              \"union field selector of %s gave a negative value\");\n",type->name);
769     
770     printf("  xbt_assert3(field_num < xbt_dynar_length(union_data.fields),\n");
771     printf("              \"union field selector of %s returned %%d but there is only %lu fields\",field_num);\n",
772                  type->name, xbt_dynar_length(union_data.fields));
773
774     /* Send the field number */
775     printf("TRYOLD(gras_dd_send_int(sock, field_num));\n");
776     
777     /* Send the content */
778     field = xbt_dynar_get_as(union_data.fields, field_num, gras_dd_cat_field_t);
779     sub_type = field->type;
780     
781     if (field->send)
782       field->send(state,data);
783     
784     TRYOLD(gras_datadesc_gen_send_rec(sock,state,refs, sub_type, data,
785                                    detect_cycle || sub_type->cycle));
786            
787     break;
788   }
789     
790   case e_gras_datadesc_type_cat_ref: {
791     gras_dd_cat_ref_t      ref_data;
792
793     void                 **ref=(void**)data;
794     void *dummy;
795     
796     ref_data = type->category.ref_data;
797     
798     /* Detect the referenced type and send it to peer if needed */
799     sub_type = ref_data.type;
800     if (sub_type == NULL) {
801       sub_type = (*ref_data.selector)(state,data);
802       TRYOLD(gras_dd_send_int(sock, sub_type->code));
803     }
804     
805     /* Send the actual value of the pointer for cycle handling */
806     if (!pointer_type) {
807       pointer_type = gras_datadesc_by_name("data pointer");
808       xbt_assert(pointer_type);
809     }
810      
811     TRYOLD(gras_trp_chunk_send(sock, (char*)data,
812                             pointer_type->size[GRAS_THISARCH]));
813     
814     /* Send the pointed data only if not already sent */
815     if (*(void**)data == NULL) {
816       VERB0("Not sending NULL referenced data");
817       break;
818     }
819     errcode = detect_cycle 
820             ? xbt_dict_get_ext(refs,(char*)ref, sizeof(void*), &dummy)
821             : mismatch_error;
822     if (errcode == mismatch_error) {
823        VERB1("Sending data referenced at %p", (void*)*ref);
824        if (detect_cycle)
825          xbt_dict_set_ext(refs, (char*)ref, sizeof(void*), ref, NULL);
826        TRYOLD(gras_datadesc_gen_send_rec(sock,state,refs, sub_type, *ref, 
827                                       detect_cycle || sub_type->cycle));
828           
829     } else if (errcode == no_error) {
830        VERB1("Not sending data referenced at %p (already done)", (void*)*ref);
831     } else {
832        return errcode;
833     }
834     
835     break;
836   }
837
838   case e_gras_datadesc_type_cat_array: {
839     gras_dd_cat_array_t    array_data;
840     long int               count;
841     char                  *ptr=data;
842     long int               elm_size;
843     
844     array_data = type->category.array_data;
845     
846     /* determine and send the element count */
847     count = array_data.fixed_size;
848     if (count == 0) {
849       count = array_data.dynamic_size(state,data);
850       xbt_assert1(count >=0,
851                    "Invalid (negative) array size for type %s",type->name);
852       TRYOLD(gras_dd_send_int(sock, count));
853     }
854     
855     /* send the content */
856     sub_type = array_data.type;
857     elm_size = sub_type->aligned_size[GRAS_THISARCH];
858     if (sub_type->category_code == e_gras_datadesc_type_cat_scalar) {
859       VERB1("Array of %ld scalars, send it in one shot",count);
860       TRYOLD(gras_trp_chunk_send(sock, data, 
861                               sub_type->aligned_size[GRAS_THISARCH] * count));
862     } else if (sub_type->category_code == e_gras_datadesc_type_cat_array &&
863                sub_type->category.array_data.fixed_size > 0 &&
864                sub_type->category.array_data.type->category_code == e_gras_datadesc_type_cat_scalar) {
865        
866       VERB1("Array of %ld fixed array of scalars, send it in one shot",count);
867       TRYOLD(gras_trp_chunk_send(sock, data, 
868                               sub_type->category.array_data.type->aligned_size[GRAS_THISARCH] 
869                                  * count * sub_type->category.array_data.fixed_size));
870        
871     } else {
872       for (cpt=0; cpt<count; cpt++) {
873         TRYOLD(gras_datadesc_gen_send_rec(sock,state,refs, sub_type, ptr, 
874                                        detect_cycle || sub_type->cycle));
875         ptr += elm_size;
876       }
877     }
878     break;
879   }
880
881   default:
882     xbt_assert0(0, "Invalid type");
883   }
884
885   return no_error;
886 }
887
888 /**
889  * gras_datadesc_gen_send:
890  *
891  * Copy the data pointed by src and described by type to the socket
892  *
893  */
894 xbt_error_t gras_datadesc_gen_send(gras_socket_t        sock, 
895                                    gras_datadesc_type_t type, 
896                                    void *src) {
897
898   xbt_error_t errcode;
899   gras_cbps_t  state;
900   xbt_dict_t  refs; /* all references already sent */
901  
902   refs = xbt_dict_new();
903   state = gras_cbps_new();
904    
905   printf("xbt_error_t gras_%s_send(gras_socket_t sock,void *dst){\n",
906          type->name);
907   errcode = gras_datadesc_gen_send_rec(sock,state,refs,type,(char*)src, 
908                                        detect_cycle || sub_type->cycle);
909   printf("}\n");
910   
911   xbt_dict_free(&refs);
912   gras_cbps_free(&state);
913
914   return errcode;
915 }
916
917 /**
918  * gras_datadesc_gen_recv_rec:
919  *
920  * Do the data reception job recursively.
921  *
922  * subsize used only to deal with vicious case of reference to dynamic array.
923  *  This size is needed at the reference reception level (to allocate enough 
924  * space) and at the array reception level (to fill enough room). 
925  * 
926  * Having this size passed as an argument of the recursive function is a crude
927  * hack, but I was told that working code is sometimes better than neat one ;)
928  */
929 static xbt_error_t
930 gras_datadesc_gen_recv_rec(gras_socket_t         sock, 
931                            gras_cbps_t           state,
932                            xbt_dict_t           refs,
933                            gras_datadesc_type_t  type,
934                            int                   r_arch,
935                            char                **r_data,
936                            long int              r_lgr,
937                            char                 *l_data,
938                            int                   subsize,
939                            int                   detect_cycle) {
940
941   xbt_error_t         errcode;
942   int                  cpt;
943   gras_datadesc_type_t sub_type;
944
945   VERB2("Recv a %s @%p", type->name, (void*)l_data);
946   xbt_assert(l_data);
947
948   switch (type->category_code) {
949   case e_gras_datadesc_type_cat_scalar:
950     if (type->size[GRAS_THISARCH] == type->size[r_arch]) {
951       TRYOLD(gras_trp_chunk_recv(sock, (char*)l_data, type->size[r_arch]));
952       if (r_arch != GRAS_THISARCH)
953         TRYOLD(gras_dd_convert_elm(type,1,r_arch, l_data,l_data));
954     } else {
955       void *ptr = xbt_malloc(type->size[r_arch]);
956
957       TRYOLD(gras_trp_chunk_recv(sock, (char*)ptr, type->size[r_arch]));
958       if (r_arch != GRAS_THISARCH)
959         TRYOLD(gras_dd_convert_elm(type,1,r_arch, ptr,l_data));
960       free(ptr);
961     }
962     break;
963
964   case e_gras_datadesc_type_cat_struct: {
965     gras_dd_cat_struct_t struct_data;
966     gras_dd_cat_field_t  field;
967
968     struct_data = type->category.struct_data;
969
970     xbt_assert1(struct_data.closed,
971                 "Please call gras_datadesc_declare_struct_close on %s before receiving it",
972                 type->name);
973     VERB1(">> Receive all fields of the structure %s",type->name);
974     xbt_dynar_foreach(struct_data.fields, cpt, field) {
975       char                 *field_data = l_data + field->offset[GRAS_THISARCH];
976
977       sub_type = field->type;
978
979       TRYOLD(gras_datadesc_gen_recv_rec(sock,state,refs, sub_type,
980                                      r_arch,NULL,0,
981                                      field_data,-1,
982                                      detect_cycle || sub_type->cycle));
983       if (field->recv)
984         field->recv(type,state,data);
985
986     }
987     VERB1("<< Received all fields of the structure %s", type->name);
988     
989     break;
990   }
991
992   case e_gras_datadesc_type_cat_union: {
993     gras_dd_cat_union_t union_data;
994     gras_dd_cat_field_t field=NULL;
995     int                 field_num;
996
997     union_data = type->category.union_data;
998
999     xbt_assert1(union_data.closed,
1000                 "Please call gras_datadesc_declare_union_close on %s before receiving it",
1001                 type->name);
1002     /* retrieve the field number */
1003     TRYOLD(gras_dd_recv_int(sock, r_arch, &field_num));
1004     if (field_num < 0)
1005       RAISE1(mismatch_error,
1006              "Received union field for %s is negative", type->name);
1007     if (field_num < xbt_dynar_length(union_data.fields)) 
1008       RAISE3(mismatch_error,
1009              "Received union field for %s is %d but there is only %lu fields",
1010              type->name, field_num, xbt_dynar_length(union_data.fields));
1011     
1012     /* Recv the content */
1013     field = xbt_dynar_get_as(union_data.fields, field_num, gras_dd_cat_field_t);
1014     sub_type = field->type;
1015     
1016     TRYOLD(gras_datadesc_gen_recv_rec(sock,state,refs, sub_type,
1017                                    r_arch,NULL,0,
1018                                    l_data,-1,
1019                                    detect_cycle || sub_type->cycle));
1020     if (field->recv)
1021         field->recv(type,state,data);
1022                   
1023     break;
1024   }
1025
1026   case e_gras_datadesc_type_cat_ref: {
1027     char             **r_ref = NULL;
1028     char             **l_ref = NULL;
1029     gras_dd_cat_ref_t  ref_data;
1030     
1031     ref_data = type->category.ref_data;
1032
1033     /* Get the referenced type locally or from peer */
1034     sub_type = ref_data.type;
1035     if (sub_type == NULL) {
1036       int ref_code;
1037       TRYOLD(gras_dd_recv_int(sock, r_arch, &ref_code));
1038       TRYOLD(gras_datadesc_by_id(ref_code, &sub_type));
1039     }
1040
1041     /* Get the actual value of the pointer for cycle handling */
1042     if (!pointer_type) {
1043       pointer_type = gras_datadesc_by_name("data pointer");
1044       xbt_assert(pointer_type);
1045     }
1046
1047     r_ref = xbt_malloc(pointer_type->size[r_arch]);
1048
1049     TRYOLD(gras_trp_chunk_recv(sock, (char*)r_ref,
1050                             pointer_type->size[r_arch]));
1051
1052     /* Receive the pointed data only if not already sent */
1053     if (gras_dd_is_r_null(r_ref, pointer_type->size[r_arch])) {
1054       VERB1("Not receiving data remotely referenced @%p since it's NULL",
1055             *(void **)r_ref);
1056       *(void**)l_data = NULL;
1057       free(r_ref);
1058       break;
1059     }
1060          
1061     errcode = detect_cycle
1062             ? xbt_dict_get_ext(refs,
1063                                 (char*)r_ref, pointer_type->size[r_arch],
1064                                 (void**)&l_ref)
1065             : mismatch_error;
1066
1067     if (errcode == mismatch_error) {
1068       int subsubcount = 0;
1069       void *l_referenced=NULL;
1070
1071       VERB2("Receiving a ref to '%s', remotely @%p",
1072             sub_type->name, *(void**)r_ref);
1073       if (sub_type->category_code == e_gras_datadesc_type_cat_array) {
1074         /* Damn. Reference to a dynamic array. Allocating the size for it 
1075            is more complicated */
1076         gras_dd_cat_array_t array_data = sub_type->category.array_data;
1077         gras_datadesc_type_t subsub_type;
1078
1079         subsubcount = array_data.fixed_size;
1080         if (subsubcount == 0)
1081           TRYOLD(gras_dd_recv_int(sock, r_arch, &subsubcount));
1082
1083         subsub_type = array_data.type;
1084
1085
1086         TRYOLD(gras_dd_alloc_ref(refs,
1087                               subsub_type->size[GRAS_THISARCH] * subsubcount, 
1088                               r_ref,pointer_type->size[r_arch], 
1089                               (char**)&l_referenced,
1090                               detect_cycle));
1091       } else {
1092         TRYOLD(gras_dd_alloc_ref(refs,sub_type->size[GRAS_THISARCH], 
1093                               r_ref,pointer_type->size[r_arch], 
1094                               (char**)&l_referenced,
1095                               detect_cycle));
1096       }
1097
1098       TRYOLD(gras_datadesc_gen_recv_rec(sock,state,refs, sub_type,
1099                                      r_arch,r_ref,pointer_type->size[r_arch],
1100                                      (char*)l_referenced, subsubcount,
1101                                      detect_cycle || sub_type->cycle));
1102                                      
1103       *(void**)l_data=l_referenced;
1104       VERB3("'%s' remotely referenced at %p locally at %p",
1105             sub_type->name, *(void**)r_ref, l_referenced);
1106       
1107     } else if (errcode == no_error) {
1108       VERB2("NOT receiving data remotely referenced @%p (already done, @%p here)",
1109             *(void**)r_ref, *(void**)l_ref);
1110
1111       *(void**)l_data=*l_ref;
1112
1113     } else {
1114       return errcode;
1115     }
1116     free(r_ref);
1117     break;
1118   }
1119
1120   case e_gras_datadesc_type_cat_array: {
1121     gras_dd_cat_array_t    array_data;
1122     int       count;
1123     char     *ptr;
1124     long int  elm_size;
1125
1126     array_data = type->category.array_data;
1127     /* determine element count locally, or from caller, or from peer */
1128     count = array_data.fixed_size;
1129     if (count == 0)
1130       count = subsize;
1131     if (count == 0)
1132       TRYOLD(gras_dd_recv_int(sock, r_arch, &count));
1133     if (count == 0)
1134       RAISE1(mismatch_error,
1135              "Invalid (=0) array size for type %s",type->name);
1136
1137     /* receive the content */
1138     sub_type = array_data.type;
1139     if (sub_type->category_code == e_gras_datadesc_type_cat_scalar) {
1140       VERB1("Array of %d scalars, get it in one shoot", count);
1141       if (sub_type->aligned_size[GRAS_THISARCH] >= 
1142           sub_type->aligned_size[r_arch]) {
1143         TRYOLD(gras_trp_chunk_recv(sock, (char*)l_data, 
1144                                 sub_type->aligned_size[r_arch] * count));
1145         if (r_arch != GRAS_THISARCH)
1146           TRYOLD(gras_dd_convert_elm(sub_type,count,r_arch, l_data,l_data));
1147       } else {
1148         ptr = xbt_malloc(sub_type->aligned_size[r_arch] * count);
1149
1150         TRYOLD(gras_trp_chunk_recv(sock, (char*)ptr, 
1151                                 sub_type->size[r_arch] * count));
1152         if (r_arch != GRAS_THISARCH)
1153           TRYOLD(gras_dd_convert_elm(sub_type,count,r_arch, ptr,l_data));
1154         free(ptr);
1155       }
1156     } else if (sub_type->category_code == e_gras_datadesc_type_cat_array &&
1157                sub_type->category.array_data.fixed_size > 0 &&
1158                sub_type->category.array_data.type->category_code == e_gras_datadesc_type_cat_scalar) {
1159       gras_datadesc_type_t subsub_type;
1160       array_data = sub_type->category.array_data;
1161       subsub_type = array_data.type;
1162        
1163       VERB1("Array of %d fixed array of scalars, get it in one shot",count);
1164       if (subsub_type->aligned_size[GRAS_THISARCH] >= 
1165           subsub_type->aligned_size[r_arch]) {
1166         TRYOLD(gras_trp_chunk_recv(sock, (char*)l_data, 
1167                                 subsub_type->aligned_size[r_arch] * count * 
1168                                   array_data.fixed_size));
1169         if (r_arch != GRAS_THISARCH)
1170           TRYOLD(gras_dd_convert_elm(subsub_type,count*array_data.fixed_size,r_arch, l_data,l_data));
1171       } else {
1172         ptr = xbt_malloc(subsub_type->aligned_size[r_arch] * count*array_data.fixed_size);
1173
1174         TRYOLD(gras_trp_chunk_recv(sock, (char*)ptr, 
1175                                 subsub_type->size[r_arch] * count*array_data.fixed_size));
1176         if (r_arch != GRAS_THISARCH)
1177           TRYOLD(gras_dd_convert_elm(subsub_type,count*array_data.fixed_size,r_arch, ptr,l_data));
1178         free(ptr);
1179       }
1180       
1181        
1182     } else {
1183       /* not scalar content, get it recursively (may contain pointers) */
1184       elm_size = sub_type->aligned_size[GRAS_THISARCH];
1185       VERB2("Receive a %d-long array of %s",count, sub_type->name);
1186
1187       ptr = l_data;
1188       for (cpt=0; cpt<count; cpt++) {
1189         TRYOLD(gras_datadesc_gen_recv_rec(sock,state,refs, sub_type,
1190                                        r_arch, NULL, 0, ptr,-1,
1191                                        detect_cycle || sub_type->cycle));
1192                                       
1193         ptr += elm_size;
1194       }
1195     }
1196     break;
1197   }
1198         
1199   default:
1200     xbt_assert0(0, "Invalid type");
1201   }
1202   
1203   if (type->recv)
1204     type->recv(type,state,l_data);
1205
1206   return no_error;
1207 }
1208
1209 /**
1210  * gras_datadesc_gen_recv:
1211  *
1212  * Get an instance of the datatype described by @type from the @socket, 
1213  * and store a pointer to it in @dst
1214  *
1215  */
1216 xbt_error_t
1217 gras_datadesc_gen_recv(gras_socket_t         sock, 
1218                        gras_datadesc_type_t  type,
1219                        int                   r_arch,
1220                        void                 *dst) {
1221
1222   xbt_error_t errcode;
1223   gras_cbps_t  state; /* callback persistent state */
1224   xbt_dict_t  refs;  /* all references already sent */
1225
1226   refs = xbt_dict_new();
1227   state = gras_cbps_new();
1228
1229   printf("xbt_error_t gras_%s_recv(gras_socket_t sock,void *dst){\n",
1230          type->name);
1231    
1232   errcode = gras_datadesc_gen_recv_rec(sock, state, refs, type, 
1233                                        r_arch, NULL, 0,
1234                                        (char *) dst,-1, 
1235                                        sub_type->cycle); 
1236
1237   printf("}\n");
1238   xbt_dict_free(&refs);
1239   gras_cbps_free(&state);
1240
1241   return errcode;
1242 }
1243 #endif