Logo AND Algorithmique Numérique Distribuée

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