X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/35471ecdf533b2701ca99d3921c42944531883ce..155479dd57524928e1cc8440b70daf44ec143f1c:/src/gras/DataDesc/ddt_create.c diff --git a/src/gras/DataDesc/ddt_create.c b/src/gras/DataDesc/ddt_create.c index 6caa963b6f..ee432e195b 100644 --- a/src/gras/DataDesc/ddt_create.c +++ b/src/gras/DataDesc/ddt_create.c @@ -13,7 +13,7 @@ #include "xbt/ex.h" #include "gras/DataDesc/datadesc_private.h" -XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ddt_create,datadesc,"Creating new datadescriptions"); +XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_ddt_create,gras_ddt,"Creating new datadescriptions"); /*** prototypes ***/ static gras_dd_cat_field_t @@ -48,20 +48,37 @@ static gras_datadesc_type_t gras_ddt_new(const char *name) { return res; } +/** @brief retrieve an existing message type from its name (or NULL if it does not exist). */ +gras_datadesc_type_t gras_datadesc_by_name_or_null (const char *name) { + xbt_ex_t e; + gras_datadesc_type_t res = NULL; + + TRY { + res = gras_datadesc_by_name(name); + } CATCH(e) { + res = NULL; + xbt_ex_free(e); + } + return res; +} /** - * This returns NULL no type of this name can be found instead of throwing exceptions which would complicate the GRAS_DEFINE_TYPE macro + * Search the given datadesc (or raises an exception if it can't be found) */ gras_datadesc_type_t gras_datadesc_by_name(const char *name) { xbt_ex_t e; gras_datadesc_type_t res = NULL; + volatile int found = 0; TRY { res = (gras_datadesc_type_t)xbt_set_get_by_name(gras_datadesc_set_local,name); + found = 1; } CATCH(e) { if (e.category != not_found_error) RETHROW; xbt_ex_free(e); - res = NULL; } + if (!found) + THROW1(not_found_error,0,"No registred datatype of that name: %s",name); + return res; } @@ -94,7 +111,7 @@ gras_datadesc_type_t long int arch; XBT_IN; - res = gras_datadesc_by_name(name); + res = gras_datadesc_by_name_or_null(name); if (res) { xbt_assert1(res->category_code == e_gras_datadesc_type_cat_scalar, "Redefinition of type %s does not match", name); @@ -110,7 +127,7 @@ gras_datadesc_type_t for (arch = 0; arch < gras_arch_count; arch ++) { res->size[arch] = gras_arches[arch].sizeofs[type]; res->alignment[arch] = gras_arches[arch].boundaries[type]; - res->aligned_size[arch] = aligned(res->size[arch], res->alignment[arch]); + res->aligned_size[arch] = ddt_aligned(res->size[arch], res->alignment[arch]); } res->category_code = e_gras_datadesc_type_cat_scalar; @@ -142,7 +159,7 @@ gras_datadesc_type_t long int arch; XBT_IN1("(%s)",name); - res = gras_datadesc_by_name(name); + res = gras_datadesc_by_name_or_null(name); if (res) { /* FIXME: Check that field redefinition matches */ xbt_assert1(res->category_code == e_gras_datadesc_type_cat_struct, @@ -176,7 +193,7 @@ gras_datadesc_struct_append(gras_datadesc_type_t struct_type, int arch; xbt_assert2(field_type, - "Cannot add the field '%s' into struct '%s': its type is NULL. Typo in get_by_name?", + "Cannot add the field '%s' into struct '%s': its type is NULL", name,struct_type->name); XBT_IN3("(%s %s.%s;)",field_type->name,struct_type->name,name); if (struct_type->category.struct_data.closed) { @@ -200,14 +217,14 @@ gras_datadesc_struct_append(gras_datadesc_type_t struct_type, for (arch=0; archoffset[arch] = aligned(struct_type->size[arch], - field_type->alignment[arch]); + field->offset[arch] = ddt_aligned(struct_type->size[arch], + field_type->alignment[arch]); struct_type->size[arch] = field->offset[arch] + field_type->size[arch]; struct_type->alignment[arch] = max(struct_type->alignment[arch], field_type->alignment[arch]); - struct_type->aligned_size[arch] = aligned(struct_type->size[arch], - struct_type->alignment[arch]); + struct_type->aligned_size[arch] = ddt_aligned(struct_type->size[arch], + struct_type->alignment[arch]); } field->type = field_type; field->send = NULL; @@ -281,7 +298,7 @@ gras_datadesc_type_t xbt_assert0(selector, "Attempt to creat an union without field_count function"); - res = gras_datadesc_by_name(name); + res = gras_datadesc_by_name_or_null(name); if (res) { /* FIXME: Check that field redefinition matches */ xbt_assert1(res->category_code == e_gras_datadesc_type_cat_union, @@ -341,8 +358,8 @@ void gras_datadesc_union_append(gras_datadesc_type_t union_type, field_type->size[arch]); union_type->alignment[arch] = max(union_type->alignment[arch], field_type->alignment[arch]); - union_type->aligned_size[arch] = aligned(union_type->size[arch], - union_type->alignment[arch]); + union_type->aligned_size[arch] = ddt_aligned(union_type->size[arch], + union_type->alignment[arch]); } } @@ -356,6 +373,22 @@ gras_datadesc_union_close(gras_datadesc_type_t union_type) { union_type->category.union_data.closed = 1; } +/** \brief Copy a type under another name + * + * This may reveal useful to circumvent parsing macro limitations + */ +gras_datadesc_type_t + gras_datadesc_copy(const char *name, + gras_datadesc_type_t copied) { + + gras_datadesc_type_t res = gras_ddt_new(name); + char *name_cpy = res->name; + + memcpy(res,copied,sizeof(s_gras_datadesc_type_t)); + res->name = name_cpy; + return res; + } + /** \brief Declare a new type being a reference to the one passed in arg */ gras_datadesc_type_t gras_datadesc_ref(const char *name, @@ -366,7 +399,7 @@ gras_datadesc_type_t int arch; XBT_IN1("(%s)",name); - res = gras_datadesc_by_name(name); + res = gras_datadesc_by_name_or_null(name); if (res) { xbt_assert1(res->category_code == e_gras_datadesc_type_cat_ref, "Redefinition of %s does not match",name); @@ -409,7 +442,7 @@ gras_datadesc_type_t int arch; XBT_IN1("(%s)",name); - res = gras_datadesc_by_name(name); + res = gras_datadesc_by_name_or_null(name); if (res) { xbt_assert1(res->category_code == e_gras_datadesc_type_cat_ref, @@ -449,7 +482,7 @@ gras_datadesc_type_t int arch; XBT_IN1("(%s)",name); - res = gras_datadesc_by_name(name); + res = gras_datadesc_by_name_or_null(name); if (res) { xbt_assert1(res->category_code == e_gras_datadesc_type_cat_array, "Redefinition of type %s does not match", name); @@ -499,7 +532,7 @@ gras_datadesc_type_t gras_datadesc_array_dyn(const char *name, "'%s' is a dynamic array without size discriminant", name); - res = gras_datadesc_by_name(name); + res = gras_datadesc_by_name_or_null(name); if (res) { xbt_assert1(res->category_code == e_gras_datadesc_type_cat_array, "Redefinition of type %s does not match", name); @@ -572,6 +605,12 @@ gras_datadesc_type_t return res; } +/* + *## + *## Constructor of container datatypes + *## + */ + #include "xbt/dynar_private.h" static void gras_datadesc_dynar_cb(gras_datadesc_type_t typedesc, gras_cbps_t vars, void *data) { gras_datadesc_type_t subtype; @@ -590,8 +629,18 @@ static void gras_datadesc_dynar_cb(gras_datadesc_type_t typedesc, gras_cbps_t va dynar->elmsize = subtype->size[GRAS_THISARCH]; dynar->size = dynar->used; + dynar->mutex = NULL; } +/** \brief Declare a new type being a dynar in which each elements are of the given type + * + * The type gets registered under the name "dynar(%s)_s", where %s is the name of the subtype. + * For example, a dynar of doubles will be called "dynar(double)_s" and a dynar of dynar of + * strings will be called "dynar(dynar(string)_s)_s". + * + * \param elm_t: the datadesc of the elements + * \param free_func: the function to use to free the elements when the dynar gets freed + */ gras_datadesc_type_t gras_datadesc_dynar(gras_datadesc_type_t elm_t, void_f_pvoid_t *free_func) { @@ -599,34 +648,96 @@ gras_datadesc_dynar(gras_datadesc_type_t elm_t, char *buffname; gras_datadesc_type_t res; - buffname=xbt_new0(char, strlen(elm_t->name)+10); - sprintf(buffname,"dynar(%s)_s",elm_t->name); + asprintf(&buffname,"s_xbt_dynar_of_%s",elm_t->name); res = gras_datadesc_struct(buffname); - gras_datadesc_struct_append(res, "size", gras_datadesc_by_name("unsigned long int")); + gras_datadesc_struct_append(res, "size", + gras_datadesc_by_name("unsigned long int")); - gras_datadesc_struct_append(res, "used", gras_datadesc_by_name("unsigned long int")); - gras_datadesc_cb_field_push(res, "used"); + gras_datadesc_struct_append(res, "used", + gras_datadesc_by_name("unsigned long int")); - gras_datadesc_struct_append(res, "elmsize", gras_datadesc_by_name("unsigned long int")); + gras_datadesc_struct_append(res, "elmsize", + gras_datadesc_by_name("unsigned long int")); - gras_datadesc_struct_append(res, "data", gras_datadesc_ref_pop_arr (elm_t)); + gras_datadesc_struct_append(res, "data", + gras_datadesc_ref_pop_arr (elm_t)); - gras_datadesc_struct_append(res, "free_f", gras_datadesc_by_name("function pointer")); + gras_datadesc_struct_append(res, "free_f", + gras_datadesc_by_name("function pointer")); memcpy(res->extra,&free_func,sizeof(free_func)); + gras_datadesc_struct_append(res, "mutex", + gras_datadesc_by_name("data pointer")); + gras_datadesc_struct_close(res); + gras_datadesc_cb_field_push(res, "used"); gras_datadesc_cb_recv(res, &gras_datadesc_dynar_cb); /* build a ref to it */ - sprintf(buffname,"dynar(%s)",elm_t->name); + free(buffname); + asprintf(&buffname,"xbt_dynar_of_%s",elm_t->name); res=gras_datadesc_ref(buffname,res); free(buffname); return res; } +#include "xbt/matrix.h" +static void gras_datadesc_matrix_cb(gras_datadesc_type_t typedesc, gras_cbps_t vars, void *data) { + gras_datadesc_type_t subtype; + xbt_matrix_t matrix=(xbt_matrix_t)data; + + memcpy(&matrix->free_f, &typedesc->extra, sizeof(matrix->free_f)); + + /* search for the elemsize in what we have. If elements are "int", typedesc got is "int[]*" */ + subtype = gras_dd_find_field(typedesc,"data")->type; + + /* this is now a ref to array of what we're looking for */ + subtype = subtype->category.ref_data.type; + subtype = subtype->category.array_data.type; + + DEBUG1("subtype is %s",subtype->name); + + matrix->elmsize = subtype->size[GRAS_THISARCH]; +} +gras_datadesc_type_t +gras_datadesc_matrix(gras_datadesc_type_t elm_t, + void_f_pvoid_t * const free_f) { + char *buffname; + gras_datadesc_type_t res; + + asprintf(&buffname,"s_xbt_matrix_t(%s)",elm_t->name); + res = gras_datadesc_struct(buffname); + + gras_datadesc_struct_append(res, "lines", + gras_datadesc_by_name("unsigned int")); + gras_datadesc_struct_append(res, "rows", + gras_datadesc_by_name("unsigned int")); + + gras_datadesc_struct_append(res, "elmsize", + gras_datadesc_by_name("unsigned long int")); + + gras_datadesc_struct_append(res, "data", + gras_datadesc_ref_pop_arr (elm_t)); + gras_datadesc_struct_append(res, "free_f", + gras_datadesc_by_name("function pointer")); + gras_datadesc_struct_close(res); + + gras_datadesc_cb_field_push(res, "lines"); + gras_datadesc_cb_field_push_multiplier(res, "rows"); + + gras_datadesc_cb_recv(res, &gras_datadesc_matrix_cb); + memcpy(res->extra,&free_f,sizeof(free_f)); + + /* build a ref to it */ + free(buffname); + asprintf(&buffname,"xbt_matrix_t(%s)",elm_t->name); + res=gras_datadesc_ref(buffname,res); + free(buffname); + return res; +} gras_datadesc_type_t gras_datadesc_import_nws(const char *name, @@ -675,7 +786,7 @@ static gras_dd_cat_field_t return field; } } - ERROR2("No field nammed %s in %s",field_name,type->name); + ERROR2("No field named '%s' in '%s'",field_name,type->name); xbt_abort(); } @@ -692,6 +803,7 @@ void gras_datadesc_cb_field_send (gras_datadesc_type_t type, field->send = send; } + /** * The value, which must be an int, unsigned int, long int or unsigned long int * is pushed to the stacks of sizes and can then be retrieved with @@ -719,6 +831,38 @@ void gras_datadesc_cb_field_push (gras_datadesc_type_t type, xbt_abort(); } } + +/** + * Any previously pushed value is poped and the field value is multiplied to + * it. The result is then pushed back into the stack of sizes. It can then be + * retrieved with \ref gras_datadesc_ref_pop_arr or directly with \ref + * gras_cbps_i_pop. + * + * The field must be an int, unsigned int, long int or unsigned long int. + */ +void gras_datadesc_cb_field_push_multiplier (gras_datadesc_type_t type, + const char *field_name) { + + gras_dd_cat_field_t field=gras_dd_find_field(type,field_name); + gras_datadesc_type_t sub_type=field->type; + + DEBUG3("add a MPUSHy cb to '%s' field (type '%s') of '%s'", + field_name,sub_type->name,type->name); + if (!strcmp("int",sub_type->name)) { + field->send = gras_datadesc_cb_push_int_mult; + } else if (!strcmp("unsigned int",sub_type->name)) { + field->send = gras_datadesc_cb_push_uint_mult; + } else if (!strcmp("long int",sub_type->name)) { + field->send = gras_datadesc_cb_push_lint_mult; + } else if (!strcmp("unsigned long int",sub_type->name)) { + field->send = gras_datadesc_cb_push_ulint_mult; + } else { + ERROR1("Field %s is not an int, unsigned int, long int neither unsigned long int", + sub_type->name); + xbt_abort(); + } +} + /** * The given datadesc must be a struct or union (abort if not). * (useful to put the function pointers to the right value, for example)