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 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(dynar,xbt,"Dynamic arrays");
20 typedef struct xbt_dynar_s {
23 unsigned long elmsize;
25 void_f_pvoid_t *free_f;
28 #define __sanity_check_dynar(dynar) \
31 #define __sanity_check_idx(idx) \
32 xbt_assert1(idx >= 0, \
33 "dynar idx(=%d) < 0", \
35 #define __check_inbound_idx(dynar, idx) \
36 xbt_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 xbt_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 xbt_assert1(dynar->used, \
45 "dynar %p contains nothing",(void*)dynar)
48 void _xbt_clear_mem(void * const ptr,
49 const unsigned long length) {
50 memset(ptr, 0, length);
55 _xbt_dynar_expand(xbt_dynar_t const dynar,
57 xbt_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 = xbt_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 _xbt_clear_mem(old_data, old_length);
81 _xbt_clear_mem(new_data + used_length, new_length - used_length);
83 dynar->size = new_size;
84 dynar->data = new_data;
92 _xbt_dynar_elm(const xbt_dynar_t 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 _xbt_dynar_get_elm(void * const dst,
103 const xbt_dynar_t dynar,
104 const unsigned long idx) {
105 void * const elm = _xbt_dynar_elm(dynar, idx);
106 const unsigned long elmsize = dynar->elmsize;
108 memcpy(dst, elm, elmsize);
113 _xbt_dynar_put_elm(const xbt_dynar_t dynar,
114 const unsigned long idx,
115 const void * const src) {
116 void * const elm = _xbt_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 xbt_dynar_new(const unsigned long elmsize,
133 void_f_pvoid_t * const free_f) {
135 xbt_dynar_t dynar = xbt_new0(s_xbt_dynar_t,1);
139 dynar->elmsize = elmsize;
141 dynar->free_f = free_f;
147 * xbt_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 xbt_dynar_free_container(xbt_dynar_t *dynar) {
155 if (dynar && *dynar) {
157 if ((*dynar)->data) {
158 _xbt_clear_mem((*dynar)->data, (*dynar)->size);
159 xbt_free((*dynar)->data);
162 _xbt_clear_mem(*dynar, sizeof(s_xbt_dynar_t));
171 * @dynar: who to squeeze
173 * Frees the content and set the size to 0
176 xbt_dynar_reset(xbt_dynar_t const dynar) {
178 __sanity_check_dynar(dynar);
180 DEBUG1("Reset the dynar %p",(void*)dynar);
182 xbt_dynar_map(dynar, dynar->free_f);
186 xbt_free(dynar->data);
195 * @dynar: poor victim
197 * kilkil a dynar and its content
201 xbt_dynar_free(xbt_dynar_t * dynar) {
202 if (dynar && *dynar) {
203 xbt_dynar_reset(*dynar);
204 xbt_dynar_free_container(dynar);
210 * @dynar: the dynar we want to mesure
212 * Returns the count of elements in a dynar
215 xbt_dynar_length(const xbt_dynar_t dynar) {
216 return (dynar ? (unsigned long) dynar->used : (unsigned long)0);
221 * @dynar: information dealer
222 * @idx: index of the slot we want to retrive
223 * @dst: where to pu the result to.
225 * Retrieve a copy of the Nth element of a dynar.
228 xbt_dynar_get_cpy(const xbt_dynar_t dynar,
232 __sanity_check_dynar(dynar);
233 __sanity_check_idx(idx);
234 __check_inbound_idx(dynar, idx);
236 _xbt_dynar_get_elm(dst, dynar, idx);
241 * @dynar: information dealer
242 * @idx: index of the slot we want to retrive
243 * @dst: where to pu the result to.
245 * Retrieve the Nth element of a dynar. Warning, the returned value is the actual content of
246 * the dynar. Make a copy before fooling with it.
249 xbt_dynar_get_ptr(const xbt_dynar_t dynar,
252 __sanity_check_dynar(dynar);
253 __sanity_check_idx(idx);
254 __check_inbound_idx(dynar, idx);
256 return _xbt_dynar_elm(dynar, idx);
263 * @src: What will be feeded to the dynar
265 * Set the Nth element of a dynar, expanding the dynar if needed, BUT NOT freeing
266 * the previous value at this position. If you want to free the previous content,
267 * use xbt_dynar_replace().
270 xbt_dynar_set(xbt_dynar_t dynar,
272 const void * const src) {
274 __sanity_check_dynar(dynar);
275 __sanity_check_idx(idx);
277 _xbt_dynar_expand(dynar, idx+1);
279 if (idx >= dynar->used) {
283 _xbt_dynar_put_elm(dynar, idx, src);
292 * Set the Nth element of a dynar, expanding the dynar if needed, AND DO
293 * free the previous value at this position. If you don't want to free the
294 * previous content, use xbt_dynar_set().
297 xbt_dynar_replace(xbt_dynar_t dynar,
299 const void * const object) {
301 __sanity_check_dynar(dynar);
302 __sanity_check_idx(idx);
304 if (idx < dynar->used && dynar->free_f) {
305 void * const old_object = _xbt_dynar_elm(dynar, idx);
307 dynar->free_f(old_object);
310 xbt_dynar_set(dynar, idx, object);
314 * xbt_dynar_insert_at_ptr:
316 * Make room for a new element in the dynar, and return a pointer to
317 * its position. You can then use regular affectation to set its value
318 * instead of relying on the slow memcpy
321 xbt_dynar_insert_at_ptr(xbt_dynar_t const dynar,
324 __sanity_check_dynar(dynar);
325 __sanity_check_idx(idx);
326 __check_sloppy_inbound_idx(dynar, idx);
329 const unsigned long old_used = dynar->used;
330 const unsigned long new_used = old_used + 1;
332 _xbt_dynar_expand(dynar, new_used);
335 const unsigned long nb_shift = old_used - idx;
338 memmove(_xbt_dynar_elm(dynar, idx+1),
339 _xbt_dynar_elm(dynar, idx),
340 nb_shift * dynar->elmsize);
343 dynar->used = new_used;
344 return _xbt_dynar_elm(dynar,idx);
349 * xbt_dynar_insert_at:
352 * @src: What will be feeded to the dynar
354 * Set the Nth element of a dynar, expanding the dynar if needed, and
355 * moving the previously existing value and all subsequent ones to one
356 * position right in the dynar.
359 xbt_dynar_insert_at(xbt_dynar_t const dynar,
361 const void * const src) {
363 /* checks done in xbt_dynar_insert_at_ptr */
364 memcpy(xbt_dynar_insert_at_ptr(dynar,idx),
370 * xbt_dynar_remove_at:
375 * Get the Nth element of a dynar, removing it from the dynar and moving
376 * all subsequent values to one position left in the dynar.
379 xbt_dynar_remove_at(xbt_dynar_t const dynar,
381 void * const object) {
383 __sanity_check_dynar(dynar);
384 __sanity_check_idx(idx);
385 __check_inbound_idx(dynar, idx);
388 _xbt_dynar_get_elm(object, dynar, idx);
391 const unsigned long old_used = dynar->used;
392 const unsigned long new_used = old_used - 1;
394 const unsigned long nb_shift = old_used-1 - idx;
395 const unsigned long elmsize = dynar->elmsize;
397 const unsigned long offset = nb_shift*elmsize;
399 void * const elm_src = _xbt_dynar_elm(dynar, idx+1);
400 void * const elm_dst = _xbt_dynar_elm(dynar, idx);
402 memmove(elm_dst, elm_src, offset);
404 dynar->used = new_used;
409 * xbt_dynar_push_ptr:
411 * Make room at the end of the dynar for a new element, and return a pointer to it
414 xbt_dynar_push_ptr(xbt_dynar_t const dynar) {
415 return xbt_dynar_insert_at_ptr(dynar, dynar->used);
423 * Add an element at the end of the dynar
426 xbt_dynar_push(xbt_dynar_t const dynar,
427 const void * const src) {
428 /* sanity checks done by insert_at */
429 xbt_dynar_insert_at(dynar, dynar->used, src);
437 * Make the last element of the dynar as unused and return a pointer to it.
440 xbt_dynar_pop_ptr(xbt_dynar_t const dynar) {
442 __check_populated_dynar(dynar);
443 DEBUG1("Pop %p",(void*)dynar);
445 return _xbt_dynar_elm(dynar,dynar->used);
453 * Get and remove the last element of the dynar
456 xbt_dynar_pop(xbt_dynar_t const dynar,
459 /* sanity checks done by remove_at */
460 DEBUG1("Pop %p",(void*)dynar);
461 xbt_dynar_remove_at(dynar, dynar->used-1, dst);
469 * Add an element at the begining of the dynar (rather long, Use
470 * xbt_dynar_push() when possible)
473 xbt_dynar_unshift(xbt_dynar_t const dynar,
474 const void * const src) {
476 /* sanity checks done by insert_at */
477 xbt_dynar_insert_at(dynar, 0, src);
485 * Get and remove the first element of the dynar (rather long, Use
486 * xbt_dynar_pop() when possible)
489 xbt_dynar_shift(xbt_dynar_t const dynar,
492 /* sanity checks done by remove_at */
493 xbt_dynar_remove_at(dynar, 0, dst);
501 * Apply a function to each member of a dynar (this function may change the
502 * value of the element itself, but should not mess with the dynar).
505 xbt_dynar_map(const xbt_dynar_t dynar,
506 void_f_pvoid_t * const operator) {
508 __sanity_check_dynar(dynar);
512 const unsigned long used = dynar->used;
515 for (i = 0; i < used; i++) {
516 _xbt_dynar_get_elm(elm, dynar, i);
523 * xbt_dynar_cursor_first:
525 * Put the cursor at the begining of the dynar. (actually, one step before
526 * the begining, so that you can iterate over the dynar with a for loop).
528 * Dynar cursor are as dumb as possible. If you insert or remove elements
529 * from the dynar between the creation and end, you'll fuck up your
534 xbt_dynar_cursor_first(const xbt_dynar_t dynar,
535 int * const cursor) {
537 DEBUG1("Set cursor on %p to the first position",(void*)dynar);
542 * xbt_dynar_cursor_step:
544 * Move the cursor to the next value (and return true), or return false.
547 xbt_dynar_cursor_step(const xbt_dynar_t dynar,
548 int * const cursor) {
554 * xbt_dynar_cursor_get:
556 * Get the current value of the cursor
559 xbt_dynar_cursor_get(const xbt_dynar_t dynar,
563 __sanity_check_dynar(dynar);
566 const int idx = *cursor;
568 if (idx >= dynar->used) {
569 DEBUG1("Cursor on %p already on last elem",(void*)dynar);
572 DEBUG2("Cash out cursor on %p at %d",(void*)dynar,idx);
574 _xbt_dynar_get_elm(dst, dynar, idx);
581 * xbt_dynar_cursor_rm:
585 * Remove (free) the entry pointed by the cursor, for use in the middle of a foreach
587 void xbt_dynar_cursor_rm(xbt_dynar_t dynar,
588 int * const cursor) {
591 if (dynar->elmsize > sizeof(void*)) {
592 DEBUG0("Elements too big to fit into a pointer");
594 dst=xbt_malloc(dynar->elmsize);
595 xbt_dynar_remove_at(dynar,(*cursor)--,dst);
596 (dynar->free_f)(dst);
599 DEBUG0("Ok, we dont care about the element without free function");
600 xbt_dynar_remove_at(dynar,(*cursor)--,NULL);
604 xbt_dynar_remove_at(dynar,(*cursor)--,&dst);
606 (dynar->free_f)(dst);