1 /* dynar - a generic dynamic array */
3 /* Copyright (c) 2004, 2005, 2006, 2007, 2009, 2010. The SimGrid Team.
4 * All rights reserved. */
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. */
12 #include <string.h> /* memcpy */
14 #include "xbt/misc.h" /* SG_BEGIN_DECL */
15 #include "xbt/function_types.h"
19 /** @addtogroup XBT_dynar
20 * @brief DynArr are dynamically sized vector which may contain any type of variables.
22 * These are the SimGrid version of the dynamically size arrays, which all C programmer recode one day or another.
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).
29 * \section XBT_dynar_exscal Example with scalar
30 * \dontinclude dynar.c
37 * \until end_of_traversal
40 * \until xbt_dynar_free
42 * \section XBT_dynar_exptr Example with pointed data
44 * \skip test_dynar_string
52 * \skip end_of_doxygen
56 /** @defgroup XBT_dynar_cons Dynar constructor and destructor
61 /** \brief Dynar data type (opaque type) */
62 typedef struct xbt_dynar_s *xbt_dynar_t;
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);
73 XBT_INLINE XBT_PUBLIC(unsigned long) xbt_dynar_length(const xbt_dynar_t
75 XBT_INLINE XBT_PUBLIC(int) xbt_dynar_is_empty(const xbt_dynar_t dynar);
76 XBT_PUBLIC(void) xbt_dynar_reset(xbt_dynar_t const dynar);
77 XBT_PUBLIC(void) xbt_dynar_shrink(xbt_dynar_t dynar, int empty_slots);
79 XBT_PUBLIC(void) xbt_dynar_dump(xbt_dynar_t dynar);
82 /** @defgroup XBT_dynar_array Dynar as a regular array
88 XBT_PUBLIC(void) xbt_dynar_get_cpy(const xbt_dynar_t dynar,
89 const unsigned long idx,
91 XBT_INLINE void *xbt_dynar_set_at_ptr(const xbt_dynar_t dynar,
92 const unsigned long idx);
93 XBT_PUBLIC(void) xbt_dynar_set(xbt_dynar_t dynar, const int idx,
95 XBT_PUBLIC(void) xbt_dynar_replace(xbt_dynar_t dynar,
96 const unsigned long idx,
99 XBT_PUBLIC(void) xbt_dynar_insert_at(xbt_dynar_t const dynar,
100 const int idx, const void *src);
101 XBT_PUBLIC(void) xbt_dynar_remove_at(xbt_dynar_t const dynar,
102 const int idx, void *const dst);
104 XBT_PUBLIC(unsigned int) xbt_dynar_search(xbt_dynar_t const dynar,
106 XBT_PUBLIC(int) xbt_dynar_member(xbt_dynar_t const dynar, void *elem);
107 XBT_PUBLIC(void) xbt_dynar_sort(xbt_dynar_t const dynar,
108 int_f_cpvoid_cpvoid_t compar_fn);
109 XBT_INLINE XBT_PUBLIC(int) xbt_dynar_compare(xbt_dynar_t d1, xbt_dynar_t d2,
110 int(*compar)(const void *, const void *));
113 /** @defgroup XBT_dynar_perl Perl-like use of dynars
119 XBT_INLINE XBT_PUBLIC(void) xbt_dynar_push(xbt_dynar_t const dynar,
121 XBT_INLINE XBT_PUBLIC(void) xbt_dynar_pop(xbt_dynar_t const dynar,
123 XBT_PUBLIC(void) xbt_dynar_unshift(xbt_dynar_t const dynar,
125 XBT_PUBLIC(void) xbt_dynar_shift(xbt_dynar_t const dynar, void *const dst);
126 XBT_PUBLIC(void) xbt_dynar_map(const xbt_dynar_t dynar,
127 void_f_pvoid_t const op);
130 /** @defgroup XBT_dynar_ctn Direct manipulation to the dynars content
133 * Those functions do not retrieve the content, but only their address.
138 XBT_INLINE XBT_PUBLIC(void *) xbt_dynar_set_at_ptr(const xbt_dynar_t dynar,
139 const unsigned long idx);
140 XBT_INLINE XBT_PUBLIC(void *) xbt_dynar_get_ptr(const xbt_dynar_t dynar,
141 const unsigned long idx);
142 XBT_PUBLIC(void *) xbt_dynar_insert_at_ptr(xbt_dynar_t const dynar,
144 XBT_PUBLIC(void *) xbt_dynar_push_ptr(xbt_dynar_t const dynar);
145 XBT_PUBLIC(void *) xbt_dynar_pop_ptr(xbt_dynar_t const dynar);
148 /** @defgroup XBT_dynar_speed Speed optimized access to dynars of scalars
151 * While the other functions use a memcpy to retrieve the content into the
152 * user provided area, those ones use a regular affectation. It only works
153 * for scalar values, but should be a little faster.
158 /** @brief Quick retrieval of scalar content
159 * @hideinitializer */
160 # define xbt_dynar_get_as(dynar,idx,type) \
161 (*(type*)xbt_dynar_get_ptr((dynar),(idx)))
162 /** @brief Quick setting of scalar content
163 * @hideinitializer */
164 # define xbt_dynar_set_as(dynar,idx,type,val) \
165 (*(type*)xbt_dynar_set_at_ptr((dynar),(idx))) = val
166 /** @brief Quick retrieval of scalar content
167 * @hideinitializer */
168 # define xbt_dynar_getlast_as(dynar,type) \
169 (*(type*)xbt_dynar_get_ptr((dynar),xbt_dynar_length(dynar)-1))
170 /** @brief Quick retrieval of scalar content
171 * @hideinitializer */
172 # define xbt_dynar_getfirst_as(dynar,type) \
173 (*(type*)xbt_dynar_get_ptr((dynar),0))
174 /** @brief Quick insertion of scalar content
175 * @hideinitializer */
176 # define xbt_dynar_insert_at_as(dynar,idx,type,value) \
177 *(type*)xbt_dynar_insert_at_ptr(dynar,idx)=value
178 /** @brief Quick insertion of scalar content
179 * @hideinitializer */
180 # define xbt_dynar_push_as(dynar,type,value) \
181 *(type*)xbt_dynar_push_ptr(dynar)=value
182 /** @brief Quick removal of scalar content
183 * @hideinitializer */
184 # define xbt_dynar_pop_as(dynar,type) \
185 (*(type*)xbt_dynar_pop_ptr(dynar))
188 /** @defgroup XBT_dynar_cursor Cursors on dynar
191 * Cursors are used to iterate over the structure. Never add elements to the
192 * DynArr during the traversal. To remove elements, use the
193 * xbt_dynar_cursor_rm() function.
195 * Do not call these functions directly, but only the xbt_dynar_foreach macro.
197 * For synchronized dynars, the dynar will be locked during the whole
198 * loop and it will get unlocked automatically if you traverse all
199 * elements. If you want to break the loop before the end, make sure
200 * to call xbt_dynar_cursor_unlock() before the <tt>break;</tt>
205 XBT_INLINE XBT_PUBLIC(void) xbt_dynar_cursor_rm(xbt_dynar_t dynar,
208 XBT_PUBLIC(void) xbt_dynar_cursor_unlock(xbt_dynar_t dynar);
210 /* do not use this structure internals directly, but use the public interface
211 * This was made public to allow:
212 * - the inlining of the foreach elements
213 * - sending such beasts over the network
216 #include "xbt/synchro_core.h"
217 typedef struct xbt_dynar_s {
220 unsigned long elmsize;
222 void_f_pvoid_t free_f;
226 static XBT_INLINE void
227 _xbt_dynar_cursor_first(const xbt_dynar_t dynar,
228 unsigned int *const cursor)
230 /* don't test for dynar!=NULL. The segfault would tell us */
231 if (dynar->mutex) /* ie _dynar_lock(dynar) but not public */
232 xbt_mutex_acquire(dynar->mutex);
234 //DEBUG1("Set cursor on %p to the first position", (void *) dynar);
238 static XBT_INLINE int
239 _xbt_dynar_cursor_get(const xbt_dynar_t dynar,
240 unsigned int idx, void *const dst)
243 if (idx >= dynar->used) {
244 //DEBUG1("Cursor on %p already on last elem", (void *) dynar);
245 if (dynar->mutex) /* unlock */
246 xbt_mutex_release(dynar->mutex);
249 // DEBUG2("Cash out cursor on %p at %u", (void *) dynar, *idx);
251 memcpy(dst, ((char *) dynar->data) + idx * dynar->elmsize,
259 /** @brief Iterates over the whole dynar.
261 * @param _dynar what to iterate over
262 * @param _cursor an integer used as cursor
266 * \note An example of usage:
271 xbt_dynar_foreach (dyn,cpt,str) {
272 printf("Seen %s\n",str);
276 #define xbt_dynar_foreach(_dynar,_cursor,_data) \
277 for (_xbt_dynar_cursor_first(_dynar,&(_cursor)) ; \
278 _xbt_dynar_cursor_get(_dynar,_cursor,&_data) ; \
284 #endif /* _XBT_DYNAR_H */