Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d4ac84b0b6af4563d9ada3620320c2b64078fb95
[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 void gras_cbps_reset(gras_cbps_t state) {
64
65   xbt_dynar_reset(state->lints);
66
67   xbt_dict_free ( &(state->space) );
68   state->space = xbt_dict_new();
69
70   xbt_dynar_reset(state->frames);
71   xbt_dynar_reset(state->globals);
72 }
73
74 /** \brief Declare a new element in the PS, and give it a value.
75  *
76  * If an element of that
77  * name already exists, it is masked by the one given here, and will be 
78  * seeable again only after a pop to remove the value this push adds.
79  */
80 void
81 gras_cbps_v_push(gras_cbps_t          ps,
82                  const char          *name,
83                  void                *data,
84                  gras_datadesc_type_t ddt) {
85
86   xbt_dynar_t     varstack=NULL,frame;
87   gras_cbps_elm_t var;
88   char           *varname = (char*)xbt_strdup(name);
89   xbt_ex_t        e;
90
91   DEBUG2("push(%s,%p)",name,(void*)data);
92
93   TRY {
94     varstack = xbt_dict_get(ps->space, name);
95   } CATCH(e) {
96     if (e.category != mismatch_error) 
97       RETHROW;
98
99     DEBUG1("Create a new variable stack for '%s' into the space",name);
100     varstack = xbt_dynar_new(sizeof (gras_cbps_elm_t *), NULL);
101     xbt_dict_set(ps->space, varname, (void **)varstack, NULL);
102     xbt_ex_free(&e);
103     /* leaking, you think? only if you do not close all the openned blocks ;)*/
104   }
105  
106   var       = xbt_new0(s_gras_cbps_elm_t,1);
107   var->type = ddt;
108   var->data = data;
109   
110   xbt_dynar_push(varstack, &var);
111   
112   xbt_dynar_pop(ps->frames, &frame);
113   DEBUG4("Push %s (%p @%p) into frame %p",varname,(void*)varname,(void*)&varname,(void*)frame);
114   xbt_dynar_push(frame, &varname);
115   xbt_dynar_push(ps->frames, &frame); 
116 }
117
118 /** \brief Retrieve an element from the PS, and remove it from the PS.
119  *
120  * If it's not present in the current block, it will fail (throwing not_found)
121  * and not search in upper blocks since this denotes a programmation error.
122  */
123 void
124 gras_cbps_v_pop (gras_cbps_t            ps, 
125                  const char            *name,
126                  gras_datadesc_type_t  *ddt,
127                  void                 **res) {
128   xbt_dynar_t          varstack,frame;
129   gras_cbps_elm_t       var            = NULL;
130   void                 *data           = NULL;
131   xbt_ex_t e;
132
133   DEBUG1("pop(%s)",name);
134   TRY {
135     varstack = xbt_dict_get(ps->space, name);
136   } CATCH(e) {
137     if (e.category != mismatch_error)
138       RETHROW;
139
140     xbt_ex_free(&e);
141     THROW1(not_found_error,1,"Asked to pop the non-existant %s", name);
142   }
143   xbt_dynar_pop(varstack, &var);
144   
145   if (!xbt_dynar_length(varstack)) {
146     DEBUG1("Last incarnation of %s poped. Kill it",name);
147     xbt_dict_remove(ps->space, name);
148     xbt_dynar_free(&varstack);
149   }
150   
151   if (ddt)
152     *ddt = var->type;  
153   data = var->data;
154   
155   free(var);
156   
157   xbt_dynar_pop(ps->frames, &frame);
158   {
159     int l = xbt_dynar_length(frame);
160     
161     while (l--) {
162       char *_name = NULL;
163                                                                                 
164       _name = xbt_dynar_get_as(frame, l, char*);
165       if (!strcmp(name, _name)) {
166         xbt_dynar_remove_at(frame, l, &_name);
167         free(_name);
168         break;
169       }
170     }
171   }
172   xbt_dynar_push(ps->frames, &frame);
173   
174   *res = data;
175 }
176
177 /** \brief Change the value of an element in the PS.
178  * 
179  * If it's not present in the current block, look in the upper ones.
180  * If it's not present in any of them, modify in the globals
181  * If not present there neither, the code may segfault (Oli?).
182  *
183  * Once a reference to an element of that name is found somewhere in the PS,
184  *   its value is changed.
185  */
186 void
187 gras_cbps_v_set (gras_cbps_t          ps,
188                  const char          *name,
189                  void                *data,
190                  gras_datadesc_type_t ddt) {
191
192   xbt_dynar_t dynar = NULL;
193   gras_cbps_elm_t elm = NULL;
194   
195   DEBUG1("set(%s)",name);
196   dynar = xbt_dict_get_or_null(ps->space, name);
197
198   if (dynar == NULL) {
199     dynar = xbt_dynar_new(sizeof (gras_cbps_elm_t), NULL);
200     xbt_dict_set(ps->space, name, (void **)dynar, NULL);
201     
202     elm   = xbt_new0(s_gras_cbps_elm_t,1);
203     xbt_dynar_push(ps->globals, &name);
204   } else {
205     xbt_dynar_pop(dynar, &elm);
206   }
207   
208   elm->type   = ddt;
209   elm->data   = data;
210  
211   xbt_dynar_push(dynar, &elm);
212
213 }
214
215 /** \brief Get the value of an element in the PS without modifying it.
216  * 
217  * (note that you get the content of the data struct and not a copy to it)
218  * If it's not present in the current block, look in the upper ones.
219  * If it's not present in any of them, look in the globals
220  * If not present there neither, the code may segfault (Oli?).
221  */
222 void *
223 gras_cbps_v_get (gras_cbps_t           ps, 
224                  const char           *name,
225                  /* OUT */gras_datadesc_type_t *ddt) {
226   
227   xbt_dynar_t    dynar = NULL;
228   gras_cbps_elm_t elm   = NULL;
229   
230   DEBUG1("get(%s)",name);
231   dynar = xbt_dict_get(ps->space, name);
232   xbt_dynar_pop(dynar, &elm);
233   xbt_dynar_push(dynar, &elm);
234   
235   if (ddt) {
236     *ddt = elm->type;
237   }
238   
239   return elm->data;
240
241 }
242
243 /** \brief Begins a new block. 
244  *
245  * Blocks are usefull to remove a whole set of declarations you don't even know
246  *
247  * E.g., they constitute an elegent solution to recursive data structures. 
248  *
249  * push/pop may be used in some cases for that, but if your recursive data 
250  * struct contains other structs needing themselves callbacks, you have to
251  * use block_{begin,end} to do the trick.
252  */
253
254 void
255 gras_cbps_block_begin(gras_cbps_t ps) {
256
257   xbt_dynar_t dynar = NULL;
258
259   DEBUG0(">>> Block begin");
260   dynar = xbt_dynar_new(sizeof (char *), NULL);
261   xbt_dynar_push(ps->frames, &dynar);
262 }
263
264 /** \brief End the current block, and go back to the upper one. */
265 void
266 gras_cbps_block_end(gras_cbps_t ps) {
267
268   xbt_dynar_t  frame        = NULL;
269   int           cursor       =    0;
270   char         *name         = NULL;
271
272   xbt_assert0(xbt_dynar_length(ps->frames),
273                "More block_end than block_begin");
274   xbt_dynar_pop(ps->frames, &frame);
275   
276   xbt_dynar_foreach(frame, cursor, name) {
277
278     xbt_dynar_t    varstack    = NULL;
279     gras_cbps_elm_t var         = NULL;
280  
281     DEBUG2("Get ride of %s (%p)",name,(void*)name);
282     varstack = xbt_dict_get(ps->space, name);
283     xbt_dynar_pop(varstack, &var);
284  
285     if (!xbt_dynar_length(varstack)) {
286       xbt_dict_remove(ps->space, name);
287       xbt_dynar_free_container(&varstack); /*already empty, save a test ;) */
288     }
289     
290     if (var->data) free(var->data);
291     free(var);
292     free(name);
293   }
294   xbt_dynar_free_container(&frame);/* we just emptied it */
295   DEBUG0("<<< Block end");
296 }
297
298
299 /** \brief Push a new integer value into the cbps. */
300 void
301 gras_cbps_i_push(gras_cbps_t ps,
302                  int val) {
303   DEBUG1("push %d as a size",val);
304   xbt_dynar_push_as(ps->lints,int,val);
305 }
306 /** \brief Pop the lastly pushed integer value from the cbps. */
307 int
308 gras_cbps_i_pop(gras_cbps_t ps) {
309   int ret;
310
311   xbt_assert0(xbt_dynar_length(ps->lints) > 0,
312                "gras_cbps_i_pop: no value to pop");
313   ret = xbt_dynar_pop_as(ps->lints,int);
314   DEBUG1("pop %d as a size",ret);
315   return ret;
316 }
317
318 /** \brief Generic cb returning the lastly pushed value
319  * 
320  * Used by \ref gras_datadesc_ref_pop_arr
321  */
322 int gras_datadesc_cb_pop(gras_datadesc_type_t ignored, gras_cbps_t vars, void *data) {
323   int res =  gras_cbps_i_pop(vars);
324   DEBUG1("Pop %d as a size",res);
325   return res;
326 }
327
328 /* ************************* */
329 /* **** PUSHy callbacks **** */
330 /* ************************* */
331
332 /** \brief Cb to push an integer. Must be attached to the field you want to push */
333 void gras_datadesc_cb_push_int(gras_datadesc_type_t ignored, gras_cbps_t vars, void *data) {
334   int *i = (int*)data;
335   gras_cbps_i_push(vars, (int) *i);
336 }
337
338 /** \brief Cb to push an unsigned integer. Must be attached to the field you want to push */
339 void gras_datadesc_cb_push_uint(gras_datadesc_type_t ignored, gras_cbps_t vars, void *data) {
340   unsigned int *i = (unsigned int*)data;
341   gras_cbps_i_push(vars, (int) *i);
342 }
343
344 /** \brief Cb to push an long integer. Must be attached to the field you want to push
345  */
346 void gras_datadesc_cb_push_lint(gras_datadesc_type_t ignored, gras_cbps_t vars, void *data) {
347   long int *i = (long int*)data;
348   gras_cbps_i_push(vars, (int) *i);
349 }
350 /** \brief Cb to push an unsigned long integer. Must be attached to the field you want to push
351  */
352 void gras_datadesc_cb_push_ulint(gras_datadesc_type_t ignored, gras_cbps_t vars, void *data) {
353   unsigned long int *i = (unsigned long int*)data;
354   gras_cbps_i_push(vars, (int) *i);
355 }
356
357 /* ************************************ */
358 /* **** PUSHy multiplier callbacks **** */
359 /* ************************************ */
360 /** \brief Cb to push an integer as multiplier. Must be attached to the field you want to push */
361 void gras_datadesc_cb_push_int_mult(gras_datadesc_type_t ignored, gras_cbps_t vars, void *data) {
362   int i = *(int*)data;
363   i *= gras_cbps_i_pop(vars);
364   gras_cbps_i_push(vars, i);
365 }
366
367 /** \brief Cb to push an unsigned integer as multiplier. Must be attached to the field you want to push */
368 void gras_datadesc_cb_push_uint_mult(gras_datadesc_type_t ignored, gras_cbps_t vars, void *data) {
369   unsigned int i = *(unsigned int*)data;
370   i *= gras_cbps_i_pop(vars);
371   gras_cbps_i_push(vars, (int) i);
372 }
373
374 /** \brief Cb to push an long integer as multiplier. Must be attached to the field you want to push
375  */
376 void gras_datadesc_cb_push_lint_mult(gras_datadesc_type_t ignored, gras_cbps_t vars, void *data) {
377   long int i = *(long int*)data;
378   i *= gras_cbps_i_pop(vars);
379   gras_cbps_i_push(vars, (int) i);
380 }
381 /** \brief Cb to push an unsigned long integer as multiplier. Must be attached to the field you want to push
382  */
383 void gras_datadesc_cb_push_ulint_mult(gras_datadesc_type_t ignored, gras_cbps_t vars, void *data) {
384   unsigned long int i = *(unsigned long int*)data;
385   i *= gras_cbps_i_pop(vars);
386   gras_cbps_i_push(vars, (int) i);
387 }