Reworked, no version bump

This commit is contained in:
2026-08-12 19:47:59 -05:00
parent f82f8288cf
commit 70638a9b46
13 changed files with 1170 additions and 1383 deletions
+138 -120
View File
@@ -1,180 +1,198 @@
#include <memory.h>
#include "malunal/allocators/arena.h"
#include "malunal/assert.h"
#include "internal.h"
typedef struct ArenaPage page_t;
typedef const page_t* page_iptr_t;
typedef page_t* page_mptr_t;
struct ArenaPage {
page_mptr_t next;
malunal_uint32_t used;
malunal_uint32_t size;
malunal_int8_t data[];
};
allocator_acquire_res_t
create_page(allocator_acquire_req_t request) {
if (request.allocator == NULL_ADDRESS) {
return (allocator_acquire_res_t) {
.threw = 1,
.exception = ALLOCATOR_EXCEPTION_NULL_UPSTREAM
};
}
allocator_acquire_res_t result =
allocator_acquire(request);
allocation_result_t
create_page(
allocator_mptr_t allocator,
malunal_size_t size
) {
malunal_size_t rounded = align_to_page(size);
allocation_result_t result = allocator_acquire(allocator, rounded);
if (result.threw)
return result;
arena_page_mptr_t page = result.address;
page->size = request.size;
page->used = sizeof(arena_page_t);
page_mptr_t page = result.address;
page->size = rounded;
page->used = sizeof(page_t);
page->next = NULL_ADDRESS;
return result;
}
allocator_exception_t
allocation_error_t
delete_page(
allocator_mptr_t allocator,
arena_page_mptr_t page
allocator_mptr_t allocator,
page_mptr_t page
) {
if (allocator == NULL_ADDRESS)
return ALLOCATOR_EXCEPTION_NULL_UPSTREAM;
return ALLOCATION_ERROR_NULL_UPSTREAM;
if (page == NULL_ADDRESS)
return ALLOCATOR_EXCEPTION_FAILURE;
return ALLOCATION_ERROR_FAILURE;
delete_page(allocator, page->next);
return allocator_release((allocator_release_req_t) {
.allocator = allocator,
.address = page,
.size = page->size
});
allocation_error_t err = delete_page(allocator, page->next);
return err == ALLOCATION_ERROR_SUCCESS
? allocator_release(allocator, page, page->size)
: err;
}
allocator_acquire_res_t
page_acquire(allocator_acquire_req_t request) {
arena_page_mptr_t page = request.allocator->context;
if (page == NULL_ADDRESS) {
return (allocator_acquire_res_t) {
.threw = 1,
.exception = ALLOCATOR_EXCEPTION_NULL_CONTEXT
};
}
malunal_uint32_t offset = page->used - sizeof(arena_page_t);
allocation_result_t
page_acquire(
page_mptr_t page,
malunal_size_t size
) {
malunal_uint32_t offset = page->used - sizeof(page_t);
malunal_mptr_t result = page->data + offset;
page->used += request.size;
return (allocator_acquire_res_t) {
page->used += size;
return (allocation_result_t) {
.threw = 0,
.address = result
};
}
allocator_t
arena_allocator(
static
allocation_error_t
arena_initialize(
impl_mptr_t allocator,
allocator_mptr_t upstream,
malunal_size_t size
malunal_size_t capacity
) {
allocator_acquire_req_t request =
(allocator_acquire_req_t) {
.allocator = upstream,
.size = platform_page_size()
};
if (upstream == NULL_ADDRESS)
return ALLOCATION_ERROR_NULL_UPSTREAM;
allocator_acquire_res_t result = create_page(request);
arena_page_mptr_t currpage =
!result.threw ? result.address : NULL_ADDRESS;
allocation_result_t result = create_page(upstream, capacity);
page_mptr_t currpage = !result.threw ? result.address : NULL_ADDRESS;
malunal_size_t reserved = 0;
if (currpage != NULL_ADDRESS) {
reserved += currpage->size;
while (reserved > size) {
result = create_page(request);
if (result.threw)
break;
currpage = currpage->next = result.address;
reserved += currpage->size;
}
}
return (allocator_t) {
.vtable = {
.acquire = (allocator_acquire_pfn_t)&arena_acquire,
.reacquire = (allocator_reacquire_pfn_t)&arena_reacquire,
.release = (allocator_release_pfn_t)&arena_release
},
.upstream = upstream,
.context = currpage
};
allocator->upstream = upstream;
allocator->context = currpage;
allocator->allocated = currpage->used;
allocator->reserved = currpage->size;
return ALLOCATION_ERROR_SUCCESS;
}
allocator_acquire_res_t
arena_acquire(allocator_acquire_req_t request) {
if (request.allocator == NULL_ADDRESS) {
return (allocator_acquire_res_t) {
.threw = 1,
.exception = ALLOCATOR_EXCEPTION_NULL_ALLOCATOR
};
}
static
allocation_error_t
arena_finalize(impl_mptr_t allocator) {
delete_page(allocator->upstream, allocator->context);
}
if (request.size > platform_page_size()) {
return (allocator_acquire_res_t) {
.threw = 1,
.exception = ALLOCATOR_EXCEPTION_OUT_OF_MEMORY
static
allocation_result_t
arena_acquire(
impl_mptr_t allocator,
malunal_size_t size
) {
if (allocator->context == NULL_ADDRESS)
return (allocation_result_t) {
.threw = 1,
.error = ALLOCATION_ERROR_NULL_CONTEXT
};
}
arena_page_mptr_t page = request.allocator->context;
while (page != NULL_ADDRESS) {
struct ArenaPage* page = allocator->context;
if (size > page->size)
return (allocation_result_t) {
.threw = 1,
.error = ALLOCATION_ERROR_OUT_OF_MEMORY
};
while (page->next != NULL_ADDRESS) {
malunal_uint32_t remaining = page->size - page->used;
if (request.size <= remaining)
return page_acquire(request);
if (size <= remaining)
return page_acquire(page, size);
page = page->next;
continue;
}
allocator_acquire_res_t result =
create_page((allocator_acquire_req_t) {
.allocator = request.allocator->upstream,
.size = platform_page_size()
});
allocation_result_t result =
create_page(allocator->upstream, page->size);
page = page->next = !result.threw
? result.address
: NULL_ADDRESS;
return page != NULL_ADDRESS
? page_acquire(request)
: (allocator_acquire_res_t) {
.threw = 1,
.exception = ALLOCATOR_EXCEPTION_OUT_OF_MEMORY
? page_acquire(page, size)
: (allocation_result_t) {
.threw = 1,
.error = ALLOCATION_ERROR_OUT_OF_MEMORY
};
}
allocator_acquire_res_t
arena_reacquire(allocator_reacquire_req_t request) {
return arena_acquire((allocator_acquire_req_t) {
.allocator = request.allocator,
.size = request.newsz
});
static
allocation_result_t
arena_reacquire(
impl_mptr_t allocator,
malunal_mptr_t address,
malunal_size_t oldsize,
malunal_size_t newsize
) {
MALUNAL_UNUSED(address);
MALUNAL_UNUSED(oldsize);
return arena_acquire(allocator, newsize);
}
allocator_exception_t
arena_release(allocator_release_req_t request) {
if (request.allocator == NULL_ADDRESS)
return ALLOCATOR_EXCEPTION_NULL_ALLOCATOR;
if (request.allocator->context == NULL_ADDRESS)
return ALLOCATOR_EXCEPTION_NULL_CONTEXT;
allocation_error_t
arena_release(
impl_mptr_t allocator,
malunal_mptr_t address,
malunal_size_t size
) {
MALUNAL_UNUSED(allocator);
MALUNAL_UNUSED(address);
MALUNAL_UNUSED(size);
}
allocator_exception_t
allocation_error_t
arena_reset(allocator_mptr_t arena) {
if (arena == NULL_ADDRESS)
return ALLOCATOR_EXCEPTION_NULL_ALLOCATOR;
return ALLOCATION_ERROR_NULL_ALLOCATOR;
arena_page_mptr_t page = arena->context;
impl_mptr_t impl = (impl_mptr_t)arena;
page_mptr_t page = impl->context;
while (page != NULL_ADDRESS) {
page->used = 0;
page->used = sizeof(page_t);
page = page->next;
}
}
allocator_exception_t
arena_free(allocator_mptr_t arena) {
if (arena == NULL_ADDRESS)
return ALLOCATOR_EXCEPTION_NULL_ALLOCATOR;
delete_page(arena->upstream, arena->context);
static const
virtual_table_t arena_vtable = {
.initialize = (allocator_initialize_pfn_t)&arena_initialize,
.finalize = (allocator_finalize_pfn_t)&arena_finalize,
.acquire = (allocator_acquire_pfn_t)&arena_acquire,
.reacquire = (allocator_reacquire_pfn_t)&arena_reacquire,
.release = (allocator_release_pfn_t)&arena_release
};
allocator_t
arena_allocator() {
implementation_t implementation = {
.vtable = &arena_vtable,
.upstream = NULL_ADDRESS,
.context = NULL_ADDRESS,
.allocated = 0,
.reserved = 0,
.acquires = 0,
.releases = 0
};
allocator_t result;
memcpy(
&result,
&implementation,
sizeof(implementation_t)
);
return result;
}