Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cosmetics
[simgrid.git] / include / xbt / dynar.h
1 /* $Id$ */
2
3 /* dynar - a generic dynamic array                                          */
4
5 /* Copyright (c) 2003, 2004 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 #ifndef _XBT_DYNAR_H
11 #define _XBT_DYNAR_H
12
13 #include <string.h> /* memcpy */
14 #include "xbt/misc.h"           /* SG_BEGIN_DECL */
15 #include "xbt/function_types.h"
16
17 SG_BEGIN_DECL()
18
19 /** @addtogroup XBT_dynar
20   * @brief DynArr are dynamically sized vector which may contain any type of variables.
21   *
22   * These are the SimGrid version of the dynamically size arrays, which all C programmer recode one day or another.
23   *  
24   * For performance concerns, the content of DynArr must be homogeneous (in
25   * contrary to dictionnaries -- see the \ref XBT_dict section). You thus
26   * have to provide the function which will be used to free the content at
27   * structure creation (of type void_f_ppvoid_t or void_f_pvoid_t).
28   *
29   * \section XBT_dynar_exscal Example with scalar
30   * \dontinclude dynar.c
31   *
32   * \skip Vars_decl
33   * \skip dyn
34   * \until iptr
35   * \skip Populate_ints
36   * \skip dyn
37   * \until end_of_traversal
38   * \skip shifting
39   * \skip val
40   * \until xbt_dynar_free
41   *
42   * \section XBT_dynar_exptr Example with pointed data
43   * 
44   * \skip test_dynar_string
45   * \skip dynar_t
46   * \until s2
47   * \skip Populate_str
48   * \skip dyn
49   * \until }
50   * \skip macro
51   * \until dynar_free
52   * \skip end_of_doxygen
53   * \until }
54   *
55   */
56 /** @defgroup XBT_dynar_cons Dynar constructor and destructor
57  *  @ingroup XBT_dynar
58  *
59  *  @{
60  */
61    /** \brief Dynar data type (opaque type) */
62      typedef struct xbt_dynar_s *xbt_dynar_t;
63
64
65 XBT_PUBLIC(xbt_dynar_t) xbt_dynar_new(const unsigned long elm_size,
66                                       void_f_pvoid_t const free_f);
67 XBT_PUBLIC(xbt_dynar_t) xbt_dynar_new_sync(const unsigned long elm_size,
68                                            void_f_pvoid_t const free_f);
69 XBT_INLINE XBT_PUBLIC(void) xbt_dynar_free(xbt_dynar_t * dynar);
70 XBT_PUBLIC(void) xbt_dynar_free_voidp(void *dynar);
71 XBT_PUBLIC(void) xbt_dynar_free_container(xbt_dynar_t * dynar);
72
73 XBT_INLINE XBT_PUBLIC(unsigned long) xbt_dynar_length(const xbt_dynar_t dynar);
74 XBT_PUBLIC(void) xbt_dynar_reset(xbt_dynar_t const dynar);
75 XBT_PUBLIC(void) xbt_dynar_shrink(xbt_dynar_t dynar, int empty_slots);
76
77 XBT_PUBLIC(void) xbt_dynar_dump(xbt_dynar_t dynar);
78
79 /** @} */
80 /** @defgroup XBT_dynar_array Dynar as a regular array
81  *  @ingroup XBT_dynar
82  *
83  *  @{
84  */
85
86 XBT_PUBLIC(void) xbt_dynar_get_cpy(const xbt_dynar_t dynar,
87                                    const unsigned long idx, void *const dst);
88
89 XBT_PUBLIC(void) xbt_dynar_set(xbt_dynar_t dynar, const int idx,
90                                const void *src);
91 XBT_PUBLIC(void) xbt_dynar_replace(xbt_dynar_t dynar, const unsigned long idx,
92                                    const void *object);
93
94 XBT_PUBLIC(void) xbt_dynar_insert_at(xbt_dynar_t const dynar, const int idx,
95                                      const void *src);
96 XBT_PUBLIC(void) xbt_dynar_remove_at(xbt_dynar_t const dynar, const int idx,
97                                      void *const dst);
98
99 XBT_PUBLIC(int) xbt_dynar_search(xbt_dynar_t const dynar, void *elem);
100 XBT_PUBLIC(int) xbt_dynar_member(xbt_dynar_t const dynar, void *elem);
101 /** @} */
102 /** @defgroup XBT_dynar_perl Perl-like use of dynars
103  *  @ingroup XBT_dynar
104  *
105  *  @{
106  */
107
108 XBT_INLINE XBT_PUBLIC(void) xbt_dynar_push(xbt_dynar_t const dynar, const void *src);
109 XBT_INLINE XBT_PUBLIC(void) xbt_dynar_pop(xbt_dynar_t const dynar, void *const dst);
110 XBT_PUBLIC(void) xbt_dynar_unshift(xbt_dynar_t const dynar, const void *src);
111 XBT_PUBLIC(void) xbt_dynar_shift(xbt_dynar_t const dynar, void *const dst);
112 XBT_PUBLIC(void) xbt_dynar_map(const xbt_dynar_t dynar,
113                                void_f_pvoid_t const op);
114
115 /** @} */
116 /** @defgroup XBT_dynar_ctn Direct manipulation to the dynars content
117  *  @ingroup XBT_dynar
118  *
119  *  Those functions do not retrieve the content, but only their address.
120  *
121  *  @{
122  */
123
124 XBT_INLINE XBT_PUBLIC(void *) xbt_dynar_get_ptr(const xbt_dynar_t dynar,
125                                      const unsigned long idx);
126 XBT_PUBLIC(void *) xbt_dynar_insert_at_ptr(xbt_dynar_t const dynar,
127                                            const int idx);
128 XBT_PUBLIC(void *) xbt_dynar_push_ptr(xbt_dynar_t const dynar);
129 XBT_PUBLIC(void *) xbt_dynar_pop_ptr(xbt_dynar_t const dynar);
130
131 /** @} */
132 /** @defgroup XBT_dynar_speed Speed optimized access to dynars of scalars
133  *  @ingroup XBT_dynar
134  *
135  *  While the other functions use a memcpy to retrieve the content into the
136  *  user provided area, those ones use a regular affectation. It only works
137  *  for scalar values, but should be a little faster.
138  *
139  *  @{
140  */
141
142   /** @brief Quick retrieval of scalar content 
143    *  @hideinitializer */
144 #  define xbt_dynar_get_as(dynar,idx,type) \
145           (*(type*)xbt_dynar_get_ptr((dynar),(idx)))
146   /** @brief Quick retrieval of scalar content 
147    *  @hideinitializer */
148 #  define xbt_dynar_getlast_as(dynar,type) \
149           (*(type*)xbt_dynar_get_ptr((dynar),xbt_dynar_length(dynar)-1))
150   /** @brief Quick retrieval of scalar content 
151    *  @hideinitializer */
152 #  define xbt_dynar_getfirst_as(dynar,type) \
153           (*(type*)xbt_dynar_get_ptr((dynar),0))
154   /** @brief Quick insertion of scalar content 
155    *  @hideinitializer */
156 #  define xbt_dynar_insert_at_as(dynar,idx,type,value) \
157           *(type*)xbt_dynar_insert_at_ptr(dynar,idx)=value
158   /** @brief Quick insertion of scalar content 
159    *  @hideinitializer */
160 #  define xbt_dynar_push_as(dynar,type,value) \
161           *(type*)xbt_dynar_push_ptr(dynar)=value
162   /** @brief Quick removal of scalar content
163    *  @hideinitializer */
164 #  define xbt_dynar_pop_as(dynar,type) \
165            (*(type*)xbt_dynar_pop_ptr(dynar))
166
167 /** @} */
168 /** @defgroup XBT_dynar_cursor Cursors on dynar
169  *  @ingroup XBT_dynar
170  *
171  * Cursors are used to iterate over the structure. Never add elements to the 
172  * DynArr during the traversal. To remove elements, use the
173  * xbt_dynar_cursor_rm() function.
174  *
175  * Do not call these functions directly, but only the xbt_dynar_foreach macro.
176  * 
177  * For synchronized dynars, the dynar will be locked during the whole
178  * loop and it will get unlocked automatically if you traverse all
179  * elements. If you want to break the loop before the end, make sure
180  * to call xbt_dynar_cursor_unlock() before the <tt>break;</tt>
181  *
182  *  @{
183  */
184
185 XBT_INLINE XBT_PUBLIC(void) xbt_dynar_cursor_rm(xbt_dynar_t dynar,
186                                      unsigned int *const cursor);
187 XBT_PUBLIC(void) xbt_dynar_cursor_unlock(xbt_dynar_t dynar);
188
189 /* do not use this structure internals directly, but use the public interface
190  * This was made public to allow:
191  *  - the inlining of the foreach elements
192  *  - sending such beasts over the network
193  */
194
195 #include "xbt/synchro_core.h"
196 typedef struct xbt_dynar_s {
197   unsigned long size;
198   unsigned long used;
199   unsigned long elmsize;
200   void *data;
201   void_f_pvoid_t free_f;
202   xbt_mutex_t mutex;
203 } s_xbt_dynar_t;
204
205 static XBT_INLINE void
206 _xbt_dynar_cursor_first(const xbt_dynar_t dynar, unsigned int *const cursor)
207 {
208   /* don't test for dynar!=NULL. The segfault would tell us */
209   if (dynar->mutex)  /* ie _dynar_lock(dynar) but not public */
210     xbt_mutex_acquire(dynar->mutex);
211
212   //DEBUG1("Set cursor on %p to the first position", (void *) dynar);
213   *cursor = 0;
214 }
215
216 static XBT_INLINE int
217 _xbt_dynar_cursor_get(const xbt_dynar_t dynar,
218                       unsigned int idx, void *const dst)
219 {
220
221   if (idx >= dynar->used) {
222     //DEBUG1("Cursor on %p already on last elem", (void *) dynar);
223     if (dynar->mutex) /* unlock */
224       xbt_mutex_release(dynar->mutex);
225     return FALSE;
226   }
227   //  DEBUG2("Cash out cursor on %p at %u", (void *) dynar, *idx);
228
229   memcpy(dst, ((char*)dynar->data) + idx * dynar->elmsize, dynar->elmsize);
230
231   return TRUE;
232 }
233
234
235
236 /** @brief Iterates over the whole dynar. 
237  * 
238  *  @param _dynar what to iterate over
239  *  @param _cursor an integer used as cursor
240  *  @param _data
241  *  @hideinitializer
242  *
243  * \note An example of usage:
244  * \code
245 xbt_dynar_t dyn;
246 unsigned int cpt;
247 string *str;
248 xbt_dynar_foreach (dyn,cpt,str) {
249   printf("Seen %s\n",str);
250 }
251 \endcode
252  */
253 #define xbt_dynar_foreach(_dynar,_cursor,_data) \
254        for (_xbt_dynar_cursor_first(_dynar,&(_cursor))      ; \
255             _xbt_dynar_cursor_get(_dynar,_cursor,&_data) ; \
256             (_cursor)++         )
257
258 /** @} */
259
260 SG_END_DECL()
261 #endif /* _XBT_DYNAR_H */