Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Revert [From A. Giersch] Fix the following compilation warnings in ISO-C99 mode:
[simgrid.git] / include / xbt / dynar.h
1 /* dynar - a generic dynamic array                                          */
2
3 /* Copyright (c) 2004, 2005, 2006, 2007, 2009, 2010. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #ifndef _XBT_DYNAR_H
10 #define _XBT_DYNAR_H
11
12 #include <string.h> /* memcpy */
13
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_INLINE XBT_PUBLIC (int) xbt_dynar_is_empty(const xbt_dynar_t dynar);
75 XBT_PUBLIC(void) xbt_dynar_reset(xbt_dynar_t const dynar);
76 XBT_PUBLIC(void) xbt_dynar_shrink(xbt_dynar_t dynar, int empty_slots);
77
78 XBT_PUBLIC(void) xbt_dynar_dump(xbt_dynar_t dynar);
79
80 /** @} */
81 /** @defgroup XBT_dynar_array Dynar as a regular array
82  *  @ingroup XBT_dynar
83  *
84  *  @{
85  */
86
87 XBT_PUBLIC(void) xbt_dynar_get_cpy(const xbt_dynar_t dynar,
88                                    const unsigned long idx, void *const dst);
89
90 XBT_PUBLIC(void) xbt_dynar_set(xbt_dynar_t dynar, const int idx,
91                                const void *src);
92 XBT_PUBLIC(void) xbt_dynar_replace(xbt_dynar_t dynar, const unsigned long idx,
93                                    const void *object);
94
95 XBT_PUBLIC(void) xbt_dynar_insert_at(xbt_dynar_t const dynar, const int idx,
96                                      const void *src);
97 XBT_PUBLIC(void) xbt_dynar_remove_at(xbt_dynar_t const dynar, const int idx,
98                                      void *const dst);
99
100 XBT_PUBLIC(int) xbt_dynar_search(xbt_dynar_t const dynar, void *elem);
101 XBT_PUBLIC(int) xbt_dynar_member(xbt_dynar_t const dynar, void *elem);
102 XBT_PUBLIC(void) xbt_dynar_sort(xbt_dynar_t const dynar, int_f_cpvoid_cpvoid_t compar_fn);
103
104 /** @} */
105 /** @defgroup XBT_dynar_perl Perl-like use of dynars
106  *  @ingroup XBT_dynar
107  *
108  *  @{
109  */
110
111 XBT_INLINE XBT_PUBLIC(void) xbt_dynar_push(xbt_dynar_t const dynar, const void *src);
112 XBT_INLINE XBT_PUBLIC(void) xbt_dynar_pop(xbt_dynar_t const dynar, void *const dst);
113 XBT_PUBLIC(void) xbt_dynar_unshift(xbt_dynar_t const dynar, const void *src);
114 XBT_PUBLIC(void) xbt_dynar_shift(xbt_dynar_t const dynar, void *const dst);
115 XBT_PUBLIC(void) xbt_dynar_map(const xbt_dynar_t dynar,
116                                void_f_pvoid_t const op);
117
118 /** @} */
119 /** @defgroup XBT_dynar_ctn Direct manipulation to the dynars content
120  *  @ingroup XBT_dynar
121  *
122  *  Those functions do not retrieve the content, but only their address.
123  *
124  *  @{
125  */
126
127 XBT_INLINE XBT_PUBLIC(void *) xbt_dynar_get_ptr(const xbt_dynar_t dynar,
128                                      const unsigned long idx);
129 XBT_PUBLIC(void *) xbt_dynar_insert_at_ptr(xbt_dynar_t const dynar,
130                                            const int idx);
131 XBT_PUBLIC(void *) xbt_dynar_push_ptr(xbt_dynar_t const dynar);
132 XBT_PUBLIC(void *) xbt_dynar_pop_ptr(xbt_dynar_t const dynar);
133
134 /** @} */
135 /** @defgroup XBT_dynar_speed Speed optimized access to dynars of scalars
136  *  @ingroup XBT_dynar
137  *
138  *  While the other functions use a memcpy to retrieve the content into the
139  *  user provided area, those ones use a regular affectation. It only works
140  *  for scalar values, but should be a little faster.
141  *
142  *  @{
143  */
144
145   /** @brief Quick retrieval of scalar content 
146    *  @hideinitializer */
147 #  define xbt_dynar_get_as(dynar,idx,type) \
148           (*(type*)xbt_dynar_get_ptr((dynar),(idx)))
149   /** @brief Quick retrieval of scalar content 
150    *  @hideinitializer */
151 #  define xbt_dynar_getlast_as(dynar,type) \
152           (*(type*)xbt_dynar_get_ptr((dynar),xbt_dynar_length(dynar)-1))
153   /** @brief Quick retrieval of scalar content 
154    *  @hideinitializer */
155 #  define xbt_dynar_getfirst_as(dynar,type) \
156           (*(type*)xbt_dynar_get_ptr((dynar),0))
157   /** @brief Quick insertion of scalar content 
158    *  @hideinitializer */
159 #  define xbt_dynar_insert_at_as(dynar,idx,type,value) \
160           *(type*)xbt_dynar_insert_at_ptr(dynar,idx)=value
161   /** @brief Quick insertion of scalar content 
162    *  @hideinitializer */
163 #  define xbt_dynar_push_as(dynar,type,value) \
164           *(type*)xbt_dynar_push_ptr(dynar)=value
165   /** @brief Quick removal of scalar content
166    *  @hideinitializer */
167 #  define xbt_dynar_pop_as(dynar,type) \
168            (*(type*)xbt_dynar_pop_ptr(dynar))
169
170 /** @} */
171 /** @defgroup XBT_dynar_cursor Cursors on dynar
172  *  @ingroup XBT_dynar
173  *
174  * Cursors are used to iterate over the structure. Never add elements to the 
175  * DynArr during the traversal. To remove elements, use the
176  * xbt_dynar_cursor_rm() function.
177  *
178  * Do not call these functions directly, but only the xbt_dynar_foreach macro.
179  * 
180  * For synchronized dynars, the dynar will be locked during the whole
181  * loop and it will get unlocked automatically if you traverse all
182  * elements. If you want to break the loop before the end, make sure
183  * to call xbt_dynar_cursor_unlock() before the <tt>break;</tt>
184  *
185  *  @{
186  */
187
188 XBT_INLINE XBT_PUBLIC(void) xbt_dynar_cursor_rm(xbt_dynar_t dynar,
189                                      unsigned int *const cursor);
190 XBT_PUBLIC(void) xbt_dynar_cursor_unlock(xbt_dynar_t dynar);
191
192 /* do not use this structure internals directly, but use the public interface
193  * This was made public to allow:
194  *  - the inlining of the foreach elements
195  *  - sending such beasts over the network
196  */
197
198 #include "xbt/synchro_core.h"
199 typedef struct xbt_dynar_s {
200   unsigned long size;
201   unsigned long used;
202   unsigned long elmsize;
203   void *data;
204   void_f_pvoid_t free_f;
205   xbt_mutex_t mutex;
206 } s_xbt_dynar_t;
207
208 static XBT_INLINE void
209 _xbt_dynar_cursor_first(const xbt_dynar_t dynar, unsigned int *const cursor)
210 {
211   /* don't test for dynar!=NULL. The segfault would tell us */
212   if (dynar->mutex)  /* ie _dynar_lock(dynar) but not public */
213     xbt_mutex_acquire(dynar->mutex);
214
215   //DEBUG1("Set cursor on %p to the first position", (void *) dynar);
216   *cursor = 0;
217 }
218
219 static XBT_INLINE int
220 _xbt_dynar_cursor_get(const xbt_dynar_t dynar,
221                       unsigned int idx, void *const dst)
222 {
223
224   if (idx >= dynar->used) {
225     //DEBUG1("Cursor on %p already on last elem", (void *) dynar);
226     if (dynar->mutex) /* unlock */
227       xbt_mutex_release(dynar->mutex);
228     return FALSE;
229   }
230   //  DEBUG2("Cash out cursor on %p at %u", (void *) dynar, *idx);
231
232   memcpy(dst, ((char*)dynar->data) + idx * dynar->elmsize, dynar->elmsize);
233
234   return TRUE;
235 }
236
237
238
239 /** @brief Iterates over the whole dynar. 
240  * 
241  *  @param _dynar what to iterate over
242  *  @param _cursor an integer used as cursor
243  *  @param _data
244  *  @hideinitializer
245  *
246  * \note An example of usage:
247  * \code
248 xbt_dynar_t dyn;
249 unsigned int cpt;
250 string *str;
251 xbt_dynar_foreach (dyn,cpt,str) {
252   printf("Seen %s\n",str);
253 }
254 \endcode
255  */
256 #define xbt_dynar_foreach(_dynar,_cursor,_data) \
257        for (_xbt_dynar_cursor_first(_dynar,&(_cursor))      ; \
258             _xbt_dynar_cursor_get(_dynar,_cursor,&_data) ; \
259             (_cursor)++         )
260
261 /** @} */
262
263 SG_END_DECL()
264 #endif /* _XBT_DYNAR_H */