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) {
38 res=xbt_new(s_gras_cbps_t,1);
40 res->lints = xbt_dynar_new(sizeof(int), NULL);
41 res->space = xbt_dict_new();
42 /* no leak, the content is freed manually on block_end */
43 res->frames = xbt_dynar_new(sizeof(xbt_dynar_t), NULL);
44 res->globals = xbt_dynar_new(sizeof(char*), NULL);
46 gras_cbps_block_begin(res);
51 void gras_cbps_free(gras_cbps_t *state) {
53 xbt_dynar_free( &( (*state)->lints ) );
55 gras_cbps_block_end(*state);
56 xbt_dict_free ( &( (*state)->space ) );
57 xbt_dynar_free( &( (*state)->frames ) );
58 xbt_dynar_free( &( (*state)->globals ) );
67 * Declare a new element in the PS, and give it a value. If an element of that
68 * name already exists, it is masked by the one given here, and will be
69 * seeable again only after a pop to remove the value this push adds.
72 gras_cbps_v_push(gras_cbps_t ps,
75 gras_datadesc_type_t ddt) {
77 xbt_dynar_t varstack,frame;
80 char *varname = (char*)strdup(name);
82 DEBUG2("push(%s,%p)",name,(void*)data);
83 errcode = xbt_dict_get(ps->space, name, (void **)&varstack);
85 if (errcode == mismatch_error) {
86 DEBUG1("Create a new variable stack for '%s' into the space",name);
87 varstack = xbt_dynar_new(sizeof (gras_cbps_elm_t *), NULL);
88 xbt_dict_set(ps->space, varname, (void **)varstack, NULL);
89 /* leaking, you think? only if you do not close all the openned blocks ;)*/
90 } else if (errcode != no_error) {
94 var = xbt_new0(s_gras_cbps_elm_t,1);
98 xbt_dynar_push(varstack, &var);
100 xbt_dynar_pop(ps->frames, &frame);
101 DEBUG4("Push %s (%p @%p) into frame %p",varname,(void*)varname,(void*)&varname,(void*)frame);
102 xbt_dynar_push(frame, &varname);
103 xbt_dynar_push(ps->frames, &frame);
110 * Retrieve an element from the PS, and remove it from the PS. If it's not
111 * present in the current block, it will fail (with abort) and not search
112 * in upper blocks since this denotes a programmation error.
115 gras_cbps_v_pop (gras_cbps_t ps,
117 gras_datadesc_type_t *ddt,
119 xbt_dynar_t varstack,frame;
120 gras_cbps_elm_t var = NULL;
124 DEBUG1("pop(%s)",name);
125 /* FIXME: Error handling */
126 errcode = xbt_dict_get(ps->space, name, (void **)&varstack);
127 if (errcode == mismatch_error) {
128 RAISE1(mismatch_error,"Asked to pop the non-existant %s",
131 xbt_dynar_pop(varstack, &var);
133 if (!xbt_dynar_length(varstack)) {
134 DEBUG1("Last incarnation of %s poped. Kill it",name);
135 xbt_dict_remove(ps->space, name);
136 xbt_dynar_free(&varstack);
145 xbt_dynar_pop(ps->frames, &frame);
147 int l = xbt_dynar_length(frame);
152 _name = xbt_dynar_get_as(frame, l, char*);
153 if (!strcmp(name, _name)) {
154 xbt_dynar_remove_at(frame, l, &_name);
160 xbt_dynar_push(ps->frames, &frame);
169 * Change the value of an element in the PS.
170 * If it's not present in the current block, look in the upper ones.
171 * If it's not present in any of them, modify in the globals
172 * If not present there neither, the code may segfault (Oli?).
174 * Once a reference to an element of that name is found somewhere in the PS,
175 * its value is changed.
178 gras_cbps_v_set (gras_cbps_t ps,
181 gras_datadesc_type_t ddt) {
183 xbt_dynar_t dynar = NULL;
184 gras_cbps_elm_t elm = NULL;
187 DEBUG1("set(%s)",name);
188 errcode = xbt_dict_get(ps->space, name, (void **)&dynar);
190 if (errcode == mismatch_error) {
191 dynar = xbt_dynar_new(sizeof (gras_cbps_elm_t), NULL);
192 xbt_dict_set(ps->space, name, (void **)dynar, NULL);
194 elm = xbt_new0(s_gras_cbps_elm_t,1);
195 xbt_dynar_push(ps->globals, &name);
197 xbt_dynar_pop(dynar, &elm);
203 xbt_dynar_push(dynar, &elm);
210 * Get the value of an element in the PS without modifying it.
211 * (note that you get the content of the data struct and not a copy to it)
212 * If it's not present in the current block, look in the upper ones.
213 * If it's not present in any of them, look in the globals
214 * If not present there neither, the code may segfault (Oli?).
217 gras_cbps_v_get (gras_cbps_t ps,
219 /* 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 /* FIXME: Error handling */
226 xbt_dict_get(ps->space, name, (void **)&dynar);
227 xbt_dynar_pop(dynar, &elm);
228 xbt_dynar_push(dynar, &elm);
239 * gras_cbps_block_begin:
241 * Begins a new block.
243 * Blocks are usefull to remove a whole set of declarations you don't even know
245 * E.g., they constitute an elegent solution to recursive data structures.
247 * push/pop may be used in some cases for that, but if your recursive data
248 * struct contains other structs needing themselves callbacks, you have to
249 * use block_{begin,end} to do the trick.
253 gras_cbps_block_begin(gras_cbps_t ps) {
255 xbt_dynar_t dynar = NULL;
257 DEBUG0(">>> Block begin");
258 dynar = xbt_dynar_new(sizeof (char *), NULL);
259 xbt_dynar_push(ps->frames, &dynar);
263 * gras_cbps_block_end:
265 * End the current block, and go back to the upper one.
268 gras_cbps_block_end(gras_cbps_t ps) {
270 xbt_dynar_t frame = NULL;
274 xbt_assert0(xbt_dynar_length(ps->frames),
275 "More block_end than block_begin");
276 xbt_dynar_pop(ps->frames, &frame);
278 xbt_dynar_foreach(frame, cursor, name) {
280 xbt_dynar_t varstack = NULL;
281 gras_cbps_elm_t var = NULL;
283 DEBUG2("Get ride of %s (%p)",name,(void*)name);
284 xbt_dict_get(ps->space, name, (void **)&varstack);
285 xbt_dynar_pop(varstack, &var);
287 if (!xbt_dynar_length(varstack)) {
288 xbt_dict_remove(ps->space, name);
289 xbt_dynar_free_container(&varstack); /*already empty, save a test ;) */
292 if (var->data) xbt_free(var->data);
296 xbt_dynar_free_container(&frame);/* we just emptied it */
297 DEBUG0("<<< Block end");
304 * Push a new long integer value into the cbps.
307 gras_cbps_i_push(gras_cbps_t ps,
310 DEBUG1("push %d as a size",val);
311 xbt_dynar_push_as(ps->lints,int,val);
316 * Pop the lastly pushed long integer value from the cbps.
319 gras_cbps_i_pop(gras_cbps_t ps) {
322 xbt_assert0(xbt_dynar_length(ps->lints) > 0,
323 "gras_cbps_i_pop: no value to pop");
324 ret = xbt_dynar_pop_as(ps->lints,int);
325 DEBUG1("pop %d as a size",ret);
330 * gras_datadesc_cb_pop:
332 * Generic cb returning the lastly pushed value
334 int gras_datadesc_cb_pop(gras_cbps_t vars, void *data) {
335 return gras_cbps_i_pop(vars);
339 * gras_datadesc_cb_push_int:
341 * Cb to push an integer. Must be attached to the field you want to push
343 void gras_datadesc_cb_push_int(gras_cbps_t vars, void *data) {
345 gras_cbps_i_push(vars, (int) *i);
349 * gras_datadesc_cb_push_uint:
351 * Cb to push an unsigned integer. Must be attached to the field you want to push
353 void gras_datadesc_cb_push_uint(gras_cbps_t vars, void *data) {
354 unsigned int *i = (unsigned int*)data;
355 gras_cbps_i_push(vars, (int) *i);
359 * gras_datadesc_cb_push_lint:
361 * Cb to push an long integer. Must be attached to the field you want to push
363 void gras_datadesc_cb_push_lint(gras_cbps_t vars, void *data) {
364 long int *i = (long int*)data;
365 gras_cbps_i_push(vars, (int) *i);
368 * gras_datadesc_cb_push_ulint:
370 * Cb to push an long integer. Must be attached to the field you want to push
372 void gras_datadesc_cb_push_ulint(gras_cbps_t vars, void *data) {
373 unsigned long int *i = (unsigned long int*)data;
374 gras_cbps_i_push(vars, (int) *i);