Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ONGOING work on exceptions plus minor cleanups.
[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         reference_is_to_send = 1;
265         xbt_ex_free(e);
266       } else {
267         RETHROW;
268       }
269     }
270
271     if (reference_is_to_send) {
272        VERB1("Sending data referenced at %p", (void*)*ref);
273        if (detect_cycle)
274          xbt_dict_set_ext(refs, (char*)ref, sizeof(void*), ref, NULL);
275        gras_datadesc_send_rec(sock,state,refs, sub_type, *ref, 
276                               detect_cycle || sub_type->cycle);
277           
278     } else {
279        VERB1("Not sending data referenced at %p (already done)", (void*)*ref);
280     } 
281     
282     break;
283   }
284
285   case e_gras_datadesc_type_cat_array: {
286     gras_dd_cat_array_t    array_data;
287     long int               count;
288     char                  *ptr=data;
289     long int               elm_size;
290     
291     array_data = type->category.array_data;
292     
293     /* determine and send the element count */
294     count = array_data.fixed_size;
295     if (count == 0) {
296       count = array_data.dynamic_size(type,state,data);
297       xbt_assert1(count >=0,
298                    "Invalid (negative) array size for type %s",type->name);
299       gras_dd_send_int(sock, count);
300     }
301     
302     /* send the content */
303     sub_type = array_data.type;
304     elm_size = sub_type->aligned_size[GRAS_THISARCH];
305     if (sub_type->category_code == e_gras_datadesc_type_cat_scalar) {
306       VERB1("Array of %ld scalars, send it in one shot",count);
307       gras_trp_chunk_send(sock, data, 
308                           sub_type->aligned_size[GRAS_THISARCH] * count);
309     } else if (sub_type->category_code == e_gras_datadesc_type_cat_array &&
310                sub_type->category.array_data.fixed_size > 0 &&
311                sub_type->category.array_data.type->category_code == e_gras_datadesc_type_cat_scalar) {
312        
313       VERB1("Array of %ld fixed array of scalars, send it in one shot",count);
314       gras_trp_chunk_send(sock, data, 
315                           sub_type->category.array_data.type->aligned_size[GRAS_THISARCH] 
316                           * count * sub_type->category.array_data.fixed_size);
317        
318     } else {
319       for (cpt=0; cpt<count; cpt++) {
320         gras_datadesc_send_rec(sock,state,refs, sub_type, ptr, 
321                                detect_cycle || sub_type->cycle);
322         ptr += elm_size;
323       }
324     }
325     break;
326   }
327
328   default:
329     xbt_assert0(0, "Invalid type");
330   }
331 }
332
333 /**
334  * gras_datadesc_send:
335  *
336  * Copy the data pointed by src and described by type to the socket
337  *
338  */
339 void gras_datadesc_send(gras_socket_t        sock, 
340                         gras_datadesc_type_t type, 
341                         void *src) {
342
343   xbt_ex_t e;
344   gras_cbps_t  state;
345   xbt_dict_t  refs; /* all references already sent */
346  
347   xbt_assert0(type,"called with NULL type descriptor");
348
349   refs = xbt_dict_new();
350   state = gras_cbps_new();
351   
352   TRY {
353     gras_datadesc_send_rec(sock,state,refs,type,(char*)src, type->cycle);
354   } CLEANUP {
355     xbt_dict_free(&refs);
356     gras_cbps_free(&state);
357   } CATCH(e) {
358     RETHROW;
359   }
360 }
361
362 /**
363  * gras_datadesc_recv_rec:
364  *
365  * Do the data reception job recursively.
366  *
367  * subsize used only to deal with vicious case of reference to dynamic array.
368  *  This size is needed at the reference reception level (to allocate enough 
369  * space) and at the array reception level (to fill enough room). 
370  * 
371  * Having this size passed as an argument of the recursive function is a crude
372  * hack, but I was told that working code is sometimes better than neat one ;)
373  */
374 static void
375 gras_datadesc_recv_rec(gras_socket_t         sock, 
376                        gras_cbps_t           state,
377                        xbt_dict_t           refs,
378                        gras_datadesc_type_t  type,
379                        int                   r_arch,
380                        char                **r_data,
381                        long int              r_lgr,
382                        char                 *l_data,
383                        int                   subsize,
384                        int                   detect_cycle) {
385
386   int                  cpt;
387   gras_datadesc_type_t sub_type;
388   xbt_ex_t e;
389
390   VERB2("Recv a %s @%p", type->name, (void*)l_data);
391   xbt_assert(l_data);
392
393   switch (type->category_code) {
394   case e_gras_datadesc_type_cat_scalar:
395     if (type->size[GRAS_THISARCH] == type->size[r_arch]) {
396       gras_trp_chunk_recv(sock, (char*)l_data, type->size[r_arch]);
397       if (r_arch != GRAS_THISARCH)
398         gras_dd_convert_elm(type,1,r_arch, l_data,l_data);
399     } else {
400       void *ptr = xbt_malloc(type->size[r_arch]);
401
402       gras_trp_chunk_recv(sock, (char*)ptr, type->size[r_arch]);
403       if (r_arch != GRAS_THISARCH)
404         gras_dd_convert_elm(type,1,r_arch, ptr,l_data);
405       free(ptr);
406     }
407     break;
408
409   case e_gras_datadesc_type_cat_struct: {
410     gras_dd_cat_struct_t struct_data;
411     gras_dd_cat_field_t  field;
412
413     struct_data = type->category.struct_data;
414
415     xbt_assert1(struct_data.closed,
416                 "Please call gras_datadesc_declare_struct_close on %s before receiving it",
417                 type->name);
418     VERB1(">> Receive all fields of the structure %s",type->name);
419     xbt_dynar_foreach(struct_data.fields, cpt, field) {
420       char                 *field_data = l_data + field->offset[GRAS_THISARCH];
421
422       sub_type = field->type;
423
424       gras_datadesc_recv_rec(sock,state,refs, sub_type,
425                              r_arch,NULL,0,
426                              field_data,-1, 
427                              detect_cycle || sub_type->cycle);
428        
429       if (field->recv)
430         field->recv(type,state,(void*)l_data);
431     
432     }
433     VERB1("<< Received all fields of the structure %s", type->name);
434     
435     break;
436   }
437
438   case e_gras_datadesc_type_cat_union: {
439     gras_dd_cat_union_t union_data;
440     gras_dd_cat_field_t field=NULL;
441     int                 field_num;
442
443     union_data = type->category.union_data;
444
445     xbt_assert1(union_data.closed,
446                 "Please call gras_datadesc_declare_union_close on %s before receiving it",
447                 type->name);
448     /* retrieve the field number */
449     gras_dd_recv_int(sock, r_arch, &field_num);
450     if (field_num < 0)
451       THROW1(mismatch_error,0,
452              "Received union field for %s is negative", type->name);
453     if (field_num < xbt_dynar_length(union_data.fields)) 
454       THROW3(mismatch_error,0,
455              "Received union field for %s is said to be #%d but there is only %lu fields",
456              type->name, field_num, xbt_dynar_length(union_data.fields));
457     
458     /* Recv the content */
459     field = xbt_dynar_get_as(union_data.fields, field_num, gras_dd_cat_field_t);
460     sub_type = field->type;
461     
462     gras_datadesc_recv_rec(sock,state,refs, sub_type,
463                            r_arch,NULL,0,
464                            l_data,-1,
465                            detect_cycle || sub_type->cycle);
466     if (field->recv)
467        field->recv(type,state,l_data);
468
469     break;
470   }
471
472   case e_gras_datadesc_type_cat_ref: {
473     char             **r_ref = NULL;
474     char             **l_ref = NULL;
475     gras_dd_cat_ref_t  ref_data;
476     int reference_is_to_recv = 0;
477     
478     ref_data = type->category.ref_data;
479
480     /* Get the referenced type locally or from peer */
481     sub_type = ref_data.type;
482     if (sub_type == NULL) {
483       int ref_code;
484       gras_dd_recv_int(sock, r_arch, &ref_code);
485       sub_type = gras_datadesc_by_id(ref_code);
486     }
487
488     /* Get the actual value of the pointer for cycle handling */
489     if (!pointer_type) {
490       pointer_type = gras_datadesc_by_name("data pointer");
491       xbt_assert(pointer_type);
492     }
493
494     r_ref = xbt_malloc(pointer_type->size[r_arch]);
495
496     gras_trp_chunk_recv(sock, (char*)r_ref,
497                         pointer_type->size[r_arch]);
498
499     /* Receive the pointed data only if not already sent */
500     if (gras_dd_is_r_null(r_ref, pointer_type->size[r_arch])) {
501       VERB1("Not receiving data remotely referenced @%p since it's NULL",
502             *(void **)r_ref);
503       *(void**)l_data = NULL;
504       free(r_ref);
505       break;
506     }
507          
508     reference_is_to_recv = 0;
509     TRY {
510       if (detect_cycle)
511         l_ref = xbt_dict_get_ext(refs, (char*)r_ref, pointer_type->size[r_arch]);
512       else 
513         reference_is_to_recv = 1;
514     } CATCH(e) {
515       if (e.category == mismatch_error) {
516         reference_is_to_recv = 1;
517         xbt_ex_free(e);
518       } else {
519         RETHROW;
520       }
521     }
522     if (reference_is_to_recv) {
523       int subsubcount = 0;
524       void *l_referenced=NULL;
525
526       VERB2("Receiving a ref to '%s', remotely @%p",
527             sub_type->name, *(void**)r_ref);
528       if (sub_type->category_code == e_gras_datadesc_type_cat_array) {
529         /* Damn. Reference to a dynamic array. Allocating the space for it 
530            is more complicated */
531         gras_dd_cat_array_t array_data = sub_type->category.array_data;
532         gras_datadesc_type_t subsub_type;
533
534         subsubcount = array_data.fixed_size;
535         if (subsubcount == 0)
536           gras_dd_recv_int(sock, r_arch, &subsubcount);
537
538         subsub_type = array_data.type;
539
540
541         gras_dd_alloc_ref(refs,
542                           subsub_type->size[GRAS_THISARCH] * subsubcount, 
543                           r_ref,pointer_type->size[r_arch], 
544                           (char**)&l_referenced,
545                           detect_cycle);
546       } else {
547         gras_dd_alloc_ref(refs,sub_type->size[GRAS_THISARCH], 
548                           r_ref,pointer_type->size[r_arch], 
549                           (char**)&l_referenced,
550                           detect_cycle);
551       }
552
553       gras_datadesc_recv_rec(sock,state,refs, sub_type,
554                              r_arch,r_ref,pointer_type->size[r_arch],
555                              (char*)l_referenced, subsubcount,
556                              detect_cycle || sub_type->cycle);
557                                
558       *(void**)l_data=l_referenced;
559       VERB3("'%s' remotely referenced at %p locally at %p",
560             sub_type->name, *(void**)r_ref, l_referenced);
561       
562     } else {
563       VERB2("NOT receiving data remotely referenced @%p (already done, @%p here)",
564             *(void**)r_ref, *(void**)l_ref);
565
566       *(void**)l_data=*l_ref;
567
568     } 
569     free(r_ref);
570     break;
571   }
572
573   case e_gras_datadesc_type_cat_array: {
574     gras_dd_cat_array_t    array_data;
575     int       count;
576     char     *ptr;
577     long int  elm_size;
578
579     array_data = type->category.array_data;
580     /* determine element count locally, or from caller, or from peer */
581     count = array_data.fixed_size;
582     if (count == 0)
583       count = subsize;
584     if (count == 0)
585       gras_dd_recv_int(sock, r_arch, &count);
586     if (count == 0)
587       THROW1(mismatch_error,0,
588              "Invalid (=0) array size for type %s",type->name);
589
590     /* receive the content */
591     sub_type = array_data.type;
592     if (sub_type->category_code == e_gras_datadesc_type_cat_scalar) {
593       VERB1("Array of %d scalars, get it in one shoot", count);
594       if (sub_type->aligned_size[GRAS_THISARCH] >= 
595           sub_type->aligned_size[r_arch]) {
596         gras_trp_chunk_recv(sock, (char*)l_data, 
597                             sub_type->aligned_size[r_arch] * count);
598         if (r_arch != GRAS_THISARCH)
599           gras_dd_convert_elm(sub_type,count,r_arch, l_data,l_data);
600       } else {
601         ptr = xbt_malloc(sub_type->aligned_size[r_arch] * count);
602
603         gras_trp_chunk_recv(sock, (char*)ptr, 
604                             sub_type->size[r_arch] * count);
605         if (r_arch != GRAS_THISARCH)
606           gras_dd_convert_elm(sub_type,count,r_arch, ptr,l_data);
607         free(ptr);
608       }
609     } else if (sub_type->category_code == e_gras_datadesc_type_cat_array &&
610                sub_type->category.array_data.fixed_size > 0 &&
611                sub_type->category.array_data.type->category_code == e_gras_datadesc_type_cat_scalar) {
612       gras_datadesc_type_t subsub_type;
613       array_data = sub_type->category.array_data;
614       subsub_type = array_data.type;
615        
616       VERB1("Array of %d fixed array of scalars, get it in one shot",count);
617       if (subsub_type->aligned_size[GRAS_THISARCH] >= 
618           subsub_type->aligned_size[r_arch]) {
619         gras_trp_chunk_recv(sock, (char*)l_data, 
620                             subsub_type->aligned_size[r_arch] * count * 
621                             array_data.fixed_size);
622         if (r_arch != GRAS_THISARCH)
623           gras_dd_convert_elm(subsub_type,count*array_data.fixed_size,r_arch, l_data,l_data);
624       } else {
625         ptr = xbt_malloc(subsub_type->aligned_size[r_arch] * count*array_data.fixed_size);
626
627         gras_trp_chunk_recv(sock, (char*)ptr, 
628                             subsub_type->size[r_arch] * count*array_data.fixed_size);
629         if (r_arch != GRAS_THISARCH)
630           gras_dd_convert_elm(subsub_type,count*array_data.fixed_size,r_arch, ptr,l_data);
631         free(ptr);
632       }
633       
634        
635     } else {
636       /* not scalar content, get it recursively (may contain pointers) */
637       elm_size = sub_type->aligned_size[GRAS_THISARCH];
638       VERB2("Receive a %d-long array of %s",count, sub_type->name);
639
640       ptr = l_data;
641       for (cpt=0; cpt<count; cpt++) {
642         gras_datadesc_recv_rec(sock,state,refs, sub_type,
643                                r_arch, NULL, 0, ptr,-1,
644                                detect_cycle || sub_type->cycle);
645                                    
646         ptr += elm_size;
647       }
648     }
649     break;
650   }
651         
652   default:
653     xbt_assert0(0, "Invalid type");
654   }
655   
656   if (type->recv)
657     type->recv(type,state,l_data);
658
659 }
660
661 /**
662  * gras_datadesc_recv:
663  *
664  * Get an instance of the datatype described by @type from the @socket, 
665  * and store a pointer to it in @dst
666  *
667  */
668 void
669 gras_datadesc_recv(gras_socket_t         sock, 
670                    gras_datadesc_type_t  type,
671                    int                   r_arch,
672                    void                 *dst) {
673
674   xbt_ex_t e;
675   gras_cbps_t  state; /* callback persistent state */
676   xbt_dict_t  refs;  /* all references already sent */
677
678   refs = xbt_dict_new();
679   state = gras_cbps_new();
680
681   xbt_assert0(type,"called with NULL type descriptor");
682   TRY {
683     gras_datadesc_recv_rec(sock, state, refs, type, 
684                            r_arch, NULL, 0,
685                            (char *) dst,-1, 
686                            type->cycle);
687   } CLEANUP {
688     xbt_dict_free(&refs);
689     gras_cbps_free(&state);
690   } CATCH(e) {
691     RETHROW;
692   }
693 }
694
695 #if 0
696 /***
697  *** IDL compiling functions
698  ***/
699
700 #define gras_datadesc_send_rec foo /* Just to make sure the copypast was ok */
701 #define gras_datadesc_send     foo /* Just to make sure the copypast was ok */
702 #define gras_datadesc_recv_rec foo /* Just to make sure the copypast was ok */
703 #define gras_datadesc_recv     foo /* Just to make sure the copypast was ok */
704
705 static xbt_error_t 
706 gras_datadesc_gen_send_rec(gras_socket_t         sock,
707                            gras_cbps_t           state,
708                            xbt_dict_t           refs,
709                            gras_datadesc_type_t  type, 
710                            char                 *data,
711                            int                   detect_cycle) {
712
713   xbt_error_t         errcode;
714   int                  cpt;
715   gras_datadesc_type_t sub_type; /* type on which we recurse */
716   
717   printf("  VERB2(\"Send a %s (%s)\");\n", 
718          type->name, gras_datadesc_cat_names[type->category_code]);
719
720   xbt_assert0(!type->send, "Callbacks not implemented in IDL compiler");
721
722   switch (type->category_code) {
723   case e_gras_datadesc_type_cat_scalar:
724     printf("  TRYOLD(gras_trp_chunk_send(sock, data, %lu));\n",type->size[GRAS_THISARCH]);
725     break;
726
727   case e_gras_datadesc_type_cat_struct: {
728     gras_dd_cat_struct_t struct_data;
729     gras_dd_cat_field_t  field;
730     char                *field_data;
731     
732     struct_data = type->category.struct_data;
733     xbt_assert1(struct_data.closed,
734       "Please call gras_datadesc_declare_struct_close on %s before sending it",
735                 type->name);
736     printf("  VERB1(\">> Send all fields of the structure %s\");\n",type->name);
737     xbt_dynar_foreach(struct_data.fields, cpt, field) {
738       field_data = data;
739       field_data += field->offset[GRAS_THISARCH];
740       
741       sub_type = field->type;
742       
743       xbt_assert0(!field->send, "Callbacks not implemented in IDL compiler");
744       
745       printf("  VERB1(\"Send field %s\");\n",field->name);
746       printf("  data += %lu;\n",field->offset[GRAS_THISARCH]);
747       TRYOLD(gras_datadesc_gen_send_rec(sock,state,refs,sub_type, field_data, 
748                                      detect_cycle || sub_type->cycle));
749       printf("  data -= %lu;\n",field->offset[GRAS_THISARCH]);
750       
751       xbt_assert0(!field->recv, "Callbacks not implemented in IDL compiler");
752     }
753     printf("  VERB1(\"<< Sent all fields of the structure %s\"", type->name);
754     
755     break;
756   }
757
758   case e_gras_datadesc_type_cat_union: {
759     gras_dd_cat_union_t union_data;
760     gras_dd_cat_field_t field=NULL;
761     int                 field_num;
762     
763     union_data = type->category.union_data;
764     
765     xbt_assert1(union_data.closed,
766                 "Please call gras_datadesc_declare_union_close on %s before sending it",
767                 type->name);
768     /* retrieve the field number */
769     printf("  field_num = union_data.selector(state, data);\n");
770     
771     printf("  xbt_assert0(field_num > 0,\n");
772     printf("              \"union field selector of %s gave a negative value\");\n",type->name);
773     
774     printf("  xbt_assert3(field_num < xbt_dynar_length(union_data.fields),\n");
775     printf("              \"union field selector of %s returned %%d but there is only %lu fields\",field_num);\n",
776                  type->name, xbt_dynar_length(union_data.fields));
777
778     /* Send the field number */
779     printf("TRYOLD(gras_dd_send_int(sock, field_num));\n");
780     
781     /* Send the content */
782     field = xbt_dynar_get_as(union_data.fields, field_num, gras_dd_cat_field_t);
783     sub_type = field->type;
784     
785     if (field->send)
786       field->send(state,data);
787     
788     TRYOLD(gras_datadesc_gen_send_rec(sock,state,refs, sub_type, data,
789                                    detect_cycle || sub_type->cycle));
790            
791     break;
792   }
793     
794   case e_gras_datadesc_type_cat_ref: {
795     gras_dd_cat_ref_t      ref_data;
796
797     void                 **ref=(void**)data;
798     void *dummy;
799     
800     ref_data = type->category.ref_data;
801     
802     /* Detect the referenced type and send it to peer if needed */
803     sub_type = ref_data.type;
804     if (sub_type == NULL) {
805       sub_type = (*ref_data.selector)(state,data);
806       TRYOLD(gras_dd_send_int(sock, sub_type->code));
807     }
808     
809     /* Send the actual value of the pointer for cycle handling */
810     if (!pointer_type) {
811       pointer_type = gras_datadesc_by_name("data pointer");
812       xbt_assert(pointer_type);
813     }
814      
815     TRYOLD(gras_trp_chunk_send(sock, (char*)data,
816                             pointer_type->size[GRAS_THISARCH]));
817     
818     /* Send the pointed data only if not already sent */
819     if (*(void**)data == NULL) {
820       VERB0("Not sending NULL referenced data");
821       break;
822     }
823     errcode = detect_cycle 
824             ? xbt_dict_get_ext(refs,(char*)ref, sizeof(void*), &dummy)
825             : mismatch_error;
826     if (errcode == mismatch_error) {
827        VERB1("Sending data referenced at %p", (void*)*ref);
828        if (detect_cycle)
829          xbt_dict_set_ext(refs, (char*)ref, sizeof(void*), ref, NULL);
830        TRYOLD(gras_datadesc_gen_send_rec(sock,state,refs, sub_type, *ref, 
831                                       detect_cycle || sub_type->cycle));
832           
833     } else if (errcode == no_error) {
834        VERB1("Not sending data referenced at %p (already done)", (void*)*ref);
835     } else {
836        return errcode;
837     }
838     
839     break;
840   }
841
842   case e_gras_datadesc_type_cat_array: {
843     gras_dd_cat_array_t    array_data;
844     long int               count;
845     char                  *ptr=data;
846     long int               elm_size;
847     
848     array_data = type->category.array_data;
849     
850     /* determine and send the element count */
851     count = array_data.fixed_size;
852     if (count == 0) {
853       count = array_data.dynamic_size(state,data);
854       xbt_assert1(count >=0,
855                    "Invalid (negative) array size for type %s",type->name);
856       TRYOLD(gras_dd_send_int(sock, count));
857     }
858     
859     /* send the content */
860     sub_type = array_data.type;
861     elm_size = sub_type->aligned_size[GRAS_THISARCH];
862     if (sub_type->category_code == e_gras_datadesc_type_cat_scalar) {
863       VERB1("Array of %ld scalars, send it in one shot",count);
864       TRYOLD(gras_trp_chunk_send(sock, data, 
865                               sub_type->aligned_size[GRAS_THISARCH] * count));
866     } else if (sub_type->category_code == e_gras_datadesc_type_cat_array &&
867                sub_type->category.array_data.fixed_size > 0 &&
868                sub_type->category.array_data.type->category_code == e_gras_datadesc_type_cat_scalar) {
869        
870       VERB1("Array of %ld fixed array of scalars, send it in one shot",count);
871       TRYOLD(gras_trp_chunk_send(sock, data, 
872                               sub_type->category.array_data.type->aligned_size[GRAS_THISARCH] 
873                                  * count * sub_type->category.array_data.fixed_size));
874        
875     } else {
876       for (cpt=0; cpt<count; cpt++) {
877         TRYOLD(gras_datadesc_gen_send_rec(sock,state,refs, sub_type, ptr, 
878                                        detect_cycle || sub_type->cycle));
879         ptr += elm_size;
880       }
881     }
882     break;
883   }
884
885   default:
886     xbt_assert0(0, "Invalid type");
887   }
888
889   return no_error;
890 }
891
892 /**
893  * gras_datadesc_gen_send:
894  *
895  * Copy the data pointed by src and described by type to the socket
896  *
897  */
898 xbt_error_t gras_datadesc_gen_send(gras_socket_t        sock, 
899                                    gras_datadesc_type_t type, 
900                                    void *src) {
901
902   xbt_error_t errcode;
903   gras_cbps_t  state;
904   xbt_dict_t  refs; /* all references already sent */
905  
906   refs = xbt_dict_new();
907   state = gras_cbps_new();
908    
909   printf("xbt_error_t gras_%s_send(gras_socket_t sock,void *dst){\n",
910          type->name);
911   errcode = gras_datadesc_gen_send_rec(sock,state,refs,type,(char*)src, 
912                                        detect_cycle || sub_type->cycle);
913   printf("}\n");
914   
915   xbt_dict_free(&refs);
916   gras_cbps_free(&state);
917
918   return errcode;
919 }
920
921 /**
922  * gras_datadesc_gen_recv_rec:
923  *
924  * Do the data reception job recursively.
925  *
926  * subsize used only to deal with vicious case of reference to dynamic array.
927  *  This size is needed at the reference reception level (to allocate enough 
928  * space) and at the array reception level (to fill enough room). 
929  * 
930  * Having this size passed as an argument of the recursive function is a crude
931  * hack, but I was told that working code is sometimes better than neat one ;)
932  */
933 static xbt_error_t
934 gras_datadesc_gen_recv_rec(gras_socket_t         sock, 
935                            gras_cbps_t           state,
936                            xbt_dict_t           refs,
937                            gras_datadesc_type_t  type,
938                            int                   r_arch,
939                            char                **r_data,
940                            long int              r_lgr,
941                            char                 *l_data,
942                            int                   subsize,
943                            int                   detect_cycle) {
944
945   xbt_error_t         errcode;
946   int                  cpt;
947   gras_datadesc_type_t sub_type;
948
949   VERB2("Recv a %s @%p", type->name, (void*)l_data);
950   xbt_assert(l_data);
951
952   switch (type->category_code) {
953   case e_gras_datadesc_type_cat_scalar:
954     if (type->size[GRAS_THISARCH] == type->size[r_arch]) {
955       TRYOLD(gras_trp_chunk_recv(sock, (char*)l_data, type->size[r_arch]));
956       if (r_arch != GRAS_THISARCH)
957         TRYOLD(gras_dd_convert_elm(type,1,r_arch, l_data,l_data));
958     } else {
959       void *ptr = xbt_malloc(type->size[r_arch]);
960
961       TRYOLD(gras_trp_chunk_recv(sock, (char*)ptr, type->size[r_arch]));
962       if (r_arch != GRAS_THISARCH)
963         TRYOLD(gras_dd_convert_elm(type,1,r_arch, ptr,l_data));
964       free(ptr);
965     }
966     break;
967
968   case e_gras_datadesc_type_cat_struct: {
969     gras_dd_cat_struct_t struct_data;
970     gras_dd_cat_field_t  field;
971
972     struct_data = type->category.struct_data;
973
974     xbt_assert1(struct_data.closed,
975                 "Please call gras_datadesc_declare_struct_close on %s before receiving it",
976                 type->name);
977     VERB1(">> Receive all fields of the structure %s",type->name);
978     xbt_dynar_foreach(struct_data.fields, cpt, field) {
979       char                 *field_data = l_data + field->offset[GRAS_THISARCH];
980
981       sub_type = field->type;
982
983       TRYOLD(gras_datadesc_gen_recv_rec(sock,state,refs, sub_type,
984                                      r_arch,NULL,0,
985                                      field_data,-1,
986                                      detect_cycle || sub_type->cycle));
987       if (field->recv)
988         field->recv(type,state,data);
989
990     }
991     VERB1("<< Received all fields of the structure %s", type->name);
992     
993     break;
994   }
995
996   case e_gras_datadesc_type_cat_union: {
997     gras_dd_cat_union_t union_data;
998     gras_dd_cat_field_t field=NULL;
999     int                 field_num;
1000
1001     union_data = type->category.union_data;
1002
1003     xbt_assert1(union_data.closed,
1004                 "Please call gras_datadesc_declare_union_close on %s before receiving it",
1005                 type->name);
1006     /* retrieve the field number */
1007     TRYOLD(gras_dd_recv_int(sock, r_arch, &field_num));
1008     if (field_num < 0)
1009       RAISE1(mismatch_error,
1010              "Received union field for %s is negative", type->name);
1011     if (field_num < xbt_dynar_length(union_data.fields)) 
1012       RAISE3(mismatch_error,
1013              "Received union field for %s is %d but there is only %lu fields",
1014              type->name, field_num, xbt_dynar_length(union_data.fields));
1015     
1016     /* Recv the content */
1017     field = xbt_dynar_get_as(union_data.fields, field_num, gras_dd_cat_field_t);
1018     sub_type = field->type;
1019     
1020     TRYOLD(gras_datadesc_gen_recv_rec(sock,state,refs, sub_type,
1021                                    r_arch,NULL,0,
1022                                    l_data,-1,
1023                                    detect_cycle || sub_type->cycle));
1024     if (field->recv)
1025         field->recv(type,state,data);
1026                   
1027     break;
1028   }
1029
1030   case e_gras_datadesc_type_cat_ref: {
1031     char             **r_ref = NULL;
1032     char             **l_ref = NULL;
1033     gras_dd_cat_ref_t  ref_data;
1034     
1035     ref_data = type->category.ref_data;
1036
1037     /* Get the referenced type locally or from peer */
1038     sub_type = ref_data.type;
1039     if (sub_type == NULL) {
1040       int ref_code;
1041       TRYOLD(gras_dd_recv_int(sock, r_arch, &ref_code));
1042       TRYOLD(gras_datadesc_by_id(ref_code, &sub_type));
1043     }
1044
1045     /* Get the actual value of the pointer for cycle handling */
1046     if (!pointer_type) {
1047       pointer_type = gras_datadesc_by_name("data pointer");
1048       xbt_assert(pointer_type);
1049     }
1050
1051     r_ref = xbt_malloc(pointer_type->size[r_arch]);
1052
1053     TRYOLD(gras_trp_chunk_recv(sock, (char*)r_ref,
1054                             pointer_type->size[r_arch]));
1055
1056     /* Receive the pointed data only if not already sent */
1057     if (gras_dd_is_r_null(r_ref, pointer_type->size[r_arch])) {
1058       VERB1("Not receiving data remotely referenced @%p since it's NULL",
1059             *(void **)r_ref);
1060       *(void**)l_data = NULL;
1061       free(r_ref);
1062       break;
1063     }
1064          
1065     errcode = detect_cycle
1066             ? xbt_dict_get_ext(refs,
1067                                 (char*)r_ref, pointer_type->size[r_arch],
1068                                 (void**)&l_ref)
1069             : mismatch_error;
1070
1071     if (errcode == mismatch_error) {
1072       int subsubcount = 0;
1073       void *l_referenced=NULL;
1074
1075       VERB2("Receiving a ref to '%s', remotely @%p",
1076             sub_type->name, *(void**)r_ref);
1077       if (sub_type->category_code == e_gras_datadesc_type_cat_array) {
1078         /* Damn. Reference to a dynamic array. Allocating the size for it 
1079            is more complicated */
1080         gras_dd_cat_array_t array_data = sub_type->category.array_data;
1081         gras_datadesc_type_t subsub_type;
1082
1083         subsubcount = array_data.fixed_size;
1084         if (subsubcount == 0)
1085           TRYOLD(gras_dd_recv_int(sock, r_arch, &subsubcount));
1086
1087         subsub_type = array_data.type;
1088
1089
1090         TRYOLD(gras_dd_alloc_ref(refs,
1091                               subsub_type->size[GRAS_THISARCH] * subsubcount, 
1092                               r_ref,pointer_type->size[r_arch], 
1093                               (char**)&l_referenced,
1094                               detect_cycle));
1095       } else {
1096         TRYOLD(gras_dd_alloc_ref(refs,sub_type->size[GRAS_THISARCH], 
1097                               r_ref,pointer_type->size[r_arch], 
1098                               (char**)&l_referenced,
1099                               detect_cycle));
1100       }
1101
1102       TRYOLD(gras_datadesc_gen_recv_rec(sock,state,refs, sub_type,
1103                                      r_arch,r_ref,pointer_type->size[r_arch],
1104                                      (char*)l_referenced, subsubcount,
1105                                      detect_cycle || sub_type->cycle));
1106                                      
1107       *(void**)l_data=l_referenced;
1108       VERB3("'%s' remotely referenced at %p locally at %p",
1109             sub_type->name, *(void**)r_ref, l_referenced);
1110       
1111     } else if (errcode == no_error) {
1112       VERB2("NOT receiving data remotely referenced @%p (already done, @%p here)",
1113             *(void**)r_ref, *(void**)l_ref);
1114
1115       *(void**)l_data=*l_ref;
1116
1117     } else {
1118       return errcode;
1119     }
1120     free(r_ref);
1121     break;
1122   }
1123
1124   case e_gras_datadesc_type_cat_array: {
1125     gras_dd_cat_array_t    array_data;
1126     int       count;
1127     char     *ptr;
1128     long int  elm_size;
1129
1130     array_data = type->category.array_data;
1131     /* determine element count locally, or from caller, or from peer */
1132     count = array_data.fixed_size;
1133     if (count == 0)
1134       count = subsize;
1135     if (count == 0)
1136       TRYOLD(gras_dd_recv_int(sock, r_arch, &count));
1137     if (count == 0)
1138       RAISE1(mismatch_error,
1139              "Invalid (=0) array size for type %s",type->name);
1140
1141     /* receive the content */
1142     sub_type = array_data.type;
1143     if (sub_type->category_code == e_gras_datadesc_type_cat_scalar) {
1144       VERB1("Array of %d scalars, get it in one shoot", count);
1145       if (sub_type->aligned_size[GRAS_THISARCH] >= 
1146           sub_type->aligned_size[r_arch]) {
1147         TRYOLD(gras_trp_chunk_recv(sock, (char*)l_data, 
1148                                 sub_type->aligned_size[r_arch] * count));
1149         if (r_arch != GRAS_THISARCH)
1150           TRYOLD(gras_dd_convert_elm(sub_type,count,r_arch, l_data,l_data));
1151       } else {
1152         ptr = xbt_malloc(sub_type->aligned_size[r_arch] * count);
1153
1154         TRYOLD(gras_trp_chunk_recv(sock, (char*)ptr, 
1155                                 sub_type->size[r_arch] * count));
1156         if (r_arch != GRAS_THISARCH)
1157           TRYOLD(gras_dd_convert_elm(sub_type,count,r_arch, ptr,l_data));
1158         free(ptr);
1159       }
1160     } else if (sub_type->category_code == e_gras_datadesc_type_cat_array &&
1161                sub_type->category.array_data.fixed_size > 0 &&
1162                sub_type->category.array_data.type->category_code == e_gras_datadesc_type_cat_scalar) {
1163       gras_datadesc_type_t subsub_type;
1164       array_data = sub_type->category.array_data;
1165       subsub_type = array_data.type;
1166        
1167       VERB1("Array of %d fixed array of scalars, get it in one shot",count);
1168       if (subsub_type->aligned_size[GRAS_THISARCH] >= 
1169           subsub_type->aligned_size[r_arch]) {
1170         TRYOLD(gras_trp_chunk_recv(sock, (char*)l_data, 
1171                                 subsub_type->aligned_size[r_arch] * count * 
1172                                   array_data.fixed_size));
1173         if (r_arch != GRAS_THISARCH)
1174           TRYOLD(gras_dd_convert_elm(subsub_type,count*array_data.fixed_size,r_arch, l_data,l_data));
1175       } else {
1176         ptr = xbt_malloc(subsub_type->aligned_size[r_arch] * count*array_data.fixed_size);
1177
1178         TRYOLD(gras_trp_chunk_recv(sock, (char*)ptr, 
1179                                 subsub_type->size[r_arch] * count*array_data.fixed_size));
1180         if (r_arch != GRAS_THISARCH)
1181           TRYOLD(gras_dd_convert_elm(subsub_type,count*array_data.fixed_size,r_arch, ptr,l_data));
1182         free(ptr);
1183       }
1184       
1185        
1186     } else {
1187       /* not scalar content, get it recursively (may contain pointers) */
1188       elm_size = sub_type->aligned_size[GRAS_THISARCH];
1189       VERB2("Receive a %d-long array of %s",count, sub_type->name);
1190
1191       ptr = l_data;
1192       for (cpt=0; cpt<count; cpt++) {
1193         TRYOLD(gras_datadesc_gen_recv_rec(sock,state,refs, sub_type,
1194                                        r_arch, NULL, 0, ptr,-1,
1195                                        detect_cycle || sub_type->cycle));
1196                                       
1197         ptr += elm_size;
1198       }
1199     }
1200     break;
1201   }
1202         
1203   default:
1204     xbt_assert0(0, "Invalid type");
1205   }
1206   
1207   if (type->recv)
1208     type->recv(type,state,l_data);
1209
1210   return no_error;
1211 }
1212
1213 /**
1214  * gras_datadesc_gen_recv:
1215  *
1216  * Get an instance of the datatype described by @type from the @socket, 
1217  * and store a pointer to it in @dst
1218  *
1219  */
1220 xbt_error_t
1221 gras_datadesc_gen_recv(gras_socket_t         sock, 
1222                        gras_datadesc_type_t  type,
1223                        int                   r_arch,
1224                        void                 *dst) {
1225
1226   xbt_error_t errcode;
1227   gras_cbps_t  state; /* callback persistent state */
1228   xbt_dict_t  refs;  /* all references already sent */
1229
1230   refs = xbt_dict_new();
1231   state = gras_cbps_new();
1232
1233   printf("xbt_error_t gras_%s_recv(gras_socket_t sock,void *dst){\n",
1234          type->name);
1235    
1236   errcode = gras_datadesc_gen_recv_rec(sock, state, refs, type, 
1237                                        r_arch, NULL, 0,
1238                                        (char *) dst,-1, 
1239                                        sub_type->cycle); 
1240
1241   printf("}\n");
1242   xbt_dict_free(&refs);
1243   gras_cbps_free(&state);
1244
1245   return errcode;
1246 }
1247 #endif