-gras_error_t
-gras_datadesc_recv_rec(gras_socket_t *sock,
- gras_dd_cbps_t *state,
- gras_dict_t *refs,
- gras_datadesc_type_t *type,
- int r_arch,
- char **r_data,
- long int r_lgr,
- char **dst) {
-
- gras_error_t errcode;
- char *l_data = *dst; /* dereference to avoid typo */
- int cpt;
- gras_datadesc_type_t *sub_type;
-
- VERB1("Recv a %s", type->name);
-
- switch (type->category_code) {
- case e_gras_datadesc_type_cat_scalar:
- if (!l_data) {
- TRY(gras_dd_alloc_ref(refs,type->size[GRAS_THISARCH],r_data,r_lgr, dst));
- l_data = *dst;
- }
-
- if (type->size[GRAS_THISARCH] >= type->size[r_arch]) {
- TRY(gras_trp_chunk_recv(sock, (char*)l_data, type->size[r_arch]));
- TRY(gras_dd_convert_elm(type,r_arch, l_data,l_data));
- } else {
- void *ptr = NULL;
- ptr = malloc((size_t)type->size[r_arch]);
- TRY(gras_trp_chunk_recv(sock, (char*)ptr, type->size[r_arch]));
- TRY(gras_dd_convert_elm(type,r_arch, ptr,l_data));
- free(ptr);
- }
- break;
-
- case e_gras_datadesc_type_cat_struct: {
- gras_dd_cat_struct_t struct_data;
- gras_dd_cat_field_t *field;
-
- struct_data = type->category.struct_data;
-
- if (!l_data) {
- TRY(gras_dd_alloc_ref(refs,type->size[GRAS_THISARCH],r_data,r_lgr, dst));
- l_data = *dst;
- }
-
- VERB1(">> Receive all fields of the structure %s",type->name);
- gras_dynar_foreach(struct_data.fields, cpt, field) {
- char *field_data = l_data + field->offset[GRAS_THISARCH];
-
- TRY(gras_datadesc_by_id(field->code, &sub_type));
-
- TRY(gras_datadesc_recv_rec(sock,state,refs, sub_type,
- r_arch,NULL,0,
- &field_data));
- }
- VERB1("<< Received all fields of the structure %s", type->name);
-
- break;
- }
-
- case e_gras_datadesc_type_cat_union: {
- gras_dd_cat_union_t union_data;
- gras_dd_cat_field_t *field;
- int field_num;
-
- union_data = type->category.union_data;
-
- if (!l_data) {
- TRY(gras_dd_alloc_ref(refs,type->size[GRAS_THISARCH],r_data,r_lgr, dst));
- l_data = *dst;
- }
-
- /* retrieve the field number */
- TRY(gras_dd_recv_int(sock, r_arch, &field_num));
- if (field_num < 0)
- RAISE1(mismatch_error,
- "Received union field for %s is negative", type->name);
- if (field_num < gras_dynar_length(union_data.fields))
- RAISE3(mismatch_error,
- "Received union field for %s is %d but there is only %d fields",
- type->name, field_num, gras_dynar_length(union_data.fields));
-
- /* Recv the content */
- gras_dynar_get(union_data.fields, field_num, field);
- TRY(gras_datadesc_by_id(field->code, &sub_type));
-
- TRY(gras_datadesc_recv_rec(sock,state,refs, sub_type,
- r_arch,NULL,0,
- dst));
- break;
- }
-
- case e_gras_datadesc_type_cat_ref: {
- char **r_ref = NULL;
- char **l_ref = NULL;
- gras_dd_cat_ref_t ref_data;
- int ref_code;
-
- ref_data = type->category.ref_data;
-
- /* Get the referenced type locally or from peer */
- ref_code = ref_data.code;
- if (ref_code < 0)
- TRY(gras_dd_recv_int(sock, r_arch, &ref_code));
-
- /* Get the actual value of the pointer for cycle handling */
- if (!pointer_type)
- TRY(gras_datadesc_by_name("data pointer", &pointer_type));
-
- if (! (r_ref = malloc((size_t)pointer_type->size[r_arch])) )
- RAISE_MALLOC;
- TRY(gras_trp_chunk_recv(sock, (char*)r_ref,
- pointer_type->size[r_arch]));
-
- if (!l_data) {
- TRY(gras_dd_alloc_ref(refs,type->size[GRAS_THISARCH],r_data,r_lgr, dst));
- l_data = *dst;
- }
-
- /* Receive the pointed data only if not already sent */
- if (gras_dd_is_r_null(r_ref, pointer_type->size[r_arch])) {
- VERB1("Not receiving data remotely referenced at %p since it's NULL",
- *(void **)r_ref);
- *(void**)l_data = NULL;
- break;
- }
- errcode = gras_dict_get_ext(refs,
- (char*)r_ref, pointer_type->size[r_arch],
- (void**)&l_ref);
-
-
- if (errcode == mismatch_error) {
- void *l_referenced=NULL;
- VERB1("Receiving data remotely referenced at %p", *(void**)r_ref);
-
- TRY(gras_datadesc_by_id(ref_code, &sub_type));
- // DEBUG2("l_ref= %p; &l_ref=%p",l_referenced,&l_referenced);
- TRY(gras_datadesc_recv_rec(sock,state,refs, sub_type,
- r_arch,r_ref,pointer_type->size[r_arch],
- (char**)&l_referenced));
- *(void**)l_data=l_referenced;
-
- } else if (errcode == no_error) {
- VERB1("NOT receiving data remotely referenced at %p (already done). ",
- *(void**)r_ref);
-
- VERB2("l_ref=%p; *l_ref=%p", l_ref,*l_ref);
-
- *(void**)l_data=*l_ref;
-
- } else {
- return errcode;
- }
- VERB1("*l_data=%p",*(void**)l_data);
- break;
- }
-
- case e_gras_datadesc_type_cat_array: {
- gras_dd_cat_array_t array_data;
- int count;
- char *ptr;
- long int elm_size;
-
- array_data = type->category.array_data;
- /* determine element count locally or from peer */
- count = array_data.fixed_size;
- if (count <= 0)
- TRY(gras_dd_recv_int(sock, r_arch, &count));
- if (count < 0)
- RAISE1(mismatch_error,
- "Invalid (negative) array size for type %s",type->name);
-
- /* receive the content */
- TRY(gras_datadesc_by_id(array_data.code, &sub_type));
- elm_size = sub_type->aligned_size[GRAS_THISARCH];
-
- if (!l_data) {
- TRY(gras_dd_alloc_ref(refs,elm_size*count,r_data,r_lgr, dst));
- l_data = *dst;
- }
-
- ptr = l_data;
- for (cpt=0; cpt<count; cpt++) {
- TRY(gras_datadesc_recv_rec(sock,state,refs, sub_type,
- r_arch, NULL, 0, &ptr));
- ptr += elm_size;
- }
- break;
- }
-
- default:
- gras_assert0(0, "Invalid type");
- }
-
- return no_error;
+static void
+gras_datadesc_recv_rec(gras_socket_t sock,
+ gras_cbps_t state,
+ xbt_dict_t refs,
+ gras_datadesc_type_t type,
+ int r_arch,
+ char **r_data,
+ long int r_lgr,
+ char *l_data,
+ int subsize,
+ int detect_cycle) {
+
+ unsigned int cpt;
+ gras_datadesc_type_t sub_type;
+ xbt_ex_t e;
+
+ VERB2("Recv a %s @%p", type->name, (void*)l_data);
+ xbt_assert(l_data);
+
+ switch (type->category_code) {
+ case e_gras_datadesc_type_cat_scalar:
+ if (type->size[GRAS_THISARCH] == type->size[r_arch]) {
+ gras_trp_recv(sock, (char*)l_data, type->size[r_arch]);
+ if (r_arch != GRAS_THISARCH)
+ gras_dd_convert_elm(type,1,r_arch, l_data,l_data);
+ } else {
+ void *ptr = xbt_malloc(type->size[r_arch]);
+
+ gras_trp_recv(sock, (char*)ptr, type->size[r_arch]);
+ if (r_arch != GRAS_THISARCH)
+ gras_dd_convert_elm(type,1,r_arch, ptr,l_data);
+ free(ptr);
+ }
+ break;
+
+ case e_gras_datadesc_type_cat_struct: {
+ gras_dd_cat_struct_t struct_data;
+ gras_dd_cat_field_t field;
+
+ struct_data = type->category.struct_data;
+
+ xbt_assert1(struct_data.closed,
+ "Please call gras_datadesc_declare_struct_close on %s before receiving it",
+ type->name);
+ VERB1(">> Receive all fields of the structure %s",type->name);
+ xbt_dynar_foreach(struct_data.fields, cpt, field) {
+ char *field_data = l_data + field->offset[GRAS_THISARCH];
+
+ sub_type = field->type;
+
+ gras_datadesc_recv_rec(sock,state,refs, sub_type,
+ r_arch,NULL,0,
+ field_data,-1,
+ detect_cycle || sub_type->cycle);
+
+ if (field->recv) {
+ DEBUG1("Run the reception callback of field %s", field->name);
+ field->recv(type,state,(void*)l_data);
+ }
+
+ }
+ VERB1("<< Received all fields of the structure %s", type->name);
+
+ break;
+ }
+
+ case e_gras_datadesc_type_cat_union: {
+ gras_dd_cat_union_t union_data;
+ gras_dd_cat_field_t field=NULL;
+ int field_num;
+
+ union_data = type->category.union_data;
+
+ xbt_assert1(union_data.closed,
+ "Please call gras_datadesc_declare_union_close on %s before receiving it",
+ type->name);
+ /* retrieve the field number */
+ gras_dd_recv_int(sock, r_arch, &field_num);
+ if (field_num < 0)
+ THROW1(mismatch_error,0,
+ "Received union field for %s is negative", type->name);
+ if (field_num > xbt_dynar_length(union_data.fields))
+ THROW3(mismatch_error,0,
+ "Received union field for %s is said to be #%d but there is only %lu fields",
+ type->name, field_num, xbt_dynar_length(union_data.fields));
+
+ /* Recv the content */
+ field = xbt_dynar_get_as(union_data.fields, field_num, gras_dd_cat_field_t);
+ sub_type = field->type;
+
+ gras_datadesc_recv_rec(sock,state,refs, sub_type,
+ r_arch,NULL,0,
+ l_data,-1,
+ detect_cycle || sub_type->cycle);
+ if (field->recv)
+ field->recv(type,state,l_data);
+
+ break;
+ }
+
+ case e_gras_datadesc_type_cat_ref: {
+ char **r_ref = NULL;
+ char **l_ref = NULL;
+ gras_dd_cat_ref_t ref_data;
+ int reference_is_to_recv = 0;
+
+ ref_data = type->category.ref_data;
+
+ /* Get the referenced type locally or from peer */
+ sub_type = ref_data.type;
+ if (sub_type == NULL) {
+ int ref_code;
+ gras_dd_recv_int(sock, r_arch, &ref_code);
+ sub_type = gras_datadesc_by_id(ref_code);
+ }
+
+ /* Get the actual value of the pointer for cycle handling */
+ if (!pointer_type) {
+ pointer_type = gras_datadesc_by_name("data pointer");
+ xbt_assert(pointer_type);
+ }
+
+ r_ref = xbt_malloc(pointer_type->size[r_arch]);
+
+ gras_trp_recv(sock, (char*)r_ref,
+ pointer_type->size[r_arch]);
+
+ /* Receive the pointed data only if not already sent */
+ if (gras_dd_is_r_null(r_ref, pointer_type->size[r_arch])) {
+ VERB1("Not receiving data remotely referenced @%p since it's NULL",
+ *(void **)r_ref);
+ *(void**)l_data = NULL;
+ free(r_ref);
+ break;
+ }
+
+ reference_is_to_recv = 0;
+ TRY {
+ if (detect_cycle) {
+ l_ref = xbt_dict_get_ext(refs, (char*)r_ref, pointer_type->size[r_arch]);
+ } else {
+ reference_is_to_recv = 1;
+ }
+ } CATCH(e) {
+ if (e.category != not_found_error)
+ RETHROW;
+ reference_is_to_recv = 1;
+ xbt_ex_free(e);
+ }
+
+ if (reference_is_to_recv) {
+ int subsubcount = -1;
+ void *l_referenced=NULL;
+
+ VERB2("Receiving a ref to '%s', remotely @%p",
+ sub_type->name, *(void**)r_ref);
+ if (sub_type->category_code == e_gras_datadesc_type_cat_array) {
+ /* Damn. Reference to a dynamic array. Allocating the space for it is more complicated */
+ gras_dd_cat_array_t array_data = sub_type->category.array_data;
+ gras_datadesc_type_t subsub_type;
+
+ subsubcount = array_data.fixed_size;
+ if (subsubcount == -1)
+ gras_dd_recv_int(sock, r_arch, &subsubcount);
+
+ subsub_type = array_data.type;
+
+ if (subsubcount != 0)
+ gras_dd_alloc_ref(refs,
+ subsub_type->size[GRAS_THISARCH] * subsubcount,
+ r_ref,pointer_type->size[r_arch],
+ (char**)&l_referenced,
+ detect_cycle);
+ else
+ l_referenced = NULL;
+ } else {
+ gras_dd_alloc_ref(refs,sub_type->size[GRAS_THISARCH],
+ r_ref,pointer_type->size[r_arch],
+ (char**)&l_referenced,
+ detect_cycle);
+ }
+
+ if (l_referenced != NULL)
+ gras_datadesc_recv_rec(sock,state,refs, sub_type,
+ r_arch,r_ref,pointer_type->size[r_arch],
+ (char*)l_referenced, subsubcount,
+ detect_cycle || sub_type->cycle);
+
+ *(void**)l_data=l_referenced;
+ VERB3("'%s' remotely referenced at %p locally at %p",
+ sub_type->name, *(void**)r_ref, l_referenced);
+
+ } else {
+ VERB2("NOT receiving data remotely referenced @%p (already done, @%p here)",
+ *(void**)r_ref, *(void**)l_ref);
+
+ *(void**)l_data=*l_ref;
+
+ }
+ free(r_ref);
+ break;
+ }
+
+ case e_gras_datadesc_type_cat_array: {
+ gras_dd_cat_array_t array_data;
+ int count;
+ char *ptr;
+ long int elm_size;
+
+ array_data = type->category.array_data;
+ /* determine element count locally, or from caller, or from peer */
+ count = array_data.fixed_size;
+ if (count == -1)
+ count = subsize;
+ if (count == -1)
+ gras_dd_recv_int(sock, r_arch, &count);
+ if (count == -1)
+ THROW1(mismatch_error,0,
+ "Invalid (=-1) array size for type %s",type->name);
+
+ /* receive the content */
+ sub_type = array_data.type;
+ if (sub_type->category_code == e_gras_datadesc_type_cat_scalar) {
+ VERB1("Array of %d scalars, get it in one shoot", count);
+ if (sub_type->aligned_size[GRAS_THISARCH] >=
+ sub_type->aligned_size[r_arch]) {
+ gras_trp_recv(sock, (char*)l_data,
+ sub_type->aligned_size[r_arch] * count);
+ if (r_arch != GRAS_THISARCH)
+ gras_dd_convert_elm(sub_type,count,r_arch, l_data,l_data);
+ } else {
+ ptr = xbt_malloc(sub_type->aligned_size[r_arch] * count);
+
+ gras_trp_recv(sock, (char*)ptr,
+ sub_type->size[r_arch] * count);
+ if (r_arch != GRAS_THISARCH)
+ gras_dd_convert_elm(sub_type,count,r_arch, ptr,l_data);
+ free(ptr);
+ }
+ } else if (sub_type->category_code == e_gras_datadesc_type_cat_array &&
+ sub_type->category.array_data.fixed_size >= 0 &&
+ sub_type->category.array_data.type->category_code == e_gras_datadesc_type_cat_scalar) {
+ gras_datadesc_type_t subsub_type;
+ array_data = sub_type->category.array_data;
+ subsub_type = array_data.type;
+
+ VERB1("Array of %d fixed array of scalars, get it in one shot",count);
+ if (subsub_type->aligned_size[GRAS_THISARCH] >=
+ subsub_type->aligned_size[r_arch]) {
+ gras_trp_recv(sock, (char*)l_data,
+ subsub_type->aligned_size[r_arch] * count *
+ array_data.fixed_size);
+ if (r_arch != GRAS_THISARCH)
+ gras_dd_convert_elm(subsub_type,count*array_data.fixed_size,r_arch, l_data,l_data);
+ } else {
+ ptr = xbt_malloc(subsub_type->aligned_size[r_arch] * count*array_data.fixed_size);
+
+ gras_trp_recv(sock, (char*)ptr,
+ subsub_type->size[r_arch] * count*array_data.fixed_size);
+ if (r_arch != GRAS_THISARCH)
+ gras_dd_convert_elm(subsub_type,count*array_data.fixed_size,r_arch, ptr,l_data);
+ free(ptr);
+ }
+
+
+ } else {
+ /* not scalar content, get it recursively (may contain pointers) */
+ elm_size = sub_type->aligned_size[GRAS_THISARCH];
+ VERB2("Receive a %d-long array of %s",count, sub_type->name);
+
+ ptr = l_data;
+ for (cpt=0; cpt<count; cpt++) {
+ gras_datadesc_recv_rec(sock,state,refs, sub_type,
+ r_arch, NULL, 0, ptr,-1,
+ detect_cycle || sub_type->cycle);
+
+ ptr += elm_size;
+ }
+ }
+ break;
+ }
+
+ default:
+ xbt_die("Invalid type");
+ }
+
+ if (type->recv)
+ type->recv(type,state,l_data);
+
+ if (!strcmp(type->name,"string"))
+ VERB1("value: '%s'",*(char**)l_data);
+