Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
allow the size of multidimentional objects to be given thru annotations in parsing...
[simgrid.git] / include / gras / datadesc.h
1 /* $Id$ */
2
3 /* gras/datadesc.h - Describing the data you want to exchange               */
4
5 /* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved.            */
6
7 /* This program is free software; you can redistribute it and/or modify it
8  * under the terms of the license (GNU LGPL) which comes with this package. */
9
10 #ifndef GRAS_DATADESC_H
11 #define GRAS_DATADESC_H
12
13 #include "xbt/misc.h" /* SG_BEGIN_DECL */
14 #include "xbt/dynar.h" /* void_f_pvoid_t */
15
16 SG_BEGIN_DECL()
17
18 /** @addtogroup GRAS_dd Data description
19  *  @brief Describing data to be exchanged (Communication facility)
20  *
21  * @section Overview
22  *
23  * Since GRAS takes care of potential representation conversion when the platform is heterogeneous, 
24  * any data which transits on the network must be described beforehand.
25  * 
26  * There is several possible interfaces for this, ranging from the really completely automatic parsing to 
27  * completely manual. Let's study each of them from the simplest to the more advanced.
28  * 
29  * \warning At least, I would like to present those sections in the right order, but doxygen prevents me 
30  * from doing so. There is a weird bug I fail to circumvent here. The right order is naturally:
31  *   -# basic operations
32  *   -# Automatic parsing
33  *   -# Simple manual definitions
34  *   -# Callback Persistant State: Simple push/pop mechanism
35  *   -# Callback Persistant State: Full featured mechanism
36  */
37 /* @{*/
38
39 /** @name 1. basic operations
40  *
41  * If you only want to send pre-existing types, simply retrieve the pre-defined description with 
42  * the \ref gras_datadesc_by_name function. Existing types entail:
43  *  - char (both signed and unsigned)
44  *  - int (short, regular, long and long long, both signed and unsigned)
45  *  - float and double
46  *  - string (which is indeed a reference to a dynamically sized array of char, strlen being used to retrive the size)
47  * 
48  * Example:\verbatim gras_datadesc_type_t i = gras_datadesc_by_name("int");
49  gras_datadesc_type_t uc = gras_datadesc_by_name("unsigned char");
50  gras_datadesc_type_t str = gras_datadesc_by_name("string");\endverbatim
51  */
52 /* @{ */
53   
54 /** @brief Opaque type describing a type description. */
55 typedef struct s_gras_datadesc_type *gras_datadesc_type_t;
56
57 /** \brief Search a type description from its name */
58 gras_datadesc_type_t gras_datadesc_by_name(const char *name);
59
60 /* @} */
61     
62 /** @name 2. Automatic parsing
63  * 
64  *  If you need to declare a new datatype, this is the simplest way to describe it to GRAS. Simply
65  *  enclose its type definition  into a \ref GRAS_DEFINE_TYPE macro call, and you're set. Here is 
66  *  an type declaration  example: \verbatim GRAS_DEFINE_TYPE(mytype,struct mytype {
67    int myfirstfield;
68    char mysecondfield;
69  });\endverbatim
70  *  The type is then both copied verbatim into your source file and stored for further parsing. This allows
71  *  you to let GRAS parse the exact version you are actually using in your program.
72  *  You can then retrieve the corresponding type description with \ref gras_datadesc_by_symbol.
73  *  Don't worry too much for the performances, the type is only parsed once and a binary representation 
74  *  is stored and used in any subsequent calls.
75  * 
76  *  If your structure contains any pointer, you have to explain GRAS the size of the pointed array. This
77  *  can be 1 in the case of simple references, or more in the case of regular arrays. For that, use the 
78  *  \ref GRAS_ANNOTE macro within the type declaration you are passing to \ref GRAS_DEFINE_TYPE. This macro
79  *  rewrites itself to nothing in the declaration (so they won't pollute the type definition copied verbatim
80  *  into your code), and give some information to GRAS about your pointer. 
81  
82  *  GRAS_ANNOTE takes two arguments being the key name and the key value. For now, the only accepted key name 
83  *  is "size", to specify the length of the pointed array. It can either be:
84  *    - the string "1" (without the quote),
85  *    - the name of another field of the structure
86  *    - a sort of computed expression for multidimensional arrays (see below -- pay attention to the warnings below).
87  *  
88  *  Here is an example:\verbatim GRAS_DEFINE_TYPE(s_clause,
89   struct s_array {
90     struct s_array *father GRAS_ANNOTE(size,1);
91     int length;
92     int *data GRAS_ANNOTE(size,length);
93     int rows;
94     int cols;
95     int *matrix GRAS_ANNOTE(size,rows*cols);
96  }
97 ;)\endverbatim
98  * It specifies that the structure s_array contains five fields, that the \a father field is a simple reference,
99  * that the size of the array pointed by \a data is the \a length field, and that the \a matrix field is an array
100  * which size is the result of \a rows times \a cols.
101  * 
102  *  \warning The mecanism for multidimensional arrays is known to be fragile and cumbersome. If you want to use it, 
103  *  you have to understand how it is implemented: the multiplication is performed using the sizes stack. In previous example,
104  *  a \ref gras_datadesc_cb_push_int callback is added to the \a rows field and a \ref gras_datadesc_cb_push_int_mult one is 
105  *  added to \a cols. So, when the structure is sent, the rows field push its value onto the stack, then the \a cols field 
106  *  retrieve this value from the stack, compute (and push) the multiplication value. The \a matrix field can then retrive this
107  *  value by poping the array. There is several ways for this to go wrong:
108  *   - if the matrix field is placed before the sizes, the right value won't get pushed into the stack soon enough. Reorder your structure fields if needed.
109  *   - if you write GRAS_ANNOTE(size,cols*rows); in previous example (inverting rows and cols in annotation),
110  *     \a rows will be given a \ref gras_datadesc_cb_push_int_mult. This cannot work since it will try to 
111  *     pop the value which will be pushed by \a cols <i>afterward</i>.
112  *   - if you have more than one matrix in your structure, don't interleave the size. They are pushed/poped in the structure order.
113  *   - if some of the sizes are used in more than one matrix, you cannot use this mecanism -- sorry.
114  *
115  * If you cannot express your datadescs with this mechanism, you'll have to use the more advanced 
116  * (and somehow complex) one described below.
117  *
118  *  \warning Since GRAS_DEFINE_TYPE is a macro, you shouldn't put any comma in your type definition 
119  *  (comma separates macro args). For example, change \verbatim int a, b;\endverbatim to \verbatim int a;
120  int b;\endverbatim
121  */
122 /** @{ */
123
124  
125 /**   @brief Automatically parse C code
126  *    @hideinitializer
127  */
128 #define GRAS_DEFINE_TYPE(name,def) \
129   static const char * _gras_this_type_symbol_does_not_exist__##name=#def; def
130  
131 /** @brief Retrieve a datadesc which was previously parsed 
132  *  @hideinitializer
133  */
134 #define gras_datadesc_by_symbol(name)  \
135   (gras_datadesc_by_name(#name) ?      \
136    gras_datadesc_by_name(#name) :      \
137      gras_datadesc_parse(#name,        \
138                          _gras_this_type_symbol_does_not_exist__##name) \
139   )
140
141 /** @def GRAS_ANNOTE
142  *  @brief Add an annotation to a type to be automatically parsed
143  */
144 #define GRAS_ANNOTE(key,val)
145
146 /* @} */
147
148 gras_datadesc_type_t 
149 gras_datadesc_parse(const char *name, const char *C_statement);
150
151 /** @name 3. Simple manual definitions
152  * 
153  * Here are the functions to use if you want to declare your description manually. 
154  * The function names should be self-explanatory in most cases.
155  * 
156  * You can add callbacks to the datatypes doing any kind of action you may want. Usually, 
157  * pre-send callbacks are used to prepare the type expedition while post-receive callbacks 
158  * are used to fix any issue after the receive.
159  * 
160  * If your types are dynamic, you'll need to add some extra callback. For example, there is a
161  * specific callback for the string type which is in charge of computing the length of the char
162  * array. This is done with the cbps mechanism, explained in next section.
163  * 
164  * If your types may contain pointer cycle, you must specify it to GRAS using the @ref gras_datadesc_cycle_set. 
165  * 
166  * Example:\verbatim
167  typedef struct {
168    unsigned char c1;
169    unsigned long int l1;
170    unsigned char c2;
171    unsigned long int l2;
172  } mystruct;
173  [...]
174   my_type=gras_datadesc_struct("mystruct");
175   gras_datadesc_struct_append(my_type,"c1", gras_datadesc_by_name("unsigned char"));
176   gras_datadesc_struct_append(my_type,"l1", gras_datadesc_by_name("unsigned long"));
177   gras_datadesc_struct_append(my_type,"c2", gras_datadesc_by_name("unsigned char"));
178   gras_datadesc_struct_append(my_type,"l2", gras_datadesc_by_name("unsigned long int"));
179   gras_datadesc_struct_close(my_type);
180
181   my_type=gras_datadesc_ref("mystruct*", gras_datadesc_by_name("mystruct"));
182   
183   [Use my_type to send pointers to mystruct data]\endverbatim
184  */
185 /* @{ */
186
187
188 /** \brief Opaque type describing a type description callback persistant state. */
189 typedef struct s_gras_cbps *gras_cbps_t;
190
191 /* callbacks prototypes */
192 /** \brief Prototype of type callbacks returning nothing. */
193 typedef void (*gras_datadesc_type_cb_void_t)(gras_datadesc_type_t typedesc, gras_cbps_t vars, void *data);
194 /** \brief Prototype of type callbacks returning an int. */
195 typedef int (*gras_datadesc_type_cb_int_t)(gras_datadesc_type_t typedesc, gras_cbps_t vars, void *data);
196 /** \brief Prototype of type callbacks selecting a type. */
197 typedef gras_datadesc_type_t (*gras_datadesc_selector_t)(gras_datadesc_type_t typedesc, gras_cbps_t vars, void *data);
198
199
200 /******************************************
201  **** Declare datadescription yourself ****
202  ******************************************/
203
204 gras_datadesc_type_t gras_datadesc_struct(const char *name);
205 void gras_datadesc_struct_append(gras_datadesc_type_t  struct_type,
206                                  const char           *name,
207                                  gras_datadesc_type_t  field_type);
208 void gras_datadesc_struct_close(gras_datadesc_type_t   struct_type);
209
210
211 gras_datadesc_type_t gras_datadesc_union(const char                 *name,
212                                          gras_datadesc_type_cb_int_t selector);
213 void gras_datadesc_union_append(gras_datadesc_type_t   union_type,
214                                 const char            *name,
215                                 gras_datadesc_type_t   field_type);
216 void gras_datadesc_union_close(gras_datadesc_type_t    union_type);
217
218
219 gras_datadesc_type_t 
220   gras_datadesc_ref(const char          *name,
221                     gras_datadesc_type_t referenced_type);
222 gras_datadesc_type_t 
223   gras_datadesc_ref_generic(const char              *name,
224                             gras_datadesc_selector_t selector);
225
226 gras_datadesc_type_t 
227   gras_datadesc_array_fixed(const char          *name,
228                             gras_datadesc_type_t element_type,
229                             long int             fixed_size);
230 gras_datadesc_type_t 
231   gras_datadesc_array_dyn(const char                 *name,
232                           gras_datadesc_type_t        element_type,
233                           gras_datadesc_type_cb_int_t dynamic_size);
234 gras_datadesc_type_t 
235   gras_datadesc_ref_pop_arr(gras_datadesc_type_t  element_type);
236
237 gras_datadesc_type_t 
238   gras_datadesc_dynar(gras_datadesc_type_t elm_t,
239                       void_f_pvoid_t *free_func);
240
241 /*********************************
242  * Change stuff within datadescs *
243  *********************************/
244
245 /** \brief Specify that this type may contain cycles */
246 void gras_datadesc_cycle_set(gras_datadesc_type_t type);
247 /** \brief Specify that this type do not contain any cycles (default) */
248 void gras_datadesc_cycle_unset(gras_datadesc_type_t type);
249 /** \brief Add a pre-send callback to this datadesc. */
250 void gras_datadesc_cb_send (gras_datadesc_type_t         type,
251                             gras_datadesc_type_cb_void_t pre);
252 /** \brief Add a post-receive callback to this datadesc.*/
253 void gras_datadesc_cb_recv(gras_datadesc_type_t          type,
254                            gras_datadesc_type_cb_void_t  post);
255 /** \brief Add a pre-send callback to the given field of the datadesc */
256 void gras_datadesc_cb_field_send (gras_datadesc_type_t   type,
257                                   const char            *field_name,
258                                   gras_datadesc_type_cb_void_t  pre);
259 /** \brief Add a post-receive callback to the given field of the datadesc */
260 void gras_datadesc_cb_field_recv(gras_datadesc_type_t    type,
261                                  const char             *field_name,
262                                  gras_datadesc_type_cb_void_t  post);
263 /** \brief Add a pre-send callback to the given field resulting in its value to be pushed */
264 void gras_datadesc_cb_field_push (gras_datadesc_type_t   type,
265                                   const char            *field_name);
266 /** \brief Add a pre-send callback to the given field resulting in its value multiplied to any previously pushed value and then pushed back */
267 void gras_datadesc_cb_field_push_multiplier (gras_datadesc_type_t type,
268                                              const char          *field_name);
269
270 /******************************
271  * Get stuff within datadescs *
272  ******************************/
273 /** \brief Returns the name of a datadescription */
274 const char * gras_datadesc_get_name(gras_datadesc_type_t ddt);
275 /** \brief Returns the identifier of a datadescription */
276 int gras_datadesc_get_id(gras_datadesc_type_t ddt);
277
278 /* @} */
279
280 /** @name 4. Callback Persistant State: Simple push/pop mechanism
281  * 
282  * Sometimes, one of the callbacks need to leave information for the next ones. If this is a simple integer (such as
283  * an array size), you can use the functions described here. If not, you'll have to play with the complete cbps interface.
284  *
285  * 
286  * Here is an example:\verbatim
287 struct s_array {
288   int length;
289   int *data;
290 }
291 [...]
292 my_type=gras_datadesc_struct("s_array");
293 gras_datadesc_struct_append(my_type,"length", gras_datadesc_by_name("int"));
294 gras_datadesc_cb_field_send (my_type, "length", gras_datadesc_cb_push_int);
295
296 gras_datadesc_struct_append(my_type,"data",
297                             gras_datadesc_array_dyn ("s_array::data",gras_datadesc_by_name("int"), gras_datadesc_cb_pop));
298 gras_datadesc_struct_close(my_type);
299 \endverbatim
300
301  *
302  * The *_mult versions are intended for multi-dimensional arrays: They multiply their value to the previously pushed one 
303  * (by another field callback) and push the result of the multiplication back. An example of use follows. Please note
304  * that the first field needs a regular push callback, not a multiplier one. Think of it as a stacked calculator (man dc(1)).\verbatim
305 struct s_matrix {
306   int row;
307   int col;
308   int *data;
309 }
310 [...]
311 my_type=gras_datadesc_struct("s_matrix");
312 gras_datadesc_struct_append(my_type,"row", gras_datadesc_by_name("int"));
313 gras_datadesc_cb_field_send (my_type, "length", gras_datadesc_cb_push_int);
314 gras_datadesc_struct_append(my_type,"col", gras_datadesc_by_name("int"));
315 gras_datadesc_cb_field_send (my_type, "length", gras_datadesc_cb_push_int_mult);
316
317 gras_datadesc_struct_append(my_type,"data",
318                             gras_datadesc_array_dyn ("s_matrix::data",gras_datadesc_by_name("int"), gras_datadesc_cb_pop));
319 gras_datadesc_struct_close(my_type);
320 \endverbatim
321  
322  */
323 /* @{ */
324
325 void
326 gras_cbps_i_push(gras_cbps_t ps, int val);
327 int 
328 gras_cbps_i_pop(gras_cbps_t ps);
329
330 int gras_datadesc_cb_pop(gras_datadesc_type_t typedesc, gras_cbps_t vars, void *data);
331
332 void gras_datadesc_cb_push_int(gras_datadesc_type_t typedesc, gras_cbps_t vars, void *data);
333 void gras_datadesc_cb_push_uint(gras_datadesc_type_t typedesc, gras_cbps_t vars, void *data);
334 void gras_datadesc_cb_push_lint(gras_datadesc_type_t typedesc, gras_cbps_t vars, void *data);
335 void gras_datadesc_cb_push_ulint(gras_datadesc_type_t typedesc, gras_cbps_t vars, void *data);
336
337 void gras_datadesc_cb_push_int_mult(gras_datadesc_type_t typedesc, gras_cbps_t vars, void *data);
338 void gras_datadesc_cb_push_uint_mult(gras_datadesc_type_t typedesc, gras_cbps_t vars, void *data);
339 void gras_datadesc_cb_push_lint_mult(gras_datadesc_type_t typedesc, gras_cbps_t vars, void *data);
340 void gras_datadesc_cb_push_ulint_mult(gras_datadesc_type_t typedesc, gras_cbps_t vars, void *data);
341
342
343 /* @} */
344
345 /** @name 5. Callback Persistant State: Full featured mechanism
346  * 
347  * Sometimes, one of the callbacks need to leave information for the next ones. If the simple push/pop mechanism
348  * introduced in previous section isn't enough, you can always use this full featured one.
349  */
350
351 /* @{ */
352
353 void   gras_cbps_v_pop (gras_cbps_t            ps, 
354                         const char            *name,
355               /* OUT */ gras_datadesc_type_t  *ddt,
356               /* OUT */ void                 **res);
357 void   gras_cbps_v_push(gras_cbps_t            ps,
358                         const char            *name,
359                         void                  *data,
360                         gras_datadesc_type_t   ddt);
361 void   gras_cbps_v_set (gras_cbps_t            ps,
362                         const char            *name,
363                         void                  *data,
364                         gras_datadesc_type_t   ddt);
365
366 void * gras_cbps_v_get (gras_cbps_t            ps, 
367                         const char            *name,
368               /* OUT */ gras_datadesc_type_t  *ddt);
369
370 void gras_cbps_block_begin(gras_cbps_t ps);
371 void gras_cbps_block_end(gras_cbps_t ps);
372
373 /* @} */
374 /* @} */
375
376
377 /*******************************
378  **** About data convertion ****
379  *******************************/
380 int gras_arch_selfid(void); /* ID of this arch */
381
382
383 /*****************************
384  **** NWS datadescription * FIXME: obsolete?
385  *****************************/
386
387 /**
388  * Basic types we can embeed in DataDescriptors.
389  */
390 typedef enum
391   {CHAR_TYPE, DOUBLE_TYPE, FLOAT_TYPE, INT_TYPE, LONG_TYPE, SHORT_TYPE,
392    UNSIGNED_INT_TYPE, UNSIGNED_LONG_TYPE, UNSIGNED_SHORT_TYPE, STRUCT_TYPE}
393   DataTypes;
394 #define SIMPLE_TYPE_COUNT 9
395
396 /**  \brief Describe a collection of data.
397  * 
398 ** A description of a collection of \a type data.  \a repetitions is used only
399 ** for arrays; it contains the number of elements.  \a offset is used only for
400 ** struct members in host format; it contains the offset of the member from the
401 ** beginning of the struct, taking into account internal padding added by the
402 ** compiler for alignment purposes.  \a members, \a length, and \a tailPadding are
403 ** used only for STRUCT_TYPE data; the \a length -long array \a members describes
404 ** the members of the nested struct, and \a tailPadding indicates how many
405 ** padding bytes the compiler adds to the end of the structure.
406 */
407
408 typedef struct DataDescriptorStruct {
409   DataTypes type;
410   size_t repetitions;
411   size_t offset;
412   /*@null@*/ struct DataDescriptorStruct *members;
413   size_t length;
414   size_t tailPadding;
415 } DataDescriptor;
416 /** DataDescriptor for an array */
417 #define SIMPLE_DATA(type,repetitions) \
418   {type, repetitions, 0, NULL, 0, 0}
419 /** DataDescriptor for an structure member */
420 #define SIMPLE_MEMBER(type,repetitions,offset) \
421   {type, repetitions, offset, NULL, 0, 0}
422 /** DataDescriptor for padding bytes */
423 #define PAD_BYTES(structType,lastMember,memberType,repetitions) \
424   sizeof(structType) - offsetof(structType, lastMember) - \
425   sizeof(memberType) * repetitions
426
427 gras_datadesc_type_t
428 gras_datadesc_import_nws(const char           *name,
429                          const DataDescriptor *desc,
430                          unsigned long         howmany);
431
432
433 SG_END_DECL()
434
435 #endif /* GRAS_DATADESC_H */