Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Renamed any gras stuff that was in xbt and should therefore be called
[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   xbt_error_t errcode;
36   gras_cbps_t  res;
37
38   res=xbt_new(s_gras_cbps_t,1);
39
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);
45
46   gras_cbps_block_begin(res);
47
48   return res;
49 }
50
51 void gras_cbps_free(gras_cbps_t *state) {
52
53   xbt_dynar_free( &( (*state)->lints   ) );
54
55   gras_cbps_block_end(*state);
56   xbt_dict_free ( &( (*state)->space   ) );
57   xbt_dynar_free( &( (*state)->frames  ) );
58   xbt_dynar_free( &( (*state)->globals ) );
59
60   xbt_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 xbt_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   xbt_dynar_t          varstack,frame;
78   gras_cbps_elm_t       var;
79   xbt_error_t errcode;
80   char *varname = (char*)strdup(name);
81
82   DEBUG2("push(%s,%p)",name,(void*)data);
83   errcode = xbt_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     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) {
91     return errcode;
92   }
93  
94   var       = xbt_new0(s_gras_cbps_elm_t,1);
95   var->type = ddt;
96   var->data = data;
97   
98   xbt_dynar_push(varstack, &var);
99   
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); 
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 xbt_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   xbt_dynar_t          varstack,frame;
120   gras_cbps_elm_t       var            = NULL;
121   void                 *data           = NULL;
122   xbt_error_t errcode;
123
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",
129            name);
130   }
131   xbt_dynar_pop(varstack, &var);
132   
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);
137   }
138   
139   if (ddt)
140     *ddt = var->type;  
141   data    = var->data;
142   
143   xbt_free(var);
144   
145   xbt_dynar_pop(ps->frames, &frame);
146   {
147     int l = xbt_dynar_length(frame);
148     
149     while (l--) {
150       char *_name = NULL;
151                                                                                 
152       _name = xbt_dynar_get_as(frame, l, char*);
153       if (!strcmp(name, _name)) {
154         xbt_dynar_remove_at(frame, l, &_name);
155         xbt_free(_name);
156         break;
157       }
158     }
159   }
160   xbt_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   xbt_dynar_t    dynar        = NULL;
184   gras_cbps_elm_t elm          = NULL;
185   xbt_error_t    errcode;
186   
187   DEBUG1("set(%s)",name);
188   errcode = xbt_dict_get(ps->space, name, (void **)&dynar);
189   
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);
193     
194     elm   = xbt_new0(s_gras_cbps_elm_t,1);
195     xbt_dynar_push(ps->globals, &name);
196   } else {
197     xbt_dynar_pop(dynar, &elm);
198   }
199   
200   elm->type   = ddt;
201   elm->data   = data;
202  
203   xbt_dynar_push(dynar, &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                  /* OUT */gras_datadesc_type_t *ddt) {
220   
221   xbt_dynar_t    dynar = NULL;
222   gras_cbps_elm_t elm   = NULL;
223   
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);
229   
230   if (ddt) {
231     *ddt = elm->type;
232   }
233   
234   return 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   xbt_dynar_t dynar = NULL;
256
257   DEBUG0(">>> Block begin");
258   dynar = xbt_dynar_new(sizeof (char *), NULL);
259   xbt_dynar_push(ps->frames, &dynar);
260 }
261
262 /**
263  * gras_cbps_block_end:
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   xbt_dynar_t  frame        = NULL;
271   int           cursor       =    0;
272   char         *name         = NULL;
273
274   xbt_assert0(xbt_dynar_length(ps->frames),
275                "More block_end than block_begin");
276   xbt_dynar_pop(ps->frames, &frame);
277   
278   xbt_dynar_foreach(frame, cursor, name) {
279
280     xbt_dynar_t    varstack    = NULL;
281     gras_cbps_elm_t var         = NULL;
282  
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);
286  
287     if (!xbt_dynar_length(varstack)) {
288       xbt_dict_remove(ps->space, name);
289       xbt_dynar_free_container(&varstack); /*already empty, save a test ;) */
290     }
291     
292     if (var->data) xbt_free(var->data);
293     xbt_free(var);
294     xbt_free(name);
295   }
296   xbt_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   xbt_error_t errcode;
310   DEBUG1("push %d as a size",val);
311   xbt_dynar_push_as(ps->lints,int,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   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);
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 }