Logo AND Algorithmique Numérique Distribuée

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