Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Removed a bunch of unused variables. Mostly some xbt_error_t errcode that
[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 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ddt_cbps,datadesc,"callback persistant state");
13
14 typedef struct {
15   gras_datadesc_type_t  type;
16   void                 *data;
17 } s_gras_cbps_elm_t, *gras_cbps_elm_t;
18
19 typedef struct s_gras_cbps {
20   xbt_dynar_t lints; /* simple stack of long integers (easy interface) */
21
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) */
25   xbt_dynar_t globals;
26 } s_gras_cbps_t;
27
28 static void free_string(void *d);
29
30 static void free_string(void *d){
31   xbt_free(*(void**)d);
32 }
33
34 gras_cbps_t gras_cbps_new(void) {
35   gras_cbps_t  res;
36
37   res=xbt_new(s_gras_cbps_t,1);
38
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);
44
45   gras_cbps_block_begin(res);
46
47   return res;
48 }
49
50 void gras_cbps_free(gras_cbps_t *state) {
51
52   xbt_dynar_free( &( (*state)->lints   ) );
53
54   gras_cbps_block_end(*state);
55   xbt_dict_free ( &( (*state)->space   ) );
56   xbt_dynar_free( &( (*state)->frames  ) );
57   xbt_dynar_free( &( (*state)->globals ) );
58
59   xbt_free(*state);
60   *state = NULL;
61 }
62
63 /**
64  * gras_cbps_v_push:
65  *
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.
69  */
70 xbt_error_t
71 gras_cbps_v_push(gras_cbps_t          ps,
72                  const char          *name,
73                  void                *data,
74                  gras_datadesc_type_t ddt) {
75
76   xbt_dynar_t          varstack,frame;
77   gras_cbps_elm_t       var;
78   xbt_error_t errcode;
79   char *varname = (char*)strdup(name);
80
81   DEBUG2("push(%s,%p)",name,(void*)data);
82   errcode = xbt_dict_get(ps->space, name, (void **)&varstack);
83  
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) {
90     return errcode;
91   }
92  
93   var       = xbt_new0(s_gras_cbps_elm_t,1);
94   var->type = ddt;
95   var->data = data;
96   
97   xbt_dynar_push(varstack, &var);
98   
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); 
103   return no_error;
104 }
105
106 /**
107  * gras_cbps_v_pop:
108  *
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.
112  */
113 xbt_error_t
114 gras_cbps_v_pop (gras_cbps_t            ps, 
115                  const char            *name,
116                  gras_datadesc_type_t  *ddt,
117                  void                 **res) {
118   xbt_dynar_t          varstack,frame;
119   gras_cbps_elm_t       var            = NULL;
120   void                 *data           = NULL;
121   xbt_error_t errcode;
122
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",
128            name);
129   }
130   xbt_dynar_pop(varstack, &var);
131   
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);
136   }
137   
138   if (ddt)
139     *ddt = var->type;  
140   data    = var->data;
141   
142   xbt_free(var);
143   
144   xbt_dynar_pop(ps->frames, &frame);
145   {
146     int l = xbt_dynar_length(frame);
147     
148     while (l--) {
149       char *_name = NULL;
150                                                                                 
151       _name = xbt_dynar_get_as(frame, l, char*);
152       if (!strcmp(name, _name)) {
153         xbt_dynar_remove_at(frame, l, &_name);
154         xbt_free(_name);
155         break;
156       }
157     }
158   }
159   xbt_dynar_push(ps->frames, &frame);
160   
161   *res = data;
162   return no_error;
163 }
164
165 /**
166  * gras_cbps_v_set:
167  *
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?).
172  *
173  * Once a reference to an element of that name is found somewhere in the PS,
174  *   its value is changed.
175  */
176 void
177 gras_cbps_v_set (gras_cbps_t          ps,
178                  const char          *name,
179                  void                *data,
180                  gras_datadesc_type_t ddt) {
181
182   xbt_dynar_t    dynar        = NULL;
183   gras_cbps_elm_t elm          = NULL;
184   xbt_error_t    errcode;
185   
186   DEBUG1("set(%s)",name);
187   errcode = xbt_dict_get(ps->space, name, (void **)&dynar);
188   
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);
192     
193     elm   = xbt_new0(s_gras_cbps_elm_t,1);
194     xbt_dynar_push(ps->globals, &name);
195   } else {
196     xbt_dynar_pop(dynar, &elm);
197   }
198   
199   elm->type   = ddt;
200   elm->data   = data;
201  
202   xbt_dynar_push(dynar, &elm);
203
204 }
205
206 /**
207  * gras_cbps_v_get:
208  *
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?).
214  */
215 void *
216 gras_cbps_v_get (gras_cbps_t           ps, 
217                  const char           *name,
218                  /* OUT */gras_datadesc_type_t *ddt) {
219   
220   xbt_dynar_t    dynar = NULL;
221   gras_cbps_elm_t elm   = NULL;
222   
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);
228   
229   if (ddt) {
230     *ddt = elm->type;
231   }
232   
233   return elm->data;
234
235 }
236
237 /**
238  * gras_cbps_block_begin:
239  *
240  * Begins a new block. 
241  *
242  * Blocks are usefull to remove a whole set of declarations you don't even know
243  *
244  * E.g., they constitute an elegent solution to recursive data structures. 
245  *
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.
249  */
250
251 void
252 gras_cbps_block_begin(gras_cbps_t ps) {
253
254   xbt_dynar_t dynar = NULL;
255
256   DEBUG0(">>> Block begin");
257   dynar = xbt_dynar_new(sizeof (char *), NULL);
258   xbt_dynar_push(ps->frames, &dynar);
259 }
260
261 /**
262  * gras_cbps_block_end:
263  *
264  * End the current block, and go back to the upper one.
265  */
266 void
267 gras_cbps_block_end(gras_cbps_t ps) {
268
269   xbt_dynar_t  frame        = NULL;
270   int           cursor       =    0;
271   char         *name         = NULL;
272
273   xbt_assert0(xbt_dynar_length(ps->frames),
274                "More block_end than block_begin");
275   xbt_dynar_pop(ps->frames, &frame);
276   
277   xbt_dynar_foreach(frame, cursor, name) {
278
279     xbt_dynar_t    varstack    = NULL;
280     gras_cbps_elm_t var         = NULL;
281  
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);
285  
286     if (!xbt_dynar_length(varstack)) {
287       xbt_dict_remove(ps->space, name);
288       xbt_dynar_free_container(&varstack); /*already empty, save a test ;) */
289     }
290     
291     if (var->data) xbt_free(var->data);
292     xbt_free(var);
293     xbt_free(name);
294   }
295   xbt_dynar_free_container(&frame);/* we just emptied it */
296   DEBUG0("<<< Block end");
297 }
298
299
300 /**
301  * gras_cbps_i_push:
302  *
303  * Push a new long integer value into the cbps.
304  */
305 void
306 gras_cbps_i_push(gras_cbps_t ps,
307                  int val) {
308   DEBUG1("push %d as a size",val);
309   xbt_dynar_push_as(ps->lints,int,val);
310 }
311 /**
312  * gras_cbps_i_pop:
313  *
314  * Pop the lastly pushed long integer value from the cbps.
315  */
316 int
317 gras_cbps_i_pop(gras_cbps_t ps) {
318   int ret;
319
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);
324   return ret;
325 }
326
327 /**
328  * gras_datadesc_cb_pop:
329  *
330  * Generic cb returning the lastly pushed value
331  */
332 int gras_datadesc_cb_pop(gras_cbps_t vars, void *data) {
333   return gras_cbps_i_pop(vars);
334 }
335
336 /**
337  * gras_datadesc_cb_push_int:
338  * 
339  * Cb to push an integer. Must be attached to the field you want to push
340  */
341 void gras_datadesc_cb_push_int(gras_cbps_t vars, void *data) {
342    int *i = (int*)data;
343    gras_cbps_i_push(vars, (int) *i);
344 }
345
346 /**
347  * gras_datadesc_cb_push_uint:
348  * 
349  * Cb to push an unsigned integer. Must be attached to the field you want to push
350  */
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);
354 }
355
356 /**
357  * gras_datadesc_cb_push_lint:
358  * 
359  * Cb to push an long integer. Must be attached to the field you want to push
360  */
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);
364 }
365 /**
366  * gras_datadesc_cb_push_ulint:
367  * 
368  * Cb to push an long integer. Must be attached to the field you want to push
369  */
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);
373 }