Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
This header is now useless (yeah)
[simgrid.git] / src / gras / DataDesc / cbps.c
1 /* $Id$ */
2
3 /* cbps - persistant states for callbacks                                   */
4
5 /* Copyright (c) 2003 Olivier Aumage.                                       */
6 /* Copyright (c) 2003, 2004 Martin Quinson.                                 */
7 /* All rights reserved.                                                     */
8
9 /* This program is free software; you can redistribute it and/or modify it
10  * under the terms of the license (GNU LGPL) which comes with this package. */
11
12 #include "xbt/ex.h"
13 #include "gras/DataDesc/datadesc_private.h"
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ddt_cbps,datadesc,"callback persistant state");
15
16 typedef struct {
17   gras_datadesc_type_t  type;
18   void                 *data;
19 } s_gras_cbps_elm_t, *gras_cbps_elm_t;
20
21 typedef struct s_gras_cbps {
22   xbt_dynar_t lints; /* simple stack of long integers (easy interface) */
23
24   xbt_dict_t  space; /* varname x dynar of gras_cbps_elm_t */
25   xbt_dynar_t frames; /* of dynar of names defined within this frame
26                            (and to pop when we leave it) */
27   xbt_dynar_t globals;
28 } s_gras_cbps_t;
29
30 static void free_string(void *d){
31   free(*(void**)d);
32 }
33
34 gras_cbps_t gras_cbps_new(void) {
35   gras_cbps_t  res;
36
37   res=xbt_new(s_gras_cbps_t,1);
38
39   res->lints = xbt_dynar_new(sizeof(int), NULL);
40   res->space = xbt_dict_new();
41   /* no leak, the content is freed manually on block_end */
42   res->frames = xbt_dynar_new(sizeof(xbt_dynar_t), NULL);
43   res->globals = xbt_dynar_new(sizeof(char*), NULL);
44
45   gras_cbps_block_begin(res);
46
47   return res;
48 }
49
50 void gras_cbps_free(gras_cbps_t *state) {
51
52   xbt_dynar_free( &( (*state)->lints   ) );
53
54   gras_cbps_block_end(*state);
55   xbt_dict_free ( &( (*state)->space   ) );
56   xbt_dynar_free( &( (*state)->frames  ) );
57   xbt_dynar_free( &( (*state)->globals ) );
58
59   free(*state);
60   *state = NULL;
61 }
62
63 /** \brief Declare a new element in the PS, and give it a value.
64  *
65  * If an element of that
66  * name already exists, it is masked by the one given here, and will be 
67  * seeable again only after a pop to remove the value this push adds.
68  */
69 xbt_error_t
70 gras_cbps_v_push(gras_cbps_t          ps,
71                  const char          *name,
72                  void                *data,
73                  gras_datadesc_type_t ddt) {
74
75   xbt_dynar_t     varstack,frame;
76   gras_cbps_elm_t var;
77   char           *varname = (char*)xbt_strdup(name);
78   xbt_ex_t        e;
79
80   DEBUG2("push(%s,%p)",name,(void*)data);
81
82   TRY {
83     varstack = xbt_dict_get(ps->space, name);
84   } CATCH(e) {
85     if (e.category != mismatch_error) 
86       RETHROW;
87
88     DEBUG1("Create a new variable stack for '%s' into the space",name);
89     varstack = xbt_dynar_new(sizeof (gras_cbps_elm_t *), NULL);
90     xbt_dict_set(ps->space, varname, (void **)varstack, NULL);
91     xbt_ex_free(e);
92     /* leaking, you think? only if you do not close all the openned blocks ;)*/
93   }
94  
95   var       = xbt_new0(s_gras_cbps_elm_t,1);
96   var->type = ddt;
97   var->data = data;
98   
99   xbt_dynar_push(varstack, &var);
100   
101   xbt_dynar_pop(ps->frames, &frame);
102   DEBUG4("Push %s (%p @%p) into frame %p",varname,(void*)varname,(void*)&varname,(void*)frame);
103   xbt_dynar_push(frame, &varname);
104   xbt_dynar_push(ps->frames, &frame); 
105   return no_error;
106 }
107
108 /** \brief Retrieve an element from the PS, and remove it from the PS.
109  *
110  * If it's not
111  * present in the current block, it will fail (with abort) and not search
112  * in upper blocks since this denotes a programmation error.
113  */
114 xbt_error_t
115 gras_cbps_v_pop (gras_cbps_t            ps, 
116                  const char            *name,
117                  gras_datadesc_type_t  *ddt,
118                  void                 **res) {
119   xbt_dynar_t          varstack,frame;
120   gras_cbps_elm_t       var            = NULL;
121   void                 *data           = NULL;
122   xbt_ex_t e;
123
124   DEBUG1("pop(%s)",name);
125   TRY {
126     varstack = xbt_dict_get(ps->space, name);
127   } CATCH(e) {
128     if (e.category != mismatch_error)
129       RETHROW;
130
131     xbt_ex_free(e);
132     THROW1(mismatch_error,1,"Asked to pop the non-existant %s", name);
133   }
134   xbt_dynar_pop(varstack, &var);
135   
136   if (!xbt_dynar_length(varstack)) {
137     DEBUG1("Last incarnation of %s poped. Kill it",name);
138     xbt_dict_remove(ps->space, name);
139     xbt_dynar_free(&varstack);
140   }
141   
142   if (ddt)
143     *ddt = var->type;  
144   data = var->data;
145   
146   free(var);
147   
148   xbt_dynar_pop(ps->frames, &frame);
149   {
150     int l = xbt_dynar_length(frame);
151     
152     while (l--) {
153       char *_name = NULL;
154                                                                                 
155       _name = xbt_dynar_get_as(frame, l, char*);
156       if (!strcmp(name, _name)) {
157         xbt_dynar_remove_at(frame, l, &_name);
158         free(_name);
159         break;
160       }
161     }
162   }
163   xbt_dynar_push(ps->frames, &frame);
164   
165   *res = data;
166   return no_error;
167 }
168
169 /** \brief Change the value of an element in the PS.
170  * 
171  * If it's not present in the current block, look in the upper ones.
172  * If it's not present in any of them, modify in the globals
173  * If not present there neither, the code may segfault (Oli?).
174  *
175  * Once a reference to an element of that name is found somewhere in the PS,
176  *   its value is changed.
177  */
178 void
179 gras_cbps_v_set (gras_cbps_t          ps,
180                  const char          *name,
181                  void                *data,
182                  gras_datadesc_type_t ddt) {
183
184   xbt_dynar_t dynar = NULL;
185   gras_cbps_elm_t elm = NULL;
186   
187   DEBUG1("set(%s)",name);
188   dynar = xbt_dict_get_or_null(ps->space, name);
189
190   if (dynar == NULL) {
191     dynar = xbt_dynar_new(sizeof (gras_cbps_elm_t), NULL);
192     xbt_dict_set(ps->space, name, (void **)dynar, NULL);
193     
194     elm   = xbt_new0(s_gras_cbps_elm_t,1);
195     xbt_dynar_push(ps->globals, &name);
196   } else {
197     xbt_dynar_pop(dynar, &elm);
198   }
199   
200   elm->type   = ddt;
201   elm->data   = data;
202  
203   xbt_dynar_push(dynar, &elm);
204
205 }
206
207 /** \brief Get the value of an element in the PS without modifying it.
208  * 
209  * (note that you get the content of the data struct and not a copy to it)
210  * If it's not present in the current block, look in the upper ones.
211  * If it's not present in any of them, look in the globals
212  * If not present there neither, the code may segfault (Oli?).
213  */
214 void *
215 gras_cbps_v_get (gras_cbps_t           ps, 
216                  const char           *name,
217                  /* OUT */gras_datadesc_type_t *ddt) {
218   
219   xbt_dynar_t    dynar = NULL;
220   gras_cbps_elm_t elm   = NULL;
221   
222   DEBUG1("get(%s)",name);
223   dynar = xbt_dict_get(ps->space, name);
224   xbt_dynar_pop(dynar, &elm);
225   xbt_dynar_push(dynar, &elm);
226   
227   if (ddt) {
228     *ddt = elm->type;
229   }
230   
231   return elm->data;
232
233 }
234
235 /** \brief Begins a new block. 
236  *
237  * Blocks are usefull to remove a whole set of declarations you don't even know
238  *
239  * E.g., they constitute an elegent solution to recursive data structures. 
240  *
241  * push/pop may be used in some cases for that, but if your recursive data 
242  * struct contains other structs needing themselves callbacks, you have to
243  * use block_{begin,end} to do the trick.
244  */
245
246 void
247 gras_cbps_block_begin(gras_cbps_t ps) {
248
249   xbt_dynar_t dynar = NULL;
250
251   DEBUG0(">>> Block begin");
252   dynar = xbt_dynar_new(sizeof (char *), NULL);
253   xbt_dynar_push(ps->frames, &dynar);
254 }
255
256 /** \brief End the current block, and go back to the upper one. */
257 void
258 gras_cbps_block_end(gras_cbps_t ps) {
259
260   xbt_dynar_t  frame        = NULL;
261   int           cursor       =    0;
262   char         *name         = NULL;
263
264   xbt_assert0(xbt_dynar_length(ps->frames),
265                "More block_end than block_begin");
266   xbt_dynar_pop(ps->frames, &frame);
267   
268   xbt_dynar_foreach(frame, cursor, name) {
269
270     xbt_dynar_t    varstack    = NULL;
271     gras_cbps_elm_t var         = NULL;
272  
273     DEBUG2("Get ride of %s (%p)",name,(void*)name);
274     varstack = xbt_dict_get(ps->space, name);
275     xbt_dynar_pop(varstack, &var);
276  
277     if (!xbt_dynar_length(varstack)) {
278       xbt_dict_remove(ps->space, name);
279       xbt_dynar_free_container(&varstack); /*already empty, save a test ;) */
280     }
281     
282     if (var->data) free(var->data);
283     free(var);
284     free(name);
285   }
286   xbt_dynar_free_container(&frame);/* we just emptied it */
287   DEBUG0("<<< Block end");
288 }
289
290
291 /** \brief Push a new integer value into the cbps. */
292 void
293 gras_cbps_i_push(gras_cbps_t ps,
294                  int val) {
295   DEBUG1("push %d as a size",val);
296   xbt_dynar_push_as(ps->lints,int,val);
297 }
298 /** \brief Pop the lastly pushed integer value from the cbps. */
299 int
300 gras_cbps_i_pop(gras_cbps_t ps) {
301   int ret;
302
303   xbt_assert0(xbt_dynar_length(ps->lints) > 0,
304                "gras_cbps_i_pop: no value to pop");
305   ret = xbt_dynar_pop_as(ps->lints,int);
306   DEBUG1("pop %d as a size",ret);
307   return ret;
308 }
309
310 /** \brief Generic cb returning the lastly pushed value
311  * 
312  * Used by \ref gras_datadesc_ref_pop_arr
313  */
314 int gras_datadesc_cb_pop(gras_datadesc_type_t ignored, gras_cbps_t vars, void *data) {
315   return gras_cbps_i_pop(vars);
316 }
317
318 /** \brief Cb to push an integer. Must be attached to the field you want to push */
319 void gras_datadesc_cb_push_int(gras_datadesc_type_t ignored, gras_cbps_t vars, void *data) {
320    int *i = (int*)data;
321    gras_cbps_i_push(vars, (int) *i);
322 }
323
324 /** \brief Cb to push an unsigned integer. Must be attached to the field you want to push */
325 void gras_datadesc_cb_push_uint(gras_datadesc_type_t ignored, gras_cbps_t vars, void *data) {
326    unsigned int *i = (unsigned int*)data;
327    gras_cbps_i_push(vars, (int) *i);
328 }
329
330 /** \brief Cb to push an long integer. Must be attached to the field you want to push
331  */
332 void gras_datadesc_cb_push_lint(gras_datadesc_type_t ignored, gras_cbps_t vars, void *data) {
333    long int *i = (long int*)data;
334    gras_cbps_i_push(vars, (int) *i);
335 }
336 /** \brief Cb to push an long integer. Must be attached to the field you want to push
337  */
338 void gras_datadesc_cb_push_ulint(gras_datadesc_type_t ignored, gras_cbps_t vars, void *data) {
339    unsigned long int *i = (unsigned long int*)data;
340    gras_cbps_i_push(vars, (int) *i);
341 }