3 /* cbps - persistant states for callbacks */
5 /* Copyright (c) 2003 Olivier Aumage. */
6 /* Copyright (c) 2003, 2004 Martin Quinson. */
7 /* All rights reserved. */
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. */
13 #include "gras/DataDesc/datadesc_private.h"
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_ddt_cbps, gras_ddt,
15 "callback persistant state");
18 gras_datadesc_type_t type;
20 } s_gras_cbps_elm_t, *gras_cbps_elm_t;
22 typedef struct s_gras_cbps {
23 xbt_dynar_t lints; /* simple stack of long integers (easy interface) */
25 xbt_dict_t space; /* varname x dynar of gras_cbps_elm_t */
26 xbt_dynar_t frames; /* of dynar of names defined within this frame
27 (and to pop when we leave it) */
31 gras_cbps_t gras_cbps_new(void)
35 res = xbt_new(s_gras_cbps_t, 1);
37 res->lints = xbt_dynar_new(sizeof(int), NULL);
38 res->space = xbt_dict_new();
39 /* no leak, the content is freed manually on block_end */
40 res->frames = xbt_dynar_new(sizeof(xbt_dynar_t), NULL);
41 res->globals = xbt_dynar_new(sizeof(char *), NULL);
43 gras_cbps_block_begin(res);
48 void gras_cbps_free(gras_cbps_t * state)
51 xbt_dynar_free(&((*state)->lints));
53 gras_cbps_block_end(*state);
54 xbt_dict_free(&((*state)->space));
55 xbt_dynar_free(&((*state)->frames));
56 xbt_dynar_free(&((*state)->globals));
62 void gras_cbps_reset(gras_cbps_t state)
65 xbt_dynar_reset(state->lints);
67 xbt_dict_reset(state->space);
69 xbt_dynar_reset(state->frames);
70 xbt_dynar_reset(state->globals);
73 /** \brief Declare a new element in the PS, and give it a value.
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.
80 gras_cbps_v_push(gras_cbps_t ps,
81 const char *name, void *data, gras_datadesc_type_t ddt)
84 xbt_dynar_t varstack = NULL, frame;
86 char *varname = (char *) xbt_strdup(name);
89 DEBUG2("push(%s,%p)", name, (void *) data);
92 varstack = xbt_dict_get(ps->space, name);
94 if (e.category != mismatch_error)
97 DEBUG1("Create a new variable stack for '%s' into the space", name);
98 varstack = xbt_dynar_new(sizeof(gras_cbps_elm_t *), NULL);
99 xbt_dict_set(ps->space, varname, (void **) varstack, NULL);
101 /* leaking, you think? only if you do not close all the openned blocks ;) */
104 var = xbt_new0(s_gras_cbps_elm_t, 1);
108 xbt_dynar_push(varstack, &var);
110 xbt_dynar_pop(ps->frames, &frame);
111 DEBUG4("Push %s (%p @%p) into frame %p", varname, (void *) varname,
112 (void *) &varname, (void *) frame);
113 xbt_dynar_push(frame, &varname);
114 xbt_dynar_push(ps->frames, &frame);
117 /** \brief Retrieve an element from the PS, and remove it from the PS.
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.
123 gras_cbps_v_pop(gras_cbps_t ps,
124 const char *name, gras_datadesc_type_t * ddt, void **res)
126 xbt_dynar_t varstack=NULL, frame=NULL;
127 gras_cbps_elm_t var = NULL;
131 DEBUG1("pop(%s)", name);
133 varstack = xbt_dict_get(ps->space, name);
135 if (e.category != mismatch_error)
139 THROW1(not_found_error, 1, "Asked to pop the non-existant %s", name);
141 xbt_dynar_pop(varstack, &var);
143 if (!xbt_dynar_length(varstack)) {
144 DEBUG1("Last incarnation of %s poped. Kill it", name);
145 xbt_dict_remove(ps->space, name);
146 xbt_dynar_free(&varstack);
155 xbt_dynar_pop(ps->frames, &frame);
157 int l = xbt_dynar_length(frame);
162 _name = xbt_dynar_get_as(frame, l, char *);
163 if (!strcmp(name, _name)) {
164 xbt_dynar_remove_at(frame, l, &_name);
170 xbt_dynar_push(ps->frames, &frame);
175 /** \brief Change the value of an element in the PS.
177 * If it's not present in the current block, look in the upper ones.
178 * If it's not present in any of them, modify in the globals
179 * If not present there neither, the code may segfault (Oli?).
181 * Once a reference to an element of that name is found somewhere in the PS,
182 * its value is changed.
185 gras_cbps_v_set(gras_cbps_t ps,
186 const char *name, void *data, gras_datadesc_type_t ddt)
189 xbt_dynar_t dynar = NULL;
190 gras_cbps_elm_t elm = NULL;
192 DEBUG1("set(%s)", name);
193 dynar = xbt_dict_get_or_null(ps->space, name);
196 dynar = xbt_dynar_new(sizeof(gras_cbps_elm_t), NULL);
197 xbt_dict_set(ps->space, name, (void **) dynar, NULL);
199 elm = xbt_new0(s_gras_cbps_elm_t, 1);
200 xbt_dynar_push(ps->globals, &name);
202 xbt_dynar_pop(dynar, &elm);
208 xbt_dynar_push(dynar, &elm);
212 /** \brief Get the value of an element in the PS without modifying it.
214 * (note that you get the content of the data struct and not a copy to it)
215 * If it's not present in the current block, look in the upper ones.
216 * If it's not present in any of them, look in the globals
217 * If not present there neither, the code may segfault (Oli?).
219 void *gras_cbps_v_get(gras_cbps_t ps, const char *name,
220 /* OUT */ gras_datadesc_type_t * ddt)
223 xbt_dynar_t dynar = NULL;
224 gras_cbps_elm_t elm = NULL;
226 DEBUG1("get(%s)", name);
227 dynar = xbt_dict_get(ps->space, name);
228 xbt_dynar_pop(dynar, &elm);
229 xbt_dynar_push(dynar, &elm);
239 /** \brief Begins a new block.
241 * Blocks are usefull to remove a whole set of declarations you don't even know
243 * E.g., they constitute an elegent solution to recursive data structures.
245 * push/pop may be used in some cases for that, but if your recursive data
246 * struct contains other structs needing themselves callbacks, you have to
247 * use block_{begin,end} to do the trick.
250 void gras_cbps_block_begin(gras_cbps_t ps)
253 xbt_dynar_t dynar = NULL;
255 DEBUG0(">>> Block begin");
256 dynar = xbt_dynar_new(sizeof(char *), NULL);
257 xbt_dynar_push(ps->frames, &dynar);
260 /** \brief End the current block, and go back to the upper one. */
261 void gras_cbps_block_end(gras_cbps_t ps)
264 xbt_dynar_t frame = NULL;
265 unsigned int cursor = 0;
268 xbt_assert0(xbt_dynar_length(ps->frames),
269 "More block_end than block_begin");
270 xbt_dynar_pop(ps->frames, &frame);
272 xbt_dynar_foreach(frame, cursor, name) {
274 xbt_dynar_t varstack = NULL;
275 gras_cbps_elm_t var = NULL;
277 DEBUG2("Get ride of %s (%p)", name, (void *) name);
278 varstack = xbt_dict_get(ps->space, name);
279 xbt_dynar_pop(varstack, &var);
281 if (!xbt_dynar_length(varstack)) {
282 xbt_dict_remove(ps->space, name);
283 xbt_dynar_free_container(&varstack); /*already empty, save a test ;) */
291 xbt_dynar_free_container(&frame); /* we just emptied it */
292 DEBUG0("<<< Block end");
296 /** \brief Push a new integer value into the cbps. */
297 void gras_cbps_i_push(gras_cbps_t ps, int val)
299 DEBUG1("push %d as a size", val);
300 xbt_dynar_push_as(ps->lints, int, val);
303 /** \brief Pop the lastly pushed integer value from the cbps. */
304 int gras_cbps_i_pop(gras_cbps_t ps)
308 xbt_assert0(xbt_dynar_length(ps->lints) > 0,
309 "gras_cbps_i_pop: no value to pop");
310 ret = xbt_dynar_pop_as(ps->lints, int);
311 DEBUG1("pop %d as a size", ret);
315 /** \brief Generic cb returning the lastly pushed value
317 * Used by \ref gras_datadesc_ref_pop_arr
319 int gras_datadesc_cb_pop(gras_datadesc_type_t ignored, gras_cbps_t vars,
322 return gras_cbps_i_pop(vars);
325 /* ************************* */
326 /* **** PUSHy callbacks **** */
327 /* ************************* */
329 /** \brief Cb to push an integer. Must be attached to the field you want to push */
330 void gras_datadesc_cb_push_int(gras_datadesc_type_t ignored, gras_cbps_t vars,
333 int *i = (int *) data;
334 gras_cbps_i_push(vars, (int) *i);
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,
339 gras_cbps_t vars, void *data)
341 unsigned int *i = (unsigned int *) data;
342 gras_cbps_i_push(vars, (int) *i);
345 /** \brief Cb to push an long integer. Must be attached to the field you want to push
347 void gras_datadesc_cb_push_lint(gras_datadesc_type_t ignored,
348 gras_cbps_t vars, void *data)
350 long int *i = (long int *) data;
351 gras_cbps_i_push(vars, (int) *i);
354 /** \brief Cb to push an unsigned long integer. Must be attached to the field you want to push
356 void gras_datadesc_cb_push_ulint(gras_datadesc_type_t ignored,
357 gras_cbps_t vars, void *data)
359 unsigned long int *i = (unsigned long int *) data;
360 gras_cbps_i_push(vars, (int) *i);
363 /* ************************************ */
364 /* **** PUSHy multiplier callbacks **** */
365 /* ************************************ */
366 /** \brief Cb to push an integer as multiplier. Must be attached to the field you want to push */
367 void gras_datadesc_cb_push_int_mult(gras_datadesc_type_t ignored,
368 gras_cbps_t vars, void *data)
370 int old = *(int *) data;
371 int new = gras_cbps_i_pop(vars);
372 DEBUG2("push %d x %d as a size", old, new);
373 gras_cbps_i_push(vars, old * new);
376 /** \brief Cb to push an unsigned integer as multiplier. Must be attached to the field you want to push */
377 void gras_datadesc_cb_push_uint_mult(gras_datadesc_type_t ignored,
378 gras_cbps_t vars, void *data)
380 unsigned int old = *(unsigned int *) data;
381 unsigned int new = gras_cbps_i_pop(vars);
383 DEBUG2("push %d x %d as a size", old, new);
384 gras_cbps_i_push(vars, (int) (old * new));
387 /** \brief Cb to push an long integer as multiplier. Must be attached to the field you want to push
389 void gras_datadesc_cb_push_lint_mult(gras_datadesc_type_t ignored,
390 gras_cbps_t vars, void *data)
392 long int i = *(long int *) data;
393 i *= gras_cbps_i_pop(vars);
394 gras_cbps_i_push(vars, (int) i);
397 /** \brief Cb to push an unsigned long integer as multiplier. Must be attached to the field you want to push
399 void gras_datadesc_cb_push_ulint_mult(gras_datadesc_type_t ignored,
400 gras_cbps_t vars, void *data)
402 unsigned long int old = *(unsigned long int *) data;
403 unsigned long int new = gras_cbps_i_pop(vars);
405 DEBUG2("push %ld x %ld as a size", old, new);
406 gras_cbps_i_push(vars, (int) (old * new));