Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
e2827dfca29d69e1d46b89216d023f8807e63954
[simgrid.git] / include / gras / 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, 2004 Martin Quinson.                                 */
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 /**
42  * gras_datadesc_type_t:
43  * 
44  * Opaque type describing a type description you don't want to open.
45  */
46 typedef struct s_gras_datadesc_type gras_datadesc_type_t;
47
48 typedef struct s_gras_cbps gras_cbps_t;
49
50 /* callbacks prototypes */
51 typedef void (*gras_datadesc_type_cb_void_t)(gras_cbps_t *vars, void *data);
52 typedef int (*gras_datadesc_type_cb_int_t)(gras_cbps_t *vars, void *data);
53
54 /***********************************************
55  **** Search and retrieve declared datatype ****
56  ***********************************************/
57 gras_datadesc_type_t *gras_datadesc_by_name(const char *name);
58
59 /******************************************
60  **** Declare datadescription yourself ****
61  ******************************************/
62
63 gras_error_t 
64 gras_datadesc_struct(const char            *name,
65                              gras_datadesc_type_t **dst);
66 gras_error_t 
67 gras_datadesc_struct_append(gras_datadesc_type_t          *struct_type,
68                                     const char                    *name,
69                                     gras_datadesc_type_t          *field_type);
70 void
71 gras_datadesc_struct_close(gras_datadesc_type_t          *struct_type);
72 gras_error_t 
73 gras_datadesc_union(const char                      *name,
74                             gras_datadesc_type_cb_int_t      selector,
75                             gras_datadesc_type_t           **dst);
76 gras_error_t 
77 gras_datadesc_union_append(gras_datadesc_type_t          *union_type,
78                                    const char                    *name,
79                                    gras_datadesc_type_t          *field_type);
80 void
81 gras_datadesc_union_close(gras_datadesc_type_t          *union_type);
82 gras_error_t 
83 gras_datadesc_ref(const char                      *name,
84                           gras_datadesc_type_t            *referenced_type,
85                           gras_datadesc_type_t           **dst);
86 gras_error_t
87 gras_datadesc_ref_generic(const char                   *name,
88                                   gras_datadesc_type_cb_int_t   discriminant,
89                                   gras_datadesc_type_t        **dst);
90 gras_error_t 
91 gras_datadesc_array_fixed(const char                    *name,
92                                   gras_datadesc_type_t          *element_type,
93                                   long int                       fixed_size,
94                                   gras_datadesc_type_t         **dst);
95 gras_error_t 
96 gras_datadesc_array_dyn(const char                      *name,
97                                 gras_datadesc_type_t            *element_type,
98                                 gras_datadesc_type_cb_int_t      dynamic_size,
99                                 gras_datadesc_type_t           **dst);
100 gras_error_t 
101 gras_datadesc_ref_pop_arr(gras_datadesc_type_t  *element_type,
102                                   gras_datadesc_type_t **dst);
103
104 /*********************************
105  * Change stuff within datadescs *
106  *********************************/
107
108 void gras_datadesc_cb_send (gras_datadesc_type_t    *type,
109                             gras_datadesc_type_cb_void_t  pre);
110 void gras_datadesc_cb_recv(gras_datadesc_type_t    *type,
111                            gras_datadesc_type_cb_void_t  post);
112
113 /******************************
114  * Get stuff within datadescs *
115  ******************************/
116 char * gras_datadesc_get_name(gras_datadesc_type_t *ddt);
117 int gras_datadesc_get_id(gras_datadesc_type_t *ddt);
118
119 /********************************************************
120  * Advanced data describing: callback persistent states *
121  ********************************************************/
122 /* simple one: push/pop sizes of arrays */
123 void
124 gras_cbps_i_push(gras_cbps_t *ps, int val);
125 int 
126 gras_cbps_i_pop(gras_cbps_t *ps);
127 int gras_datadesc_cb_pop(gras_cbps_t *vars, void *data);
128
129 /* complex one: complete variable environment support */
130 gras_error_t
131 gras_cbps_v_pop (gras_cbps_t        *ps, 
132                     const char            *name,
133                     gras_datadesc_type_t **ddt,
134                   void                 **res);
135 gras_error_t
136 gras_cbps_v_push(gras_cbps_t        *ps,
137                     const char            *name,
138                     void                  *data,
139                     gras_datadesc_type_t  *ddt);
140 void
141 gras_cbps_v_set (gras_cbps_t        *ps,
142                     const char            *name,
143                     void                  *data,
144                     gras_datadesc_type_t  *ddt);
145
146 void *
147 gras_cbps_v_get (gras_cbps_t        *ps, 
148                     const char            *name,
149                     gras_datadesc_type_t **ddt);
150
151 void
152 gras_cbps_block_begin(gras_cbps_t *ps);
153 void
154 gras_cbps_block_end(gras_cbps_t *ps);
155
156
157
158
159 /*******************************
160  **** About data convertion ****
161  *******************************/
162 int gras_arch_selfid(void); /* ID of this arch */
163
164 /****************************
165  **** Parse C statements ****
166  ****************************/
167 gras_datadesc_type_t *
168 gras_datadesc_parse(const char *name,
169                     const char *Cdefinition);
170 #define GRAS_DEFINE_TYPE(name,def) \
171   static const char * _gras_this_type_symbol_does_not_exist__##name=#def; def
172  
173 #define gras_datadesc_by_symbol(name)  \
174   (gras_datadesc_by_name(#name) ?      \
175    gras_datadesc_by_name(#name) :      \
176      gras_datadesc_parse(#name,        \
177                          _gras_this_type_symbol_does_not_exist__##name) \
178   )
179
180 /*****************************
181  **** NWS datadescription ****
182  *****************************/
183
184 /**
185  * Basic types we can embeed in DataDescriptors.
186  */
187 typedef enum
188   {CHAR_TYPE, DOUBLE_TYPE, FLOAT_TYPE, INT_TYPE, LONG_TYPE, SHORT_TYPE,
189    UNSIGNED_INT_TYPE, UNSIGNED_LONG_TYPE, UNSIGNED_SHORT_TYPE, STRUCT_TYPE}
190   DataTypes;
191 #define SIMPLE_TYPE_COUNT 9
192
193 /*!  \brief Describe a collection of data.
194  * 
195 ** A description of a collection of #type# data.  #repetitions# is used only
196 ** for arrays; it contains the number of elements.  #offset# is used only for
197 ** struct members in host format; it contains the offset of the member from the
198 ** beginning of the struct, taking into account internal padding added by the
199 ** compiler for alignment purposes.  #members#, #length#, and #tailPadding# are
200 ** used only for STRUCT_TYPE data; the #length#-long array #members# describes
201 ** the members of the nested struct, and #tailPadding# indicates how many
202 ** padding bytes the compiler adds to the end of the structure.
203 */
204
205 typedef struct DataDescriptorStruct {
206   DataTypes type;
207   size_t repetitions;
208   size_t offset;
209   /*@null@*/ struct DataDescriptorStruct *members;
210   size_t length;
211   size_t tailPadding;
212 } DataDescriptor;
213 /** DataDescriptor for an array */
214 #define SIMPLE_DATA(type,repetitions) \
215   {type, repetitions, 0, NULL, 0, 0}
216 /** DataDescriptor for an structure member */
217 #define SIMPLE_MEMBER(type,repetitions,offset) \
218   {type, repetitions, offset, NULL, 0, 0}
219 /** DataDescriptor for padding bytes */
220 #define PAD_BYTES(structType,lastMember,memberType,repetitions) \
221   sizeof(structType) - offsetof(structType, lastMember) - \
222   sizeof(memberType) * repetitions
223
224 gras_error_t
225 gras_datadesc_import_nws(const char           *name,
226                          const DataDescriptor *desc,
227                          size_t                howmany,
228                          gras_datadesc_type_t **dst);
229
230 END_DECL
231
232 #endif /* GRAS_DATADESC_H */