Logo AND Algorithmique Numérique Distribuée

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