From a05d39fce3a9ce6bc1b072be46f1eadb5eeac19f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Strau=C3=9F?= Date: Sun, 22 Dec 2024 00:17:48 +0100 Subject: [PATCH] allocated to free --- acu_util/acu_allc.c | 10 +++++----- acu_util/acu_allc.h | 2 +- acu_util/acu_dall.c | 3 ++- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/acu_util/acu_allc.c b/acu_util/acu_allc.c index fa67680..972c775 100644 --- a/acu_util/acu_allc.c +++ b/acu_util/acu_allc.c @@ -30,7 +30,7 @@ void acu_initStaticAllocator(ACU_StaticAllocator* allocator, size_t itemSize, si size_t i; size_t elementSize = offsetof(ACU_AllocatorItem, itemBuffer) + itemSize; allocator->maxElements = maxElements; - allocator->allocatedElements = 0; + allocator->freeElements = maxElements; allocator->freeContext = freeContext; allocator->buffer = acu_emalloc(elementSize * maxElements); @@ -54,7 +54,7 @@ void* acu_allocStaticAllocator(ACU_StaticAllocator* allocator) { ACU_AllocatorItem* allocatorItem = allocator->nextItem; if (allocatorItem->status == ACU_BUFFER_STATUS_OCCUPIED) { - if (allocator->allocatedElements == allocator->maxElements) { + if (allocator->freeElements == 0) { return NULL; } else { @@ -64,7 +64,7 @@ void* acu_allocStaticAllocator(ACU_StaticAllocator* allocator) } } allocatorItem->status = ACU_BUFFER_STATUS_OCCUPIED; - allocator->allocatedElements++; + allocator->freeElements--; allocator->nextItem = allocatorItem->nextItem; return &allocatorItem->itemBuffer; } @@ -75,8 +75,8 @@ void acu_freeStaticAllocator(void* buffer) ACU_StaticAllocator* allocator = item->allocator; item->status = ACU_BUFFER_STATUS_FREE; allocator->nextItem = item; - allocator->allocatedElements--; - if (allocator->allocatedElements == 0 && allocator->freeContext) { + allocator->freeElements++; + if ((allocator->freeElements == allocator->maxElements) && allocator->freeContext) { allocator->freeContext->freeFunc(allocator); } } diff --git a/acu_util/acu_allc.h b/acu_util/acu_allc.h index 8149dd5..99b116c 100644 --- a/acu_util/acu_allc.h +++ b/acu_util/acu_allc.h @@ -51,7 +51,7 @@ typedef struct ACU_FreeContext_ { typedef struct ACU_StaticAllocator_ { ACU_AllocatorItem* nextItem; - size_t allocatedElements; + size_t freeElements; size_t maxElements; char* buffer; ACU_FreeContext* freeContext; diff --git a/acu_util/acu_dall.c b/acu_util/acu_dall.c index 2ad7369..4ddb23c 100644 --- a/acu_util/acu_dall.c +++ b/acu_util/acu_dall.c @@ -101,7 +101,8 @@ void* acu_allocAllocator(ACU_DynamicAllocator* allocator) } static void __accumulateAllocatedElements(const void* data, void* visitorContext) { - (*(size_t*)visitorContext) += ((ACU_StaticAllocator*) data)->allocatedElements; + ACU_StaticAllocator* allocator = (ACU_StaticAllocator*) data; + (*(size_t*)visitorContext) += allocator->maxElements - allocator->freeElements; } size_t acu_getAllocatedElements(ACU_DynamicAllocator* allocator)