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);
222 * @dynar: information dealer
223 * @idx: index of the slot we want to retrive
224 * @dst: where to pu the result to.
226 * Retrieve the Nth element of a dynar. Warning, the returned value is the actual content of
227 * the dynar. Make a copy before fooling with it.
230 gras_dynar_get(const gras_dynar_t * const dynar,
234 __sanity_check_dynar(dynar);
235 __sanity_check_idx(idx);
236 __check_inbound_idx(dynar, idx);
238 _gras_dynar_get_elm(dst, dynar, idx);
245 * @src: What will be feeded to the dynar
247 * Set the Nth element of a dynar, expanding the dynar if needed, BUT NOT freeing
248 * the previous value at this position. If you want to free the previous content,
249 * use gras_dynar_remplace().
252 gras_dynar_set(gras_dynar_t * const dynar,
254 const void * const src) {
256 __sanity_check_dynar(dynar);
257 __sanity_check_idx(idx);
259 _gras_dynar_expand(dynar, idx+1);
261 if (idx >= dynar->used) {
265 _gras_dynar_put_elm(dynar, idx, src);
269 * gras_dynar_remplace:
274 * Set the Nth element of a dynar, expanding the dynar if needed, AND DO
275 * free the previous value at this position. If you don't want to free the
276 * previous content, use gras_dynar_set().
279 gras_dynar_remplace(gras_dynar_t * const dynar,
281 const void * const object) {
283 __sanity_check_dynar(dynar);
284 __sanity_check_idx(idx);
286 if (idx < dynar->used && dynar->free) {
287 void * const old_object = _gras_dynar_elm(dynar, idx);
289 dynar->free(old_object);
292 gras_dynar_set(dynar, idx, object);
296 * gras_dynar_insert_at:
299 * @src: What will be feeded to the dynar
301 * Set the Nth element of a dynar, expanding the dynar if needed, and
302 * moving the previously existing value and all subsequent ones to one
303 * position right in the dynar.
306 gras_dynar_insert_at(gras_dynar_t * const dynar,
308 const void * const src) {
310 __sanity_check_dynar(dynar);
311 __sanity_check_idx(idx);
312 __check_sloppy_inbound_idx(dynar, idx);
315 const unsigned long old_used = dynar->used;
316 const unsigned long new_used = old_used + 1;
318 _gras_dynar_expand(dynar, new_used);
321 const unsigned long nb_shift = old_used - idx;
322 const unsigned long elmsize = dynar->elmsize;
324 const unsigned long offset = nb_shift*elmsize;
326 void * const elm_src = _gras_dynar_elm(dynar, idx);
327 void * const elm_dst = _gras_dynar_elm(dynar, idx+1);
329 memmove(elm_dst, elm_src, offset);
332 _gras_dynar_put_elm(dynar, idx, src);
333 dynar->used = new_used;
338 * gras_dynar_remove_at:
343 * Get the Nth element of a dynar, removing it from the dynar and moving
344 * all subsequent values to one position left in the dynar.
347 gras_dynar_remove_at(gras_dynar_t * const dynar,
349 void * const object) {
351 __sanity_check_dynar(dynar);
352 __sanity_check_idx(idx);
353 __check_inbound_idx(dynar, idx);
356 _gras_dynar_get_elm(object, dynar, idx);
359 const unsigned long old_used = dynar->used;
360 const unsigned long new_used = old_used - 1;
362 const unsigned long nb_shift = old_used-1 - idx;
363 const unsigned long elmsize = dynar->elmsize;
365 const unsigned long offset = nb_shift*elmsize;
367 void * const elm_src = _gras_dynar_elm(dynar, idx+1);
368 void * const elm_dst = _gras_dynar_elm(dynar, idx);
370 memmove(elm_dst, elm_src, offset);
372 dynar->used = new_used;
381 * Add an element at the end of the dynar
384 gras_dynar_push(gras_dynar_t * const dynar,
385 const void * const src) {
386 __sanity_check_dynar(dynar);
387 gras_dynar_insert_at(dynar, dynar->used, src);
395 * Get and remove the last element of the dynar
398 gras_dynar_pop(gras_dynar_t * const dynar,
400 __sanity_check_dynar(dynar);
401 __check_populated_dynar(dynar);
402 DEBUG1("Pop %p",(void*)dynar);
403 gras_dynar_remove_at(dynar, dynar->used-1, dst);
407 * gras_dynar_unshift:
411 * Add an element at the begining of the dynar (rather long, Use
412 * gras_dynar_push() when possible)
415 gras_dynar_unshift(gras_dynar_t * const dynar,
416 const void * const src) {
417 __sanity_check_dynar(dynar);
418 gras_dynar_insert_at(dynar, 0, src);
426 * Get and remove the first element of the dynar (rather long, Use
427 * gras_dynar_pop() when possible)
430 gras_dynar_shift(gras_dynar_t * const dynar,
433 __sanity_check_dynar(dynar);
434 __check_populated_dynar(dynar);
435 gras_dynar_remove_at(dynar, 0, dst);
443 * Apply a function to each member of a dynar (this function may change the
444 * value of the element itself, but should not mess with the dynar).
447 gras_dynar_map(const gras_dynar_t * const dynar,
448 void_f_pvoid_t * const operator) {
450 __sanity_check_dynar(dynar);
454 const unsigned long used = dynar->used;
457 for (i = 0; i < used; i++) {
458 _gras_dynar_get_elm(elm, dynar, i);
467 * Put the cursor at the begining of the dynar. (actually, one step before
468 * the begining, so that you can iterate over the dynar with a for loop).
470 * Dynar cursor are as dumb as possible. If you insert or remove elements
471 * from the dynar between the creation and end, you'll fuck up your
476 gras_dynar_cursor_first(const gras_dynar_t * const dynar,
477 int * const cursor) {
479 __sanity_check_dynar(dynar);
480 DEBUG1("Set cursor on %p to the first position",(void*)dynar);
485 * gras_dynar_cursor_step:
487 * Move the cursor to the next value (and return true), or return false.
490 gras_dynar_cursor_step(const gras_dynar_t * const dynar,
491 int * const cursor) {
493 __sanity_check_dynar(dynar);
498 * gras_dynar_cursor_get:
500 * Get the current value of the cursor
503 gras_dynar_cursor_get(const gras_dynar_t * const dynar,
507 __sanity_check_dynar(dynar);
510 const int idx = *cursor;
512 if (idx >= dynar->used) {
513 DEBUG1("Cursor on %p already on last elem",(void*)dynar);
516 DEBUG2("Cash out cursor on %p at %d",(void*)dynar,idx);
518 _gras_dynar_get_elm(dst, dynar, idx);
525 * gras_dynar_cursor_rm:
529 * Remove (free) the entry pointed by the cursor, for use in the middle of a foreach
531 void gras_dynar_cursor_rm(gras_dynar_t * dynar,
532 int * const cursor) {
535 if (dynar->elmsize > sizeof(void*)) {
536 DEBUG0("Elements too big to fit into a pointer");
538 dst=gras_malloc(dynar->elmsize);
539 gras_dynar_remove_at(dynar,(*cursor)--,dst);
543 DEBUG0("Ok, we dont care about the element when no free function");
544 gras_dynar_remove_at(dynar,(*cursor)--,NULL);
548 gras_dynar_remove_at(dynar,(*cursor)--,&dst);