Logo AND Algorithmique Numérique Distribuée

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