New allocators, testing framework, overall improvements
This commit is contained in:
+32
-92
@@ -1,109 +1,49 @@
|
||||
#include <stdlib.h>
|
||||
#include <memory.h>
|
||||
#include "internal.h"
|
||||
|
||||
#include "malunal/allocator.h"
|
||||
|
||||
static
|
||||
allocation_error_t
|
||||
libc_initialize(
|
||||
impl_mptr_t allocator,
|
||||
allocator_mptr_t upstream,
|
||||
malunal_size_t capacity
|
||||
error_t
|
||||
libc_allocator_acquire(
|
||||
allocator_mptr_t allocator,
|
||||
malunal_size_t size,
|
||||
malunal_mptr_t* out
|
||||
) {
|
||||
MALUNAL_UNUSED(allocator);
|
||||
MALUNAL_UNUSED(upstream);
|
||||
MALUNAL_UNUSED(capacity);
|
||||
return ALLOCATION_ERROR_SUCCESS;
|
||||
|
||||
*out = malloc(size);
|
||||
return *out == null
|
||||
? (error_t) {
|
||||
.domain = &ERROR_DOMAIN_ALLOCATOR_T,
|
||||
.code = ALLOCATOR_ERROR_OUT_OF_MEMORY
|
||||
}
|
||||
: NO_ERROR;
|
||||
}
|
||||
|
||||
static
|
||||
allocation_error_t
|
||||
libc_finalize(impl_mptr_t allocator) {
|
||||
error_t
|
||||
libc_allocator_dispose(
|
||||
allocator_mptr_t allocator,
|
||||
malunal_mptr_t address,
|
||||
malunal_size_t size
|
||||
) {
|
||||
MALUNAL_UNUSED(allocator);
|
||||
return ALLOCATION_ERROR_SUCCESS;
|
||||
}
|
||||
|
||||
static
|
||||
allocation_result_t
|
||||
libc_acquire(
|
||||
impl_mptr_t allocator,
|
||||
malunal_size_t size
|
||||
) {
|
||||
malunal_mptr_t addr = malloc(size);
|
||||
if (addr == NULL_ADDRESS) {
|
||||
return (allocation_result_t) {
|
||||
.threw = 1,
|
||||
.error = ALLOCATION_ERROR_OUT_OF_MEMORY
|
||||
};
|
||||
}
|
||||
|
||||
allocator->allocated += size;
|
||||
allocator->reserved += size;
|
||||
allocator->acquires += 1;
|
||||
return (allocation_result_t) {
|
||||
.threw = 0,
|
||||
.address = addr
|
||||
};
|
||||
}
|
||||
|
||||
static
|
||||
allocation_error_t
|
||||
libc_release(
|
||||
impl_mptr_t allocator,
|
||||
malunal_mptr_t address,
|
||||
malunal_size_t size
|
||||
) {
|
||||
MALUNAL_UNUSED(size);
|
||||
free(address);
|
||||
|
||||
allocator->allocated -= size;
|
||||
allocator->reserved -= size;
|
||||
allocator->releases += 1;
|
||||
return ALLOCATION_ERROR_SUCCESS;
|
||||
}
|
||||
|
||||
static
|
||||
allocation_result_t
|
||||
libc_reacquire(
|
||||
impl_mptr_t allocator,
|
||||
malunal_mptr_t address,
|
||||
malunal_size_t oldsize,
|
||||
malunal_size_t newsize
|
||||
) {
|
||||
allocation_result_t result = libc_acquire(allocator, newsize);
|
||||
if (result.threw)
|
||||
return result;
|
||||
|
||||
result.address = memcpy( result.address, address, oldsize);
|
||||
libc_release(allocator, address, oldsize);
|
||||
return result;
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
static const
|
||||
virtual_table_t libc_vtable = {
|
||||
.initialize = (allocator_initialize_pfn_t)&libc_initialize,
|
||||
.finalize = (allocator_finalize_pfn_t)&libc_finalize,
|
||||
.acquire = (allocator_acquire_pfn_t)&libc_acquire,
|
||||
.reacquire = (allocator_reacquire_pfn_t)&libc_reacquire,
|
||||
.release = (allocator_release_pfn_t)&libc_release
|
||||
allocator_vtable_t libc_allocator_vtable = {
|
||||
.acquire = (allocator_acquire_pfn_t)&libc_allocator_acquire,
|
||||
.dispose = (allocator_dispose_pfn_t)&libc_allocator_dispose
|
||||
};
|
||||
|
||||
allocator_t
|
||||
libc_allocator() {
|
||||
implementation_t implementation = {
|
||||
.vtable = &libc_vtable,
|
||||
.upstream = NULL_ADDRESS,
|
||||
.context = NULL_ADDRESS,
|
||||
.allocated = 0,
|
||||
.reserved = 0,
|
||||
.acquires = 0,
|
||||
.releases = 0
|
||||
};
|
||||
static const
|
||||
allocator_t libc_allocator_instance = {
|
||||
.vtable = &libc_allocator_vtable
|
||||
};
|
||||
|
||||
allocator_t result;
|
||||
memcpy(
|
||||
&result,
|
||||
&implementation,
|
||||
sizeof(implementation_t)
|
||||
);
|
||||
return result;
|
||||
allocator_mptr_t
|
||||
libc_allocator() {
|
||||
return &libc_allocator_instance;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user