Logo AND Algorithmique Numérique Distribuée

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