New allocators, testing framework, overall improvements

This commit is contained in:
2026-08-22 02:13:04 -05:00
parent 69437a14ad
commit c1c603a250
24 changed files with 2004 additions and 1515 deletions
+28 -88
View File
@@ -2,10 +2,9 @@
#if MALUNAL_PLATFORM_LINUX
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include "internal.h"
#include "malunal/allocator.h"
static
@@ -19,32 +18,12 @@ platform_page_size() {
: g_platform_page_size;
}
static
allocation_error_t
linux_initialize(
impl_mptr_t allocator,
allocator_mptr_t upstream,
malunal_size_t capacity
) {
MALUNAL_UNUSED(allocator);
MALUNAL_UNUSED(upstream);
MALUNAL_UNUSED(capacity);
return ALLOCATION_ERROR_SUCCESS;
}
static
allocation_error_t
linux_finalize(impl_mptr_t allocator) {
MALUNAL_UNUSED(allocator);
return ALLOCATION_ERROR_SUCCESS;
}
static
allocation_result_t
linux_acquire(
impl_mptr_t allocator,
malunal_size_t size
error_t
linux_allocator_acquire(
allocator_mptr_t allocator,
malunal_size_t size,
malunal_mptr_t* out
) {
MALUNAL_UNUSED(allocator);
@@ -52,81 +31,42 @@ linux_acquire(
const malunal_int32_t memprms = MAP_PRIVATE | MAP_ANONYMOUS;
malunal_mptr_t ptr = mmap(0, size, memops, memprms, -1, 0);
if (ptr == MAP_FAILED || ptr == NULL_ADDRESS)
return (allocation_result_t) {
.threw = 1,
.error = ALLOCATION_ERROR_OUT_OF_MEMORY
if (ptr == MAP_FAILED || ptr == null)
return (error_t) {
.domain = &ERROR_DOMAIN_ALLOCATOR_T,
.code = ALLOCATOR_ERROR_OUT_OF_MEMORY
};
allocator->allocated += size;
allocator->reserved += size;
allocator->acquires += 1;
return (allocation_result_t) {
.threw = 0,
.address = ptr
};
*out = ptr;
return NO_ERROR;
}
static
allocation_error_t
linux_release(
impl_mptr_t allocator,
malunal_mptr_t address,
malunal_size_t size
error_t
linux_allocator_dispose(
allocator_mptr_t allocator,
malunal_mptr_t address,
malunal_size_t size
) {
MALUNAL_UNUSED(allocator);
munmap(address, size);
allocator->allocated -= size;
allocator->reserved -= size;
allocator->releases += 1;
return ALLOCATION_ERROR_SUCCESS;
return NO_ERROR;
}
static
allocation_result_t
linux_reacquire(
impl_mptr_t allocator,
malunal_mptr_t address,
malunal_size_t oldsize,
malunal_size_t newsize
) {
allocation_result_t result = linux_acquire(allocator, newsize);
if (result.threw)
return result;
result.address = memcpy(result.address, address, oldsize);
linux_release(allocator, address, oldsize);
return result;
}
static const
virtual_table_t linux_vtable = {
.initialize = (allocator_initialize_pfn_t)&linux_initialize,
.finalize = (allocator_finalize_pfn_t)&linux_finalize,
.acquire = (allocator_acquire_pfn_t)&linux_acquire,
.reacquire = (allocator_reacquire_pfn_t)&linux_reacquire,
.release = (allocator_release_pfn_t)&linux_release
allocator_vtable_t linux_allocator_vtable = {
.acquire = (allocator_acquire_pfn_t)&linux_allocator_acquire,
.dispose = (allocator_dispose_pfn_t)&linux_allocator_dispose
};
allocator_t
static const
allocator_t linux_allocator_instance = {
.vtable = &linux_allocator_vtable
};
allocator_mptr_t
linux_allocator() {
implementation_t implementation = {
.vtable = &linux_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;
return &linux_allocator_instance;
}
#endif /* MALUNAL_PLATFORM_LINUX */