Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cfbb4af274faacc27fd9936df5bbb31d1d90a3e8
[simgrid.git] / src / gras / DataDesc / cbps.c
1 /* $Id$ */
2
3 /* cbps - persistant states for callbacks                                   */
4
5 /* Authors: Olivier Aumage, Martin Quinson                                  */
6 /* Copyright (C) 2003, 2004 da GRAS posse.                                  */
7
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. */
10
11 #include "gras/DataDesc/datadesc_private.h"
12 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(ddt_cbps,datadesc,"callback persistant state");
13
14 typedef struct {
15   gras_datadesc_type_t *type;
16   void                 *data;
17 } gras_cbps_elm_t;
18
19 struct s_gras_cbps {
20   gras_dynar_t *lints; /* simple stack of long integers (easy interface) */
21
22   gras_dict_t  *space; /* varname x dynar of gras_cbps_elm_t */
23   gras_dynar_t *frames; /* of dynar of names defined within this frame
24                            (and to pop when we leave it) */
25   gras_dynar_t *globals;
26 };
27
28 static void free_string(void *d);
29
30 static void free_string(void *d){
31   gras_free(*(void**)d);
32 }
33
34 gras_cbps_t * gras_cbps_new(void) {
35   gras_error_t errcode;
36   gras_cbps_t *res;
37
38   res=gras_new(gras_cbps_t,1);
39
40   gras_dynar_new(&(res->lints), sizeof(int), NULL);
41   gras_dict_new(&(res->space));
42   /* no leak, the content is freed manually on block_end */
43   gras_dynar_new(&(res->frames), sizeof(gras_dynar_t*), NULL);
44   gras_dynar_new(&(res->globals), sizeof(char*), NULL);
45
46   gras_cbps_block_begin(res);
47
48   return res;
49 }
50
51 void gras_cbps_free(gras_cbps_t **state) {
52
53   gras_dynar_free(    (*state)->lints   );
54
55   gras_cbps_block_end(*state);
56   gras_dict_free ( &( (*state)->space   ) );
57   gras_dynar_free(    (*state)->frames    );
58   gras_dynar_free(    (*state)->globals   );
59
60   gras_free(*state);
61   *state = NULL;
62 }
63
64 /**
65  * gras_cbps_v_push:
66  *
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.
70  */
71 gras_error_t
72 gras_cbps_v_push(gras_cbps_t        *ps,
73                  const char            *name,
74                  void                  *data,
75                  gras_datadesc_type_t  *ddt) {
76
77   gras_dynar_t            *varstack,*frame;
78   gras_cbps_elm_t      *p_var;
79   gras_error_t errcode;
80   char *varname = (char*)strdup(name);
81
82   DEBUG2("push(%s,%p)",name,(void*)data);
83   errcode = gras_dict_get(ps->space, name, (void **)&varstack);
84  
85   if (errcode == mismatch_error) {
86     DEBUG1("Create a new variable stack for '%s' into the space",name);
87     gras_dynar_new(&varstack, sizeof (gras_cbps_elm_t *), NULL);
88     gras_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) {
91     return errcode;
92   }
93  
94   p_var       = gras_new0(gras_cbps_elm_t,1);
95   p_var->type = ddt;
96   p_var->data = data;
97   
98   gras_dynar_push(varstack, &p_var);
99   
100   gras_dynar_pop(ps->frames, &frame);
101   DEBUG4("Push %s (%p @%p) into frame %p",varname,(void*)varname,(void*)&varname,(void*)frame);
102   gras_dynar_push(frame, &varname);
103   gras_dynar_push(ps->frames, &frame); 
104   return no_error;
105 }
106
107 /**
108  * gras_cbps_v_pop:
109  *
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.
113  */
114 gras_error_t
115 gras_cbps_v_pop (gras_cbps_t        *ps, 
116                     const char            *name,
117                     gras_datadesc_type_t **ddt,
118                     void                 **res) {
119   gras_dynar_t            *varstack,*frame;
120   gras_cbps_elm_t      *var          = NULL;
121   void                    *data           = NULL;
122   gras_error_t errcode;
123
124   DEBUG1("pop(%s)",name);
125   /* FIXME: Error handling */
126   errcode = gras_dict_get(ps->space, name, (void **)&varstack);
127   if (errcode == mismatch_error) {
128     RAISE1(mismatch_error,"Asked to pop the non-existant %s",
129            name);
130   }
131   gras_dynar_pop(varstack, &var);
132   
133   if (!gras_dynar_length(varstack)) {
134     DEBUG1("Last incarnation of %s poped. Kill it",name);
135     gras_dict_remove(ps->space, name);
136     gras_dynar_free(varstack);
137   }
138   
139   if (ddt)
140     *ddt = var->type;  
141   data    = var->data;
142   
143   gras_free(var);
144   
145   gras_dynar_pop(ps->frames, &frame);
146   {
147     int l = gras_dynar_length(frame);
148     
149     while (l--) {
150       char *_name = NULL;
151                                                                                 
152       gras_dynar_get(frame, l, &_name);
153       if (!strcmp(name, _name)) {
154         gras_dynar_remove_at(frame, l, &_name);
155         gras_free(_name);
156         break;
157       }
158     }
159   }
160   gras_dynar_push(ps->frames, &frame);
161   
162   *res = data;
163   return no_error;
164 }
165
166 /**
167  * gras_cbps_v_set:
168  *
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?).
173  *
174  * Once a reference to an element of that name is found somewhere in the PS,
175  *   its value is changed.
176  */
177 void
178 gras_cbps_v_set (gras_cbps_t        *ps,
179                     const char            *name,
180                     void                  *data,
181                     gras_datadesc_type_t  *ddt) {
182
183   gras_dynar_t            *p_dynar        = NULL;
184   gras_cbps_elm_t      *p_elm          = NULL;
185   gras_error_t errcode;
186   
187   DEBUG1("set(%s)",name);
188   errcode = gras_dict_get(ps->space, name, (void **)&p_dynar);
189   
190   if (errcode == mismatch_error) {
191     gras_dynar_new(&p_dynar, sizeof (gras_cbps_elm_t *), NULL);
192     gras_dict_set(ps->space, name, (void **)p_dynar, NULL);
193     
194     p_elm   = gras_new0(gras_cbps_elm_t,1);
195     gras_dynar_push(ps->globals, &name);
196   } else {
197     gras_dynar_pop(p_dynar, &p_elm);
198   }
199   
200   p_elm->type   = ddt;
201   p_elm->data   = data;
202  
203   gras_dynar_push(p_dynar, &p_elm);
204
205 }
206
207 /**
208  * gras_cbps_v_get:
209  *
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?).
215  */
216 void *
217 gras_cbps_v_get (gras_cbps_t        *ps, 
218                     const char            *name,
219                     gras_datadesc_type_t **ddt) {
220   
221   gras_dynar_t            *p_dynar        = NULL;
222   gras_cbps_elm_t      *p_elm          = NULL;
223   
224   DEBUG1("get(%s)",name);
225   /* FIXME: Error handling */
226   gras_dict_get(ps->space, name, (void **)&p_dynar);
227   gras_dynar_pop(p_dynar, &p_elm);
228   gras_dynar_push(p_dynar, &p_elm);
229   
230   if (ddt) {
231     *ddt = p_elm->type;
232   }
233   
234   return p_elm->data;
235
236 }
237
238 /**
239  * gras_cbps_block_begin:
240  *
241  * Begins a new block. 
242  *
243  * Blocks are usefull to remove a whole set of declarations you don't even know
244  *
245  * E.g., they constitute an elegent solution to recursive data structures. 
246  *
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.
250  */
251
252 void
253 gras_cbps_block_begin(gras_cbps_t *ps) {
254
255   gras_dynar_t            *p_dynar        = NULL;
256
257   DEBUG0(">>> Block begin");
258   gras_dynar_new(&p_dynar, sizeof (char *), NULL);
259   gras_dynar_push(ps->frames, &p_dynar);
260 }
261
262 /**
263  * gras_cbps_block_begin:
264  *
265  * End the current block, and go back to the upper one.
266  */
267 void
268 gras_cbps_block_end(gras_cbps_t *ps) {
269
270   gras_dynar_t            *frame        = NULL;
271   int                      cursor         =    0;
272   char                    *name           = NULL;
273
274   gras_assert0(gras_dynar_length(ps->frames),
275                "More block_end than block_begin");
276   gras_dynar_pop(ps->frames, &frame);
277   
278   gras_dynar_foreach(frame, cursor, name) {
279
280     gras_dynar_t            *varstack    = NULL;
281     gras_cbps_elm_t      *var         = NULL;
282  
283     DEBUG2("Get ride of %s (%p)",name,(void*)name);
284     gras_dict_get(ps->space, name, (void **)&varstack);
285     gras_dynar_pop(varstack, &var);
286  
287     if (!gras_dynar_length(varstack)) {
288       gras_dict_remove(ps->space, name);
289       gras_dynar_free_container(varstack); /*already empty, save a test ;) */
290     }
291     
292     if (var->data) gras_free(var->data);
293     gras_free(var);
294     gras_free(name);
295   }
296   gras_dynar_free_container(frame);/* we just emptied it */
297   DEBUG0("<<< Block end");
298 }
299
300
301 /**
302  * gras_cbps_i_push:
303  *
304  * Push a new long integer value into the cbps.
305  */
306 void
307 gras_cbps_i_push(gras_cbps_t        *ps, 
308                  int val) {
309   gras_error_t errcode;
310   DEBUG1("push %d as a size",val);
311   gras_dynar_push(ps->lints,&val);
312 }
313 /**
314  * gras_cbps_i_pop:
315  *
316  * Pop the lastly pushed long integer value from the cbps.
317  */
318 int
319 gras_cbps_i_pop(gras_cbps_t        *ps) {
320   int ret;
321
322   gras_assert0(gras_dynar_length(ps->lints) > 0,
323                "gras_cbps_i_pop: no value to pop");
324   gras_dynar_pop(ps->lints, &ret);
325   DEBUG1("pop %d as a size",ret);
326   return ret;
327 }
328
329 /**
330  * gras_datadesc_cb_pop:
331  *
332  * Generic cb returning the lastly pushed value
333  */
334 int gras_datadesc_cb_pop(gras_cbps_t *vars, void *data) {
335   return gras_cbps_i_pop(vars);
336 }
337
338 /**
339  * gras_datadesc_cb_push_int:
340  * 
341  * Cb to push an integer. Must be attached to the field you want to push
342  */
343 void gras_datadesc_cb_push_int(gras_cbps_t *vars, void *data) {
344    int *i = (int*)data;
345    gras_cbps_i_push(vars, (int) *i);
346 }
347
348 /**
349  * gras_datadesc_cb_push_uint:
350  * 
351  * Cb to push an unsigned integer. Must be attached to the field you want to push
352  */
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);
356 }
357
358 /**
359  * gras_datadesc_cb_push_lint:
360  * 
361  * Cb to push an long integer. Must be attached to the field you want to push
362  */
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);
366 }
367 /**
368  * gras_datadesc_cb_push_ulint:
369  * 
370  * Cb to push an long integer. Must be attached to the field you want to push
371  */
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);
375 }