rsys

Basic data structures and low-level features
git clone git://git.meso-star.fr/rsys.git
Log | Files | Refs | README | LICENSE

commit fb12566597cf3b00535a4a9e679157e1a932a35a
parent 0addd00886f579f744bd9d08bd30369ff8c5d25a
Author: vaplv <vaplv@free.fr>
Date:   Thu, 22 Dec 2016 15:22:26 +0100

Add the dynamic_array purge function

Clear the dynamic_array and force the release of the memory used to store
the dynamic_array data.

Diffstat:
Msrc/dynamic_array.h | 12++++++++++++
Msrc/test_dynamic_array.c | 5++++-
2 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/src/dynamic_array.h b/src/dynamic_array.h @@ -141,6 +141,18 @@ DARRAY_FUNC__(release)(struct DARRAY_TYPE__* darray) MEM_RM(darray->allocator, darray->data); } +/* Clean up the array and, unlike the clear function, ensure that the memory + * used to store the data is effectively released. */ +static INLINE void +DARRAY_FUNC__(purge)(struct DARRAY_TYPE__* darray) +{ + struct mem_allocator* allocator; + ASSERT(darray); + allocator = darray->allocator; + DARRAY_FUNC__(release)(darray); + DARRAY_FUNC__(init)(allocator, darray); +} + static INLINE res_T DARRAY_FUNC__(reserve)(struct DARRAY_TYPE__* darray, const size_t sz) { diff --git a/src/test_dynamic_array.c b/src/test_dynamic_array.c @@ -75,7 +75,7 @@ test_cstr(struct mem_allocator* allocator) FOR_EACH(i, 0, nstrs) { CHECK(strcmp(darray_str_cdata_get(&darray)[i], strs[i]), 0); } - darray_str_clear(&darray); + darray_str_purge(&darray); CHECK(darray_str_size_get(&darray), 0); darray_str_release(&darray); @@ -148,6 +148,9 @@ test_string CHECK(strcmp(str_cget(str2), strs[i]), 0); } darray_string_clear(&darray2); + i = MEM_ALLOCATED_SIZE(allocator1); + darray_string_purge(&darray2); + CHECK(MEM_ALLOCATED_SIZE(allocator1) < i, 1); CHECK(darray_string_size_get(&darray2), 0); darray_string_copy_and_clear(&darray2, &darray);