Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
f50c9c4ae34dbb70fea9da9b01030dea8f62f406
[simgrid.git] / include / datadesc.h
1 /* $Id$ */
2
3 /* gras/datadesc.h - Describing the data you want to exchange               */
4
5 /* Authors: Martin Quinson                                                  */
6 /* Copyright (C) 2003 the OURAGAN project.                                  */
7
8 /* This program is free software; you can redistribute it and/or modify it
9    under the terms of the license (GNU LGPL) which comes with this package. */
10
11 #ifndef GRAS_DATADESC_H
12 #define GRAS_DATADESC_H
13
14 #include <stddef.h>    /* offsetof() */
15 #include <sys/types.h>  /* size_t */
16 #include <stdarg.h>
17
18
19 /*! C++ users need love */
20 #ifndef BEGIN_DECL
21 # ifdef __cplusplus
22 #  define BEGIN_DECL extern "C" {
23 # else
24 #  define BEGIN_DECL 
25 # endif
26 #endif
27
28 /*! C++ users need love */
29 #ifndef END_DECL
30 # ifdef __cplusplus
31 #  define END_DECL }
32 # else
33 #  define END_DECL 
34 # endif
35 #endif
36 /* End of cruft for C++ */
37
38 BEGIN_DECL
39
40
41 typedef struct s_gras_datadesc_type gras_datadesc_type_t;
42
43 /***********************************************
44  **** Search and retrieve declared datatype ****
45  ***********************************************/
46 long int gras_datadesc_get_id_from_name(const char *name);
47
48 gras_error_t gras_datadesc_by_name(const char *name,
49                                    gras_datadesc_type_t **type);
50 gras_error_t gras_datadesc_by_id  (long int code,
51                                    gras_datadesc_type_t **type);
52 #define gras_datadesc_by_code(a,b) gras_datadesc_by_id(a,b)
53
54 /*********************************************
55  **** DataDesc callback persistent states ****
56  *********************************************/
57 typedef struct s_gras_dd_cbps gras_dd_cbps_t;
58
59 void *
60 gras_dd_cbps_pop (gras_dd_cbps_t        *ps, 
61                   const char            *name,
62                   gras_datadesc_type_t **ddt);
63 void
64 gras_dd_cbps_push(gras_dd_cbps_t        *ps,
65                   const char            *name,
66                   void                  *data,
67                   gras_datadesc_type_t  *ddt);
68 void
69 gras_dd_cbps_set (gras_dd_cbps_t        *ps,
70                   const char            *name,
71                   void                  *data,
72                   gras_datadesc_type_t  *ddt);
73
74 void *
75 gras_dd_cbps_get (gras_dd_cbps_t        *ps, 
76                   const char            *name,
77                   gras_datadesc_type_t **ddt);
78
79 void
80 gras_dd_cbps_block_begin(gras_dd_cbps_t *ps);
81 void
82 gras_dd_cbps_block_end(gras_dd_cbps_t *ps);
83
84
85 /******************************************
86  **** Declare datadescription manually ****
87  ******************************************/
88
89 typedef void (*gras_datadesc_type_cb_void_t)(void                 *vars,
90                                              gras_datadesc_type_t *p_type,
91                                              void                 *data);
92 typedef int (*gras_datadesc_type_cb_int_t)(void                 *vars,
93                                            gras_datadesc_type_t *p_type,
94                                            void                 *data);
95
96
97
98 /* Create a new type and register it on the local machine */
99 #define gras_datadesc_declare_struct(   name,             code) \
100         gras_datadesc_declare_struct_cb(name, NULL, NULL, code)
101
102 #define gras_datadesc_declare_struct_add_name(   struct_code,field_name,field_type_name) \
103         gras_datadesc_declare_struct_add_name_cb(struct_code,field_name,field_type_name, NULL, NULL)
104
105 #define gras_datadesc_declare_struct_add_code(   struct_code,field_name,field_type_code) \
106         gras_datadesc_declare_struct_add_code_cb(struct_code,field_name,field_type_code, NULL, NULL)
107
108 gras_error_t 
109 gras_datadesc_declare_struct_cb(const char                   *name,
110                                 gras_datadesc_type_cb_void_t  pre_cb,
111                                 gras_datadesc_type_cb_void_t  post_cb,
112                                 long int                     *code);
113 gras_error_t 
114 gras_datadesc_declare_struct_add_name_cb(long int                      struct_code,
115                                          const char                   *field_name,
116                                          const char                   *field_type_name,
117                                          gras_datadesc_type_cb_void_t  pre_cb,
118                                          gras_datadesc_type_cb_void_t  post_cb);
119
120 gras_error_t 
121 gras_datadesc_declare_struct_add_code_cb(long int                      struct_code,
122                                          const char                   *field_name,
123                                          long int                      field_code,
124                                          gras_datadesc_type_cb_void_t  pre_cb,
125                                          gras_datadesc_type_cb_void_t  post_cb);
126 /* union */
127 #define gras_datadesc_declare_union(   name,             code) \
128         gras_datadesc_declare_union_cb(name, NULL, NULL, code)
129
130 #define gras_datadesc_declare_union_add_name(   union_code,field_name,field_type_name) \
131         gras_datadesc_declare_union_add_name_cb(union_code,field_name,field_type_name, NULL, NULL)
132
133 #define gras_datadesc_declare_union_add_code(   union_code,field_name,field_type_code) \
134         gras_datadesc_declare_union_add_code_cb(union_code,field_name,field_type_code, NULL, NULL)
135
136 gras_error_t 
137 gras_datadesc_declare_union_cb(const char                   *name,
138                                gras_datadesc_type_cb_int_t   field_count,
139                                gras_datadesc_type_cb_void_t  post,
140                                long int                     *code);
141 gras_error_t 
142 gras_datadesc_declare_union_add_name_cb(long int                      union_code,
143                                         const char                   *field_name,
144                                         const char                   *field_type_name,
145                                         gras_datadesc_type_cb_void_t  pre_cb,
146                                         gras_datadesc_type_cb_void_t  post_cb);
147 gras_error_t 
148 gras_datadesc_declare_union_add_code_cb(long int                      union_code,
149                                         const char                   *field_name,
150                                         long int                      field_code,
151                                         gras_datadesc_type_cb_void_t  pre_cb,
152                                         gras_datadesc_type_cb_void_t  post_cb);
153 /* ref */
154 #define gras_datadesc_declare_ref(name,ref_type, code) \
155         gras_datadesc_declare_ref_cb(name, ref_type, NULL, NULL, code)
156 #define gras_datadesc_declare_ref_disc(name,discriminant, code) \
157         gras_datadesc_declare_ref_cb(name, NULL, discriminant,  NULL, code)
158
159 gras_error_t
160 gras_datadesc_declare_ref_cb(const char                      *name,
161                              gras_datadesc_type_t            *referenced_type,
162                              gras_datadesc_type_cb_int_t      discriminant,
163                              gras_datadesc_type_cb_void_t     post,
164                              long int                        *code);
165 /* array */
166 #define gras_datadesc_declare_array(name,elm_type, size, code) \
167         gras_datadesc_declare_array_cb(name, elm_type, size, NULL,         NULL, code)
168 #define gras_datadesc_declare_array_dyn(name,elm_type, dynamic_size, code) \
169         gras_datadesc_declare_array_cb(name, elm_type, -1,   dynamic_size, NULL, code)
170
171 gras_error_t 
172 gras_datadesc_declare_array_cb(const char                      *name,
173                                gras_datadesc_type_t            *element_type,
174                                long int                         fixed_size,
175                                gras_datadesc_type_cb_int_t      dynamic_size,
176                                gras_datadesc_type_cb_void_t     post,
177                                long int                        *code);
178
179 /*******************************
180  **** About data convertion ****
181  *******************************/
182 int gras_arch_selfid(void); /* ID of this arch */
183
184 /****************************
185  **** Parse C statements ****
186  ****************************/
187 gras_error_t
188 gras_datadesc_parse(const char *name,
189                     const char *Cdefinition,
190                     long int   *code);
191 #define GRAS_DEFINE_TYPE(name,def) \
192   static const char * _gras_this_type_symbol_does_not_exist__##name=#def; def
193  
194 #define gras_type_symbol_parse(bag,name)                                  \
195  _gs_type_parse(bag, _gs_this_type_symbol_does_not_exist__##name)
196  
197 #define gs_type_get_by_symbol(bag,name)                  \
198   (bag->bag_ops->get_type_by_name(bag, NULL, #name) ?    \
199      bag->bag_ops->get_type_by_name(bag, NULL, #name) :  \
200      gras_type_symbol_parse(bag, name)                   \
201   )
202
203 /*****************************
204  **** NWS datadescription ****
205  *****************************/
206
207 /**
208  * Basic types we can embeed in DataDescriptors.
209  */
210 typedef enum
211   {CHAR_TYPE, DOUBLE_TYPE, FLOAT_TYPE, INT_TYPE, LONG_TYPE, SHORT_TYPE,
212    UNSIGNED_INT_TYPE, UNSIGNED_LONG_TYPE, UNSIGNED_SHORT_TYPE, STRUCT_TYPE}
213   DataTypes;
214 #define SIMPLE_TYPE_COUNT 9
215
216 /*!  \brief Describe a collection of data.
217  * 
218 ** A description of a collection of #type# data.  #repetitions# is used only
219 ** for arrays; it contains the number of elements.  #offset# is used only for
220 ** struct members in host format; it contains the offset of the member from the
221 ** beginning of the struct, taking into account internal padding added by the
222 ** compiler for alignment purposes.  #members#, #length#, and #tailPadding# are
223 ** used only for STRUCT_TYPE data; the #length#-long array #members# describes
224 ** the members of the nested struct, and #tailPadding# indicates how many
225 ** padding bytes the compiler adds to the end of the structure.
226 */
227
228 typedef struct DataDescriptorStruct {
229   DataTypes type;
230   size_t repetitions;
231   size_t offset;
232   /*@null@*/ struct DataDescriptorStruct *members;
233   size_t length;
234   size_t tailPadding;
235 } DataDescriptor;
236 /** DataDescriptor for an array */
237 #define SIMPLE_DATA(type,repetitions) \
238   {type, repetitions, 0, NULL, 0, 0}
239 /** DataDescriptor for an structure member */
240 #define SIMPLE_MEMBER(type,repetitions,offset) \
241   {type, repetitions, offset, NULL, 0, 0}
242 /** DataDescriptor for padding bytes */
243 #define PAD_BYTES(structType,lastMember,memberType,repetitions) \
244   sizeof(structType) - offsetof(structType, lastMember) - \
245   sizeof(memberType) * repetitions
246
247 gras_error_t
248 gras_datadesc_from_nws(const char           *name,
249                        const DataDescriptor *desc,
250                        size_t                howmany,
251                        long int             *code);
252
253
254 END_DECL
255
256 #endif /* GRAS_DATADESC_H */