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(ddt_cbps,datadesc,"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 /** \brief Declare a new element in the PS, and give it a value.
65 * If an element of that
66 * name already exists, it is masked by the one given here, and will be
67 * seeable again only after a pop to remove the value this push adds.
70 gras_cbps_v_push(gras_cbps_t ps,
73 gras_datadesc_type_t ddt) {
75 xbt_dynar_t varstack,frame;
77 char *varname = (char*)xbt_strdup(name);
80 DEBUG2("push(%s,%p)",name,(void*)data);
83 varstack = xbt_dict_get(ps->space, name);
85 if (e.category != mismatch_error)
88 DEBUG1("Create a new variable stack for '%s' into the space",name);
89 varstack = xbt_dynar_new(sizeof (gras_cbps_elm_t *), NULL);
90 xbt_dict_set(ps->space, varname, (void **)varstack, NULL);
92 /* leaking, you think? only if you do not close all the openned blocks ;)*/
95 var = xbt_new0(s_gras_cbps_elm_t,1);
99 xbt_dynar_push(varstack, &var);
101 xbt_dynar_pop(ps->frames, &frame);
102 DEBUG4("Push %s (%p @%p) into frame %p",varname,(void*)varname,(void*)&varname,(void*)frame);
103 xbt_dynar_push(frame, &varname);
104 xbt_dynar_push(ps->frames, &frame);
107 /** \brief Retrieve an element from the PS, and remove it from the PS.
109 * If it's not present in the current block, it will fail (throwing not_found)
110 * and not search in upper blocks since this denotes a programmation error.
113 gras_cbps_v_pop (gras_cbps_t ps,
115 gras_datadesc_type_t *ddt,
117 xbt_dynar_t varstack,frame;
118 gras_cbps_elm_t var = NULL;
122 DEBUG1("pop(%s)",name);
124 varstack = xbt_dict_get(ps->space, name);
126 if (e.category != mismatch_error)
130 THROW1(not_found_error,1,"Asked to pop the non-existant %s", name);
132 xbt_dynar_pop(varstack, &var);
134 if (!xbt_dynar_length(varstack)) {
135 DEBUG1("Last incarnation of %s poped. Kill it",name);
136 xbt_dict_remove(ps->space, name);
137 xbt_dynar_free(&varstack);
146 xbt_dynar_pop(ps->frames, &frame);
148 int l = xbt_dynar_length(frame);
153 _name = xbt_dynar_get_as(frame, l, char*);
154 if (!strcmp(name, _name)) {
155 xbt_dynar_remove_at(frame, l, &_name);
161 xbt_dynar_push(ps->frames, &frame);
166 /** \brief Change the value of an element in the PS.
168 * If it's not present in the current block, look in the upper ones.
169 * If it's not present in any of them, modify in the globals
170 * If not present there neither, the code may segfault (Oli?).
172 * Once a reference to an element of that name is found somewhere in the PS,
173 * its value is changed.
176 gras_cbps_v_set (gras_cbps_t ps,
179 gras_datadesc_type_t ddt) {
181 xbt_dynar_t dynar = NULL;
182 gras_cbps_elm_t elm = NULL;
184 DEBUG1("set(%s)",name);
185 dynar = xbt_dict_get_or_null(ps->space, name);
188 dynar = xbt_dynar_new(sizeof (gras_cbps_elm_t), NULL);
189 xbt_dict_set(ps->space, name, (void **)dynar, NULL);
191 elm = xbt_new0(s_gras_cbps_elm_t,1);
192 xbt_dynar_push(ps->globals, &name);
194 xbt_dynar_pop(dynar, &elm);
200 xbt_dynar_push(dynar, &elm);
204 /** \brief Get the value of an element in the PS without modifying it.
206 * (note that you get the content of the data struct and not a copy to it)
207 * If it's not present in the current block, look in the upper ones.
208 * If it's not present in any of them, look in the globals
209 * If not present there neither, the code may segfault (Oli?).
212 gras_cbps_v_get (gras_cbps_t ps,
214 /* OUT */gras_datadesc_type_t *ddt) {
216 xbt_dynar_t dynar = NULL;
217 gras_cbps_elm_t elm = NULL;
219 DEBUG1("get(%s)",name);
220 dynar = xbt_dict_get(ps->space, name);
221 xbt_dynar_pop(dynar, &elm);
222 xbt_dynar_push(dynar, &elm);
232 /** \brief Begins a new block.
234 * Blocks are usefull to remove a whole set of declarations you don't even know
236 * E.g., they constitute an elegent solution to recursive data structures.
238 * push/pop may be used in some cases for that, but if your recursive data
239 * struct contains other structs needing themselves callbacks, you have to
240 * use block_{begin,end} to do the trick.
244 gras_cbps_block_begin(gras_cbps_t ps) {
246 xbt_dynar_t dynar = NULL;
248 DEBUG0(">>> Block begin");
249 dynar = xbt_dynar_new(sizeof (char *), NULL);
250 xbt_dynar_push(ps->frames, &dynar);
253 /** \brief End the current block, and go back to the upper one. */
255 gras_cbps_block_end(gras_cbps_t ps) {
257 xbt_dynar_t frame = NULL;
261 xbt_assert0(xbt_dynar_length(ps->frames),
262 "More block_end than block_begin");
263 xbt_dynar_pop(ps->frames, &frame);
265 xbt_dynar_foreach(frame, cursor, name) {
267 xbt_dynar_t varstack = NULL;
268 gras_cbps_elm_t var = NULL;
270 DEBUG2("Get ride of %s (%p)",name,(void*)name);
271 varstack = xbt_dict_get(ps->space, name);
272 xbt_dynar_pop(varstack, &var);
274 if (!xbt_dynar_length(varstack)) {
275 xbt_dict_remove(ps->space, name);
276 xbt_dynar_free_container(&varstack); /*already empty, save a test ;) */
279 if (var->data) free(var->data);
283 xbt_dynar_free_container(&frame);/* we just emptied it */
284 DEBUG0("<<< Block end");
288 /** \brief Push a new integer value into the cbps. */
290 gras_cbps_i_push(gras_cbps_t ps,
292 DEBUG1("push %d as a size",val);
293 xbt_dynar_push_as(ps->lints,int,val);
295 /** \brief Pop the lastly pushed integer value from the cbps. */
297 gras_cbps_i_pop(gras_cbps_t ps) {
300 xbt_assert0(xbt_dynar_length(ps->lints) > 0,
301 "gras_cbps_i_pop: no value to pop");
302 ret = xbt_dynar_pop_as(ps->lints,int);
303 DEBUG1("pop %d as a size",ret);
307 /** \brief Generic cb returning the lastly pushed value
309 * Used by \ref gras_datadesc_ref_pop_arr
311 int gras_datadesc_cb_pop(gras_datadesc_type_t ignored, gras_cbps_t vars, void *data) {
312 return gras_cbps_i_pop(vars);
315 /** \brief Cb to push an integer. Must be attached to the field you want to push */
316 void gras_datadesc_cb_push_int(gras_datadesc_type_t ignored, gras_cbps_t vars, void *data) {
318 gras_cbps_i_push(vars, (int) *i);
321 /** \brief Cb to push an unsigned integer. Must be attached to the field you want to push */
322 void gras_datadesc_cb_push_uint(gras_datadesc_type_t ignored, gras_cbps_t vars, void *data) {
323 unsigned int *i = (unsigned int*)data;
324 gras_cbps_i_push(vars, (int) *i);
327 /** \brief Cb to push an long integer. Must be attached to the field you want to push
329 void gras_datadesc_cb_push_lint(gras_datadesc_type_t ignored, gras_cbps_t vars, void *data) {
330 long int *i = (long int*)data;
331 gras_cbps_i_push(vars, (int) *i);
333 /** \brief Cb to push an long integer. Must be attached to the field you want to push
335 void gras_datadesc_cb_push_ulint(gras_datadesc_type_t ignored, gras_cbps_t vars, void *data) {
336 unsigned long int *i = (unsigned long int*)data;
337 gras_cbps_i_push(vars, (int) *i);