1 /* ddt_exchange - send/recv data described */
3 /* Copyright (c) 2004, 2005, 2006, 2007, 2009, 2010. The SimGrid Team.
4 * All rights reserved. */
6 /* This program is free software; you can redistribute it and/or modify it
7 * under the terms of the license (GNU LGPL) which comes with this package. */
10 #include "gras/DataDesc/datadesc_private.h"
11 #include "gras/Transport/transport_interface.h" /* gras_trp_send/recv */
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_ddt_exchange, gras_ddt,
14 "Sending data over the network");
15 const char *gras_datadesc_cat_names[9] = {
17 "scalar", "struct", "union", "ref", "array", "ignored",
21 static gras_datadesc_type_t int_type = NULL;
22 static gras_datadesc_type_t pointer_type = NULL;
24 static XBT_INLINE void
25 gras_dd_send_int(gras_socket_t sock, int *i, int stable)
29 int_type = gras_datadesc_by_name("int");
33 XBT_DEBUG("send_int(%u)", *i);
34 gras_trp_send(sock, (char *) i, int_type->size[GRAS_THISARCH], stable);
37 static XBT_INLINE void
38 gras_dd_recv_int(gras_socket_t sock, int r_arch, int *i)
42 int_type = gras_datadesc_by_name("int");
46 if (int_type->size[GRAS_THISARCH] >= int_type->size[r_arch]) {
47 gras_trp_recv(sock, (char *) i, int_type->size[r_arch]);
48 if (r_arch != GRAS_THISARCH)
49 gras_dd_convert_elm(int_type, 1, r_arch, i, i);
51 void *ptr = xbt_malloc(int_type->size[r_arch]);
53 gras_trp_recv(sock, (char *) ptr, int_type->size[r_arch]);
54 if (r_arch != GRAS_THISARCH)
55 gras_dd_convert_elm(int_type, 1, r_arch, ptr, i);
58 XBT_DEBUG("recv_int(%u)", *i);
62 * Note: here we suppose that the remote NULL is a sequence
63 * of 'length' bytes set to 0.
64 * FIXME: Check in configure?
66 static XBT_INLINE int gras_dd_is_r_null(char **r_ptr, long int length)
70 for (i = 0; i < length; i++) {
71 if (((unsigned char *) r_ptr)[i]) {
79 static XBT_INLINE void gras_dd_alloc_ref(xbt_dict_t refs, long int size, char **r_ref, long int r_len, /* pointer_type->size[r_arch] */
80 char **l_ref, int detect_cycle)
84 xbt_assert1(size > 0, "Cannot allocate %ld bytes!", size);
85 l_data = xbt_malloc((size_t) size);
89 ("alloc_ref: l_data=%p, &l_data=%p; r_ref=%p; *r_ref=%p, r_len=%ld",
90 (void *) l_data, (void *) &l_data, (void *) r_ref,
91 (void *) (r_ref ? *r_ref : NULL), r_len);
92 if (detect_cycle && r_ref && !gras_dd_is_r_null(r_ref, r_len)) {
93 void *ptr = xbt_malloc(sizeof(void *));
95 memcpy(ptr, l_ref, sizeof(void *));
97 XBT_DEBUG("Insert l_ref=%p under r_ref=%p", *(void **) ptr,
101 xbt_dict_set_ext(refs, (const char *) r_ref, r_len, ptr, xbt_free_f);
106 gras_datadesc_memcpy_rec(gras_cbps_t state,
108 gras_datadesc_type_t type,
109 char *src, char *dst, int subsize,
115 gras_datadesc_type_t sub_type; /* type on which we recurse */
118 XBT_VERB("Copy a %s (%s) from %p to %p (local sizeof=%ld)",
119 type->name, gras_datadesc_cat_names[type->category_code],
120 src, dst, type->size[GRAS_THISARCH]);
123 type->send(type, state, src);
126 switch (type->category_code) {
127 case e_gras_datadesc_type_cat_scalar:
128 memcpy(dst, src, type->size[GRAS_THISARCH]);
129 count += type->size[GRAS_THISARCH];
132 case e_gras_datadesc_type_cat_struct:{
133 gras_dd_cat_struct_t struct_data;
134 gras_dd_cat_field_t field;
138 struct_data = type->category.struct_data;
139 xbt_assert1(struct_data.closed,
140 "Please call gras_datadesc_declare_struct_close on %s before copying it",
142 XBT_VERB(">> Copy all fields of the structure %s", type->name);
143 xbt_dynar_foreach(struct_data.fields, cpt, field) {
144 field_src = src + field->offset[GRAS_THISARCH];
145 field_dst = dst + field->offset[GRAS_THISARCH];
147 sub_type = field->type;
150 field->send(type, state, field_src);
152 XBT_DEBUG("Copy field %s", field->name);
154 gras_datadesc_memcpy_rec(state, refs, sub_type, field_src,
155 field_dst, 0, detect_cycle
158 if (XBT_LOG_ISENABLED(gras_ddt_exchange, xbt_log_priority_verbose)) {
159 if (sub_type == gras_datadesc_by_name("unsigned int")) {
160 XBT_VERB("Copied value for field '%s': %d (type: unsigned int)",
161 field->name, *(unsigned int *) field_dst);
162 } else if (sub_type == gras_datadesc_by_name("int")) {
163 XBT_VERB("Copied value for field '%s': %d (type: int)",
164 field->name, *(int *) field_dst);
166 } else if (sub_type ==
167 gras_datadesc_by_name("unsigned long int")) {
169 ("Copied value for field '%s': %ld (type: unsigned long int)",
170 field->name, *(unsigned long int *) field_dst);
171 } else if (sub_type == gras_datadesc_by_name("long int")) {
172 XBT_VERB("Copied value for field '%s': %ld (type: long int)",
173 field->name, *(long int *) field_dst);
175 } else if (sub_type == gras_datadesc_by_name("string")) {
176 XBT_VERB("Copied value for field '%s': '%s' (type: string)",
177 field->name, *(char **) field_dst);
179 XBT_VERB("Copied a value for field '%s' (type not scalar?)",
185 XBT_VERB("<< Copied all fields of the structure %s", type->name);
190 case e_gras_datadesc_type_cat_union:{
191 gras_dd_cat_union_t union_data;
192 gras_dd_cat_field_t field = NULL;
193 unsigned int field_num;
195 union_data = type->category.union_data;
197 xbt_assert1(union_data.closed,
198 "Please call gras_datadesc_declare_union_close on %s before copying it",
200 /* retrieve the field number */
201 field_num = union_data.selector(type, state, src);
203 xbt_assert1(field_num > 0,
204 "union field selector of %s gave a negative value",
207 xbt_assert3(field_num < xbt_dynar_length(union_data.fields),
208 "union field selector of %s returned %d but there is only %lu fields",
209 type->name, field_num,
210 xbt_dynar_length(union_data.fields));
212 /* Copy the content */
214 xbt_dynar_get_as(union_data.fields, field_num,
215 gras_dd_cat_field_t);
216 sub_type = field->type;
219 field->send(type, state, src);
221 count += gras_datadesc_memcpy_rec(state, refs, sub_type, src, dst, 0,
222 detect_cycle || sub_type->cycle);
227 case e_gras_datadesc_type_cat_ref:{
228 gras_dd_cat_ref_t ref_data;
231 int reference_is_to_cpy;
233 ref_data = type->category.ref_data;
235 /* Detect the referenced type */
236 sub_type = ref_data.type;
237 if (sub_type == NULL) {
238 sub_type = (*ref_data.selector) (type, state, src);
241 /* Send the pointed data only if not already sent */
242 if (*(void **) src == NULL) {
243 XBT_VERB("Not copying NULL referenced data");
244 *(void **) dst = NULL;
247 o_ref = (char **) src;
249 reference_is_to_cpy = 1;
252 xbt_dict_get_or_null_ext(refs, (char *) o_ref,
254 /* already known, no need to copy it */
255 //XBT_INFO("Cycle detected");
256 reference_is_to_cpy = 0;
259 if (reference_is_to_cpy) {
260 int subsubcount = -1;
261 void *l_referenced = NULL;
262 XBT_VERB("Copy a ref to '%s' referenced at %p", sub_type->name,
266 pointer_type = gras_datadesc_by_name("data pointer");
267 xbt_assert(pointer_type);
270 if (sub_type->category_code == e_gras_datadesc_type_cat_array) {
271 /* Damn. Reference to a dynamic array. Allocating the space for it is more complicated */
272 gras_dd_cat_array_t array_data = sub_type->category.array_data;
273 gras_datadesc_type_t subsub_type;
275 subsub_type = array_data.type;
276 subsubcount = array_data.fixed_size;
277 if (subsubcount == -1)
279 array_data.dynamic_size(subsub_type, state, *o_ref);
281 if (subsubcount != 0)
282 gras_dd_alloc_ref(refs,
283 subsub_type->size[GRAS_THISARCH] *
285 pointer_type->size[GRAS_THISARCH],
286 (char **) &l_referenced, detect_cycle);
288 gras_dd_alloc_ref(refs, sub_type->size[GRAS_THISARCH],
289 o_ref, pointer_type->size[GRAS_THISARCH],
290 (char **) &l_referenced, detect_cycle);
293 count += gras_datadesc_memcpy_rec(state, refs, sub_type,
294 *o_ref, (char *) l_referenced,
295 subsubcount, detect_cycle
298 *(void **) dst = l_referenced;
299 XBT_VERB("'%s' previously referenced at %p now at %p",
300 sub_type->name, *(void **) o_ref, l_referenced);
304 ("NOT copying data previously referenced @%p (already done, @%p now)",
305 *(void **) o_ref, *(void **) n_ref);
307 *(void **) dst = *n_ref;
313 case e_gras_datadesc_type_cat_array:{
314 gras_dd_cat_array_t array_data;
315 unsigned long int array_count;
320 array_data = type->category.array_data;
322 /* determine and send the element count */
323 array_count = array_data.fixed_size;
324 if (array_count == -1)
325 array_count = subsize;
326 if (array_count == -1) {
327 array_count = array_data.dynamic_size(type, state, src);
328 xbt_assert1(array_count >= 0,
329 "Invalid (negative) array size for type %s",
333 /* send the content */
334 sub_type = array_data.type;
335 elm_size = sub_type->aligned_size[GRAS_THISARCH];
336 if (sub_type->category_code == e_gras_datadesc_type_cat_scalar) {
337 XBT_VERB("Array of %ld scalars, copy it in one shot", array_count);
339 sub_type->aligned_size[GRAS_THISARCH] * array_count);
340 count += sub_type->aligned_size[GRAS_THISARCH] * array_count;
341 } else if (sub_type->category_code == e_gras_datadesc_type_cat_array
342 && sub_type->category.array_data.fixed_size > 0
343 && sub_type->category.array_data.type->category_code ==
344 e_gras_datadesc_type_cat_scalar) {
346 XBT_VERB("Array of %ld fixed array of scalars, copy it in one shot",
349 sub_type->category.array_data.
350 type->aligned_size[GRAS_THISARCH]
351 * array_count * sub_type->category.array_data.fixed_size);
353 sub_type->category.array_data.type->aligned_size[GRAS_THISARCH]
354 * array_count * sub_type->category.array_data.fixed_size;
357 XBT_VERB("Array of %ld stuff, copy it in one after the other",
359 for (cpt = 0; cpt < array_count; cpt++) {
360 XBT_VERB("Copy the %dth stuff out of %ld", cpt, array_count);
362 gras_datadesc_memcpy_rec(state, refs, sub_type, src_ptr,
363 dst_ptr, 0, detect_cycle
373 xbt_die("Invalid type");
380 * gras_datadesc_memcpy:
382 * Copy the data pointed by src and described by type
383 * to a new location, and store a pointer to it in dst.
386 int gras_datadesc_memcpy(gras_datadesc_type_t type, void *src, void *dst)
389 static gras_cbps_t state = NULL;
390 static xbt_dict_t refs = NULL; /* all references already sent */
393 xbt_assert0(type, "called with NULL type descriptor");
395 XBT_DEBUG("Memcopy a %s from %p to %p", gras_datadesc_get_name(type), src,
398 state = gras_cbps_new();
399 refs = xbt_dict_new();
404 gras_datadesc_memcpy_rec(state, refs, type, (char *) src,
405 (char *) dst, 0, type->cycle);
407 xbt_dict_reset(refs);
408 gras_cbps_reset(state);
416 *** Direct use functions
420 gras_datadesc_send_rec(gras_socket_t sock,
423 gras_datadesc_type_t type,
424 char *data, int detect_cycle)
428 gras_datadesc_type_t sub_type; /* type on which we recurse */
430 XBT_VERB("Send a %s (%s)",
431 type->name, gras_datadesc_cat_names[type->category_code]);
433 if (!strcmp(type->name, "string"))
434 XBT_VERB("value: '%s'", *(char **) data);
437 type->send(type, state, data);
438 XBT_DEBUG("Run the emission callback");
441 switch (type->category_code) {
442 case e_gras_datadesc_type_cat_scalar:
443 gras_trp_send(sock, data, type->size[GRAS_THISARCH], 1);
446 case e_gras_datadesc_type_cat_struct:{
447 gras_dd_cat_struct_t struct_data;
448 gras_dd_cat_field_t field;
451 struct_data = type->category.struct_data;
452 xbt_assert1(struct_data.closed,
453 "Please call gras_datadesc_declare_struct_close on %s before sending it",
455 XBT_VERB(">> Send all fields of the structure %s", type->name);
456 xbt_dynar_foreach(struct_data.fields, cpt, field) {
458 field_data += field->offset[GRAS_THISARCH];
460 sub_type = field->type;
463 XBT_DEBUG("Run the emission callback of field %s", field->name);
464 field->send(type, state, field_data);
467 XBT_VERB("Send field %s", field->name);
468 gras_datadesc_send_rec(sock, state, refs, sub_type, field_data,
469 detect_cycle || sub_type->cycle);
472 XBT_VERB("<< Sent all fields of the structure %s", type->name);
477 case e_gras_datadesc_type_cat_union:{
478 gras_dd_cat_union_t union_data;
479 gras_dd_cat_field_t field = NULL;
482 union_data = type->category.union_data;
484 xbt_assert1(union_data.closed,
485 "Please call gras_datadesc_declare_union_close on %s before sending it",
487 /* retrieve the field number */
488 field_num = union_data.selector(type, state, data);
490 xbt_assert1(field_num > 0,
491 "union field selector of %s gave a negative value",
494 xbt_assert3(field_num < xbt_dynar_length(union_data.fields),
495 "union field selector of %s returned %d but there is only %lu fields",
496 type->name, field_num,
497 xbt_dynar_length(union_data.fields));
499 /* Send the field number */
500 gras_dd_send_int(sock, &field_num, 0 /* not stable */ );
502 /* Send the content */
504 xbt_dynar_get_as(union_data.fields, field_num,
505 gras_dd_cat_field_t);
506 sub_type = field->type;
509 field->send(type, state, data);
511 gras_datadesc_send_rec(sock, state, refs, sub_type, data,
512 detect_cycle || sub_type->cycle);
517 case e_gras_datadesc_type_cat_ref:{
518 gras_dd_cat_ref_t ref_data;
519 void **ref = (void **) data;
520 int reference_is_to_send;
522 ref_data = type->category.ref_data;
524 /* Detect the referenced type and send it to peer if needed */
525 sub_type = ref_data.type;
526 if (sub_type == NULL) {
527 sub_type = (*ref_data.selector) (type, state, data);
528 gras_dd_send_int(sock, &(sub_type->code), 1 /*stable */ );
531 /* Send the actual value of the pointer for cycle handling */
533 pointer_type = gras_datadesc_by_name("data pointer");
534 xbt_assert(pointer_type);
537 gras_trp_send(sock, (char *) data,
538 pointer_type->size[GRAS_THISARCH], 1 /*stable */ );
540 /* Send the pointed data only if not already sent */
541 if (*(void **) data == NULL) {
542 XBT_VERB("Not sending NULL referenced data");
546 reference_is_to_send = 1;
547 /* return ignored. Just checking whether it's known or not */
549 && xbt_dict_get_or_null_ext(refs, (char *) ref,
551 //XBT_INFO("Cycle detected");
552 reference_is_to_send = 0;
555 if (reference_is_to_send) {
556 XBT_VERB("Sending data referenced at %p", (void *) *ref);
558 xbt_dict_set_ext(refs, (char *) ref, sizeof(void *), ref, NULL);
559 gras_datadesc_send_rec(sock, state, refs, sub_type, *ref,
560 detect_cycle || sub_type->cycle);
563 XBT_VERB("Not sending data referenced at %p (already done)",
570 case e_gras_datadesc_type_cat_array:{
571 gras_dd_cat_array_t array_data;
576 array_data = type->category.array_data;
578 /* determine and send the element count */
579 count = array_data.fixed_size;
581 count = array_data.dynamic_size(type, state, data);
582 xbt_assert1(count >= 0,
583 "Invalid (negative) array size for type %s",
585 gras_dd_send_int(sock, &count, 0 /*non-stable */ );
588 /* send the content */
589 sub_type = array_data.type;
590 elm_size = sub_type->aligned_size[GRAS_THISARCH];
591 if (sub_type->category_code == e_gras_datadesc_type_cat_scalar) {
592 XBT_VERB("Array of %d scalars, send it in one shot", count);
593 gras_trp_send(sock, data,
594 sub_type->aligned_size[GRAS_THISARCH] * count,
595 0 /* not stable */ );
596 } else if (sub_type->category_code == e_gras_datadesc_type_cat_array
597 && sub_type->category.array_data.fixed_size > 0
598 && sub_type->category.array_data.type->category_code ==
599 e_gras_datadesc_type_cat_scalar) {
601 XBT_VERB("Array of %d fixed array of scalars, send it in one shot",
603 gras_trp_send(sock, data,
604 sub_type->category.array_data.
605 type->aligned_size[GRAS_THISARCH]
606 * count * sub_type->category.array_data.fixed_size,
607 0 /* not stable */ );
610 for (cpt = 0; cpt < count; cpt++) {
611 gras_datadesc_send_rec(sock, state, refs, sub_type, ptr,
612 detect_cycle || sub_type->cycle);
620 xbt_die("Invalid type");
625 * gras_datadesc_send:
627 * Copy the data pointed by src and described by type to the socket
630 void gras_datadesc_send(gras_socket_t sock,
631 gras_datadesc_type_t type, void *src)
635 static gras_cbps_t state = NULL;
636 static xbt_dict_t refs = NULL; /* all references already sent */
638 xbt_assert0(type, "called with NULL type descriptor");
641 state = gras_cbps_new();
642 refs = xbt_dict_new();
646 gras_datadesc_send_rec(sock, state, refs, type, (char *) src,
649 xbt_dict_reset(refs);
650 gras_cbps_reset(state);
657 * gras_datadesc_recv_rec:
659 * Do the data reception job recursively.
661 * subsize used only to deal with vicious case of reference to dynamic array.
662 * This size is needed at the reference reception level (to allocate enough
663 * space) and at the array reception level (to fill enough room).
665 * Having this size passed as an argument of the recursive function is a crude
666 * hack, but I was told that working code is sometimes better than neat one ;)
669 gras_datadesc_recv_rec(gras_socket_t sock,
672 gras_datadesc_type_t type,
676 char *l_data, int subsize, int detect_cycle)
680 gras_datadesc_type_t sub_type;
682 XBT_VERB("Recv a %s @%p", type->name, (void *) l_data);
685 switch (type->category_code) {
686 case e_gras_datadesc_type_cat_scalar:
687 if (type->size[GRAS_THISARCH] == type->size[r_arch]) {
688 gras_trp_recv(sock, (char *) l_data, type->size[r_arch]);
689 if (r_arch != GRAS_THISARCH)
690 gras_dd_convert_elm(type, 1, r_arch, l_data, l_data);
692 void *ptr = xbt_malloc(type->size[r_arch]);
694 gras_trp_recv(sock, (char *) ptr, type->size[r_arch]);
695 if (r_arch != GRAS_THISARCH)
696 gras_dd_convert_elm(type, 1, r_arch, ptr, l_data);
701 case e_gras_datadesc_type_cat_struct:{
702 gras_dd_cat_struct_t struct_data;
703 gras_dd_cat_field_t field;
705 struct_data = type->category.struct_data;
707 xbt_assert1(struct_data.closed,
708 "Please call gras_datadesc_declare_struct_close on %s before receiving it",
710 XBT_VERB(">> Receive all fields of the structure %s", type->name);
711 xbt_dynar_foreach(struct_data.fields, cpt, field) {
712 char *field_data = l_data + field->offset[GRAS_THISARCH];
714 sub_type = field->type;
716 gras_datadesc_recv_rec(sock, state, refs, sub_type,
719 detect_cycle || sub_type->cycle);
722 XBT_DEBUG("Run the reception callback of field %s", field->name);
723 field->recv(type, state, (void *) l_data);
727 XBT_VERB("<< Received all fields of the structure %s", type->name);
732 case e_gras_datadesc_type_cat_union:{
733 gras_dd_cat_union_t union_data;
734 gras_dd_cat_field_t field = NULL;
737 union_data = type->category.union_data;
739 xbt_assert1(union_data.closed,
740 "Please call gras_datadesc_declare_union_close on %s before receiving it",
742 /* retrieve the field number */
743 gras_dd_recv_int(sock, r_arch, &field_num);
745 THROW1(mismatch_error, 0,
746 "Received union field for %s is negative", type->name);
747 if (field_num > xbt_dynar_length(union_data.fields))
748 THROW3(mismatch_error, 0,
749 "Received union field for %s is said to be #%d but there is only %lu fields",
750 type->name, field_num, xbt_dynar_length(union_data.fields));
752 /* Recv the content */
754 xbt_dynar_get_as(union_data.fields, field_num,
755 gras_dd_cat_field_t);
756 sub_type = field->type;
758 gras_datadesc_recv_rec(sock, state, refs, sub_type,
760 l_data, -1, detect_cycle || sub_type->cycle);
762 field->recv(type, state, l_data);
767 case e_gras_datadesc_type_cat_ref:{
770 gras_dd_cat_ref_t ref_data;
771 int reference_is_to_recv = 0;
773 ref_data = type->category.ref_data;
775 /* Get the referenced type locally or from peer */
776 sub_type = ref_data.type;
777 if (sub_type == NULL) {
779 gras_dd_recv_int(sock, r_arch, &ref_code);
780 sub_type = gras_datadesc_by_id(ref_code);
783 /* Get the actual value of the pointer for cycle handling */
785 pointer_type = gras_datadesc_by_name("data pointer");
786 xbt_assert(pointer_type);
789 r_ref = xbt_malloc(pointer_type->size[r_arch]);
791 gras_trp_recv(sock, (char *) r_ref, pointer_type->size[r_arch]);
793 /* Receive the pointed data only if not already sent */
794 if (gras_dd_is_r_null(r_ref, pointer_type->size[r_arch])) {
795 XBT_VERB("Not receiving data remotely referenced @%p since it's NULL",
797 *(void **) l_data = NULL;
802 reference_is_to_recv = 1;
803 if (detect_cycle && (l_ref =
804 xbt_dict_get_or_null_ext(refs, (char *) r_ref,
807 reference_is_to_recv = 0;
808 //XBT_INFO("Cycle detected");
811 if (reference_is_to_recv) {
812 int subsubcount = -1;
813 void *l_referenced = NULL;
815 XBT_VERB("Receiving a ref to '%s', remotely @%p",
816 sub_type->name, *(void **) r_ref);
817 if (sub_type->category_code == e_gras_datadesc_type_cat_array) {
818 /* Damn. Reference to a dynamic array. Allocating the space for it is more complicated */
819 gras_dd_cat_array_t array_data = sub_type->category.array_data;
820 gras_datadesc_type_t subsub_type;
822 subsubcount = array_data.fixed_size;
823 if (subsubcount == -1)
824 gras_dd_recv_int(sock, r_arch, &subsubcount);
826 subsub_type = array_data.type;
828 if (subsubcount != 0)
829 gras_dd_alloc_ref(refs,
830 subsub_type->size[GRAS_THISARCH] *
832 pointer_type->size[r_arch],
833 (char **) &l_referenced, detect_cycle);
837 gras_dd_alloc_ref(refs, sub_type->size[GRAS_THISARCH],
838 r_ref, pointer_type->size[r_arch],
839 (char **) &l_referenced, detect_cycle);
842 if (l_referenced != NULL)
843 gras_datadesc_recv_rec(sock, state, refs, sub_type,
844 r_arch, r_ref, pointer_type->size[r_arch],
845 (char *) l_referenced, subsubcount,
846 detect_cycle || sub_type->cycle);
848 *(void **) l_data = l_referenced;
849 XBT_VERB("'%s' remotely referenced at %p locally at %p",
850 sub_type->name, *(void **) r_ref, l_referenced);
854 ("NOT receiving data remotely referenced @%p (already done, @%p here)",
855 *(void **) r_ref, *(void **) l_ref);
857 *(void **) l_data = *l_ref;
864 case e_gras_datadesc_type_cat_array:{
865 gras_dd_cat_array_t array_data;
870 array_data = type->category.array_data;
871 /* determine element count locally, or from caller, or from peer */
872 count = array_data.fixed_size;
876 gras_dd_recv_int(sock, r_arch, &count);
878 THROW1(mismatch_error, 0,
879 "Invalid (=-1) array size for type %s", type->name);
881 /* receive the content */
882 sub_type = array_data.type;
883 if (sub_type->category_code == e_gras_datadesc_type_cat_scalar) {
884 XBT_VERB("Array of %d scalars, get it in one shoot", count);
885 if (sub_type->aligned_size[GRAS_THISARCH] >=
886 sub_type->aligned_size[r_arch]) {
887 gras_trp_recv(sock, (char *) l_data,
888 sub_type->aligned_size[r_arch] * count);
889 if (r_arch != GRAS_THISARCH)
890 gras_dd_convert_elm(sub_type, count, r_arch, l_data, l_data);
892 ptr = xbt_malloc(sub_type->aligned_size[r_arch] * count);
894 gras_trp_recv(sock, (char *) ptr,
895 sub_type->size[r_arch] * count);
896 if (r_arch != GRAS_THISARCH)
897 gras_dd_convert_elm(sub_type, count, r_arch, ptr, l_data);
900 } else if (sub_type->category_code == e_gras_datadesc_type_cat_array
901 && sub_type->category.array_data.fixed_size >= 0
902 && sub_type->category.array_data.type->category_code ==
903 e_gras_datadesc_type_cat_scalar) {
904 gras_datadesc_type_t subsub_type;
905 array_data = sub_type->category.array_data;
906 subsub_type = array_data.type;
908 XBT_VERB("Array of %d fixed array of scalars, get it in one shot",
910 if (subsub_type->aligned_size[GRAS_THISARCH] >=
911 subsub_type->aligned_size[r_arch]) {
912 gras_trp_recv(sock, (char *) l_data,
913 subsub_type->aligned_size[r_arch] * count *
914 array_data.fixed_size);
915 if (r_arch != GRAS_THISARCH)
916 gras_dd_convert_elm(subsub_type, count * array_data.fixed_size,
917 r_arch, l_data, l_data);
920 xbt_malloc(subsub_type->aligned_size[r_arch] * count *
921 array_data.fixed_size);
923 gras_trp_recv(sock, (char *) ptr,
924 subsub_type->size[r_arch] * count *
925 array_data.fixed_size);
926 if (r_arch != GRAS_THISARCH)
927 gras_dd_convert_elm(subsub_type, count * array_data.fixed_size,
928 r_arch, ptr, l_data);
934 /* not scalar content, get it recursively (may contain pointers) */
935 elm_size = sub_type->aligned_size[GRAS_THISARCH];
936 XBT_VERB("Receive a %d-long array of %s", count, sub_type->name);
939 for (cpt = 0; cpt < count; cpt++) {
940 gras_datadesc_recv_rec(sock, state, refs, sub_type,
941 r_arch, NULL, 0, ptr, -1,
942 detect_cycle || sub_type->cycle);
951 xbt_die("Invalid type");
955 type->recv(type, state, l_data);
957 if (!strcmp(type->name, "string"))
958 XBT_VERB("value: '%s'", *(char **) l_data);
963 * gras_datadesc_recv:
965 * Get an instance of the datatype described by @type from the @socket,
966 * and store a pointer to it in @dst
970 gras_datadesc_recv(gras_socket_t sock,
971 gras_datadesc_type_t type, int r_arch, void *dst)
975 static gras_cbps_t state = NULL; /* callback persistent state */
976 static xbt_dict_t refs = NULL; /* all references already sent */
979 state = gras_cbps_new();
980 refs = xbt_dict_new();
983 xbt_assert0(type, "called with NULL type descriptor");
985 gras_datadesc_recv_rec(sock, state, refs, type,
986 r_arch, NULL, 0, (char *) dst, -1, type->cycle);
988 xbt_dict_reset(refs);
989 gras_cbps_reset(state);