Logo AND Algorithmique Numérique Distribuée

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