Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
More verbose debug
[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 "DataDesc/datadesc_private.h"
12 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(cbps,datadesc);
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   free(*(void**)d);
32 }
33
34 gras_error_t
35 gras_cbps_new(gras_cbps_t **dst) {
36   gras_error_t errcode;
37   gras_cbps_t *res;
38
39   if (!(res=malloc(sizeof(gras_cbps_t))))
40     RAISE_MALLOC;
41
42   TRY(gras_dynar_new(&(res->lints), sizeof(int), NULL));
43
44   TRY(gras_dict_new(&(res->space)));
45   /* no leak, the content is freed manually on block_end */
46   TRY(gras_dynar_new(&(res->frames), sizeof(gras_dynar_t*), NULL));
47   TRY(gras_dynar_new(&(res->globals), sizeof(char*), NULL));
48
49   gras_cbps_block_begin(res);
50   *dst = res;
51   return no_error;
52 }
53
54 void
55 gras_cbps_free(gras_cbps_t **state) {
56
57   gras_dynar_free(    (*state)->lints   );
58
59   gras_cbps_block_end(*state);
60   gras_dict_free ( &( (*state)->space   ) );
61   gras_dynar_free(    (*state)->frames    );
62   gras_dynar_free(    (*state)->globals   );
63
64   free(*state);
65   *state = NULL;
66 }
67
68 /**
69  * gras_cbps_v_push:
70  *
71  * Declare a new element in the PS, and give it a value. If an element of that
72  * name already exists, it is masked by the one given here, and will be 
73  * seeable again only after a pop to remove the value this push adds.
74  */
75 gras_error_t
76 gras_cbps_v_push(gras_cbps_t        *ps,
77                     const char            *name,
78                     void                  *data,
79                     gras_datadesc_type_t  *ddt) {
80
81   gras_dynar_t            *varstack,*frame;
82   gras_cbps_elm_t      *p_var;
83   gras_error_t errcode;
84   char *varname = strdup(name);
85
86   DEBUG2("push(%s,%p)",name,data);
87   errcode = gras_dict_get(ps->space, name, (void **)&varstack);
88  
89   if (errcode == mismatch_error) {
90     DEBUG1("Create a new variable stack for '%s' into the space",name);
91     gras_dynar_new(&varstack, sizeof (gras_cbps_elm_t *), NULL);
92     gras_dict_set(ps->space, varname, (void **)varstack, NULL);
93     /* leaking, you think? only if you do not close all the openned blocks ;)*/
94   } else if (errcode != no_error) {
95     return errcode;
96   }
97  
98   p_var       = calloc(1, sizeof(gras_cbps_elm_t));
99   p_var->type = ddt;
100   p_var->data = data;
101   
102   gras_dynar_push(varstack, &p_var);
103   
104   gras_dynar_pop(ps->frames, &frame);
105   DEBUG4("Push %s (%p @%p) into frame %p",varname,varname,&varname,frame);
106   gras_dynar_push(frame, &varname);
107   gras_dynar_push(ps->frames, &frame); 
108   return no_error;
109 }
110
111 /**
112  * gras_cbps_v_pop:
113  *
114  * Retrieve an element from the PS, and remove it from the PS. If it's not
115  * present in the current block, it will fail (with abort) and not search
116  * in upper blocks since this denotes a programmation error.
117  */
118 gras_error_t
119 gras_cbps_v_pop (gras_cbps_t        *ps, 
120                     const char            *name,
121                     gras_datadesc_type_t **ddt,
122                     void                 **res) {
123   gras_dynar_t            *varstack,*frame;
124   gras_cbps_elm_t      *var          = NULL;
125   void                    *data           = NULL;
126   gras_error_t errcode;
127
128   DEBUG1("pop(%s)",name);
129   /* FIXME: Error handling */
130   errcode = gras_dict_get(ps->space, name, (void **)&varstack);
131   if (errcode == mismatch_error) {
132     RAISE1(mismatch_error,"Asked to pop the non-existant %s",
133            name);
134   }
135   gras_dynar_pop(varstack, &var);
136   
137   if (!gras_dynar_length(varstack)) {
138     DEBUG1("Last incarnation of %s poped. Kill it",name);
139     gras_dict_remove(ps->space, name);
140     gras_dynar_free(varstack);
141   }
142   
143   if (ddt)
144     *ddt = var->type;  
145   data    = var->data;
146   
147   free(var);
148   
149   gras_dynar_pop(ps->frames, &frame);
150   {
151     int l = gras_dynar_length(frame);
152     
153     while (l--) {
154       char *_name = NULL;
155                                                                                 
156       gras_dynar_get(frame, l, &_name);
157       if (!strcmp(name, _name)) {
158         gras_dynar_remove_at(frame, l, &_name);
159         free(_name);
160         break;
161       }
162     }
163   }
164   gras_dynar_push(ps->frames, &frame);
165   
166   *res = data;
167   return no_error;
168 }
169
170 /**
171  * gras_cbps_v_set:
172  *
173  * Change the value of an element in the PS.  
174  * If it's not present in the current block, look in the upper ones.
175  * If it's not present in any of them, modify in the globals
176  * If not present there neither, the code may segfault (Oli?).
177  *
178  * Once a reference to an element of that name is found somewhere in the PS,
179  *   its value is changed.
180  */
181 void
182 gras_cbps_v_set (gras_cbps_t        *ps,
183                     const char            *name,
184                     void                  *data,
185                     gras_datadesc_type_t  *ddt) {
186
187   gras_dynar_t            *p_dynar        = NULL;
188   gras_cbps_elm_t      *p_elm          = NULL;
189   gras_error_t errcode;
190   
191   DEBUG1("set(%s)",name);
192   errcode = gras_dict_get(ps->space, name, (void **)&p_dynar);
193   
194   if (errcode == mismatch_error) {
195     gras_dynar_new(&p_dynar, sizeof (gras_cbps_elm_t *), NULL);
196     gras_dict_set(ps->space, name, (void **)p_dynar, NULL);
197     
198     p_elm   = calloc(1, sizeof(gras_cbps_elm_t));
199     gras_dynar_push(ps->globals, &name);
200   } else {
201     gras_dynar_pop(p_dynar, &p_elm);
202   }
203   
204   p_elm->type   = ddt;
205   p_elm->data   = data;
206  
207   gras_dynar_push(p_dynar, &p_elm);
208
209 }
210
211 /**
212  * gras_cbps_v_get:
213  *
214  * Get the value of an element in the PS without modifying it. 
215  * (note that you get the content of the data struct and not a copy to it)
216  * If it's not present in the current block, look in the upper ones.
217  * If it's not present in any of them, look in the globals
218  * If not present there neither, the code may segfault (Oli?).
219  */
220 void *
221 gras_cbps_v_get (gras_cbps_t        *ps, 
222                     const char            *name,
223                     gras_datadesc_type_t **ddt) {
224   
225   gras_dynar_t            *p_dynar        = NULL;
226   gras_cbps_elm_t      *p_elm          = NULL;
227   
228   DEBUG1("get(%s)",name);
229   /* FIXME: Error handling */
230   gras_dict_get(ps->space, name, (void **)&p_dynar);
231   gras_dynar_pop(p_dynar, &p_elm);
232   gras_dynar_push(p_dynar, &p_elm);
233   
234   if (ddt) {
235     *ddt = p_elm->type;
236   }
237   
238   return p_elm->data;
239
240 }
241
242 /**
243  * gras_cbps_block_begin:
244  *
245  * Begins a new block. 
246  *
247  * Blocks are usefull to remove a whole set of declarations you don't even know
248  *
249  * E.g., they constitute an elegent solution to recursive data structures. 
250  *
251  * push/pop may be used in some cases for that, but if your recursive data 
252  * struct contains other structs needing themselves callbacks, you have to
253  * use block_{begin,end} to do the trick.
254  */
255
256 void
257 gras_cbps_block_begin(gras_cbps_t *ps) {
258
259   gras_dynar_t            *p_dynar        = NULL;
260
261   DEBUG0(">>> Block begin");
262   gras_dynar_new(&p_dynar, sizeof (char *), NULL);
263   gras_dynar_push(ps->frames, &p_dynar);
264 }
265
266 /**
267  * gras_cbps_block_begin:
268  *
269  * End the current block, and go back to the upper one.
270  */
271 void
272 gras_cbps_block_end(gras_cbps_t *ps) {
273
274   gras_dynar_t            *frame        = NULL;
275   int                      cursor         =    0;
276   char                    *name           = NULL;
277
278   gras_assert0(gras_dynar_length(ps->frames),
279                "More block_end than block_begin");
280   gras_dynar_pop(ps->frames, &frame);
281   
282   gras_dynar_foreach(frame, cursor, name) {
283
284     gras_dynar_t            *varstack    = NULL;
285     gras_cbps_elm_t      *var         = NULL;
286  
287     DEBUG2("Get ride of %s (%p)",name,name);
288     gras_dict_get(ps->space, name, (void **)&varstack);
289     gras_dynar_pop(varstack, &var);
290  
291     if (!gras_dynar_length(varstack)) {
292       gras_dict_remove(ps->space, name);
293       gras_dynar_free_container(varstack); /*already empty, save a test ;) */
294     }
295     
296     if (var->data) free(var->data);
297     free(var);
298     free(name);
299   }
300   gras_dynar_free_container(frame);/* we just emptied it */
301   DEBUG0("<<< Block end");
302 }
303
304
305 /**
306  * gras_cbps_i_push:
307  *
308  * Push a new long integer value into the cbps.
309  */
310 void
311 gras_cbps_i_push(gras_cbps_t        *ps, 
312                  int val) {
313   gras_error_t errcode;
314   DEBUG1("push %d as a size",val);
315   TRYFAIL(gras_dynar_push(ps->lints,&val));
316 }
317 /**
318  * gras_cbps_i_pop:
319  *
320  * Pop the lastly pushed long integer value from the cbps.
321  */
322 int
323 gras_cbps_i_pop(gras_cbps_t        *ps) {
324   int ret;
325
326   gras_assert0(gras_dynar_length(ps->lints) > 0,
327                "gras_cbps_i_pop: no value to pop");
328   gras_dynar_pop(ps->lints, &ret);
329   DEBUG1("pop %d as a size",ret);
330   return ret;
331 }
332
333 /**
334  * gras_dd_cb_pop:
335  *
336  * Generic cb returning the lastly pushed value
337  */
338 int gras_datadesc_cb_pop(gras_cbps_t *vars, void *data) {
339   return gras_cbps_i_pop(vars);
340 }