Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
callbacks' persistant state. Almost no change from GS/vars.c: function renaming;...
[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
13 typedef struct {
14   gras_datadesc_type_t *type;
15   void                 *data;
16 } gras_dd_cbps_elm_t;
17
18 struct s_gras_dd_cbps {
19   gras_dict_t  *space;
20   gras_dynar_t *stack;
21   gras_dynar_t *globals;
22
23 };
24
25 /**
26  * gras_dd_cbps_push:
27  *
28  * Declare a new element in the PS, and give it a value. If an element of that name already exists, 
29  * it becomes masked by the one given here, and will be seable again only after a pop to remove the 
30  * value this push adds.
31  */
32 void
33 gras_dd_cbps_push(gras_dd_cbps_t        *ps,
34                   const char            *name,
35                   void                  *data,
36                   gras_datadesc_type_t  *ddt) {
37
38   gras_dynar_t            *p_dynar        = NULL;
39   gras_dd_cbps_elm_t      *p_var          = NULL;
40  
41   gras_dict_retrieve(ps->space, name, (void **)&p_dynar);
42  
43   if (!p_dynar) {
44     gras_dynar_new(&p_dynar, sizeof (gras_dd_cbps_elm_t *), NULL);
45     gras_dict_insert(ps->space, name, (void **)p_dynar, NULL);
46     /* FIXME: leaking on dynar. Insert in dict with a way to free it */
47   }
48  
49   p_var       = calloc(1, sizeof(gras_dd_cbps_elm_t));
50   p_var->type = ddt;
51   p_var->data = data;
52   
53   gras_dynar_push(p_dynar, &p_var);
54   
55   gras_dynar_pop(ps->stack, &p_dynar);
56   gras_dynar_push(p_dynar, &name);
57   gras_dynar_push(ps->stack, &p_dynar); 
58 }
59
60 /**
61  * gras_dd_cbps_pop:
62  *
63  * Retrieve an element from the PS, and remove it from the PS. If it's not present in the current block,
64  * it will fail (with abort) and not search in upper blocks since this denotes a programmation error.
65  */
66 void *
67 gras_dd_cbps_pop (gras_dd_cbps_t        *ps, 
68                   const char            *name,
69                   gras_datadesc_type_t **ddt) {
70   gras_dynar_t            *p_dynar        = NULL;
71   gras_dd_cbps_elm_t      *p_elm          = NULL;
72   void                    *data           = NULL;
73
74   /* FIXME: Error handling */
75   gras_dict_retrieve(ps->space, name, (void **)&p_dynar);
76   gras_dynar_pop(p_dynar, &p_elm);
77   
78   if (!gras_dynar_length(p_dynar)) {
79     gras_dict_remove(ps->space, name);
80                 gras_dynar_free_container(p_dynar);
81   }
82   
83   if (ddt) {
84     *ddt = p_elm->type;
85   }
86   
87   data    = p_elm->data;
88   
89   free(p_elm);
90   
91   gras_dynar_pop(ps->stack, &p_dynar);
92   {
93     int l = gras_dynar_length(p_dynar);
94     
95     while (l--) {
96       char *_name = NULL;
97                                                                                 
98       gras_dynar_get(p_dynar, l, &_name);
99       if (!strcmp(name, _name)) {
100         gras_dynar_remove_at(p_dynar, l, &_name);
101         free(_name);
102         break;
103       }
104     }
105   }
106   gras_dynar_push(ps->stack, &p_dynar);
107   
108   
109   return data;
110 }
111
112 /**
113  * gras_dd_cbps_set:
114  *
115  * Change the value of an element in the PS.  If it's not present in the current block, look in the upper ones.
116  * If it's not present in any of them, look in the globals (FIXME: which no function of this API allows to set). 
117  * If not present there neither, the code may segfault (Oli?).
118  *
119  * Once a reference to an element of that name is found somewhere in the PS, its value is changed.
120  */
121 void
122 gras_dd_cbps_set (gras_dd_cbps_t        *ps,
123                   const char            *name,
124                   void                  *data,
125                   gras_datadesc_type_t  *ddt) {
126
127   gras_dynar_t            *p_dynar        = NULL;
128   gras_dd_cbps_elm_t      *p_elm          = NULL;
129   
130   gras_dict_retrieve(ps->space, name, (void **)&p_dynar);
131   
132   if (!p_dynar) {
133     gras_dynar_new(&p_dynar, sizeof (gras_dd_cbps_elm_t *), NULL);
134     gras_dict_insert(ps->space, name, (void **)p_dynar, NULL);
135     
136     p_elm   = calloc(1, sizeof(gras_dd_cbps_elm_t));
137     gras_dynar_push(ps->globals, &name);
138   } else {
139     gras_dynar_pop(p_dynar, &p_elm);
140   }
141   
142   p_elm->type   = ddt;
143   p_elm->data   = data;
144  
145   gras_dynar_push(p_dynar, &p_elm);
146
147 }
148
149 /**
150  * gras_dd_cbps_get:
151  *
152  * Get the value of an element in the PS without modifying it.
153  * If it's not present in the current block, look in the upper ones.
154  * If it's not present in any of them, look in the globals (FIXME: which no function of this API allows to set). 
155  * If not present there neither, the code may segfault (Oli?).
156  */
157 void *
158 gras_dd_cbps_get (gras_dd_cbps_t        *ps, 
159                   const char            *name,
160                   gras_datadesc_type_t **ddt) {
161   
162   gras_dynar_t            *p_dynar        = NULL;
163   gras_dd_cbps_elm_t      *p_elm          = NULL;
164   
165   /* FIXME: Error handling */
166   gras_dict_retrieve(ps->space, name, (void **)&p_dynar);
167   gras_dynar_pop(p_dynar, &p_elm);
168   gras_dynar_push(p_dynar, &p_elm);
169   
170   if (ddt) {
171     *ddt = p_elm->type;
172   }
173   
174   return p_elm->data;
175
176 }
177
178 /**
179  * gras_dd_cbps_block_begin:
180  *
181  * Begins a new block. 
182  *
183  * Blocks are usefull to remove a whole set of declarations you don't even know. 
184  *
185  * For example, they constitute a very elegent solution to recursive data structures. 
186  * push/pop may be used in some cases for that, but if your recursive data struct contains 
187  * other structs needing themselves callbacks, you have to use block_{begin,end} to do the trick.
188  */
189
190 void
191 gras_dd_cbps_block_begin(gras_dd_cbps_t *ps) {
192
193   gras_dynar_t            *p_dynar        = NULL;
194   
195   gras_dynar_new(&p_dynar, sizeof (char *), NULL);
196   gras_dynar_push(ps->stack, &p_dynar);
197 }
198
199 /**
200  * gras_dd_cbps_block_begin:
201  *
202  * End the current block, and go back to the upper one.
203  */
204 void
205 gras_dd_cbps_block_end(gras_dd_cbps_t *ps) {
206
207   gras_dynar_t            *p_dynar        = NULL;
208   int                      cursor         =    0;
209   char                    *name           = NULL;
210   
211   gras_dynar_pop(ps->stack, &p_dynar);
212   
213   gras_dynar_foreach(p_dynar, cursor, name) {
214     /*  inline the code of  _gs_vars_pop(p_vars, name), which was used only once */
215
216     gras_dynar_t            *p_dynar_elm    = NULL;
217     gras_dd_cbps_elm_t      *p_elm          = NULL;
218  
219     gras_dict_retrieve(ps->space, name, (void **)&p_dynar_elm);
220     gras_dynar_pop(p_dynar_elm, &p_elm);
221  
222     if (!gras_dynar_length(p_dynar_elm)) {
223       gras_dict_remove(ps->space, name);
224       gras_dynar_free_container(p_dynar_elm);
225     }
226     
227     free(p_elm);
228     free(name);
229   }
230                                                                                 
231   gras_dynar_free_container(p_dynar);
232 }
233
234
235