Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
646fa440a9f827d889f7746507886b0b9324226e
[simgrid.git] / src / gras / DataDesc / cbps.c
1 /* $Id$ */
2
3 /* cbps - persistant states for callbacks                                   */
4
5 /* Copyright (c) 2004 Olivier Aumage, Martin Quinson. All rights reserved.  */
6
7 /* This program is free software; you can redistribute it and/or modify it
8  * under the terms of the license (GNU LGPL) which comes with this package. */
9
10 #include "gras/DataDesc/datadesc_private.h"
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ddt_cbps,datadesc,"callback persistant state");
12
13 typedef struct {
14   gras_datadesc_type_t  type;
15   void                 *data;
16 } s_gras_cbps_elm_t, *gras_cbps_elm_t;
17
18 typedef struct s_gras_cbps {
19   xbt_dynar_t lints; /* simple stack of long integers (easy interface) */
20
21   xbt_dict_t  space; /* varname x dynar of gras_cbps_elm_t */
22   xbt_dynar_t frames; /* of dynar of names defined within this frame
23                            (and to pop when we leave it) */
24   xbt_dynar_t globals;
25 } s_gras_cbps_t;
26
27 static void free_string(void *d);
28
29 static void free_string(void *d){
30   xbt_free(*(void**)d);
31 }
32
33 gras_cbps_t gras_cbps_new(void) {
34   gras_cbps_t  res;
35
36   res=xbt_new(s_gras_cbps_t,1);
37
38   res->lints = xbt_dynar_new(sizeof(int), NULL);
39   res->space = xbt_dict_new();
40   /* no leak, the content is freed manually on block_end */
41   res->frames = xbt_dynar_new(sizeof(xbt_dynar_t), NULL);
42   res->globals = xbt_dynar_new(sizeof(char*), NULL);
43
44   gras_cbps_block_begin(res);
45
46   return res;
47 }
48
49 void gras_cbps_free(gras_cbps_t *state) {
50
51   xbt_dynar_free( &( (*state)->lints   ) );
52
53   gras_cbps_block_end(*state);
54   xbt_dict_free ( &( (*state)->space   ) );
55   xbt_dynar_free( &( (*state)->frames  ) );
56   xbt_dynar_free( &( (*state)->globals ) );
57
58   xbt_free(*state);
59   *state = NULL;
60 }
61
62 /**
63  * gras_cbps_v_push:
64  *
65  * Declare a new element in the PS, and give it a value. If an element of that
66  * name already exists, it is masked by the one given here, and will be 
67  * seeable again only after a pop to remove the value this push adds.
68  */
69 xbt_error_t
70 gras_cbps_v_push(gras_cbps_t          ps,
71                  const char          *name,
72                  void                *data,
73                  gras_datadesc_type_t ddt) {
74
75   xbt_dynar_t          varstack,frame;
76   gras_cbps_elm_t       var;
77   xbt_error_t errcode;
78   char *varname = (char*)strdup(name);
79
80   DEBUG2("push(%s,%p)",name,(void*)data);
81   errcode = xbt_dict_get(ps->space, name, (void **)&varstack);
82  
83   if (errcode == mismatch_error) {
84     DEBUG1("Create a new variable stack for '%s' into the space",name);
85     varstack = xbt_dynar_new(sizeof (gras_cbps_elm_t *), NULL);
86     xbt_dict_set(ps->space, varname, (void **)varstack, NULL);
87     /* leaking, you think? only if you do not close all the openned blocks ;)*/
88   } else if (errcode != no_error) {
89     return errcode;
90   }
91  
92   var       = xbt_new0(s_gras_cbps_elm_t,1);
93   var->type = ddt;
94   var->data = data;
95   
96   xbt_dynar_push(varstack, &var);
97   
98   xbt_dynar_pop(ps->frames, &frame);
99   DEBUG4("Push %s (%p @%p) into frame %p",varname,(void*)varname,(void*)&varname,(void*)frame);
100   xbt_dynar_push(frame, &varname);
101   xbt_dynar_push(ps->frames, &frame); 
102   return no_error;
103 }
104
105 /**
106  * gras_cbps_v_pop:
107  *
108  * Retrieve an element from the PS, and remove it from the PS. If it's not
109  * present in the current block, it will fail (with abort) and not search
110  * in upper blocks since this denotes a programmation error.
111  */
112 xbt_error_t
113 gras_cbps_v_pop (gras_cbps_t            ps, 
114                  const char            *name,
115                  gras_datadesc_type_t  *ddt,
116                  void                 **res) {
117   xbt_dynar_t          varstack,frame;
118   gras_cbps_elm_t       var            = NULL;
119   void                 *data           = NULL;
120   xbt_error_t errcode;
121
122   DEBUG1("pop(%s)",name);
123   /* FIXME: Error handling */
124   errcode = xbt_dict_get(ps->space, name, (void **)&varstack);
125   if (errcode == mismatch_error) {
126     RAISE1(mismatch_error,"Asked to pop the non-existant %s",
127            name);
128   }
129   xbt_dynar_pop(varstack, &var);
130   
131   if (!xbt_dynar_length(varstack)) {
132     DEBUG1("Last incarnation of %s poped. Kill it",name);
133     xbt_dict_remove(ps->space, name);
134     xbt_dynar_free(&varstack);
135   }
136   
137   if (ddt)
138     *ddt = var->type;  
139   data    = var->data;
140   
141   xbt_free(var);
142   
143   xbt_dynar_pop(ps->frames, &frame);
144   {
145     int l = xbt_dynar_length(frame);
146     
147     while (l--) {
148       char *_name = NULL;
149                                                                                 
150       _name = xbt_dynar_get_as(frame, l, char*);
151       if (!strcmp(name, _name)) {
152         xbt_dynar_remove_at(frame, l, &_name);
153         xbt_free(_name);
154         break;
155       }
156     }
157   }
158   xbt_dynar_push(ps->frames, &frame);
159   
160   *res = data;
161   return no_error;
162 }
163
164 /**
165  * gras_cbps_v_set:
166  *
167  * Change the value of an element in the PS.  
168  * If it's not present in the current block, look in the upper ones.
169  * If it's not present in any of them, modify in the globals
170  * If not present there neither, the code may segfault (Oli?).
171  *
172  * Once a reference to an element of that name is found somewhere in the PS,
173  *   its value is changed.
174  */
175 void
176 gras_cbps_v_set (gras_cbps_t          ps,
177                  const char          *name,
178                  void                *data,
179                  gras_datadesc_type_t ddt) {
180
181   xbt_dynar_t    dynar        = NULL;
182   gras_cbps_elm_t elm          = NULL;
183   xbt_error_t    errcode;
184   
185   DEBUG1("set(%s)",name);
186   errcode = xbt_dict_get(ps->space, name, (void **)&dynar);
187   
188   if (errcode == mismatch_error) {
189     dynar = xbt_dynar_new(sizeof (gras_cbps_elm_t), NULL);
190     xbt_dict_set(ps->space, name, (void **)dynar, NULL);
191     
192     elm   = xbt_new0(s_gras_cbps_elm_t,1);
193     xbt_dynar_push(ps->globals, &name);
194   } else {
195     xbt_dynar_pop(dynar, &elm);
196   }
197   
198   elm->type   = ddt;
199   elm->data   = data;
200  
201   xbt_dynar_push(dynar, &elm);
202
203 }
204
205 /**
206  * gras_cbps_v_get:
207  *
208  * Get the value of an element in the PS without modifying it. 
209  * (note that you get the content of the data struct and not a copy to it)
210  * If it's not present in the current block, look in the upper ones.
211  * If it's not present in any of them, look in the globals
212  * If not present there neither, the code may segfault (Oli?).
213  */
214 void *
215 gras_cbps_v_get (gras_cbps_t           ps, 
216                  const char           *name,
217                  /* OUT */gras_datadesc_type_t *ddt) {
218   
219   xbt_dynar_t    dynar = NULL;
220   gras_cbps_elm_t elm   = NULL;
221   
222   DEBUG1("get(%s)",name);
223   /* FIXME: Error handling */
224   xbt_dict_get(ps->space, name, (void **)&dynar);
225   xbt_dynar_pop(dynar, &elm);
226   xbt_dynar_push(dynar, &elm);
227   
228   if (ddt) {
229     *ddt = elm->type;
230   }
231   
232   return elm->data;
233
234 }
235
236 /**
237  * gras_cbps_block_begin:
238  *
239  * Begins a new block. 
240  *
241  * Blocks are usefull to remove a whole set of declarations you don't even know
242  *
243  * E.g., they constitute an elegent solution to recursive data structures. 
244  *
245  * push/pop may be used in some cases for that, but if your recursive data 
246  * struct contains other structs needing themselves callbacks, you have to
247  * use block_{begin,end} to do the trick.
248  */
249
250 void
251 gras_cbps_block_begin(gras_cbps_t ps) {
252
253   xbt_dynar_t dynar = NULL;
254
255   DEBUG0(">>> Block begin");
256   dynar = xbt_dynar_new(sizeof (char *), NULL);
257   xbt_dynar_push(ps->frames, &dynar);
258 }
259
260 /**
261  * gras_cbps_block_end:
262  *
263  * End the current block, and go back to the upper one.
264  */
265 void
266 gras_cbps_block_end(gras_cbps_t ps) {
267
268   xbt_dynar_t  frame        = NULL;
269   int           cursor       =    0;
270   char         *name         = NULL;
271
272   xbt_assert0(xbt_dynar_length(ps->frames),
273                "More block_end than block_begin");
274   xbt_dynar_pop(ps->frames, &frame);
275   
276   xbt_dynar_foreach(frame, cursor, name) {
277
278     xbt_dynar_t    varstack    = NULL;
279     gras_cbps_elm_t var         = NULL;
280  
281     DEBUG2("Get ride of %s (%p)",name,(void*)name);
282     xbt_dict_get(ps->space, name, (void **)&varstack);
283     xbt_dynar_pop(varstack, &var);
284  
285     if (!xbt_dynar_length(varstack)) {
286       xbt_dict_remove(ps->space, name);
287       xbt_dynar_free_container(&varstack); /*already empty, save a test ;) */
288     }
289     
290     if (var->data) xbt_free(var->data);
291     xbt_free(var);
292     xbt_free(name);
293   }
294   xbt_dynar_free_container(&frame);/* we just emptied it */
295   DEBUG0("<<< Block end");
296 }
297
298
299 /**
300  * gras_cbps_i_push:
301  *
302  * Push a new long integer value into the cbps.
303  */
304 void
305 gras_cbps_i_push(gras_cbps_t ps,
306                  int val) {
307   DEBUG1("push %d as a size",val);
308   xbt_dynar_push_as(ps->lints,int,val);
309 }
310 /**
311  * gras_cbps_i_pop:
312  *
313  * Pop the lastly pushed long integer value from the cbps.
314  */
315 int
316 gras_cbps_i_pop(gras_cbps_t ps) {
317   int ret;
318
319   xbt_assert0(xbt_dynar_length(ps->lints) > 0,
320                "gras_cbps_i_pop: no value to pop");
321   ret = xbt_dynar_pop_as(ps->lints,int);
322   DEBUG1("pop %d as a size",ret);
323   return ret;
324 }
325
326 /**
327  * gras_datadesc_cb_pop:
328  *
329  * Generic cb returning the lastly pushed value
330  */
331 int gras_datadesc_cb_pop(gras_cbps_t vars, void *data) {
332   return gras_cbps_i_pop(vars);
333 }
334
335 /**
336  * gras_datadesc_cb_push_int:
337  * 
338  * Cb to push an integer. Must be attached to the field you want to push
339  */
340 void gras_datadesc_cb_push_int(gras_cbps_t vars, void *data) {
341    int *i = (int*)data;
342    gras_cbps_i_push(vars, (int) *i);
343 }
344
345 /**
346  * gras_datadesc_cb_push_uint:
347  * 
348  * Cb to push an unsigned integer. Must be attached to the field you want to push
349  */
350 void gras_datadesc_cb_push_uint(gras_cbps_t vars, void *data) {
351    unsigned int *i = (unsigned int*)data;
352    gras_cbps_i_push(vars, (int) *i);
353 }
354
355 /**
356  * gras_datadesc_cb_push_lint:
357  * 
358  * Cb to push an long integer. Must be attached to the field you want to push
359  */
360 void gras_datadesc_cb_push_lint(gras_cbps_t vars, void *data) {
361    long int *i = (long int*)data;
362    gras_cbps_i_push(vars, (int) *i);
363 }
364 /**
365  * gras_datadesc_cb_push_ulint:
366  * 
367  * Cb to push an long integer. Must be attached to the field you want to push
368  */
369 void gras_datadesc_cb_push_ulint(gras_cbps_t vars, void *data) {
370    unsigned long int *i = (unsigned long int*)data;
371    gras_cbps_i_push(vars, (int) *i);
372 }