rsys

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

commit 70b234ffc9f2c30d3ba961d5f8d82c428c96d8bd
parent 816c64a5d927456be6ace2b39de50bf487289c65
Author: vaplv <vaplv@free.fr>
Date:   Mon, 29 Oct 2018 17:35:08 +0100

Fix aligned LIFO memory allocation

Diffstat:
Msrc/mem_lifo_allocator.c | 25+++++++++++++++----------
1 file changed, 15 insertions(+), 10 deletions(-)

diff --git a/src/mem_lifo_allocator.c b/src/mem_lifo_allocator.c @@ -75,18 +75,21 @@ lifo_alloc_aligned align_adjusted = align < LIFO_DEFAULT_ALIGNMENT ? LIFO_DEFAULT_ALIGNMENT : align; - header_size = ALIGN_SIZE(sizeof(int64_t), align_adjusted); - footer_size = sizeof(int64_t); - data_size = ALIGN_SIZE(size, sizeof(int64_t)); - entry_size = header_size + data_size + footer_size; - - ASSERT(data_size < (size_t)(((int64_t)1<<48)-1)); - ASSERT(header_size < (1<<16)-1); - header = (int64_t)data_size | ((int64_t)header_size<<48); - footer = (int64_t)data_size | ((int64_t)1<<48); - mutex_lock(lifo->mutex); { /* Critical section */ + intptr_t data_addr = (intptr_t)(lifo->top + sizeof(int64_t)); + data_addr = ALIGN_SIZE(data_addr, (intptr_t)align_adjusted); + + header_size = (size_t)(data_addr - (intptr_t)lifo->top); + footer_size = sizeof(int64_t); + data_size = ALIGN_SIZE(size, sizeof(int64_t)); + entry_size = header_size + data_size + footer_size; + + ASSERT(data_size < (size_t)(((int64_t)1<<48)-1)); + ASSERT(header_size < (1<<16)-1); + header = (int64_t)data_size | ((int64_t)header_size<<48); + footer = (int64_t)data_size | ((int64_t)1<<48); + if(lifo->remain < entry_size) { mem = NULL; } else { @@ -98,6 +101,8 @@ lifo_alloc_aligned *(int64_t*)(mem + data_size) = footer; } } + CHK(IS_ALIGNED(mem, align)); + mutex_unlock(lifo->mutex); return mem; }