1 /* cbps - persistant states for callbacks */
3 /* Copyright (c) 2003 Olivier Aumage. */
4 /* Copyright (c) 2003, 2004 Martin Quinson. */
5 /* All rights reserved. */
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. */
11 #include "gras/DataDesc/datadesc_private.h"
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_ddt_cbps, gras_ddt,
13 "callback persistant state");
16 gras_datadesc_type_t type;
18 } s_gras_cbps_elm_t, *gras_cbps_elm_t;
20 typedef struct s_gras_cbps {
21 xbt_dynar_t lints; /* simple stack of long integers (easy interface) */
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) */
29 gras_cbps_t gras_cbps_new(void)
33 res = xbt_new(s_gras_cbps_t, 1);
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);
41 gras_cbps_block_begin(res);
46 void gras_cbps_free(gras_cbps_t * state)
49 xbt_dynar_free(&((*state)->lints));
51 gras_cbps_block_end(*state);
52 xbt_dict_free(&((*state)->space));
53 xbt_dynar_free(&((*state)->frames));
54 xbt_dynar_free(&((*state)->globals));
60 void gras_cbps_reset(gras_cbps_t state)
63 xbt_dynar_reset(state->lints);
65 xbt_dict_reset(state->space);
67 xbt_dynar_reset(state->frames);
68 xbt_dynar_reset(state->globals);
71 /** \brief Declare a new element in the PS, and give it a value.
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.
78 gras_cbps_v_push(gras_cbps_t ps,
79 const char *name, void *data, gras_datadesc_type_t ddt)
82 xbt_dynar_t varstack = NULL, frame;
84 char *varname = (char *) xbt_strdup(name);
87 DEBUG2("push(%s,%p)", name, (void *) data);
90 varstack = xbt_dict_get(ps->space, name);
92 if (e.category != mismatch_error)
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);
99 /* leaking, you think? only if you do not close all the openned blocks ;) */
102 var = xbt_new0(s_gras_cbps_elm_t, 1);
106 xbt_dynar_push(varstack, &var);
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);
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 gras_cbps_v_pop(gras_cbps_t ps,
122 const char *name, gras_datadesc_type_t * ddt, void **res)
124 xbt_dynar_t varstack=NULL, frame=NULL;
125 gras_cbps_elm_t var = NULL;
129 DEBUG1("pop(%s)", name);
131 varstack = xbt_dict_get(ps->space, name);
133 if (e.category != mismatch_error)
137 THROW1(not_found_error, 1, "Asked to pop the non-existant %s", name);
139 xbt_dynar_pop(varstack, &var);
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);
153 xbt_dynar_pop(ps->frames, &frame);
155 int l = xbt_dynar_length(frame);
160 _name = xbt_dynar_get_as(frame, l, char *);
161 if (!strcmp(name, _name)) {
162 xbt_dynar_remove_at(frame, l, &_name);
168 xbt_dynar_push(ps->frames, &frame);
173 /** \brief Change the value of an element in the PS.
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?).
179 * Once a reference to an element of that name is found somewhere in the PS,
180 * its value is changed.
183 gras_cbps_v_set(gras_cbps_t ps,
184 const char *name, void *data, gras_datadesc_type_t ddt)
187 xbt_dynar_t dynar = NULL;
188 gras_cbps_elm_t elm = NULL;
190 DEBUG1("set(%s)", name);
191 dynar = xbt_dict_get_or_null(ps->space, name);
194 dynar = xbt_dynar_new(sizeof(gras_cbps_elm_t), NULL);
195 xbt_dict_set(ps->space, name, (void **) dynar, NULL);
197 elm = xbt_new0(s_gras_cbps_elm_t, 1);
198 xbt_dynar_push(ps->globals, &name);
200 xbt_dynar_pop(dynar, &elm);
206 xbt_dynar_push(dynar, &elm);
210 /** \brief Get the value of an element in the PS without modifying it.
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?).
217 void *gras_cbps_v_get(gras_cbps_t ps, const char *name,
218 /* OUT */ gras_datadesc_type_t * ddt)
221 xbt_dynar_t dynar = NULL;
222 gras_cbps_elm_t elm = NULL;
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);
237 /** \brief Begins a new block.
239 * Blocks are usefull to remove a whole set of declarations you don't even know
241 * E.g., they constitute an elegent solution to recursive data structures.
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.
248 void gras_cbps_block_begin(gras_cbps_t ps)
251 xbt_dynar_t dynar = NULL;
253 DEBUG0(">>> Block begin");
254 dynar = xbt_dynar_new(sizeof(char *), NULL);
255 xbt_dynar_push(ps->frames, &dynar);
258 /** \brief End the current block, and go back to the upper one. */
259 void gras_cbps_block_end(gras_cbps_t ps)
262 xbt_dynar_t frame = NULL;
263 unsigned int cursor = 0;
266 xbt_assert0(xbt_dynar_length(ps->frames),
267 "More block_end than block_begin");
268 xbt_dynar_pop(ps->frames, &frame);
270 xbt_dynar_foreach(frame, cursor, name) {
272 xbt_dynar_t varstack = NULL;
273 gras_cbps_elm_t var = NULL;
275 DEBUG2("Get ride of %s (%p)", name, (void *) name);
276 varstack = xbt_dict_get(ps->space, name);
277 xbt_dynar_pop(varstack, &var);
279 if (!xbt_dynar_length(varstack)) {
280 xbt_dict_remove(ps->space, name);
281 xbt_dynar_free_container(&varstack); /*already empty, save a test ;) */
289 xbt_dynar_free_container(&frame); /* we just emptied it */
290 DEBUG0("<<< Block end");
294 /** \brief Push a new integer value into the cbps. */
295 void gras_cbps_i_push(gras_cbps_t ps, int val)
297 DEBUG1("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 gras_cbps_i_pop(gras_cbps_t ps)
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);
313 /** \brief Generic cb returning the lastly pushed value
315 * Used by \ref gras_datadesc_ref_pop_arr
317 int gras_datadesc_cb_pop(gras_datadesc_type_t ignored, gras_cbps_t vars,
320 return gras_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 gras_datadesc_cb_push_int(gras_datadesc_type_t ignored, gras_cbps_t vars,
331 int *i = (int *) data;
332 gras_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 gras_datadesc_cb_push_uint(gras_datadesc_type_t ignored,
337 gras_cbps_t vars, void *data)
339 unsigned int *i = (unsigned int *) data;
340 gras_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 gras_datadesc_cb_push_lint(gras_datadesc_type_t ignored,
346 gras_cbps_t vars, void *data)
348 long int *i = (long int *) data;
349 gras_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 gras_datadesc_cb_push_ulint(gras_datadesc_type_t ignored,
355 gras_cbps_t vars, void *data)
357 unsigned long int *i = (unsigned long int *) data;
358 gras_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 gras_datadesc_cb_push_int_mult(gras_datadesc_type_t ignored,
366 gras_cbps_t vars, void *data)
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);
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)
378 unsigned int old = *(unsigned int *) data;
379 unsigned int new = gras_cbps_i_pop(vars);
381 DEBUG2("push %d x %d as a size", old, new);
382 gras_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 gras_datadesc_cb_push_lint_mult(gras_datadesc_type_t ignored,
388 gras_cbps_t vars, void *data)
390 long int i = *(long int *) data;
391 i *= gras_cbps_i_pop(vars);
392 gras_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 gras_datadesc_cb_push_ulint_mult(gras_datadesc_type_t ignored,
398 gras_cbps_t vars, void *data)
400 unsigned long int old = *(unsigned long int *) data;
401 unsigned long int new = gras_cbps_i_pop(vars);
403 DEBUG2("push %ld x %ld as a size", old, new);
404 gras_cbps_i_push(vars, (int) (old * new));