New allocators, testing framework, overall improvements
This commit is contained in:
+52
-111
@@ -1,130 +1,71 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "internal.h"
|
||||
#include "malunal/allocator.h"
|
||||
|
||||
|
||||
allocation_error_t
|
||||
allocator_upstream(
|
||||
allocator_mptr_t allocator,
|
||||
allocator_mptr_t* out
|
||||
) {
|
||||
impl_mptr_t implementation = (impl_mptr_t)allocator;
|
||||
if (implementation == NULL_ADDRESS)
|
||||
return ALLOCATION_ERROR_NULL_ALLOCATOR;
|
||||
*out = implementation->upstream;
|
||||
return ALLOCATION_ERROR_SUCCESS;
|
||||
const uuid_t UUID_ALLOCATOR_T = {
|
||||
.tl = 0xEB14C3C3,
|
||||
.tm = 0x5F0E,
|
||||
.thv = 0x48A4,
|
||||
.csr = 0x8C,
|
||||
.csl = 0xFD,
|
||||
.nbs = {
|
||||
0x58, 0x04, 0x13,
|
||||
0x19, 0xF9, 0xB2
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
static
|
||||
malunal_cstr_t
|
||||
describe(malunal_int32_t code) {
|
||||
switch (code) {
|
||||
case ALLOCATOR_ERROR_FAILURE:
|
||||
return "Generic allocator error";
|
||||
case ALLOCATOR_ERROR_NULL_ALLOCATOR:
|
||||
return "Allocator provided was null";
|
||||
case ALLOCATOR_ERROR_OUT_OF_MEMORY:
|
||||
return "Allocator out of memory";
|
||||
case ALLOCATOR_ERROR_NOT_MY_ADDRESS:
|
||||
return "Address does not belong to allocator";
|
||||
case ALLOCATOR_ERROR_LEAKY_MEMORY:
|
||||
return "Allocator is leaking memory";
|
||||
}
|
||||
return "Unknown allocator error";
|
||||
}
|
||||
|
||||
allocation_error_t
|
||||
allocator_allocated_bytes(
|
||||
allocator_mptr_t allocator,
|
||||
malunal_size_t* out
|
||||
) {
|
||||
impl_mptr_t implementation = (impl_mptr_t)allocator;
|
||||
if (implementation == NULL_ADDRESS)
|
||||
return ALLOCATION_ERROR_NULL_ALLOCATOR;
|
||||
*out = implementation->allocated;
|
||||
return ALLOCATION_ERROR_SUCCESS;
|
||||
}
|
||||
const error_domain_t ERROR_DOMAIN_ALLOCATOR_T = {
|
||||
.describe = &describe,
|
||||
.name = "malunal.allocator.error"
|
||||
};
|
||||
|
||||
allocation_error_t
|
||||
allocator_reserved_bytes(
|
||||
allocator_mptr_t allocator,
|
||||
malunal_size_t* out
|
||||
) {
|
||||
impl_mptr_t implementation = (impl_mptr_t)allocator;
|
||||
if (implementation == NULL_ADDRESS)
|
||||
return ALLOCATION_ERROR_NULL_ALLOCATOR;
|
||||
*out = implementation->reserved;
|
||||
return ALLOCATION_ERROR_SUCCESS;
|
||||
}
|
||||
|
||||
allocation_error_t
|
||||
allocator_acquires_count(
|
||||
allocator_mptr_t allocator,
|
||||
malunal_size_t* out
|
||||
) {
|
||||
impl_mptr_t implementation = (impl_mptr_t)allocator;
|
||||
if (implementation == NULL_ADDRESS)
|
||||
return ALLOCATION_ERROR_NULL_ALLOCATOR;
|
||||
*out = implementation->acquires;
|
||||
return ALLOCATION_ERROR_SUCCESS;
|
||||
}
|
||||
|
||||
allocation_error_t
|
||||
allocator_releases_count(
|
||||
allocator_mptr_t allocator,
|
||||
malunal_size_t* out
|
||||
) {
|
||||
impl_mptr_t implementation = (impl_mptr_t)allocator;
|
||||
if (implementation == NULL_ADDRESS)
|
||||
return ALLOCATION_ERROR_NULL_ALLOCATOR;
|
||||
*out = implementation->releases;
|
||||
return ALLOCATION_ERROR_SUCCESS;
|
||||
}
|
||||
|
||||
allocation_error_t
|
||||
allocator_initialize(
|
||||
allocator_mptr_t allocator,
|
||||
allocator_mptr_t upstream,
|
||||
malunal_size_t capacity
|
||||
) {
|
||||
impl_mptr_t implementation = (impl_mptr_t)allocator;
|
||||
return implementation != NULL_ADDRESS
|
||||
? implementation->vtable->initialize(allocator, upstream, capacity)
|
||||
: ALLOCATION_ERROR_NULL_ALLOCATOR;
|
||||
}
|
||||
|
||||
allocation_error_t
|
||||
allocator_finalize(
|
||||
allocator_mptr_t allocator
|
||||
) {
|
||||
impl_mptr_t implementation = (impl_mptr_t)allocator;
|
||||
return implementation != NULL_ADDRESS
|
||||
? implementation->vtable->finalize(allocator)
|
||||
: ALLOCATION_ERROR_NULL_ALLOCATOR;
|
||||
}
|
||||
|
||||
allocation_result_t
|
||||
error_t
|
||||
allocator_acquire(
|
||||
allocator_mptr_t allocator,
|
||||
malunal_size_t size
|
||||
malunal_size_t size,
|
||||
malunal_mptr_t* out
|
||||
) {
|
||||
impl_mptr_t implementation = (impl_mptr_t)allocator;
|
||||
return implementation != NULL_ADDRESS
|
||||
? implementation->vtable->acquire(allocator, size)
|
||||
: (allocation_result_t) {
|
||||
.threw = 1,
|
||||
.error = ALLOCATION_ERROR_NULL_ALLOCATOR
|
||||
};
|
||||
return allocator != null
|
||||
? allocator->vtable->acquire(allocator, size, out)
|
||||
: (error_t) {
|
||||
.domain = &ERROR_DOMAIN_ALLOCATOR_T,
|
||||
.code = ALLOCATOR_ERROR_NULL_ALLOCATOR
|
||||
};
|
||||
}
|
||||
|
||||
allocation_result_t
|
||||
allocator_reacquire(
|
||||
allocator_mptr_t allocator,
|
||||
malunal_mptr_t address,
|
||||
malunal_size_t oldsize,
|
||||
malunal_size_t newsize
|
||||
) {
|
||||
impl_mptr_t implementation = (impl_mptr_t)allocator;
|
||||
return implementation != NULL_ADDRESS
|
||||
? implementation->vtable->reacquire(allocator, address, oldsize, newsize)
|
||||
: (allocation_result_t) {
|
||||
.threw = 1,
|
||||
.error = ALLOCATION_ERROR_NULL_ALLOCATOR
|
||||
};
|
||||
}
|
||||
|
||||
allocation_error_t
|
||||
allocator_release(
|
||||
error_t
|
||||
allocator_dispose(
|
||||
allocator_mptr_t allocator,
|
||||
malunal_mptr_t address,
|
||||
malunal_size_t size
|
||||
) {
|
||||
impl_mptr_t implementation = (impl_mptr_t)allocator;
|
||||
return implementation != NULL_ADDRESS
|
||||
? implementation->vtable->release(allocator, address, size)
|
||||
: ALLOCATION_ERROR_NULL_ALLOCATOR;
|
||||
return allocator != null
|
||||
? allocator->vtable->dispose(allocator, address, size)
|
||||
: (error_t) {
|
||||
.domain = &ERROR_DOMAIN_ALLOCATOR_T,
|
||||
.code = ALLOCATOR_ERROR_NULL_ALLOCATOR
|
||||
};
|
||||
}
|
||||
|
||||
malunal_size_t
|
||||
@@ -140,7 +81,7 @@ align_to_page(malunal_size_t size) {
|
||||
return align_to(size, platform_page_size());
|
||||
}
|
||||
|
||||
allocator_t
|
||||
allocator_mptr_t
|
||||
platform_allocator() {
|
||||
#if MALUNAL_PLATFORM_LINUX
|
||||
return linux_allocator();
|
||||
|
||||
Reference in New Issue
Block a user