From ae31c9605daeec056883c914f4f0d4cd676a8d4b Mon Sep 17 00:00:00 2001 From: mquinson Date: Thu, 23 Jun 2005 14:39:16 +0000 Subject: [PATCH 1/1] in remove_at, if the user didn't provide room to retrieve the object and if there is a freeing function in this dynar, do free the removed object ourselves (plus cosmetics) git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@1379 48e7efb5-ca39-0410-a469-dd3cf9ba447f --- src/xbt/dynar.c | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/xbt/dynar.c b/src/xbt/dynar.c index be9fef6c02..8534dde7c8 100644 --- a/src/xbt/dynar.c +++ b/src/xbt/dynar.c @@ -243,7 +243,7 @@ xbt_dynar_get_cpy(const xbt_dynar_t dynar, */ void* xbt_dynar_get_ptr(const xbt_dynar_t dynar, - const int idx) { + const int idx) { __sanity_check_dynar(dynar); __sanity_check_idx(idx); @@ -364,29 +364,29 @@ xbt_dynar_remove_at(xbt_dynar_t const dynar, const int idx, void * const object) { + unsigned long nb_shift; + unsigned long offset; + __sanity_check_dynar(dynar); __sanity_check_idx(idx); __check_inbound_idx(dynar, idx); - if (object) + if (object) { _xbt_dynar_get_elm(object, dynar, idx); + } else if (dynar->free_f) { + char elm[SIZEOF_MAX]; + _xbt_dynar_get_elm(elm, dynar, idx); + (*dynar->free_f)(elm); + } - { - const unsigned long old_used = dynar->used; - const unsigned long new_used = old_used - 1; - - const unsigned long nb_shift = old_used-1 - idx; - const unsigned long elmsize = dynar->elmsize; - - const unsigned long offset = nb_shift*elmsize; - - void * const elm_src = _xbt_dynar_elm(dynar, idx+1); - void * const elm_dst = _xbt_dynar_elm(dynar, idx); + nb_shift = dynar->used-1 - idx; + offset = nb_shift * dynar->elmsize; - memmove(elm_dst, elm_src, offset); + memmove(_xbt_dynar_elm(dynar, idx), + _xbt_dynar_elm(dynar, idx+1), + offset); - dynar->used = new_used; - } + dynar->used--; } /** @brief Make room at the end of the dynar for a new element, and return a pointer to it. -- 2.20.1