X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/4b33ddb6ae916d3b716daf109fc740d38e1766a9..3d05abe5cc7afadebd42020798544c7709558da8:/src/xbt/dynar.c diff --git a/src/xbt/dynar.c b/src/xbt/dynar.c index 964f92d580..2dbfcf5da4 100644 --- a/src/xbt/dynar.c +++ b/src/xbt/dynar.c @@ -53,6 +53,8 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_dyn,xbt,"Dynamic arrays"); THROW1(bound_error,0, \ "dynar %p is empty", dynar) +static void _dynar_map(const xbt_dynar_t dynar, + void_f_pvoid_t const op); static XBT_INLINE void _xbt_clear_mem(void * const ptr, @@ -63,7 +65,7 @@ void _xbt_clear_mem(void * const ptr, static XBT_INLINE void _xbt_dynar_expand(xbt_dynar_t const dynar, - const int nb) { + const unsigned long nb) { const unsigned long old_size = dynar->size; if (nb > old_size) { @@ -79,7 +81,7 @@ _xbt_dynar_expand(xbt_dynar_t const dynar, const unsigned long new_length = new_size*elmsize; char * const new_data = (char *) xbt_malloc0(elmsize*new_size); - DEBUG3("expend %p from %lu to %d elements", (void*)dynar, (unsigned long)old_size, nb); + DEBUG3("expend %p from %lu to %lu elements", (void*)dynar, (unsigned long)old_size, nb); if (old_data) { memcpy(new_data, old_data, used_length); @@ -128,7 +130,7 @@ _xbt_dynar_put_elm(const xbt_dynar_t dynar, static XBT_INLINE void _xbt_dynar_remove_at(xbt_dynar_t const dynar, - const int idx, + const unsigned long idx, void * const object) { unsigned long nb_shift; @@ -180,7 +182,7 @@ xbt_dynar_dump(xbt_dynar_t dynar) { */ xbt_dynar_t xbt_dynar_new(const unsigned long elmsize, - void_f_pvoid_t * const free_f) { + void_f_pvoid_t const free_f) { xbt_dynar_t dynar = xbt_new0(s_xbt_dynar_t,1); @@ -201,7 +203,7 @@ xbt_dynar_new(const unsigned long elmsize, */ xbt_dynar_t xbt_dynar_new_sync(const unsigned long elmsize, - void_f_pvoid_t * const free_f) { + void_f_pvoid_t const free_f) { xbt_dynar_t res = xbt_dynar_new(elmsize,free_f); res->mutex = xbt_mutex_init(); return res; @@ -245,7 +247,7 @@ xbt_dynar_reset(xbt_dynar_t const dynar) { DEBUG1("Reset the dynar %p",(void*)dynar); if (dynar->free_f) { - xbt_dynar_map(dynar, dynar->free_f); + _dynar_map(dynar, dynar->free_f); } /* if (dynar->data) @@ -276,7 +278,7 @@ xbt_dynar_reset(xbt_dynar_t const dynar) { * array is not expanded and nothing is done. */ void xbt_dynar_shrink(xbt_dynar_t dynar, int empty_slots_wanted) { - int size_wanted; + unsigned long size_wanted; _dynar_lock(dynar); @@ -324,7 +326,7 @@ xbt_dynar_length(const xbt_dynar_t dynar) { */ void xbt_dynar_get_cpy(const xbt_dynar_t dynar, - const int idx, + const unsigned long idx, void * const dst) { _dynar_lock(dynar); _sanity_check_dynar(dynar); @@ -345,7 +347,7 @@ xbt_dynar_get_cpy(const xbt_dynar_t dynar, * Make a copy before fooling with it. */ void* -xbt_dynar_get_ptr(const xbt_dynar_t dynar, const int idx) { +xbt_dynar_get_ptr(const xbt_dynar_t dynar, const unsigned long idx) { void *res; _dynar_lock(dynar); @@ -361,7 +363,7 @@ xbt_dynar_get_ptr(const xbt_dynar_t dynar, const int idx) { static void XBT_INLINE /* not synchronized */ _xbt_dynar_set(xbt_dynar_t dynar, - const int idx, + const unsigned long idx, const void * const src) { _sanity_check_dynar(dynar); @@ -406,7 +408,7 @@ xbt_dynar_set(xbt_dynar_t dynar, */ void xbt_dynar_replace(xbt_dynar_t dynar, - const int idx, + const unsigned long idx, const void * const object) { _dynar_lock(dynar); _sanity_check_dynar(dynar); @@ -415,7 +417,7 @@ xbt_dynar_replace(xbt_dynar_t dynar, if (idx < dynar->used && dynar->free_f) { void * const old_object = _xbt_dynar_elm(dynar, idx); - dynar->free_f(old_object); + (*(dynar->free_f))(old_object); } _xbt_dynar_set(dynar, idx, object); @@ -424,19 +426,22 @@ xbt_dynar_replace(xbt_dynar_t dynar, static XBT_INLINE void * _xbt_dynar_insert_at_ptr(xbt_dynar_t const dynar, - const int idx) { + const unsigned long idx) { void *res; + unsigned long old_used; + unsigned long new_used; + unsigned long nb_shift; _sanity_check_dynar(dynar); _sanity_check_idx(idx); _check_sloppy_inbound_idx(dynar, idx); - const unsigned long old_used = dynar->used; - const unsigned long new_used = old_used + 1; + old_used = dynar->used; + new_used = old_used + 1; _xbt_dynar_expand(dynar, new_used); - const unsigned long nb_shift = old_used - idx; + nb_shift = old_used - idx; if (nb_shift) memmove(_xbt_dynar_elm(dynar, idx+1), @@ -509,7 +514,7 @@ xbt_dynar_remove_at(xbt_dynar_t const dynar, int xbt_dynar_search(xbt_dynar_t const dynar, void *const elem) { - int it; + unsigned long it; _dynar_lock(dynar); for (it=0; it< dynar->used; it++) @@ -611,6 +616,18 @@ xbt_dynar_shift(xbt_dynar_t const dynar, xbt_dynar_remove_at(dynar, 0, dst); } +static void _dynar_map(const xbt_dynar_t dynar, + void_f_pvoid_t const op) { + char elm[SIZEOF_MAX]; + const unsigned long used = dynar->used; + unsigned long i = 0; + + for (i = 0; i < used; i++) { + _xbt_dynar_get_elm(elm, dynar, i); + (*op)(elm); + } +} + /** @brief Apply a function to each member of a dynar * * The mapped function may change the value of the element itself, @@ -622,21 +639,13 @@ xbt_dynar_shift(xbt_dynar_t const dynar, */ void xbt_dynar_map(const xbt_dynar_t dynar, - void_f_pvoid_t * const op) { + void_f_pvoid_t const op) { _dynar_lock(dynar); _sanity_check_dynar(dynar); - { - char elm[SIZEOF_MAX]; - const unsigned long used = dynar->used; - unsigned long i = 0; + _dynar_map(dynar,op); - for (i = 0; i < used; i++) { - _xbt_dynar_get_elm(elm, dynar, i); - op(elm); - } - } _dynar_unlock(dynar); } @@ -679,14 +688,14 @@ _xbt_dynar_cursor_get(const xbt_dynar_t dynar, _sanity_check_dynar(dynar); { - const int idx = *cursor; + const unsigned long idx = *cursor; if (idx >= dynar->used) { DEBUG1("Cursor on %p already on last elem",(void*)dynar); _dynar_unlock(dynar); return FALSE; } - DEBUG2("Cash out cursor on %p at %d",(void*)dynar,idx); + DEBUG2("Cash out cursor on %p at %lu",(void*)dynar,idx); _xbt_dynar_get_elm(dst, dynar, idx); }