1 /* cbps - persistent states for callbacks */
3 /* Copyright (c) 2004, 2005, 2006, 2007, 2008, 2009, 2010. The SimGrid Team.
4 * All rights reserved. */
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. */
10 #include "xbt/datadesc/datadesc_private.h"
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_ddt_cbps, xbt_ddt,
12 "callback persistent state");
15 xbt_datadesc_type_t type;
17 } s_xbt_cbps_elm_t, *xbt_cbps_elm_t;
19 typedef struct s_xbt_cbps {
20 xbt_dynar_t lints; /* simple stack of long integers (easy interface) */
22 xbt_dict_t space; /* varname x dynar of xbt_cbps_elm_t */
23 xbt_dynar_t frames; /* of dynar of names defined within this frame
24 (and to pop when we leave it) */
28 xbt_cbps_t xbt_cbps_new(void)
32 res = xbt_new(s_xbt_cbps_t, 1);
34 res->lints = xbt_dynar_new(sizeof(int), NULL);
35 res->space = xbt_dict_new_homogeneous(NULL);
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);
40 xbt_cbps_block_begin(res);
45 void xbt_cbps_free(xbt_cbps_t * state)
48 xbt_dynar_free(&((*state)->lints));
50 xbt_cbps_block_end(*state);
51 xbt_dict_free(&((*state)->space));
52 xbt_dynar_free(&((*state)->frames));
53 xbt_dynar_free(&((*state)->globals));
59 void xbt_cbps_reset(xbt_cbps_t state)
62 xbt_dynar_reset(state->lints);
64 xbt_dict_reset(state->space);
66 xbt_dynar_reset(state->frames);
67 xbt_dynar_reset(state->globals);
70 /** \brief Declare a new element in the PS, and give it a value.
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.
77 xbt_cbps_v_push(xbt_cbps_t ps,
78 const char *name, void *data, xbt_datadesc_type_t ddt)
81 xbt_dynar_t varstack = NULL, frame;
83 char *varname = (char *) xbt_strdup(name);
86 XBT_DEBUG("push(%s,%p)", name, (void *) data);
89 varstack = xbt_dict_get(ps->space, name);
92 if (e.category != mismatch_error)
95 XBT_DEBUG("Create a new variable stack for '%s' into the space", name);
96 varstack = xbt_dynar_new(sizeof(xbt_cbps_elm_t *), NULL);
97 xbt_dict_set(ps->space, name, (void **) varstack, NULL);
99 /* leaking, you think? only if you do not close all the openned blocks ;) */
102 var = xbt_new0(s_xbt_cbps_elm_t, 1);
106 xbt_dynar_push(varstack, &var);
108 xbt_dynar_pop(ps->frames, &frame);
109 XBT_DEBUG("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);
115 /** \brief Retrieve an element from the PS, and remove it from the PS.
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.
121 xbt_cbps_v_pop(xbt_cbps_t ps,
122 const char *name, xbt_datadesc_type_t * ddt, void **res)
124 xbt_dynar_t varstack = NULL, frame = NULL;
125 xbt_cbps_elm_t var = NULL;
129 XBT_DEBUG("pop(%s)", name);
131 varstack = xbt_dict_get(ps->space, name);
134 if (e.category != mismatch_error)
138 THROWF(not_found_error, 1, "Asked to pop the non-existant %s", name);
140 xbt_dynar_pop(varstack, &var);
142 if (xbt_dynar_is_empty(varstack)) {
143 XBT_DEBUG("Last incarnation of %s poped. Kill it", name);
144 xbt_dict_remove(ps->space, name);
145 xbt_dynar_free(&varstack);
154 xbt_dynar_pop(ps->frames, &frame);
156 int l = xbt_dynar_length(frame);
161 _name = xbt_dynar_get_as(frame, l, char *);
162 if (!strcmp(name, _name)) {
163 xbt_dynar_remove_at(frame, l, &_name);
169 xbt_dynar_push(ps->frames, &frame);
174 /** \brief Change the value of an element in the PS.
176 * If it's not present in the current block, look in the upper ones.
177 * If it's not present in any of them, modify in the globals
178 * If not present there neither, the code may segfault (Oli?).
180 * Once a reference to an element of that name is found somewhere in the PS,
181 * its value is changed.
184 xbt_cbps_v_set(xbt_cbps_t ps,
185 const char *name, void *data, xbt_datadesc_type_t ddt)
188 xbt_dynar_t dynar = NULL;
189 xbt_cbps_elm_t elm = NULL;
191 XBT_DEBUG("set(%s)", name);
192 dynar = xbt_dict_get_or_null(ps->space, name);
195 dynar = xbt_dynar_new(sizeof(xbt_cbps_elm_t), NULL);
196 xbt_dict_set(ps->space, name, (void **) dynar, NULL);
198 elm = xbt_new0(s_xbt_cbps_elm_t, 1);
199 xbt_dynar_push(ps->globals, &name);
201 xbt_dynar_pop(dynar, &elm);
207 xbt_dynar_push(dynar, &elm);
211 /** \brief Get the value of an element in the PS without modifying it.
213 * (note that you get the content of the data struct and not a copy to it)
214 * If it's not present in the current block, look in the upper ones.
215 * If it's not present in any of them, look in the globals
216 * If not present there neither, the code may segfault (Oli?).
218 void *xbt_cbps_v_get(xbt_cbps_t ps, const char *name,
219 /* OUT */ xbt_datadesc_type_t * ddt)
222 xbt_dynar_t dynar = NULL;
223 xbt_cbps_elm_t elm = NULL;
225 XBT_DEBUG("get(%s)", name);
226 dynar = xbt_dict_get(ps->space, name);
227 xbt_dynar_pop(dynar, &elm);
228 xbt_dynar_push(dynar, &elm);
238 /** \brief Begins a new block.
240 * Blocks are usefull to remove a whole set of declarations you don't even know
242 * E.g., they constitute an elegent solution to recursive data structures.
244 * push/pop may be used in some cases for that, but if your recursive data
245 * struct contains other structs needing themselves callbacks, you have to
246 * use block_{begin,end} to do the trick.
249 void xbt_cbps_block_begin(xbt_cbps_t ps)
252 xbt_dynar_t dynar = NULL;
254 XBT_DEBUG(">>> Block begin");
255 dynar = xbt_dynar_new(sizeof(char *), NULL);
256 xbt_dynar_push(ps->frames, &dynar);
259 /** \brief End the current block, and go back to the upper one. */
260 void xbt_cbps_block_end(xbt_cbps_t ps)
263 xbt_dynar_t frame = NULL;
264 unsigned int cursor = 0;
267 xbt_assert(xbt_dynar_length(ps->frames),
268 "More block_end than block_begin");
269 xbt_dynar_pop(ps->frames, &frame);
271 xbt_dynar_foreach(frame, cursor, name) {
273 xbt_dynar_t varstack = NULL;
274 xbt_cbps_elm_t var = NULL;
276 XBT_DEBUG("Get ride of %s (%p)", name, (void *) name);
277 varstack = xbt_dict_get(ps->space, name);
278 xbt_dynar_pop(varstack, &var);
280 if (xbt_dynar_is_empty(varstack)) {
281 xbt_dict_remove(ps->space, name);
282 xbt_dynar_free_container(&varstack); /*already empty, save a test ;) */
289 xbt_dynar_free_container(&frame); /* we just emptied it */
290 XBT_DEBUG("<<< Block end");
294 /** \brief Push a new integer value into the cbps. */
295 void xbt_cbps_i_push(xbt_cbps_t ps, int val)
297 XBT_DEBUG("push %d as a size", val);
298 xbt_dynar_push_as(ps->lints, int, val);
301 /** \brief Pop the lastly pushed integer value from the cbps. */
302 int xbt_cbps_i_pop(xbt_cbps_t ps)
306 xbt_assert(!xbt_dynar_is_empty(ps->lints),
307 "xbt_cbps_i_pop: no value to pop");
308 ret = xbt_dynar_pop_as(ps->lints, int);
309 XBT_DEBUG("pop %d as a size", ret);
313 /** \brief Generic cb returning the lastly pushed value
315 * Used by \ref xbt_datadesc_ref_pop_arr
317 int xbt_datadesc_cb_pop(xbt_datadesc_type_t ignored, xbt_cbps_t vars,
320 return xbt_cbps_i_pop(vars);
323 /* ************************* */
324 /* **** PUSHy callbacks **** */
325 /* ************************* */
327 /** \brief Cb to push an integer. Must be attached to the field you want to push */
328 void xbt_datadesc_cb_push_int(xbt_datadesc_type_t ignored,
329 xbt_cbps_t vars, void *data)
331 int *i = (int *) data;
332 xbt_cbps_i_push(vars, (int) *i);
335 /** \brief Cb to push an unsigned integer. Must be attached to the field you want to push */
336 void xbt_datadesc_cb_push_uint(xbt_datadesc_type_t ignored,
337 xbt_cbps_t vars, void *data)
339 unsigned int *i = (unsigned int *) data;
340 xbt_cbps_i_push(vars, (int) *i);
343 /** \brief Cb to push an long integer. Must be attached to the field you want to push
345 void xbt_datadesc_cb_push_lint(xbt_datadesc_type_t ignored,
346 xbt_cbps_t vars, void *data)
348 long int *i = (long int *) data;
349 xbt_cbps_i_push(vars, (int) *i);
352 /** \brief Cb to push an unsigned long integer. Must be attached to the field you want to push
354 void xbt_datadesc_cb_push_ulint(xbt_datadesc_type_t ignored,
355 xbt_cbps_t vars, void *data)
357 unsigned long int *i = (unsigned long int *) data;
358 xbt_cbps_i_push(vars, (int) *i);
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 xbt_datadesc_cb_push_int_mult(xbt_datadesc_type_t ignored,
366 xbt_cbps_t vars, void *data)
368 int old = *(int *) data;
369 int new = xbt_cbps_i_pop(vars);
370 XBT_DEBUG("push %d x %d as a size", old, new);
371 xbt_cbps_i_push(vars, old * new);
374 /** \brief Cb to push an unsigned integer as multiplier. Must be attached to the field you want to push */
375 void xbt_datadesc_cb_push_uint_mult(xbt_datadesc_type_t ignored,
376 xbt_cbps_t vars, void *data)
378 unsigned int old = *(unsigned int *) data;
379 unsigned int new = xbt_cbps_i_pop(vars);
381 XBT_DEBUG("push %u x %u as a size", old, new);
382 xbt_cbps_i_push(vars, (int) (old * new));
385 /** \brief Cb to push an long integer as multiplier. Must be attached to the field you want to push
387 void xbt_datadesc_cb_push_lint_mult(xbt_datadesc_type_t ignored,
388 xbt_cbps_t vars, void *data)
390 long int i = *(long int *) data;
391 i *= xbt_cbps_i_pop(vars);
392 xbt_cbps_i_push(vars, (int) i);
395 /** \brief Cb to push an unsigned long integer as multiplier. Must be attached to the field you want to push
397 void xbt_datadesc_cb_push_ulint_mult(xbt_datadesc_type_t ignored,
398 xbt_cbps_t vars, void *data)
400 unsigned long int old = *(unsigned long int *) data;
401 unsigned long int new = xbt_cbps_i_pop(vars);
403 XBT_DEBUG("push %lu x %lu as a size", old, new);
404 xbt_cbps_i_push(vars, (int) (old * new));