3 /* a generic DYNamic ARray */
5 /* Authors: Martin Quinson */
6 /* Copyright (C) 2003 the OURAGAN project. */
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. */
12 #include "xbt/sysdep.h"
14 #include "xbt/error.h"
15 #include "xbt/dynar.h"
16 #include <sys/types.h>
18 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(dynar,xbt,"Dynamic arrays");
23 unsigned long elmsize;
28 #define __sanity_check_dynar(dynar) \
31 #define __sanity_check_idx(idx) \
32 gras_assert1(idx >= 0, \
33 "dynar idx(=%d) < 0", \
35 #define __check_inbound_idx(dynar, idx) \
36 gras_assert2(idx < dynar->used, \
37 "dynar is not that long. You asked %d, but it's only %lu long", \
38 (int) (idx), (unsigned long) dynar->used)
39 #define __check_sloppy_inbound_idx(dynar, idx) \
40 gras_assert2(idx <= dynar->used, \
41 "dynar is not that long. You asked %d, but it's only %lu long", \
42 (int) (idx), (unsigned long) dynar->used)
43 #define __check_populated_dynar(dynar) \
44 gras_assert1(dynar->used, \
45 "dynar %p contains nothing",(void*)dynar)
48 void _gras_clear_mem(void * const ptr,
49 const unsigned long length) {
50 memset(ptr, 0, length);
55 _gras_dynar_expand(gras_dynar_t * const dynar,
57 gras_error_t errcode = no_error;
58 const unsigned long old_size = dynar->size;
61 char * const old_data = dynar->data;
63 const unsigned long elmsize = dynar->elmsize;
64 const unsigned long old_length = old_size*elmsize;
66 const unsigned long used = dynar->used;
67 const unsigned long used_length = used*elmsize;
69 const unsigned long new_size = nb > (2*(old_size+1)) ? nb : (2*(old_size+1));
70 const unsigned long new_length = new_size*elmsize;
71 char * const new_data = gras_malloc0(elmsize*new_size);
73 DEBUG3("expend %p from %lu to %d elements", (void*)dynar, (unsigned long)old_size, nb);
76 memcpy(new_data, old_data, used_length);
77 _gras_clear_mem(old_data, old_length);
81 _gras_clear_mem(new_data + used_length, new_length - used_length);
83 dynar->size = new_size;
84 dynar->data = new_data;
92 _gras_dynar_elm(const gras_dynar_t * const dynar,
93 const unsigned long idx) {
94 char * const data = dynar->data;
95 const unsigned long elmsize = dynar->elmsize;
97 return data + idx*elmsize;
102 _gras_dynar_get_elm(void * const dst,
103 const gras_dynar_t * const dynar,
104 const unsigned long idx) {
105 void * const elm = _gras_dynar_elm(dynar, idx);
106 const unsigned long elmsize = dynar->elmsize;
108 memcpy(dst, elm, elmsize);
113 _gras_dynar_put_elm(const gras_dynar_t * const dynar,
114 const unsigned long idx,
115 const void * const src) {
116 void * const elm = _gras_dynar_elm(dynar, idx);
117 const unsigned long elmsize = dynar->elmsize;
119 memcpy(elm, src, elmsize);
124 * @elm_size: size of each element in the dynar
125 * @free_func: function to call each time we want to get rid of an element (or NULL if nothing to do).
127 * Creates a new dynar. If a free_func is provided, the elements have to be
128 * pointer of pointer. That is to say that dynars can contain either base
129 * types (int, char, double, etc) or pointer of pointers (struct **).
132 gras_dynar_new(const unsigned long elmsize,
133 void_f_pvoid_t * const free_func) {
135 gras_dynar_t *dynar = gras_new0(gras_dynar_t,1);
139 dynar->elmsize = elmsize;
141 dynar->free = free_func;
147 * gras_dynar_free_container:
148 * @dynar: poor victim
150 * kilkil a dynar BUT NOT its content. Ie, the array is freed, but not what
151 * its contain points to.
154 gras_dynar_free_container(gras_dynar_t * const dynar) {
158 _gras_clear_mem(dynar->data, dynar->size);
159 gras_free(dynar->data);
162 _gras_clear_mem(dynar, sizeof(gras_dynar_t));
170 * @dynar: who to squeeze
172 * Frees the content and set the size to 0
175 gras_dynar_reset(gras_dynar_t * const dynar) {
177 __sanity_check_dynar(dynar);
179 DEBUG1("Reset the dynar %p",(void*)dynar);
181 gras_dynar_map(dynar, dynar->free);
185 _gras_clear_mem(dynar->data, dynar->size);
186 gras_free(dynar->data);
196 * @dynar: poor victim
198 * kilkil a dynar and its content
202 gras_dynar_free(gras_dynar_t * const dynar) {
204 gras_dynar_reset(dynar);
205 gras_dynar_free_container(dynar);
211 * @dynar: the dynar we want to mesure
213 * Returns the count of elements in a dynar
216 gras_dynar_length(const gras_dynar_t * const dynar) {
217 return (dynar ? (unsigned long) dynar->used : (unsigned long)0);
221 * gras_dynar_get_cpy:
222 * @dynar: information dealer
223 * @idx: index of the slot we want to retrive
224 * @dst: where to pu the result to.
226 * Retrieve a copy of the Nth element of a dynar.
229 gras_dynar_get_cpy(const gras_dynar_t * const dynar,
233 __sanity_check_dynar(dynar);
234 __sanity_check_idx(idx);
235 __check_inbound_idx(dynar, idx);
237 _gras_dynar_get_elm(dst, dynar, idx);
242 * @dynar: information dealer
243 * @idx: index of the slot we want to retrive
244 * @dst: where to pu the result to.
246 * Retrieve the Nth element of a dynar. Warning, the returned value is the actual content of
247 * the dynar. Make a copy before fooling with it.
250 gras_dynar_get_ptr(const gras_dynar_t * const dynar,
253 __sanity_check_dynar(dynar);
254 __sanity_check_idx(idx);
255 __check_inbound_idx(dynar, idx);
257 return _gras_dynar_elm(dynar, idx);
264 * @src: What will be feeded to the dynar
266 * Set the Nth element of a dynar, expanding the dynar if needed, BUT NOT freeing
267 * the previous value at this position. If you want to free the previous content,
268 * use gras_dynar_replace().
271 gras_dynar_set(gras_dynar_t * const dynar,
273 const void * const src) {
275 __sanity_check_dynar(dynar);
276 __sanity_check_idx(idx);
278 _gras_dynar_expand(dynar, idx+1);
280 if (idx >= dynar->used) {
284 _gras_dynar_put_elm(dynar, idx, src);
288 * gras_dynar_remplace:
293 * Set the Nth element of a dynar, expanding the dynar if needed, AND DO
294 * free the previous value at this position. If you don't want to free the
295 * previous content, use gras_dynar_set().
298 gras_dynar_remplace(gras_dynar_t * const dynar,
300 const void * const object) {
302 __sanity_check_dynar(dynar);
303 __sanity_check_idx(idx);
305 if (idx < dynar->used && dynar->free) {
306 void * const old_object = _gras_dynar_elm(dynar, idx);
308 dynar->free(old_object);
311 gras_dynar_set(dynar, idx, object);
315 * gras_dynar_insert_at:
318 * @src: What will be feeded to the dynar
320 * Set the Nth element of a dynar, expanding the dynar if needed, and
321 * moving the previously existing value and all subsequent ones to one
322 * position right in the dynar.
325 gras_dynar_insert_at(gras_dynar_t * const dynar,
327 const void * const src) {
329 __sanity_check_dynar(dynar);
330 __sanity_check_idx(idx);
331 __check_sloppy_inbound_idx(dynar, idx);
334 const unsigned long old_used = dynar->used;
335 const unsigned long new_used = old_used + 1;
337 _gras_dynar_expand(dynar, new_used);
340 const unsigned long nb_shift = old_used - idx;
341 const unsigned long elmsize = dynar->elmsize;
343 const unsigned long offset = nb_shift*elmsize;
345 void * const elm_src = _gras_dynar_elm(dynar, idx);
346 void * const elm_dst = _gras_dynar_elm(dynar, idx+1);
348 memmove(elm_dst, elm_src, offset);
351 _gras_dynar_put_elm(dynar, idx, src);
352 dynar->used = new_used;
357 * gras_dynar_remove_at:
362 * Get the Nth element of a dynar, removing it from the dynar and moving
363 * all subsequent values to one position left in the dynar.
366 gras_dynar_remove_at(gras_dynar_t * const dynar,
368 void * const object) {
370 __sanity_check_dynar(dynar);
371 __sanity_check_idx(idx);
372 __check_inbound_idx(dynar, idx);
375 _gras_dynar_get_elm(object, dynar, idx);
378 const unsigned long old_used = dynar->used;
379 const unsigned long new_used = old_used - 1;
381 const unsigned long nb_shift = old_used-1 - idx;
382 const unsigned long elmsize = dynar->elmsize;
384 const unsigned long offset = nb_shift*elmsize;
386 void * const elm_src = _gras_dynar_elm(dynar, idx+1);
387 void * const elm_dst = _gras_dynar_elm(dynar, idx);
389 memmove(elm_dst, elm_src, offset);
391 dynar->used = new_used;
400 * Add an element at the end of the dynar
403 gras_dynar_push(gras_dynar_t * const dynar,
404 const void * const src) {
405 __sanity_check_dynar(dynar);
406 gras_dynar_insert_at(dynar, dynar->used, src);
414 * Get and remove the last element of the dynar
417 gras_dynar_pop(gras_dynar_t * const dynar,
419 __sanity_check_dynar(dynar);
420 __check_populated_dynar(dynar);
421 DEBUG1("Pop %p",(void*)dynar);
422 gras_dynar_remove_at(dynar, dynar->used-1, dst);
426 * gras_dynar_unshift:
430 * Add an element at the begining of the dynar (rather long, Use
431 * gras_dynar_push() when possible)
434 gras_dynar_unshift(gras_dynar_t * const dynar,
435 const void * const src) {
436 __sanity_check_dynar(dynar);
437 gras_dynar_insert_at(dynar, 0, src);
445 * Get and remove the first element of the dynar (rather long, Use
446 * gras_dynar_pop() when possible)
449 gras_dynar_shift(gras_dynar_t * const dynar,
452 __sanity_check_dynar(dynar);
453 __check_populated_dynar(dynar);
454 gras_dynar_remove_at(dynar, 0, dst);
462 * Apply a function to each member of a dynar (this function may change the
463 * value of the element itself, but should not mess with the dynar).
466 gras_dynar_map(const gras_dynar_t * const dynar,
467 void_f_pvoid_t * const operator) {
469 __sanity_check_dynar(dynar);
473 const unsigned long used = dynar->used;
476 for (i = 0; i < used; i++) {
477 _gras_dynar_get_elm(elm, dynar, i);
486 * Put the cursor at the begining of the dynar. (actually, one step before
487 * the begining, so that you can iterate over the dynar with a for loop).
489 * Dynar cursor are as dumb as possible. If you insert or remove elements
490 * from the dynar between the creation and end, you'll fuck up your
495 gras_dynar_cursor_first(const gras_dynar_t * const dynar,
496 int * const cursor) {
498 __sanity_check_dynar(dynar);
499 DEBUG1("Set cursor on %p to the first position",(void*)dynar);
504 * gras_dynar_cursor_step:
506 * Move the cursor to the next value (and return true), or return false.
509 gras_dynar_cursor_step(const gras_dynar_t * const dynar,
510 int * const cursor) {
512 __sanity_check_dynar(dynar);
517 * gras_dynar_cursor_get:
519 * Get the current value of the cursor
522 gras_dynar_cursor_get(const gras_dynar_t * const dynar,
526 __sanity_check_dynar(dynar);
529 const int idx = *cursor;
531 if (idx >= dynar->used) {
532 DEBUG1("Cursor on %p already on last elem",(void*)dynar);
535 DEBUG2("Cash out cursor on %p at %d",(void*)dynar,idx);
537 _gras_dynar_get_elm(dst, dynar, idx);
544 * gras_dynar_cursor_rm:
548 * Remove (free) the entry pointed by the cursor, for use in the middle of a foreach
550 void gras_dynar_cursor_rm(gras_dynar_t * dynar,
551 int * const cursor) {
554 if (dynar->elmsize > sizeof(void*)) {
555 DEBUG0("Elements too big to fit into a pointer");
557 dst=gras_malloc(dynar->elmsize);
558 gras_dynar_remove_at(dynar,(*cursor)--,dst);
562 DEBUG0("Ok, we dont care about the element when no free function");
563 gras_dynar_remove_at(dynar,(*cursor)--,NULL);
567 gras_dynar_remove_at(dynar,(*cursor)--,&dst);