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. */
11 #include "gras_private.h"
12 #include <sys/types.h>
14 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(dynar,gros,"Dynamic arrays");
24 #define __sanity_check_dynar(dynar) \
27 #define __sanity_check_idx(idx) \
28 gras_assert1(idx >= 0, \
29 "dynar idx(=%d) < 0", \
31 #define __check_inbound_idx(dynar, idx) \
32 gras_assert2(idx < dynar->used, \
33 "dynar is not that long. You asked %d, but it's only %lu long", \
34 (int) (idx), (unsigned long) dynar->used)
35 #define __check_sloppy_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_populated_dynar(dynar) \
40 gras_assert1(dynar->used, \
41 "dynar %p contains nothing",(void*)dynar)
44 void _gras_clear_mem(void * const ptr,
45 const size_t length) {
46 memset(ptr, 0, length);
51 _gras_dynar_expand(gras_dynar_t * const dynar,
53 gras_error_t errcode = no_error;
54 const size_t old_size = dynar->size;
57 char * const old_data = dynar->data;
59 const size_t elmsize = dynar->elmsize;
60 const size_t old_length = old_size*elmsize;
62 const size_t used = dynar->used;
63 const size_t used_length = used*elmsize;
65 const size_t new_size = nb > (2*(old_size+1)) ? nb : (2*(old_size+1));
66 const size_t new_length = new_size*elmsize;
67 char * const new_data = gras_malloc0(elmsize*new_size);
69 DEBUG3("expend %p from %lu to %d elements", (void*)dynar, (unsigned long)old_size, nb);
72 memcpy(new_data, old_data, used_length);
73 _gras_clear_mem(old_data, old_length);
77 _gras_clear_mem(new_data + used_length, new_length - used_length);
79 dynar->size = new_size;
80 dynar->data = new_data;
88 _gras_dynar_elm(const gras_dynar_t * const dynar,
90 char * const data = dynar->data;
91 const size_t elmsize = dynar->elmsize;
93 return data + idx*elmsize;
98 _gras_dynar_get_elm(void * const dst,
99 const gras_dynar_t * const dynar,
101 void * const elm = _gras_dynar_elm(dynar, idx);
102 const size_t elmsize = dynar->elmsize;
104 memcpy(dst, elm, elmsize);
109 _gras_dynar_put_elm(const gras_dynar_t * const dynar,
111 const void * const src) {
112 void * const elm = _gras_dynar_elm(dynar, idx);
113 const size_t elmsize = dynar->elmsize;
115 memcpy(elm, src, elmsize);
120 * @whereto: pointer to where the dynar should be created
121 * @elm_size: size of each element in the dynar
122 * @free_func: function to call each time we want to get rid of an element (or NULL if nothing to do).
124 * Creates a new dynar. If a free_func is provided, the elements have to be
125 * pointer of pointer. That is to say that dynars can contain either base
126 * types (int, char, double, etc) or pointer of pointers (struct **).
129 gras_dynar_new(gras_dynar_t ** const p_dynar,
130 const size_t elmsize,
131 void_f_pvoid_t * const free_func) {
133 gras_dynar_t *dynar = gras_new0(gras_dynar_t,1);
137 dynar->elmsize = elmsize;
139 dynar->free = free_func;
145 * gras_dynar_free_container:
146 * @dynar: poor victim
148 * kilkil a dynar BUT NOT its content. Ie, the array is freed, but not what
149 * its contain points to.
152 gras_dynar_free_container(gras_dynar_t * const dynar) {
156 _gras_clear_mem(dynar->data, dynar->size);
157 gras_free(dynar->data);
160 _gras_clear_mem(dynar, sizeof(gras_dynar_t));
168 * @dynar: who to squeeze
170 * Frees the content and set the size to 0
173 gras_dynar_reset(gras_dynar_t * const dynar) {
175 __sanity_check_dynar(dynar);
177 DEBUG1("Reset the dynar %p",(void*)dynar);
179 gras_dynar_map(dynar, dynar->free);
183 _gras_clear_mem(dynar->data, dynar->size);
184 gras_free(dynar->data);
194 * @dynar: poor victim
196 * kilkil a dynar and its content
200 gras_dynar_free(gras_dynar_t * const dynar) {
202 gras_dynar_reset(dynar);
203 gras_dynar_free_container(dynar);
209 * @dynar: the dynar we want to mesure
211 * Returns the count of elements in a dynar
214 gras_dynar_length(const gras_dynar_t * const dynar) {
215 return (dynar ? (unsigned long) dynar->used : (unsigned long)0);
220 * @dynar: information dealer
221 * @idx: index of the slot we want to retrive
222 * @dst: where to pu the result to.
224 * Retrieve the Nth element of a dynar. Warning, the returned value is the actual content of
225 * the dynar. Make a copy before fooling with it.
228 gras_dynar_get(const gras_dynar_t * const dynar,
232 __sanity_check_dynar(dynar);
233 __sanity_check_idx(idx);
234 __check_inbound_idx(dynar, idx);
236 _gras_dynar_get_elm(dst, dynar, idx);
243 * @src: What will be feeded to the dynar
245 * Set the Nth element of a dynar, expanding the dynar if needed, BUT NOT freeing
246 * the previous value at this position. If you want to free the previous content,
247 * use gras_dynar_remplace().
250 gras_dynar_set(gras_dynar_t * const dynar,
252 const void * const src) {
254 __sanity_check_dynar(dynar);
255 __sanity_check_idx(idx);
257 _gras_dynar_expand(dynar, idx+1);
259 if (idx >= dynar->used) {
263 _gras_dynar_put_elm(dynar, idx, src);
267 * gras_dynar_remplace:
272 * Set the Nth element of a dynar, expanding the dynar if needed, AND DO
273 * free the previous value at this position. If you don't want to free the
274 * previous content, use gras_dynar_set().
277 gras_dynar_remplace(gras_dynar_t * const dynar,
279 const void * const object) {
281 __sanity_check_dynar(dynar);
282 __sanity_check_idx(idx);
284 if (idx < dynar->used && dynar->free) {
285 void * const old_object = _gras_dynar_elm(dynar, idx);
287 dynar->free(old_object);
290 gras_dynar_set(dynar, idx, object);
294 * gras_dynar_insert_at:
297 * @src: What will be feeded to the dynar
299 * Set the Nth element of a dynar, expanding the dynar if needed, and
300 * moving the previously existing value and all subsequent ones to one
301 * position right in the dynar.
304 gras_dynar_insert_at(gras_dynar_t * const dynar,
306 const void * const src) {
308 __sanity_check_dynar(dynar);
309 __sanity_check_idx(idx);
310 __check_sloppy_inbound_idx(dynar, idx);
313 const size_t old_used = dynar->used;
314 const size_t new_used = old_used + 1;
316 _gras_dynar_expand(dynar, new_used);
319 const size_t nb_shift = old_used - idx;
320 const size_t elmsize = dynar->elmsize;
322 const size_t offset = nb_shift*elmsize;
324 void * const elm_src = _gras_dynar_elm(dynar, idx);
325 void * const elm_dst = _gras_dynar_elm(dynar, idx+1);
327 memmove(elm_dst, elm_src, offset);
330 _gras_dynar_put_elm(dynar, idx, src);
331 dynar->used = new_used;
336 * gras_dynar_remove_at:
341 * Get the Nth element of a dynar, removing it from the dynar and moving
342 * all subsequent values to one position left in the dynar.
345 gras_dynar_remove_at(gras_dynar_t * const dynar,
347 void * const object) {
349 __sanity_check_dynar(dynar);
350 __sanity_check_idx(idx);
351 __check_inbound_idx(dynar, idx);
354 _gras_dynar_get_elm(object, dynar, idx);
357 const size_t old_used = dynar->used;
358 const size_t new_used = old_used - 1;
360 const size_t nb_shift = old_used-1 - idx;
361 const size_t elmsize = dynar->elmsize;
363 const size_t offset = nb_shift*elmsize;
365 void * const elm_src = _gras_dynar_elm(dynar, idx+1);
366 void * const elm_dst = _gras_dynar_elm(dynar, idx);
368 memmove(elm_dst, elm_src, offset);
370 dynar->used = new_used;
379 * Add an element at the end of the dynar
382 gras_dynar_push(gras_dynar_t * const dynar,
383 const void * const src) {
384 __sanity_check_dynar(dynar);
385 gras_dynar_insert_at(dynar, dynar->used, src);
393 * Get and remove the last element of the dynar
396 gras_dynar_pop(gras_dynar_t * const dynar,
398 __sanity_check_dynar(dynar);
399 __check_populated_dynar(dynar);
400 DEBUG1("Pop %p",(void*)dynar);
401 gras_dynar_remove_at(dynar, dynar->used-1, dst);
405 * gras_dynar_unshift:
409 * Add an element at the begining of the dynar (rather long, Use
410 * gras_dynar_push() when possible)
413 gras_dynar_unshift(gras_dynar_t * const dynar,
414 const void * const src) {
415 __sanity_check_dynar(dynar);
416 gras_dynar_insert_at(dynar, 0, src);
424 * Get and remove the first element of the dynar (rather long, Use
425 * gras_dynar_pop() when possible)
428 gras_dynar_shift(gras_dynar_t * const dynar,
431 __sanity_check_dynar(dynar);
432 __check_populated_dynar(dynar);
433 gras_dynar_remove_at(dynar, 0, dst);
441 * Apply a function to each member of a dynar (this function may change the
442 * value of the element itself, but should not mess with the dynar).
445 gras_dynar_map(const gras_dynar_t * const dynar,
446 void_f_pvoid_t * const operator) {
448 __sanity_check_dynar(dynar);
452 const size_t used = dynar->used;
455 for (i = 0; i < used; i++) {
456 _gras_dynar_get_elm(elm, dynar, i);
465 * Put the cursor at the begining of the dynar. (actually, one step before
466 * the begining, so that you can iterate over the dynar with a for loop).
468 * Dynar cursor are as dumb as possible. If you insert or remove elements
469 * from the dynar between the creation and end, you'll fuck up your
474 gras_dynar_cursor_first(const gras_dynar_t * const dynar,
475 int * const cursor) {
477 __sanity_check_dynar(dynar);
478 DEBUG1("Set cursor on %p to the first position",(void*)dynar);
483 * gras_dynar_cursor_step:
485 * Move the cursor to the next value (and return true), or return false.
488 gras_dynar_cursor_step(const gras_dynar_t * const dynar,
489 int * const cursor) {
491 __sanity_check_dynar(dynar);
496 * gras_dynar_cursor_get:
498 * Get the current value of the cursor
501 gras_dynar_cursor_get(const gras_dynar_t * const dynar,
505 __sanity_check_dynar(dynar);
508 const int idx = *cursor;
510 if (idx >= dynar->used) {
511 DEBUG1("Cursor on %p already on last elem",(void*)dynar);
514 DEBUG2("Cash out cursor on %p at %d",(void*)dynar,idx);
516 _gras_dynar_get_elm(dst, dynar, idx);
523 * gras_dynar_cursor_rm:
527 * Remove (free) the entry pointed by the cursor, for use in the middle of a foreach
529 void gras_dynar_cursor_rm(gras_dynar_t * dynar,
530 int * const cursor) {
533 if (dynar->elmsize > sizeof(void*)) {
534 DEBUG0("Elements too big to fit into a pointer");
536 dst=gras_malloc(dynar->elmsize);
537 gras_dynar_remove_at(dynar,(*cursor)--,dst);
541 DEBUG0("Ok, we dont care about the element when no free function");
542 gras_dynar_remove_at(dynar,(*cursor)--,NULL);
546 gras_dynar_remove_at(dynar,(*cursor)--,&dst);