From c3bb4a70ced17ee2c6461b24c445d511fd074ec7 Mon Sep 17 00:00:00 2001 From: thiery Date: Tue, 15 Aug 2006 07:55:23 +0000 Subject: [PATCH] Add a function xbt_dynar_shrink git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@2716 48e7efb5-ca39-0410-a469-dd3cf9ba447f --- include/xbt/dynar.h | 1 + src/xbt/dynar.c | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/include/xbt/dynar.h b/include/xbt/dynar.h index 85dc88edde..1007daf5cd 100644 --- a/include/xbt/dynar.h +++ b/include/xbt/dynar.h @@ -70,6 +70,7 @@ SG_BEGIN_DECL() unsigned long xbt_dynar_length(const xbt_dynar_t dynar); void xbt_dynar_reset(xbt_dynar_t dynar); + void xbt_dynar_shrink(xbt_dynar_t dynar, int empty_slots); void xbt_dynar_dump(xbt_dynar_t dynar); diff --git a/src/xbt/dynar.c b/src/xbt/dynar.c index ede861850e..5cfc493e38 100644 --- a/src/xbt/dynar.c +++ b/src/xbt/dynar.c @@ -187,6 +187,29 @@ xbt_dynar_reset(xbt_dynar_t const dynar) { /* dynar->data = NULL;*/ } +/** + * \brief Shrink the dynar by removing empty slots at the end of the internal array + * \param dynar a dynar + * \param empty_slots_wanted number of empty slots you want to keep at the end of the + * internal array for further insertions + * + * Reduces the internal array size of the dynar to the number of elements plus + * \a empty_slots_wanted. + * After removing elements from the dynar, you can call this function to make + * the dynar use less memory. + * Set \a empty_slots_wanted to zero to reduce the dynar internal array as much + * as possible. + * Note that if \a empty_slots_wanted is greater than the array size, the internal + * array is not expanded and nothing is done. + */ +void xbt_dynar_shrink(xbt_dynar_t dynar, int empty_slots_wanted) { + int size_wanted = dynar->used + empty_slots_wanted; + if (size_wanted < dynar->size) { + dynar->size = size_wanted; + dynar->data = xbt_realloc(dynar->data, sizeof(void*) * dynar->size); + } +} + /** @brief Destructor * * \param dynar poor victim -- 2.20.1