X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/3d54962f9e6f3dd33c20d85f335022f5842a4a93..c3bb4a70ced17ee2c6461b24c445d511fd074ec7:/src/xbt/dynar.c diff --git a/src/xbt/dynar.c b/src/xbt/dynar.c index 09876099d5..5cfc493e38 100644 --- a/src/xbt/dynar.c +++ b/src/xbt/dynar.c @@ -18,7 +18,7 @@ #include "xbt/dynar_private.h" /* type definition, which we share with the code in charge of sending this across the net */ -XBT_LOG_NEW_DEFAULT_SUBCATEGORY(dynar,xbt,"Dynamic arrays"); +XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_dyn,xbt,"Dynamic arrays"); #define __sanity_check_dynar(dynar) \ @@ -127,7 +127,7 @@ xbt_dynar_dump(xbt_dynar_t dynar) { * types (int, char, double, etc) or pointer of pointers (struct **). */ xbt_dynar_t -xbt_dynar_new(const unsigned long elmsize, +xbt_dynar_new(const unsigned long elmsize, void_f_pvoid_t * const free_f) { xbt_dynar_t dynar = xbt_new0(s_xbt_dynar_t,1); @@ -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 @@ -374,9 +397,16 @@ xbt_dynar_remove_at(xbt_dynar_t const dynar, 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); + if (dynar->elmsize <= SIZEOF_MAX) { + char elm[SIZEOF_MAX]; + _xbt_dynar_get_elm(elm, dynar, idx); + (*dynar->free_f)(elm); + } else { + char *elm=malloc(dynar->elmsize); + _xbt_dynar_get_elm(elm, dynar, idx); + (*dynar->free_f)(elm); + free(elm); + } } nb_shift = dynar->used-1 - idx; @@ -589,8 +619,8 @@ void xbt_dynar_cursor_rm(xbt_dynar_t dynar, #define NB_ELEM 5000 XBT_TEST_SUITE("dynar","Dynar data container"); -XBT_LOG_EXTERNAL_CATEGORY(dynar); -XBT_LOG_DEFAULT_CATEGORY(dynar); +XBT_LOG_EXTERNAL_CATEGORY(xbt_dyn); +XBT_LOG_DEFAULT_CATEGORY(xbt_dyn); XBT_TEST_UNIT("int",test_dynar_int,"Dyars of integers") { /* Vars_decl [doxygen cruft] */