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);
74 memcpy(new_data, old_data, used_length);
75 _gras_clear_mem(old_data, old_length);
79 _gras_clear_mem(new_data + used_length, new_length - used_length);
81 dynar->size = new_size;
82 dynar->data = new_data;
90 _gras_dynar_elm(const gras_dynar_t * const dynar,
92 char * const data = dynar->data;
93 const size_t elmsize = dynar->elmsize;
95 return data + idx*elmsize;
100 _gras_dynar_get_elm(void * const dst,
101 const gras_dynar_t * const dynar,
103 void * const elm = _gras_dynar_elm(dynar, idx);
104 const size_t elmsize = dynar->elmsize;
106 memcpy(dst, elm, elmsize);
111 _gras_dynar_put_elm(const gras_dynar_t * const dynar,
113 const void * const src) {
114 void * const elm = _gras_dynar_elm(dynar, idx);
115 const size_t elmsize = dynar->elmsize;
117 memcpy(elm, src, elmsize);
122 * @whereto: pointer to where the dynar should be created
123 * @elm_size: size of each element in the dynar
124 * @free_func: function to call each time we want to get rid of an element (or NULL if nothing to do).
125 * @Returns: malloc_error or no_error
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(gras_dynar_t ** const p_dynar,
133 const size_t elmsize,
134 void_f_pvoid_t * const free_func) {
135 gras_error_t errcode = no_error;
136 gras_dynar_t *dynar = NULL;
138 if (!(dynar = gras_new0(gras_dynar_t,1)))
143 dynar->elmsize = elmsize;
145 dynar->free = free_func;
153 * gras_dynar_free_container:
154 * @dynar: poor victim
156 * kilkil a dynar BUT NOT its content. Ie, the array is freed, but not what
157 * its contain points to.
160 gras_dynar_free_container(gras_dynar_t * const dynar) {
164 _gras_clear_mem(dynar->data, dynar->size);
165 gras_free(dynar->data);
168 _gras_clear_mem(dynar, sizeof(gras_dynar_t));
176 * @dynar: who to squeeze
178 * Frees the content and set the size to 0
181 gras_dynar_reset(gras_dynar_t * const dynar) {
183 __sanity_check_dynar(dynar);
185 DEBUG1("Reset the dynar %p",(void*)dynar);
187 gras_dynar_map(dynar, dynar->free);
191 _gras_clear_mem(dynar->data, dynar->size);
192 gras_free(dynar->data);
202 * @dynar: poor victim
204 * kilkil a dynar and its content
208 gras_dynar_free(gras_dynar_t * const dynar) {
210 gras_dynar_reset(dynar);
211 gras_dynar_free_container(dynar);
217 * @dynar: the dynar we want to mesure
219 * Returns the count of elements in a dynar
222 gras_dynar_length(const gras_dynar_t * const dynar) {
223 return (dynar ? (unsigned long) dynar->used : (unsigned long)0);
228 * @dynar: information dealer
229 * @idx: index of the slot we want to retrive
230 * @dst: where to pu the result to.
232 * Retrieve the Nth element of a dynar. Warning, the returned value is the actual content of
233 * the dynar. Make a copy before fooling with it.
236 gras_dynar_get(const gras_dynar_t * const dynar,
240 __sanity_check_dynar(dynar);
241 __sanity_check_idx(idx);
242 __check_inbound_idx(dynar, idx);
244 _gras_dynar_get_elm(dst, dynar, idx);
251 * @src: What will be feeded to the dynar
252 * @Returns: malloc_error or no_error
254 * Set the Nth element of a dynar, expanding the dynar if needed, BUT NOT freeing
255 * the previous value at this position. If you want to free the previous content,
256 * use gras_dynar_remplace().
259 gras_dynar_set(gras_dynar_t * const dynar,
261 const void * const src) {
262 gras_error_t errcode = no_error;
264 __sanity_check_dynar(dynar);
265 __sanity_check_idx(idx);
267 TRY(_gras_dynar_expand(dynar, idx+1));
269 if (idx >= dynar->used) {
273 _gras_dynar_put_elm(dynar, idx, src);
279 * gras_dynar_remplace:
283 * @Returns: malloc_error or no_error
285 * Set the Nth element of a dynar, expanding the dynar if needed, AND DO
286 * free the previous value at this position. If you don't want to free the
287 * previous content, use gras_dynar_set().
290 gras_dynar_remplace(gras_dynar_t * const dynar,
292 const void * const object) {
293 gras_error_t errcode = no_error;
295 __sanity_check_dynar(dynar);
296 __sanity_check_idx(idx);
298 if (idx < dynar->used && dynar->free) {
299 void * const old_object = _gras_dynar_elm(dynar, idx);
301 dynar->free(old_object);
304 errcode = gras_dynar_set(dynar, idx, object);
310 * gras_dynar_insert_at:
313 * @src: What will be feeded to the dynar
314 * @Returns: malloc_error or no_error
316 * Set the Nth element of a dynar, expanding the dynar if needed, and
317 * moving the previously existing value and all subsequent ones to one
318 * position right in the dynar.
321 gras_dynar_insert_at(gras_dynar_t * const dynar,
323 const void * const src) {
324 gras_error_t errcode = no_error;
326 __sanity_check_dynar(dynar);
327 __sanity_check_idx(idx);
328 __check_sloppy_inbound_idx(dynar, idx);
331 const size_t old_used = dynar->used;
332 const size_t new_used = old_used + 1;
334 TRY(_gras_dynar_expand(dynar, new_used));
337 const size_t nb_shift = old_used - idx;
338 const size_t elmsize = dynar->elmsize;
340 const size_t offset = nb_shift*elmsize;
342 void * const elm_src = _gras_dynar_elm(dynar, idx);
343 void * const elm_dst = _gras_dynar_elm(dynar, idx+1);
345 memmove(elm_dst, elm_src, offset);
348 _gras_dynar_put_elm(dynar, idx, src);
349 dynar->used = new_used;
356 * gras_dynar_remove_at:
361 * Get the Nth element of a dynar, removing it from the dynar and moving
362 * all subsequent values to one position left in the dynar.
365 gras_dynar_remove_at(gras_dynar_t * const dynar,
367 void * const object) {
369 __sanity_check_dynar(dynar);
370 __sanity_check_idx(idx);
371 __check_inbound_idx(dynar, idx);
374 _gras_dynar_get_elm(object, dynar, idx);
377 const size_t old_used = dynar->used;
378 const size_t new_used = old_used - 1;
380 const size_t nb_shift = old_used-1 - idx;
381 const size_t elmsize = dynar->elmsize;
383 const size_t offset = nb_shift*elmsize;
385 void * const elm_src = _gras_dynar_elm(dynar, idx+1);
386 void * const elm_dst = _gras_dynar_elm(dynar, idx);
388 memmove(elm_dst, elm_src, offset);
390 dynar->used = new_used;
398 * @Returns: malloc_error or no_error
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 return 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:
429 * @Returns: malloc_error or no_error
431 * Add an element at the begining of the dynar (rather long, Use
432 * gras_dynar_push() when possible)
435 gras_dynar_unshift(gras_dynar_t * const dynar,
436 const void * const src) {
437 __sanity_check_dynar(dynar);
438 return gras_dynar_insert_at(dynar, 0, src);
446 * Get and remove the first element of the dynar (rather long, Use
447 * gras_dynar_pop() when possible)
450 gras_dynar_shift(gras_dynar_t * const dynar,
453 __sanity_check_dynar(dynar);
454 __check_populated_dynar(dynar);
455 gras_dynar_remove_at(dynar, 0, dst);
463 * Apply a function to each member of a dynar (this function may change the
464 * value of the element itself, but should not mess with the dynar).
467 gras_dynar_map(const gras_dynar_t * const dynar,
468 void_f_pvoid_t * const operator) {
470 __sanity_check_dynar(dynar);
474 const size_t used = dynar->used;
477 for (i = 0; i < used; i++) {
478 _gras_dynar_get_elm(elm, dynar, i);
487 * Put the cursor at the begining of the dynar. (actually, one step before
488 * the begining, so that you can iterate over the dynar with a for loop).
490 * Dynar cursor are as dumb as possible. If you insert or remove elements
491 * from the dynar between the creation and end, you'll fuck up your
496 gras_dynar_cursor_first(const gras_dynar_t * const dynar,
497 int * const cursor) {
499 __sanity_check_dynar(dynar);
500 DEBUG1("Set cursor on %p to the first position",(void*)dynar);
505 * gras_dynar_cursor_step:
507 * Move the cursor to the next value (and return true), or return false.
510 gras_dynar_cursor_step(const gras_dynar_t * const dynar,
511 int * const cursor) {
513 __sanity_check_dynar(dynar);
518 * gras_dynar_cursor_get:
520 * Get the current value of the cursor
523 gras_dynar_cursor_get(const gras_dynar_t * const dynar,
527 __sanity_check_dynar(dynar);
530 const int idx = *cursor;
532 if (idx >= dynar->used) {
533 DEBUG1("Cursor on %p already on last elem",(void*)dynar);
536 DEBUG2("Cash out cursor on %p at %d",(void*)dynar,idx);
538 _gras_dynar_get_elm(dst, dynar, idx);
545 * gras_dynar_cursor_rm:
549 * Remove (free) the entry pointed by the cursor, for use in the middle of a foreach
551 void gras_dynar_cursor_rm(gras_dynar_t * dynar,
552 int * const cursor) {
555 if (dynar->elmsize > sizeof(void*)) {
556 DEBUG0("Elements too big to fit into a pointer");
558 dst=gras_malloc(dynar->elmsize);
559 gras_dynar_remove_at(dynar,(*cursor)--,dst);
563 DEBUG0("Ok, we dont care about the element when no free function");
564 gras_dynar_remove_at(dynar,(*cursor)--,NULL);
568 gras_dynar_remove_at(dynar,(*cursor)--,&dst);