From 40bdb11e84c563daed9fcd3d81e7ad5971f3367b Mon Sep 17 00:00:00 2001 From: mquinson Date: Mon, 22 May 2006 20:37:35 +0000 Subject: [PATCH] Allow to exchange fixed-sized arrays which size is \#defined git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@2283 48e7efb5-ca39-0410-a469-dd3cf9ba447f --- include/gras/datadesc.h | 36 +++++++++++++++++++++++++--- src/gras/DataDesc/datadesc.c | 3 +++ src/gras/DataDesc/datadesc_private.h | 5 ++++ src/gras/DataDesc/ddt_parse.c | 21 ++++++++++++++-- 4 files changed, 60 insertions(+), 5 deletions(-) diff --git a/include/gras/datadesc.h b/include/gras/datadesc.h index 1e49d2a3e8..7d9e037cd8 100644 --- a/include/gras/datadesc.h +++ b/include/gras/datadesc.h @@ -107,9 +107,35 @@ gras_datadesc_type_t gras_datadesc_by_name(const char *name); * * \warning Since GRAS_DEFINE_TYPE is a macro, you shouldn't put any comma in your type definition * (comma separates macro args). For example, change \verbatim int a, b;\endverbatim to \verbatim int a; - int b;\endverbatim +int b;\endverbatim * - *

Defining multidimentional arrays

+ * \section gras_dd_define \#define and fixed size array + * + * If you want to exchange arrays which size is given at compilation time by a + * \#defined constant, you need to keep GRAS informed. It would be done the + * following way: + +\verbatim #define BLOCK_SIZE 32 +GRAS_DEFINE_TYPE(s_toto, +struct { + double data[BLOCK_SIZE]; +} s_toto;) + +void register_messages() { + gras_datadesc_type_t toto_type; + + gras_datadesc_set_const("BLOCK_SIZE",BLOCK_SIZE); + toto_type = gras_datadesc_by_symbol(s_toto); +}\endverbatim + * + * The form gras_datadesc_set_const("BLOCK_SIZE",BLOCK_SIZE); ensures + * that when you change the definition of the constant, GRAS keeps informed of + * the right value. Passing the numerical value of the constant as second + * argument would be a bad idea to that regard. Of course, the call to + * gras_datadesc_set_const() should come before any gras_datadesc_by_symbol() + * containing references to it. + * + * \section GRAS_dd_multidim Defining multidimentional arrays * * The mecanism for multidimensional arrays is known to be fragile and cumbersome. If you want to use it, * you have to understand how it is implemented: the multiplication is performed using the sizes stack. In previous example, @@ -128,7 +154,7 @@ gras_datadesc_type_t gras_datadesc_by_name(const char *name); * If you cannot express your datadescs with this mechanism, you'll have to use the more advanced * (and somehow complex) one described in the \ref GRAS_dd_cb_full. * - *

Projects spanning over multiple files

+ * \section GRAS_dd_multifile Projects spanning over multiple files * * GRAS_DEFINE_TYPE declares some symbols to work, it needs some special * care when used in several files. In such case, you want the regular type @@ -215,6 +241,10 @@ int server(int argc, char *argv[]) { * @brief Add an annotation to a type to be automatically parsed */ #define GRAS_ANNOTE(key,val) + +/** @brief Defines the value of a define to the datatype parsing infrastructure + */ +void gras_datadesc_set_const(const char*name, int value); /* @} */ diff --git a/src/gras/DataDesc/datadesc.c b/src/gras/DataDesc/datadesc.c index bedca88a9b..bca8502d82 100644 --- a/src/gras/DataDesc/datadesc.c +++ b/src/gras/DataDesc/datadesc.c @@ -140,6 +140,9 @@ gras_datadesc_init(void) { gras_datadesc_struct_close(ddt); ddt = gras_datadesc_ref("xbt_host_t",ddt); + + /* Dict containing the constant value (for the parsing macro) */ + gras_dd_constants = xbt_dict_new(); } diff --git a/src/gras/DataDesc/datadesc_private.h b/src/gras/DataDesc/datadesc_private.h index 82d77a826e..946a0e742a 100644 --- a/src/gras/DataDesc/datadesc_private.h +++ b/src/gras/DataDesc/datadesc_private.h @@ -258,5 +258,10 @@ gras_dd_convert_elm(gras_datadesc_type_t type, int count, int r_arch, void *src, void *dst); +/******************************************************************** + * Dictionnary containing the constant values for the parsing macro * + ********************************************************************/ +extern xbt_dict_t gras_dd_constants; /* lives in ddt_parse.c of course */ + #endif /* GRAS_DATADESC_PRIVATE_H */ diff --git a/src/gras/DataDesc/ddt_parse.c b/src/gras/DataDesc/ddt_parse.c index 341767e09c..7cb63fcc66 100644 --- a/src/gras/DataDesc/ddt_parse.c +++ b/src/gras/DataDesc/ddt_parse.c @@ -315,8 +315,16 @@ static void parse_statement(char *definition, char *end; long int size=strtol(gras_ddt_parse_text, &end, 10); - if (end == gras_ddt_parse_text || *end != '\0') - PARSE_ERROR1("Unparsable size of array (found '%c', expected number)",*end); + if (end == gras_ddt_parse_text || *end != '\0') { + /* Not a number. Get the constant value, if any */ + int *storage=xbt_dict_get_or_null(gras_dd_constants,gras_ddt_parse_text); + if (storage) { + size = *storage; + } else { + PARSE_ERROR1("Unparsable size of array. Found '%s', expected number or known constant. Need to use gras_datadesc_set_const(), huh?", + gras_ddt_parse_text); + } + } /* replace the previously pushed type to an array of it */ change_to_fixed_array(identifiers,size); @@ -645,3 +653,12 @@ gras_datadesc_parse(const char *name, XBT_OUT; return res; } + +xbt_dict_t gras_dd_constants; +/** \brief Declare a constant to the parsing mecanism. See the "\#define and fixed size array" section */ +void gras_datadesc_set_const(const char*name, int value) { + int *stored = xbt_new(int, 1); + *stored=value; + + xbt_dict_set(gras_dd_constants,name, stored, free); +} -- 2.20.1