Logo AND Algorithmique Numérique Distribuée

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