3 /* cbps - persistant states for callbacks */
5 /* Authors: Olivier Aumage, Martin Quinson */
6 /* Copyright (C) 2003, 2004 da GRAS posse. */
8 /* This program is free software; you can redistribute it and/or modify it
9 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(ddt_cbps,datadesc,"callback persistant state");
15 gras_datadesc_type_t type;
17 } s_gras_cbps_elm_t, *gras_cbps_elm_t;
19 typedef struct s_gras_cbps {
20 xbt_dynar_t lints; /* simple stack of long integers (easy interface) */
22 xbt_dict_t space; /* varname x dynar of gras_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 static void free_string(void *d);
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 ) );
66 * Declare a new element in the PS, and give it a value. If an element of that
67 * name already exists, it is masked by the one given here, and will be
68 * seeable again only after a pop to remove the value this push adds.
71 gras_cbps_v_push(gras_cbps_t ps,
74 gras_datadesc_type_t ddt) {
76 xbt_dynar_t varstack,frame;
79 char *varname = (char*)strdup(name);
81 DEBUG2("push(%s,%p)",name,(void*)data);
82 errcode = xbt_dict_get(ps->space, name, (void **)&varstack);
84 if (errcode == mismatch_error) {
85 DEBUG1("Create a new variable stack for '%s' into the space",name);
86 varstack = xbt_dynar_new(sizeof (gras_cbps_elm_t *), NULL);
87 xbt_dict_set(ps->space, varname, (void **)varstack, NULL);
88 /* leaking, you think? only if you do not close all the openned blocks ;)*/
89 } else if (errcode != no_error) {
93 var = xbt_new0(s_gras_cbps_elm_t,1);
97 xbt_dynar_push(varstack, &var);
99 xbt_dynar_pop(ps->frames, &frame);
100 DEBUG4("Push %s (%p @%p) into frame %p",varname,(void*)varname,(void*)&varname,(void*)frame);
101 xbt_dynar_push(frame, &varname);
102 xbt_dynar_push(ps->frames, &frame);
109 * Retrieve an element from the PS, and remove it from the PS. If it's not
110 * present in the current block, it will fail (with abort) and not search
111 * in upper blocks since this denotes a programmation error.
114 gras_cbps_v_pop (gras_cbps_t ps,
116 gras_datadesc_type_t *ddt,
118 xbt_dynar_t varstack,frame;
119 gras_cbps_elm_t var = NULL;
123 DEBUG1("pop(%s)",name);
124 /* FIXME: Error handling */
125 errcode = xbt_dict_get(ps->space, name, (void **)&varstack);
126 if (errcode == mismatch_error) {
127 RAISE1(mismatch_error,"Asked to pop the non-existant %s",
130 xbt_dynar_pop(varstack, &var);
132 if (!xbt_dynar_length(varstack)) {
133 DEBUG1("Last incarnation of %s poped. Kill it",name);
134 xbt_dict_remove(ps->space, name);
135 xbt_dynar_free(&varstack);
144 xbt_dynar_pop(ps->frames, &frame);
146 int l = xbt_dynar_length(frame);
151 _name = xbt_dynar_get_as(frame, l, char*);
152 if (!strcmp(name, _name)) {
153 xbt_dynar_remove_at(frame, l, &_name);
159 xbt_dynar_push(ps->frames, &frame);
168 * Change the value of an element in the PS.
169 * If it's not present in the current block, look in the upper ones.
170 * If it's not present in any of them, modify in the globals
171 * If not present there neither, the code may segfault (Oli?).
173 * Once a reference to an element of that name is found somewhere in the PS,
174 * its value is changed.
177 gras_cbps_v_set (gras_cbps_t ps,
180 gras_datadesc_type_t ddt) {
182 xbt_dynar_t dynar = NULL;
183 gras_cbps_elm_t elm = NULL;
186 DEBUG1("set(%s)",name);
187 errcode = xbt_dict_get(ps->space, name, (void **)&dynar);
189 if (errcode == mismatch_error) {
190 dynar = xbt_dynar_new(sizeof (gras_cbps_elm_t), NULL);
191 xbt_dict_set(ps->space, name, (void **)dynar, NULL);
193 elm = xbt_new0(s_gras_cbps_elm_t,1);
194 xbt_dynar_push(ps->globals, &name);
196 xbt_dynar_pop(dynar, &elm);
202 xbt_dynar_push(dynar, &elm);
209 * Get the value of an element in the PS without modifying it.
210 * (note that you get the content of the data struct and not a copy to it)
211 * If it's not present in the current block, look in the upper ones.
212 * If it's not present in any of them, look in the globals
213 * If not present there neither, the code may segfault (Oli?).
216 gras_cbps_v_get (gras_cbps_t ps,
218 /* OUT */gras_datadesc_type_t *ddt) {
220 xbt_dynar_t dynar = NULL;
221 gras_cbps_elm_t elm = NULL;
223 DEBUG1("get(%s)",name);
224 /* FIXME: Error handling */
225 xbt_dict_get(ps->space, name, (void **)&dynar);
226 xbt_dynar_pop(dynar, &elm);
227 xbt_dynar_push(dynar, &elm);
238 * gras_cbps_block_begin:
240 * Begins a new block.
242 * Blocks are usefull to remove a whole set of declarations you don't even know
244 * E.g., they constitute an elegent solution to recursive data structures.
246 * push/pop may be used in some cases for that, but if your recursive data
247 * struct contains other structs needing themselves callbacks, you have to
248 * use block_{begin,end} to do the trick.
252 gras_cbps_block_begin(gras_cbps_t ps) {
254 xbt_dynar_t dynar = NULL;
256 DEBUG0(">>> Block begin");
257 dynar = xbt_dynar_new(sizeof (char *), NULL);
258 xbt_dynar_push(ps->frames, &dynar);
262 * gras_cbps_block_end:
264 * End the current block, and go back to the upper one.
267 gras_cbps_block_end(gras_cbps_t ps) {
269 xbt_dynar_t frame = NULL;
273 xbt_assert0(xbt_dynar_length(ps->frames),
274 "More block_end than block_begin");
275 xbt_dynar_pop(ps->frames, &frame);
277 xbt_dynar_foreach(frame, cursor, name) {
279 xbt_dynar_t varstack = NULL;
280 gras_cbps_elm_t var = NULL;
282 DEBUG2("Get ride of %s (%p)",name,(void*)name);
283 xbt_dict_get(ps->space, name, (void **)&varstack);
284 xbt_dynar_pop(varstack, &var);
286 if (!xbt_dynar_length(varstack)) {
287 xbt_dict_remove(ps->space, name);
288 xbt_dynar_free_container(&varstack); /*already empty, save a test ;) */
291 if (var->data) xbt_free(var->data);
295 xbt_dynar_free_container(&frame);/* we just emptied it */
296 DEBUG0("<<< Block end");
303 * Push a new long integer value into the cbps.
306 gras_cbps_i_push(gras_cbps_t ps,
308 DEBUG1("push %d as a size",val);
309 xbt_dynar_push_as(ps->lints,int,val);
314 * Pop the lastly pushed long integer value from the cbps.
317 gras_cbps_i_pop(gras_cbps_t ps) {
320 xbt_assert0(xbt_dynar_length(ps->lints) > 0,
321 "gras_cbps_i_pop: no value to pop");
322 ret = xbt_dynar_pop_as(ps->lints,int);
323 DEBUG1("pop %d as a size",ret);
328 * gras_datadesc_cb_pop:
330 * Generic cb returning the lastly pushed value
332 int gras_datadesc_cb_pop(gras_cbps_t vars, void *data) {
333 return gras_cbps_i_pop(vars);
337 * gras_datadesc_cb_push_int:
339 * Cb to push an integer. Must be attached to the field you want to push
341 void gras_datadesc_cb_push_int(gras_cbps_t vars, void *data) {
343 gras_cbps_i_push(vars, (int) *i);
347 * gras_datadesc_cb_push_uint:
349 * Cb to push an unsigned integer. Must be attached to the field you want to push
351 void gras_datadesc_cb_push_uint(gras_cbps_t vars, void *data) {
352 unsigned int *i = (unsigned int*)data;
353 gras_cbps_i_push(vars, (int) *i);
357 * gras_datadesc_cb_push_lint:
359 * Cb to push an long integer. Must be attached to the field you want to push
361 void gras_datadesc_cb_push_lint(gras_cbps_t vars, void *data) {
362 long int *i = (long int*)data;
363 gras_cbps_i_push(vars, (int) *i);
366 * gras_datadesc_cb_push_ulint:
368 * Cb to push an long integer. Must be attached to the field you want to push
370 void gras_datadesc_cb_push_ulint(gras_cbps_t vars, void *data) {
371 unsigned long int *i = (unsigned long int*)data;
372 gras_cbps_i_push(vars, (int) *i);