rsys

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

commit 07061827c4018b47979c366f02effde49f1231a6
parent bd58534ffc0d4a2bfd3c7ea492cd61df5bd5f91e
Author: vaplv <vaplv@free.fr>
Date:   Thu, 14 Mar 2019 15:30:30 +0100

Update the allocation policy of the dynamic arrays

The reserve/resize function strictly allocates the given size if there
is no sufficient space already allocated. Previously, they allocate the
submitted size rounded up to a power of two. However, the push_back
function still reserves a size that is a power of two.

Diffstat:
Msrc/dynamic_array.h | 27+++++++++++++--------------
Msrc/hash_table.h | 2+-
2 files changed, 14 insertions(+), 15 deletions(-)

diff --git a/src/dynamic_array.h b/src/dynamic_array.h @@ -157,19 +157,14 @@ static INLINE res_T DARRAY_FUNC__(reserve)(struct DARRAY_TYPE__* darray, const size_t sz) { DARRAY_DATA* data = NULL; - size_t sz_adjusted; ASSERT(darray); if(sz <= darray->capacity) return RES_OK; - sz_adjusted = round_up_pow2(sz); data = (DARRAY_DATA*)MEM_ALLOC_ALIGNED - (darray->allocator, - sz_adjusted * sizeof(DARRAY_DATA), - DARRAY_ALIGNMENT__); - if(!data) - return RES_MEM_ERR; + (darray->allocator, sz * sizeof(DARRAY_DATA), DARRAY_ALIGNMENT__); + if(!data) return RES_MEM_ERR; if(darray->size) { size_t i = 0; @@ -186,7 +181,7 @@ DARRAY_FUNC__(reserve)(struct DARRAY_TYPE__* darray, const size_t sz) MEM_RM(darray->allocator, darray->data); darray->data = data; - darray->capacity = sz_adjusted; + darray->capacity = sz; return RES_OK; } @@ -217,14 +212,18 @@ DARRAY_FUNC__(push_back) (struct DARRAY_TYPE__* darray, DARRAY_DATA const* data) { - size_t sz = 0; + DARRAY_DATA* dst; + size_t sz_adjusted; res_T res; ASSERT(darray && data); - sz = darray->size; - res = DARRAY_FUNC__(resize)(darray, sz+1); - if(res != RES_OK) - return res; - DARRAY_FUNCTOR_COPY(darray->data+sz, data); + + sz_adjusted = round_up_pow2(darray->size + 1); + res = DARRAY_FUNC__(reserve)(darray, sz_adjusted); + if(res != RES_OK) return res; + dst = darray->data + darray->size; + DARRAY_FUNCTOR_INIT(darray->allocator, dst); + DARRAY_FUNCTOR_COPY(dst, data); + ++darray->size; return RES_OK; } diff --git a/src/hash_table.h b/src/hash_table.h @@ -34,7 +34,7 @@ * - HTABLE_DATA: type of the data registered into the hash table; * - HTABLE_KEY: type of the key indexing the hash table data; * - HTABLE_KEY_FUNCTOR_HASH: hash functor on HTABLE_KEY. If not defined, use - * the default function that hashes the bytes of HTABLE_KEY; + * the default function that hashes the bytes of HTABLE_KEY; * - HTABLE_KEY_FUNCTOR_EQ: equality functor on 2 HTABLE_KEY; * - HTABLE_<DATA|KEY>_FUNCTOR_INIT: init functor on HTABLE_<DATA|KEY>. If not * defined, no specific treatment is performed on the created data;