bugfix(arena): Fixed some bugs which caused crashes.

This commit is contained in:
Eric-Paul Ickhorn 2024-05-23 01:43:12 +02:00
parent 3fd1b69293
commit 5f7545ffd6
1 changed files with 5 additions and 4 deletions

View File

@ -63,6 +63,7 @@ void * ufn_arena_alloc(ufn_arena_s *arena, uint32_t size);
char * ufn_arena_clone_string(ufn_arena_s *arena, const char *string);
// END OF API, START OF IMPLEMENTATION.
#ifdef UFN_IMPLEMENTATION
#include <stdlib.h>
#include <string.h>
@ -94,13 +95,13 @@ void ufn_free_arena(ufn_arena_s *arena)
void * ufn_arena_alloc(ufn_arena_s *arena, uint32_t size)
{
if((arena->usage = size) >= arena->capacity)
if((arena->usage + size) >= arena->capacity)
{
if(arena->continuation == NULL)
{
uint32_t continuation_size = arena->capacity * 2;
// Clamp the continuation's size to the maximum allowed one.
if(continuation_size > (arena->maximum))
if(continuation_size > arena->maximum)
{
continuation_size = arena->maximum;
}
@ -108,7 +109,7 @@ void * ufn_arena_alloc(ufn_arena_s *arena, uint32_t size)
// If no continuation of this arena tree can fulfill
// a request of this size,
// -> Deny the request and return NULL.
if(size > arena->maximum)
if(size >= arena->maximum)
{
return NULL;
}
@ -120,7 +121,7 @@ void * ufn_arena_alloc(ufn_arena_s *arena, uint32_t size)
}
arena->continuation = ufn_new_dynamic_arena(continuation_size, continued_maximum);
}
ufn_arena_alloc(arena->continuation, size);
return ufn_arena_alloc(arena->continuation, size);
}
void *block = arena->allocation + arena->usage;
arena->usage += size;