Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
doxygenification
[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" /* BEGIN_DECL */
14
15 BEGIN_DECL()
16
17 /** @defgroup GRAS_dd Data description
18  *  @brief Describing data to be exchanged (Communication facility)
19  *
20  * @section Overview
21  *
22  * Since GRAS takes care of potential representation conversion when the platform is heterogeneous, 
23  * any data which transits on the network must be described beforehand.
24  * 
25  * There is several possible interfaces for this, ranging from the really completely automatic parsing to 
26  * completely manual. Let's study each of them from the simplest to the more advanced.
27  */
28   
29 /** @name a) basic operations
30  *  @ingroup GRAS_dd
31  *
32  * If you only want to send pre-existing types, simply retrieve the pre-defined description with 
33  * the \ref gras_datadesc_by_name function. Existing types entail:
34  *  - char (both signed and unsigned)
35  *  - int (short, regular, long and long long, both signed and unsigned)
36  *  - float and double
37  *  - string (which is indeed a reference to a dynamically sized array of char, strlen being used to retrive the size)
38  * 
39  * Example:\verbatim gras_datadesc_type_t i = gras_datadesc_by_name("int");
40  gras_datadesc_type_t uc = gras_datadesc_by_name("unsigned char");
41  gras_datadesc_type_t str = gras_datadesc_by_name("string");\endverbatim
42  */
43 /** @{ */
44   
45 /** @brief Opaque type describing a type description. */
46 typedef struct s_gras_datadesc_type *gras_datadesc_type_t;
47
48 /** \brief Search a type description from its name */
49 gras_datadesc_type_t gras_datadesc_by_name(const char *name);
50
51
52 /** @} */
53   
54 /** @fn gras_datadesc_type_t gras_datadesc_parse(const char *name, const char *C_statement) 
55  *  @ingroup GRAS_dd_implem 
56  *
57  *  Helper function doing the crude job of type parsing.
58  */
59   
60 /** @name b) Automatic parsing
61  *  @ingroup GRAS_dd
62  * 
63  *  If you need to declare a new datatype, this is the simplest way to describe it to GRAS. Simply
64  *  enclose its type definition  into a \ref GRAS_DEFINE_TYPE macro call, and you're set. Here is 
65  *  an type declaration  example: \verbatim GRAS_DEFINE_TYPE(mytype,struct mytype {
66    int myfirstfield;
67    char mysecondfield;
68  });\endverbatim
69  *  The type is then both copied verbatim into your source file and stored for further parsing. This allows
70  *  you to let GRAS parse the exact version you are actually using in your program.
71  *  You can then retrieve the corresponding type description with \ref gras_datadesc_by_symbol.
72  *  Don't worry too much for the performances, the type is only parsed once and a binary representation 
73  *  is stored and used in any subsequent calls.
74  * 
75  *  If your structure contains any pointer, you have to explain GRAS the size of the pointed array. This
76  *  can be 1 in the case of simple references, or more in the case of regular arrays. For that, use the 
77  *  \ref GRAS_ANNOTE macro within the type declaration you are passing to \ref GRAS_DEFINE_TYPE. This macro
78  *  rewrites itself to nothing in the declaration (so they won't pollute the type definition copied verbatim
79  *  into your code), and give some information to GRAS about your pointer. 
80  
81  *  GRAS_ANNOTE takes two arguments being the key name and the key value. For now, the only accepted key name 
82  *  is "size", to specify the length of the pointed array. It can either be the string "1" (without the quote)
83  *  or the name of another field of the structure.
84  *  
85  *  Here is an example:\verbatim GRAS_DEFINE_TYPE(s_clause,
86   struct s_array {
87     int length;
88     int *data GRAS_ANNOTE(size,length);
89     struct s_array *father GRAS_ANNOTE(size,1);
90  }
91 ;)\endverbatim
92  * It specifies that the structure s_array contains two fields, and that the size of the array pointed 
93  * by \a data is the \a length field, and that the \a father field is a simple reference.
94  * 
95  * If you cannot express your datadescs with this mecanism, you'll have to use the more advanced 
96  * (and somehow complex) one described below.
97  * 
98  *  \warning Since GRAS_DEFINE_TYPE is a macro, you shouldn't  put any comma in your type definition 
99  *  (comma separates macro args). 
100  * 
101  *  For example, change \verbatim int a, b;\endverbatim to \verbatim int a;
102  int b:\endverbatim
103  */
104 /** @{ */
105
106  
107 /**   @def GRAS_DEFINE_TYPE
108  *    @hideinitializer
109  *    @brief Automatically parse C code
110  */
111   
112
113 #define GRAS_DEFINE_TYPE(name,def) \
114   static const char * _gras_this_type_symbol_does_not_exist__##name=#def; def
115  
116 /** @brief Retrieve a datadesc which was previously parsed 
117  *  @hideinitializer
118  */
119 #define gras_datadesc_by_symbol(name)  \
120   (gras_datadesc_by_name(#name) ?      \
121    gras_datadesc_by_name(#name) :      \
122      gras_datadesc_parse(#name,        \
123                          _gras_this_type_symbol_does_not_exist__##name) \
124   )
125
126 /** @def GRAS_ANNOTE
127  *  @brief Add an annotation to a type to be automatically parsed
128  */
129 #define GRAS_ANNOTE(key,val)
130
131 /*@}*/
132
133 gras_datadesc_type_t 
134 gras_datadesc_parse(const char *name, const char *C_statement);
135
136 /** @name c) Simple manual definitions
137  *  @ingroup GRAS_dd
138  * 
139  * Here are the functions to use if you want to declare your description manually. 
140  * The function names should be self-explanatory in most cases.
141  * 
142  * You can add callbacks to the datatypes doing any kind of action you may want. Usually, 
143  * pre-send callbacks are used to prepare the type expedition while post-receive callbacks 
144  * are used to fix any issue after the receive.
145  * 
146  * If your types are dynamic, you'll need to add some extra callback. For example, there is a
147  * specific callback for the string type which is in charge of computing the length of the char
148  * array. This is done with the cbps mecanism, explained in next section.
149  * 
150  * If your types may contain pointer cycle, you must specify it to GRAS using the @ref gras_datadesc_cycle_set. 
151  * 
152  * Example:\verbatim
153  typedef struct {
154    unsigned char c1;
155    unsigned long int l1;
156    unsigned char c2;
157    unsigned long int l2;
158  } mystruct;
159  [...]
160   my_type=gras_datadesc_struct("mystruct");
161   gras_datadesc_struct_append(my_type,"c1", gras_datadesc_by_name("unsigned char"));
162   gras_datadesc_struct_append(my_type,"l1", gras_datadesc_by_name("unsigned long"));
163   gras_datadesc_struct_append(my_type,"c2", gras_datadesc_by_name("unsigned char"));
164   gras_datadesc_struct_append(my_type,"l2", gras_datadesc_by_name("unsigned long int"));
165   gras_datadesc_struct_close(my_type);
166
167   my_type=gras_datadesc_ref("mystruct*", gras_datadesc_by_name("mystruct"));
168   
169   [Use my_type to send pointers to mystruct data]\endverbatim
170  */
171 /*@{*/
172
173
174 /** \brief Opaque type describing a type description callback persistant state. */
175 typedef struct s_gras_cbps *gras_cbps_t;
176
177 /* callbacks prototypes */
178 /** \brief Prototype of type callbacks returning nothing. */
179 typedef void (*gras_datadesc_type_cb_void_t)(gras_cbps_t vars, void *data);
180 /** \brief Prototype of type callbacks returning an int. */
181 typedef int (*gras_datadesc_type_cb_int_t)(gras_cbps_t vars, void *data);
182 /** \brief Prototype of type callbacks selecting a type. */
183 typedef gras_datadesc_type_t (*gras_datadesc_selector_t)(gras_cbps_t vars, void *data);
184
185
186 /******************************************
187  **** Declare datadescription yourself ****
188  ******************************************/
189
190 /** \brief Declare a new structure description */
191 gras_datadesc_type_t gras_datadesc_struct(const char *name);
192
193 /** \brief Append a new field to a structure description */
194 void
195   gras_datadesc_struct_append(gras_datadesc_type_t  struct_type,
196                               const char           *name,
197                               gras_datadesc_type_t  field_type);
198 /** \brief Close a structure description */
199 void
200   gras_datadesc_struct_close(gras_datadesc_type_t   struct_type);
201
202 /** \brief Declare a new union description */
203 gras_datadesc_type_t 
204   gras_datadesc_union(const char                   *name,
205                       gras_datadesc_type_cb_int_t   selector);
206 /** \brief Append a new field to an union description */
207 void
208   gras_datadesc_union_append(gras_datadesc_type_t   union_type,
209                              const char            *name,
210                              gras_datadesc_type_t   field_type);
211 /** \brief Close an union description */
212 void
213   gras_datadesc_union_close(gras_datadesc_type_t    union_type);
214
215
216 /** \brief Declare a new type being a reference to the one passed in arg */
217 gras_datadesc_type_t 
218   gras_datadesc_ref(const char                     *name,
219                     gras_datadesc_type_t            referenced_type);
220 /** \brief Declare a new type being a generic reference. */
221 gras_datadesc_type_t 
222   gras_datadesc_ref_generic(const char                *name,
223                             gras_datadesc_selector_t   selector);
224
225 /** \brief Declare a new type being an array of fixed size and content */
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
231 /** \brief Declare a new type being an array of fixed size, but accepting several content types. */
232 gras_datadesc_type_t 
233   gras_datadesc_array_dyn(const char                 *name,
234                           gras_datadesc_type_t        element_type,
235                           gras_datadesc_type_cb_int_t dynamic_size);
236
237 /** \brief Declare a new type being an array which size can be found with \ref gras_cbps_i_pop */
238 gras_datadesc_type_t 
239   gras_datadesc_ref_pop_arr(gras_datadesc_type_t  element_type);
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
267 /******************************
268  * Get stuff within datadescs *
269  ******************************/
270 /** \brief Returns the name of a datadescription */
271 char * gras_datadesc_get_name(gras_datadesc_type_t ddt);
272 /** \brief Returns the identifier of a datadescription */
273 int gras_datadesc_get_id(gras_datadesc_type_t ddt);
274
275 /*@}*/
276
277 /** @name Callback Persistant State: Simple push/pop mecanism
278  *  @ingroup GRAS_dd
279  * 
280  * Sometimes, one of the callbacks need to leave information for the next ones. If this is a simple integer (such as
281  * an array size), you can use the functions described here. If not, you'll have to play with the complete cbps interface.
282  */
283 /*@{*/
284
285 void
286 gras_cbps_i_push(gras_cbps_t ps, int val);
287 int 
288 gras_cbps_i_pop(gras_cbps_t ps);
289
290 int gras_datadesc_cb_pop(gras_cbps_t vars, void *data);
291 void gras_datadesc_cb_push_int(gras_cbps_t vars, void *data);
292 void gras_datadesc_cb_push_uint(gras_cbps_t vars, void *data);
293 void gras_datadesc_cb_push_lint(gras_cbps_t vars, void *data);
294 void gras_datadesc_cb_push_ulint(gras_cbps_t vars, void *data);
295
296
297 /*@}*/
298
299 /** @name Callback Persistant State: Full featured mecanism
300  *  @ingroup GRAS_dd
301  * 
302  * Sometimes, one of the callbacks need to leave information for the next ones. If the simple push/pop mecanism
303  * introduced in previous section isn't enough, you can always use this full featured one.
304  */
305
306 /*@{*/
307
308 xbt_error_t
309   gras_cbps_v_pop (gras_cbps_t            ps, 
310                    const char            *name,
311          /* OUT */ gras_datadesc_type_t  *ddt,
312          /* OUT */ void                 **res);
313 xbt_error_t
314 gras_cbps_v_push(gras_cbps_t            ps,
315                  const char            *name,
316                  void                  *data,
317                  gras_datadesc_type_t   ddt);
318 void
319 gras_cbps_v_set (gras_cbps_t            ps,
320                  const char            *name,
321                  void                  *data,
322                  gras_datadesc_type_t   ddt);
323
324 void *
325 gras_cbps_v_get (gras_cbps_t            ps, 
326                  const char            *name,
327        /* OUT */ gras_datadesc_type_t  *ddt);
328
329 void
330 gras_cbps_block_begin(gras_cbps_t ps);
331 void
332 gras_cbps_block_end(gras_cbps_t ps);
333
334 /* @} */
335
336
337 /*******************************
338  **** About data convertion ****
339  *******************************/
340 int gras_arch_selfid(void); /* ID of this arch */
341
342
343 /*****************************
344  **** NWS datadescription * FIXME: obsolete?
345  *****************************/
346
347 /**
348  * Basic types we can embeed in DataDescriptors.
349  */
350 typedef enum
351   {CHAR_TYPE, DOUBLE_TYPE, FLOAT_TYPE, INT_TYPE, LONG_TYPE, SHORT_TYPE,
352    UNSIGNED_INT_TYPE, UNSIGNED_LONG_TYPE, UNSIGNED_SHORT_TYPE, STRUCT_TYPE}
353   DataTypes;
354 #define SIMPLE_TYPE_COUNT 9
355
356 /*!  \brief Describe a collection of data.
357  * 
358 ** A description of a collection of #type# data.  #repetitions# is used only
359 ** for arrays; it contains the number of elements.  #offset# is used only for
360 ** struct members in host format; it contains the offset of the member from the
361 ** beginning of the struct, taking into account internal padding added by the
362 ** compiler for alignment purposes.  #members#, #length#, and #tailPadding# are
363 ** used only for STRUCT_TYPE data; the #length#-long array #members# describes
364 ** the members of the nested struct, and #tailPadding# indicates how many
365 ** padding bytes the compiler adds to the end of the structure.
366 */
367
368 typedef struct DataDescriptorStruct {
369   DataTypes type;
370   size_t repetitions;
371   size_t offset;
372   /*@null@*/ struct DataDescriptorStruct *members;
373   size_t length;
374   size_t tailPadding;
375 } DataDescriptor;
376 /** DataDescriptor for an array */
377 #define SIMPLE_DATA(type,repetitions) \
378   {type, repetitions, 0, NULL, 0, 0}
379 /** DataDescriptor for an structure member */
380 #define SIMPLE_MEMBER(type,repetitions,offset) \
381   {type, repetitions, offset, NULL, 0, 0}
382 /** DataDescriptor for padding bytes */
383 #define PAD_BYTES(structType,lastMember,memberType,repetitions) \
384   sizeof(structType) - offsetof(structType, lastMember) - \
385   sizeof(memberType) * repetitions
386
387 xbt_error_t
388 gras_datadesc_import_nws(const char           *name,
389                          const DataDescriptor *desc,
390                          unsigned long         howmany,
391                /* OUT */ gras_datadesc_type_t *dst);
392
393
394 END_DECL()
395
396 #endif /* GRAS_DATADESC_H */