Logo AND Algorithmique Numérique Distribuée

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