Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
3a904bee1d45a7833b82083be493149c784634d3
[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 /* datadesc */
42 typedef struct s_gras_datadesc_type gras_datadesc_type_t;
43
44 /* callbacks prototypes */
45 typedef void (*gras_datadesc_type_cb_void_t)(void                 *vars,
46                                              gras_datadesc_type_t *p_type,
47                                              void                 *data);
48 typedef int (*gras_datadesc_type_cb_int_t)(void                 *vars,
49                                            gras_datadesc_type_t *p_type,
50                                            void                 *data);
51
52 /***********************************************
53  **** Search and retrieve declared datatype ****
54  ***********************************************/
55 gras_error_t gras_datadesc_by_name(const char *name,
56                                    gras_datadesc_type_t **type);
57
58 /******************************************
59  **** Declare datadescription yourself ****
60  ******************************************/
61
62 gras_error_t 
63 gras_datadesc_declare_struct(const char            *name,
64                              gras_datadesc_type_t **dst);
65 gras_error_t 
66 gras_datadesc_declare_struct_append(gras_datadesc_type_t          *struct_type,
67                                     const char                    *name,
68                                     gras_datadesc_type_t          *field_type);
69 gras_error_t 
70 gras_datadesc_declare_struct_append_name(gras_datadesc_type_t *struct_type,
71                                          const char           *name,
72                                          const char          *field_type_name);
73 gras_error_t 
74 gras_datadesc_declare_union(const char                      *name,
75                             gras_datadesc_type_cb_int_t      selector,
76                             gras_datadesc_type_t           **dst);
77 gras_error_t 
78 gras_datadesc_declare_union_append(gras_datadesc_type_t          *union_type,
79                                    const char                    *name,
80                                    gras_datadesc_type_t          *field_type);
81 gras_error_t 
82 gras_datadesc_declare_union_append_name(gras_datadesc_type_t *union_type,
83                                         const char           *name,
84                                         const char           *field_type_name);
85 gras_error_t 
86 gras_datadesc_declare_ref(const char                      *name,
87                           gras_datadesc_type_t            *referenced_type,
88                           gras_datadesc_type_t           **dst);
89 gras_error_t
90 gras_datadesc_declare_ref_generic(const char                   *name,
91                                   gras_datadesc_type_cb_int_t   discriminant,
92                                   gras_datadesc_type_t        **dst);
93 gras_error_t 
94 gras_datadesc_declare_array_fixed(const char                    *name,
95                                   gras_datadesc_type_t          *element_type,
96                                   long int                       fixed_size,
97                                   gras_datadesc_type_t         **dst);
98 gras_error_t 
99 gras_datadesc_declare_array_dyn(const char                      *name,
100                                 gras_datadesc_type_t            *element_type,
101                                 gras_datadesc_type_cb_int_t      dynamic_size,
102                                 gras_datadesc_type_t           **dst);
103
104 /*********************************
105  * Change stuff within datadescs *
106  *********************************/
107
108 typedef struct s_gras_dd_cbps gras_dd_cbps_t;
109 void gras_datadesc_cb_set_pre (gras_datadesc_type_t    *type,
110                                gras_datadesc_type_cb_void_t  pre);
111 void gras_datadesc_cb_set_post(gras_datadesc_type_t    *type,
112                                gras_datadesc_type_cb_void_t  post);
113
114 /********************************************************
115  * Advanced data describing: callback persistent states *
116  ********************************************************/
117
118 void *
119 gras_dd_cbps_pop (gras_dd_cbps_t        *ps, 
120                   const char            *name,
121                   gras_datadesc_type_t **ddt);
122 void
123 gras_dd_cbps_push(gras_dd_cbps_t        *ps,
124                   const char            *name,
125                   void                  *data,
126                   gras_datadesc_type_t  *ddt);
127 void
128 gras_dd_cbps_set (gras_dd_cbps_t        *ps,
129                   const char            *name,
130                   void                  *data,
131                   gras_datadesc_type_t  *ddt);
132
133 void *
134 gras_dd_cbps_get (gras_dd_cbps_t        *ps, 
135                   const char            *name,
136                   gras_datadesc_type_t **ddt);
137
138 void
139 gras_dd_cbps_block_begin(gras_dd_cbps_t *ps);
140 void
141 gras_dd_cbps_block_end(gras_dd_cbps_t *ps);
142
143
144
145
146 /*******************************
147  **** About data convertion ****
148  *******************************/
149 int gras_arch_selfid(void); /* ID of this arch */
150
151 /****************************
152  **** Parse C statements ****
153  ****************************/
154 gras_error_t
155 gras_datadesc_parse(const char *name,
156                     const char *Cdefinition,
157                     gras_datadesc_type_t **dst);
158 #define GRAS_DEFINE_TYPE(name,def) \
159   static const char * _gras_this_type_symbol_does_not_exist__##name=#def; def
160  
161 /*#define gras_type_symbol_parse(name)                                  \
162  _gras_datadesc_parse(_gras_this_datadesc_type_symbol_does_not_exist__##name)
163 */ 
164 #define gras_datadesc_by_symbol(name)                    \
165   (bag->bag_ops->get_type_by_name(bag, NULL, #name) ?    \
166      bag->bag_ops->get_type_by_name(bag, NULL, #name) :  \
167      gras_datadesc_parse(name)                           \
168   )
169
170 /*****************************
171  **** NWS datadescription ****
172  *****************************/
173
174 /**
175  * Basic types we can embeed in DataDescriptors.
176  */
177 typedef enum
178   {CHAR_TYPE, DOUBLE_TYPE, FLOAT_TYPE, INT_TYPE, LONG_TYPE, SHORT_TYPE,
179    UNSIGNED_INT_TYPE, UNSIGNED_LONG_TYPE, UNSIGNED_SHORT_TYPE, STRUCT_TYPE}
180   DataTypes;
181 #define SIMPLE_TYPE_COUNT 9
182
183 /*!  \brief Describe a collection of data.
184  * 
185 ** A description of a collection of #type# data.  #repetitions# is used only
186 ** for arrays; it contains the number of elements.  #offset# is used only for
187 ** struct members in host format; it contains the offset of the member from the
188 ** beginning of the struct, taking into account internal padding added by the
189 ** compiler for alignment purposes.  #members#, #length#, and #tailPadding# are
190 ** used only for STRUCT_TYPE data; the #length#-long array #members# describes
191 ** the members of the nested struct, and #tailPadding# indicates how many
192 ** padding bytes the compiler adds to the end of the structure.
193 */
194
195 typedef struct DataDescriptorStruct {
196   DataTypes type;
197   size_t repetitions;
198   size_t offset;
199   /*@null@*/ struct DataDescriptorStruct *members;
200   size_t length;
201   size_t tailPadding;
202 } DataDescriptor;
203 /** DataDescriptor for an array */
204 #define SIMPLE_DATA(type,repetitions) \
205   {type, repetitions, 0, NULL, 0, 0}
206 /** DataDescriptor for an structure member */
207 #define SIMPLE_MEMBER(type,repetitions,offset) \
208   {type, repetitions, offset, NULL, 0, 0}
209 /** DataDescriptor for padding bytes */
210 #define PAD_BYTES(structType,lastMember,memberType,repetitions) \
211   sizeof(structType) - offsetof(structType, lastMember) - \
212   sizeof(memberType) * repetitions
213
214 gras_error_t
215 gras_datadesc_import_nws(const char           *name,
216                          const DataDescriptor *desc,
217                          size_t                howmany,
218                          gras_datadesc_type_t **dst);
219
220 END_DECL
221
222 #endif /* GRAS_DATADESC_H */