151 lines
3.7 KiB
C
151 lines
3.7 KiB
C
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include "internal.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;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
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
|
|
allocator_acquire(
|
|
allocator_mptr_t allocator,
|
|
malunal_size_t size
|
|
) {
|
|
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
|
|
};
|
|
}
|
|
|
|
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(
|
|
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;
|
|
}
|
|
|
|
malunal_size_t
|
|
align_to(
|
|
malunal_size_t size,
|
|
malunal_size_t alignment
|
|
) {
|
|
return (size + alignment - 1) & ~(alignment - 1);
|
|
}
|
|
|
|
malunal_size_t
|
|
align_to_page(malunal_size_t size) {
|
|
return align_to(size, platform_page_size());
|
|
}
|
|
|
|
allocator_t
|
|
platform_allocator() {
|
|
#if MALUNAL_PLATFORM_LINUX
|
|
return linux_allocator();
|
|
#elif MALUNAL_PLATFORM_WIN32
|
|
return win32_allocator();
|
|
#endif /* Platform specific allocators */
|
|
}
|