Files
allocators/sources/win32.c
T

75 lines
1.5 KiB
C

#include "malunal/config.h"
#if MALUNAL_PLATFORM_WIN32
#include <stdlib.h>
#include <memoryapi.h>
#include "malunal/allocator.h"
static
malunal_size_t
g_platform_page_size = 0;
malunal_size_t
platform_page_size() {
if (g_platform_page_size != 0)
return g_platform_page_size;
SYSTEM_INFO si;
GetSystemInfo(&si);
g_platform_page_size = si.dwPageSize;
return g_platform_page_size;
}
static
error_t
win32_allocator_acquire(
allocator_mptr_t allocator,
malunal_size_t size,
malunal_mptr_t* out
) {
MALUNAL_UNUSED(allocator);
const malunal_int32_t memops = MEM_COMMIT | MEM_RESERVE;
const malunal_int32_t pageops = PAGE_READWRITE;
malunal_mptr_t addr = VirtualAlloc(NULL_ADDRESS, size, memops, pageops);
if (addr == NULL_ADDRESS)
return (error_t) {
.domain = &ERROR_DOMAIN_ALLOCATOR_T,
.code = ALLOCATOR_ERROR_OUT_OF_MEMORY
};
*out = addr;
return NO_ERROR;
}
static
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);
return NO_ERROR;
}
static const
allocator_vtable_t win32_alllocator_vtable = {
.acquire = (allocator_acquire_pfn_t)&win32_allocator_acquire,
.dispose = (allocator_dispose_pfn_t)&win32_allocator_release
};
static const
allocator_t win32_allocator_instance = {
.vtable = &win32_alllocator_vtable
};
allocator_mptr_t
win32_allocator() {
return &win32_allocator_instance;
}
#endif /* MALUNAL_PLATFORM_WIN32 */