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,"callback persistant state");
17 gras_datadesc_type_t type;
19 } s_gras_cbps_elm_t, *gras_cbps_elm_t;
21 typedef struct s_gras_cbps {
22 xbt_dynar_t lints; /* simple stack of long integers (easy interface) */
24 xbt_dict_t space; /* varname x dynar of gras_cbps_elm_t */
25 xbt_dynar_t frames; /* of dynar of names defined within this frame
26 (and to pop when we leave it) */
30 static void free_string(void *d){
34 gras_cbps_t gras_cbps_new(void) {
37 res=xbt_new(s_gras_cbps_t,1);
39 res->lints = xbt_dynar_new(sizeof(int), NULL);
40 res->space = xbt_dict_new();
41 /* no leak, the content is freed manually on block_end */
42 res->frames = xbt_dynar_new(sizeof(xbt_dynar_t), NULL);
43 res->globals = xbt_dynar_new(sizeof(char*), NULL);
45 gras_cbps_block_begin(res);
50 void gras_cbps_free(gras_cbps_t *state) {
52 xbt_dynar_free( &( (*state)->lints ) );
54 gras_cbps_block_end(*state);
55 xbt_dict_free ( &( (*state)->space ) );
56 xbt_dynar_free( &( (*state)->frames ) );
57 xbt_dynar_free( &( (*state)->globals ) );
63 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,
83 gras_datadesc_type_t ddt) {
85 xbt_dynar_t varstack=NULL,frame;
87 char *varname = (char*)xbt_strdup(name);
90 DEBUG2("push(%s,%p)",name,(void*)data);
93 varstack = xbt_dict_get(ps->space, name);
95 if (e.category != mismatch_error)
98 DEBUG1("Create a new variable stack for '%s' into the space",name);
99 varstack = xbt_dynar_new(sizeof (gras_cbps_elm_t *), NULL);
100 xbt_dict_set(ps->space, varname, (void **)varstack, NULL);
102 /* leaking, you think? only if you do not close all the openned blocks ;)*/
105 var = xbt_new0(s_gras_cbps_elm_t,1);
109 xbt_dynar_push(varstack, &var);
111 xbt_dynar_pop(ps->frames, &frame);
112 DEBUG4("Push %s (%p @%p) into frame %p",varname,(void*)varname,(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,
125 gras_datadesc_type_t *ddt,
127 xbt_dynar_t varstack,frame;
128 gras_cbps_elm_t var = NULL;
132 DEBUG1("pop(%s)",name);
134 varstack = xbt_dict_get(ps->space, name);
136 if (e.category != mismatch_error)
140 THROW1(not_found_error,1,"Asked to pop the non-existant %s", name);
142 xbt_dynar_pop(varstack, &var);
144 if (!xbt_dynar_length(varstack)) {
145 DEBUG1("Last incarnation of %s poped. Kill it",name);
146 xbt_dict_remove(ps->space, name);
147 xbt_dynar_free(&varstack);
156 xbt_dynar_pop(ps->frames, &frame);
158 int l = xbt_dynar_length(frame);
163 _name = xbt_dynar_get_as(frame, l, char*);
164 if (!strcmp(name, _name)) {
165 xbt_dynar_remove_at(frame, l, &_name);
171 xbt_dynar_push(ps->frames, &frame);
176 /** \brief Change the value of an element in the PS.
178 * If it's not present in the current block, look in the upper ones.
179 * If it's not present in any of them, modify in the globals
180 * If not present there neither, the code may segfault (Oli?).
182 * Once a reference to an element of that name is found somewhere in the PS,
183 * its value is changed.
186 gras_cbps_v_set (gras_cbps_t ps,
189 gras_datadesc_type_t ddt) {
191 xbt_dynar_t dynar = NULL;
192 gras_cbps_elm_t elm = NULL;
194 DEBUG1("set(%s)",name);
195 dynar = xbt_dict_get_or_null(ps->space, name);
198 dynar = xbt_dynar_new(sizeof (gras_cbps_elm_t), NULL);
199 xbt_dict_set(ps->space, name, (void **)dynar, NULL);
201 elm = xbt_new0(s_gras_cbps_elm_t,1);
202 xbt_dynar_push(ps->globals, &name);
204 xbt_dynar_pop(dynar, &elm);
210 xbt_dynar_push(dynar, &elm);
214 /** \brief Get the value of an element in the PS without modifying it.
216 * (note that you get the content of the data struct and not a copy to it)
217 * If it's not present in the current block, look in the upper ones.
218 * If it's not present in any of them, look in the globals
219 * If not present there neither, the code may segfault (Oli?).
222 gras_cbps_v_get (gras_cbps_t ps,
224 /* OUT */gras_datadesc_type_t *ddt) {
226 xbt_dynar_t dynar = NULL;
227 gras_cbps_elm_t elm = NULL;
229 DEBUG1("get(%s)",name);
230 dynar = xbt_dict_get(ps->space, name);
231 xbt_dynar_pop(dynar, &elm);
232 xbt_dynar_push(dynar, &elm);
242 /** \brief Begins a new block.
244 * Blocks are usefull to remove a whole set of declarations you don't even know
246 * E.g., they constitute an elegent solution to recursive data structures.
248 * push/pop may be used in some cases for that, but if your recursive data
249 * struct contains other structs needing themselves callbacks, you have to
250 * use block_{begin,end} to do the trick.
254 gras_cbps_block_begin(gras_cbps_t ps) {
256 xbt_dynar_t dynar = NULL;
258 DEBUG0(">>> Block begin");
259 dynar = xbt_dynar_new(sizeof (char *), NULL);
260 xbt_dynar_push(ps->frames, &dynar);
263 /** \brief End the current block, and go back to the upper one. */
265 gras_cbps_block_end(gras_cbps_t ps) {
267 xbt_dynar_t frame = NULL;
271 xbt_assert0(xbt_dynar_length(ps->frames),
272 "More block_end than block_begin");
273 xbt_dynar_pop(ps->frames, &frame);
275 xbt_dynar_foreach(frame, cursor, name) {
277 xbt_dynar_t varstack = NULL;
278 gras_cbps_elm_t var = NULL;
280 DEBUG2("Get ride of %s (%p)",name,(void*)name);
281 varstack = xbt_dict_get(ps->space, name);
282 xbt_dynar_pop(varstack, &var);
284 if (!xbt_dynar_length(varstack)) {
285 xbt_dict_remove(ps->space, name);
286 xbt_dynar_free_container(&varstack); /*already empty, save a test ;) */
289 if (var->data) free(var->data);
293 xbt_dynar_free_container(&frame);/* we just emptied it */
294 DEBUG0("<<< Block end");
298 /** \brief Push a new integer value into the cbps. */
300 gras_cbps_i_push(gras_cbps_t ps,
302 DEBUG1("push %d as a size",val);
303 xbt_dynar_push_as(ps->lints,int,val);
305 /** \brief Pop the lastly pushed integer value from the cbps. */
307 gras_cbps_i_pop(gras_cbps_t ps) {
310 xbt_assert0(xbt_dynar_length(ps->lints) > 0,
311 "gras_cbps_i_pop: no value to pop");
312 ret = xbt_dynar_pop_as(ps->lints,int);
313 DEBUG1("pop %d as a size",ret);
317 /** \brief Generic cb returning the lastly pushed value
319 * Used by \ref gras_datadesc_ref_pop_arr
321 int gras_datadesc_cb_pop(gras_datadesc_type_t ignored, gras_cbps_t vars, void *data) {
322 int res = gras_cbps_i_pop(vars);
323 DEBUG1("Pop %d as a size",res);
327 /* ************************* */
328 /* **** PUSHy callbacks **** */
329 /* ************************* */
331 /** \brief Cb to push an integer. Must be attached to the field you want to push */
332 void gras_datadesc_cb_push_int(gras_datadesc_type_t ignored, gras_cbps_t vars, void *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, 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, gras_cbps_t vars, void *data) {
346 long int *i = (long int*)data;
347 gras_cbps_i_push(vars, (int) *i);
349 /** \brief Cb to push an unsigned long integer. Must be attached to the field you want to push
351 void gras_datadesc_cb_push_ulint(gras_datadesc_type_t ignored, gras_cbps_t vars, void *data) {
352 unsigned long int *i = (unsigned long int*)data;
353 gras_cbps_i_push(vars, (int) *i);
356 /* ************************************ */
357 /* **** PUSHy multiplier callbacks **** */
358 /* ************************************ */
359 /** \brief Cb to push an integer as multiplier. Must be attached to the field you want to push */
360 void gras_datadesc_cb_push_int_mult(gras_datadesc_type_t ignored, gras_cbps_t vars, void *data) {
361 int old = *(int*)data;
362 int new = gras_cbps_i_pop(vars);
363 DEBUG2("push %d x %d as a size",old,new);
364 gras_cbps_i_push(vars, old*new);
367 /** \brief Cb to push an unsigned integer as multiplier. Must be attached to the field you want to push */
368 void gras_datadesc_cb_push_uint_mult(gras_datadesc_type_t ignored, gras_cbps_t vars, void *data) {
369 unsigned int old = *(unsigned int*)data;
370 unsigned int new = gras_cbps_i_pop(vars);
372 DEBUG2("push %d x %d as a size",old,new);
373 gras_cbps_i_push(vars, (int) (old*new));
376 /** \brief Cb to push an long integer as multiplier. Must be attached to the field you want to push
378 void gras_datadesc_cb_push_lint_mult(gras_datadesc_type_t ignored, gras_cbps_t vars, void *data) {
379 long int i = *(long int*)data;
380 i *= gras_cbps_i_pop(vars);
381 gras_cbps_i_push(vars, (int) i);
383 /** \brief Cb to push an unsigned long integer as multiplier. Must be attached to the field you want to push
385 void gras_datadesc_cb_push_ulint_mult(gras_datadesc_type_t ignored, gras_cbps_t vars, void *data) {
386 unsigned long int old = *(unsigned long int*)data;
387 unsigned long int new = gras_cbps_i_pop(vars);
389 DEBUG2("push %ld x %ld as a size",old,new);
390 gras_cbps_i_push(vars, (int) (old * new) );