Skip to content

Commit

Permalink
allocated to free
Browse files Browse the repository at this point in the history
  • Loading branch information
thstrauss committed Dec 21, 2024
1 parent 73774c3 commit a05d39f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
10 changes: 5 additions & 5 deletions acu_util/acu_allc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 {
Expand All @@ -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;
}
Expand All @@ -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);
}
}
2 changes: 1 addition & 1 deletion acu_util/acu_allc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion acu_util/acu_dall.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit a05d39f

Please sign in to comment.