New allocators, testing framework, overall improvements
This commit is contained in:
+27
-87
@@ -2,9 +2,8 @@
|
||||
|
||||
#if MALUNAL_PLATFORM_WIN32
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <memoryapi.h>
|
||||
#include "internal.h"
|
||||
#include "malunal/allocator.h"
|
||||
|
||||
|
||||
static
|
||||
@@ -23,30 +22,11 @@ platform_page_size() {
|
||||
}
|
||||
|
||||
static
|
||||
allocation_error_t
|
||||
win32_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
|
||||
win32_finalize(impl_mptr_t allocator) {
|
||||
MALUNAL_UNUSED(allocator);
|
||||
return ALLOCATION_ERROR_SUCCESS;
|
||||
}
|
||||
|
||||
static
|
||||
allocation_result_t
|
||||
win32_acquire(
|
||||
impl_mptr_t allocator,
|
||||
malunal_size_t size
|
||||
error_t
|
||||
win32_allocator_acquire(
|
||||
allocator_mptr_t allocator,
|
||||
malunal_size_t size,
|
||||
malunal_mptr_t* out
|
||||
) {
|
||||
MALUNAL_UNUSED(allocator);
|
||||
|
||||
@@ -55,80 +35,40 @@ win32_acquire(
|
||||
|
||||
malunal_mptr_t addr = VirtualAlloc(NULL_ADDRESS, size, memops, pageops);
|
||||
if (addr == NULL_ADDRESS)
|
||||
return (allocation_result_t) {
|
||||
.threw = 1,
|
||||
.error = ALLOCATION_ERROR_OUT_OF_MEMORY
|
||||
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 = addr
|
||||
};
|
||||
*out = addr;
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
static
|
||||
allocation_error_t
|
||||
win32_release(
|
||||
impl_mptr_t allocator,
|
||||
malunal_mptr_t address,
|
||||
malunal_size_t size
|
||||
error_t
|
||||
win32_allocator_release(
|
||||
allocator_mptr_t allocator,
|
||||
malunal_mptr_t address,
|
||||
malunal_size_t size
|
||||
) {
|
||||
MALUNAL_UNUSED(allocator);
|
||||
VirtualFree(address, size, MEM_RELEASE);
|
||||
allocator->allocated -= size;
|
||||
allocator->reserved -= size;
|
||||
allocator->releases += 1;
|
||||
return ALLOCATION_ERROR_SUCCESS;
|
||||
}
|
||||
|
||||
static
|
||||
allocation_result_t
|
||||
win32_reacquire(
|
||||
impl_mptr_t allocator,
|
||||
malunal_mptr_t address,
|
||||
malunal_size_t oldsize,
|
||||
malunal_size_t newsize
|
||||
) {
|
||||
allocation_result_t result = win32_acquire(allocator, newsize);
|
||||
if (result.threw)
|
||||
return result;
|
||||
|
||||
result.address = memcpy(result.address, address, oldsize);
|
||||
win32_release(allocator, address, oldsize);
|
||||
return result;
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
static const
|
||||
virtual_table_t win32_vtable = {
|
||||
.initialize = (allocator_initialize_pfn_t)&win32_initialize,
|
||||
.finalize = (allocator_finalize_pfn_t)&win32_finalize,
|
||||
.acquire = (allocator_acquire_pfn_t)&win32_acquire,
|
||||
.reacquire = (allocator_reacquire_pfn_t)&win32_reacquire,
|
||||
.release = (allocator_release_pfn_t)&win32_release
|
||||
allocator_vtable_t win32_alllocator_vtable = {
|
||||
.acquire = (allocator_acquire_pfn_t)&win32_allocator_acquire,
|
||||
.dispose = (allocator_dispose_pfn_t)&win32_allocator_release
|
||||
};
|
||||
|
||||
allocator_t
|
||||
static const
|
||||
allocator_t win32_allocator_instance = {
|
||||
.vtable = &win32_alllocator_vtable
|
||||
};
|
||||
|
||||
allocator_mptr_t
|
||||
win32_allocator() {
|
||||
implementation_t implementation = {
|
||||
.vtable = &win32_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 &win32_allocator_instance;
|
||||
}
|
||||
#endif /* MALUNAL_PLATFORM_WIN32 */
|
||||
|
||||
Reference in New Issue
Block a user