Reworked, no version bump

This commit is contained in:
2026-08-12 19:47:59 -05:00
parent f82f8288cf
commit 70638a9b46
13 changed files with 1170 additions and 1383 deletions
+116 -325
View File
@@ -1,56 +1,130 @@
#include <stdlib.h>
#include <string.h>
#include "malunal/allocators.h"
#include "malunal/assert.h"
#if MALUNAL_PLATFORM_LINUX
# include <sys/mman.h>
# include <unistd.h>
#elif MALUNAL_PLATFORM_WIN32
# include <memoryapi.h>
#endif /* Platform headers */
#include "internal.h"
allocator_acquire_res_t
allocator_acquire(allocator_acquire_req_t request) {
if (request.allocator == NULL_ADDRESS)
return (allocator_acquire_res_t) {
.threw = 1,
.exception = ALLOCATOR_EXCEPTION_NULL_ALLOCATOR
};
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;
}
return request.allocator->vtable.acquire != NULL_ADDRESS
? request.allocator->vtable.acquire(request)
: (allocator_acquire_res_t) {
.threw = 0,
.address = NULL_ADDRESS
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
};
}
allocator_acquire_res_t
allocator_reacquire(allocator_reacquire_req_t request) {
if (request.allocator == NULL_ADDRESS)
return (allocator_acquire_res_t) {
.threw = 1,
.exception = ALLOCATOR_EXCEPTION_NULL_ALLOCATOR
};
return request.allocator->vtable.reacquire != NULL_ADDRESS
? request.allocator->vtable.reacquire(request)
: (allocator_acquire_res_t) {
.threw = 0,
.address = NULL_ADDRESS
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
};
}
allocator_exception_t
allocator_release(allocator_release_req_t request) {
if (request.allocator == NULL_ADDRESS)
return ALLOCATOR_EXCEPTION_NULL_ALLOCATOR;
return request.allocator->vtable.release != NULL_ADDRESS
? request.allocator->vtable.release(request)
: ALLOCATOR_EXCEPTION_SUCCESS;
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
@@ -66,111 +140,6 @@ align_to_page(malunal_size_t size) {
return align_to(size, platform_page_size());
}
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;
#if MALUNAL_PLATFORM_LINUX
g_platform_page_size = sysconf(_SC_PAGESIZE);
#elif MALUNAL_PLATFORM_WIN32
SYSTEM_INFO si;
GetSystemInfo(&si);
g_platform_page_size = si.dwPageSize;
#endif
return g_platform_page_size;
}
static
allocator_acquire_res_t
libc_acquire(allocator_acquire_req_t request);
static
allocator_acquire_res_t
libc_reacquire(allocator_reacquire_req_t request);
static
allocator_exception_t
libc_release(allocator_release_req_t request);
static
allocator_acquire_res_t
libc_acquire(allocator_acquire_req_t request) {
allocator_acquire_res_t result;
malunal_mptr_t addr = malloc(request.size);
if (addr == NULL_ADDRESS) {
return (allocator_acquire_res_t) {
.threw = 1,
.exception = ALLOCATOR_EXCEPTION_OUT_OF_MEMORY
};
}
request.allocator->allocated += request.size;
request.allocator->reserved += request.size;
request.allocator->acquires += 1;
return (allocator_acquire_res_t) {
.threw = 0,
.address = addr
};
}
static
allocator_acquire_res_t
libc_reacquire(allocator_reacquire_req_t request) {
allocator_acquire_res_t result =
libc_acquire((allocator_acquire_req_t) {
.allocator = request.allocator,
.size = request.newsz
});
if (result.threw)
return result;
result.address = memcpy(
result.address,
request.prev,
request.oldsz
);
libc_release((allocator_release_req_t) {
.allocator = request.allocator,
.address = request.prev,
.size = request.oldsz
});
return result;
}
static
allocator_exception_t
libc_release(allocator_release_req_t request) {
free(request.address);
request.allocator->allocated -= request.size;
request.allocator->reserved -= request.size;
request.allocator->releases += 1;
return ALLOCATOR_EXCEPTION_SUCCESS;
}
allocator_t
libc_allocator() {
return (allocator_t) {
.vtable = {
.acquire = &libc_acquire,
.reacquire = &libc_reacquire,
.release = &libc_release
},
.context = NULL_ADDRESS
};
}
allocator_t
platform_allocator() {
#if MALUNAL_PLATFORM_LINUX
@@ -179,181 +148,3 @@ platform_allocator() {
return win32_allocator();
#endif /* Platform specific allocators */
}
#if MALUNAL_PLATFORM_LINUX
static
allocator_acquire_res_t
linux_acquire(allocator_acquire_req_t request);
static
allocator_acquire_res_t
linux_reacquire(allocator_reacquire_req_t request);
static
allocator_exception_t
linux_release(allocator_release_req_t request);
static
allocator_acquire_res_t
linux_acquire(allocator_acquire_req_t request) {
const malunal_int32_t memops = PROT_READ | PROT_WRITE;
const malunal_int32_t memprms = MAP_PRIVATE | MAP_ANONYMOUS;
allocator_acquire_res_t result;
// TODO:
malunal_size_t size = request.size;
malunal_mptr_t ptr = mmap(0, size, memops, memprms, -1, 0);
if (ptr == MAP_FAILED || ptr == NULL_ADDRESS) {
result = (allocator_acquire_res_t) {
.threw = 1,
.exception = ALLOCATOR_EXCEPTION_OUT_OF_MEMORY
};
} else {
result = (allocator_acquire_res_t) {
.threw = 0,
.address = ptr
};
}
return result;
}
static
allocator_acquire_res_t
linux_reacquire(allocator_reacquire_req_t request) {
allocator_acquire_res_t result =
linux_acquire((allocator_acquire_req_t) {
.allocator = request.allocator,
.size = request.newsz
});
if (result.threw)
return result;
result.address = memcpy(
result.address,
request.prev,
request.oldsz
);
linux_release((allocator_release_req_t) {
.allocator = request.allocator,
.address = request.prev,
.size = request.oldsz
});
return result;
}
static
allocator_exception_t
linux_release(allocator_release_req_t request) {
munmap(request.address, request.size);
return ALLOCATOR_EXCEPTION_SUCCESS;
}
allocator_t
linux_allocator() {
return (allocator_t) {
.vtable = {
.acquire = &linux_acquire,
.reacquire = &linux_reacquire,
.release = &linux_release
},
.context = NULL_ADDRESS
};
}
#elif MALUNAL_PLATFORM_WIN32
static
allocator_acquire_res_t
win32_acquire(allocator_acquire_req_t request);
static
allocator_acquire_res_t
win32_reacquire(allocator_reacquire_req_t request);
static
allocator_exception_t
win32_release(allocator_release_req_t request);
static
allocator_acquire_res_t
win32_acquire(allocator_acquire_req_t request) {
const malunal_int32_t memops = MEM_COMMIT | MEM_RESERVE;
const malunal_int32_t pageops = PAGE_READWRITE;
malunal_mptr_t addr = VirtualAlloc(
NULL_ADDRESS,
request.size,
memops,
pageops
);
if (addr == NULL_ADDRESS) {
result = (allocator_acquire_res_t) {
.threw = 1,
.exception = ALLOCATOR_EXCEPTION_OUT_OF_MEMORY
};
} else {
result = (allocator_acquire_res_t) {
.threw = 0,
.address = addr
};
}
return result;
}
static
allocator_acquire_res_t
win32_reacquire(allocator_reacquire_req_t request) {
allocator_acquire_res_t result =
win32_acquire((allocator_acquire_req_t) {
.allocator = request.allocator,
.size = request.newsz
});
if (result.threw)
return result;
result.address = memcpy(
result.address,
request.prev,
request.oldsz
);
win32_release((allocator_release_req_t) {
.allocator = request.allocator,
.address = request.prev,
.size = request.oldsz
});
return result;
}
static
allocator_exception_t
win32_release(allocator_release_req_t request) {
VirtualFree(
request.address,
request.size,
MEM_RELEASE
);
return ALLOCATOR_EXCEPTION_SUCCESS;
}
allocator_t
win32_allocator() {
return (allocator_t) {
.vtable = {
.acquire = &win32_acquire,
.reacquire = &win32_reacquire,
.release = &win32_release
},
.context = NULL_ADDRESS
};
}
#endif /* Platform specific allocators */
+138 -120
View File
@@ -1,180 +1,198 @@
#include <memory.h>
#include "malunal/allocators/arena.h"
#include "malunal/assert.h"
#include "internal.h"
typedef struct ArenaPage page_t;
typedef const page_t* page_iptr_t;
typedef page_t* page_mptr_t;
struct ArenaPage {
page_mptr_t next;
malunal_uint32_t used;
malunal_uint32_t size;
malunal_int8_t data[];
};
allocator_acquire_res_t
create_page(allocator_acquire_req_t request) {
if (request.allocator == NULL_ADDRESS) {
return (allocator_acquire_res_t) {
.threw = 1,
.exception = ALLOCATOR_EXCEPTION_NULL_UPSTREAM
};
}
allocator_acquire_res_t result =
allocator_acquire(request);
allocation_result_t
create_page(
allocator_mptr_t allocator,
malunal_size_t size
) {
malunal_size_t rounded = align_to_page(size);
allocation_result_t result = allocator_acquire(allocator, rounded);
if (result.threw)
return result;
arena_page_mptr_t page = result.address;
page->size = request.size;
page->used = sizeof(arena_page_t);
page_mptr_t page = result.address;
page->size = rounded;
page->used = sizeof(page_t);
page->next = NULL_ADDRESS;
return result;
}
allocator_exception_t
allocation_error_t
delete_page(
allocator_mptr_t allocator,
arena_page_mptr_t page
allocator_mptr_t allocator,
page_mptr_t page
) {
if (allocator == NULL_ADDRESS)
return ALLOCATOR_EXCEPTION_NULL_UPSTREAM;
return ALLOCATION_ERROR_NULL_UPSTREAM;
if (page == NULL_ADDRESS)
return ALLOCATOR_EXCEPTION_FAILURE;
return ALLOCATION_ERROR_FAILURE;
delete_page(allocator, page->next);
return allocator_release((allocator_release_req_t) {
.allocator = allocator,
.address = page,
.size = page->size
});
allocation_error_t err = delete_page(allocator, page->next);
return err == ALLOCATION_ERROR_SUCCESS
? allocator_release(allocator, page, page->size)
: err;
}
allocator_acquire_res_t
page_acquire(allocator_acquire_req_t request) {
arena_page_mptr_t page = request.allocator->context;
if (page == NULL_ADDRESS) {
return (allocator_acquire_res_t) {
.threw = 1,
.exception = ALLOCATOR_EXCEPTION_NULL_CONTEXT
};
}
malunal_uint32_t offset = page->used - sizeof(arena_page_t);
allocation_result_t
page_acquire(
page_mptr_t page,
malunal_size_t size
) {
malunal_uint32_t offset = page->used - sizeof(page_t);
malunal_mptr_t result = page->data + offset;
page->used += request.size;
return (allocator_acquire_res_t) {
page->used += size;
return (allocation_result_t) {
.threw = 0,
.address = result
};
}
allocator_t
arena_allocator(
static
allocation_error_t
arena_initialize(
impl_mptr_t allocator,
allocator_mptr_t upstream,
malunal_size_t size
malunal_size_t capacity
) {
allocator_acquire_req_t request =
(allocator_acquire_req_t) {
.allocator = upstream,
.size = platform_page_size()
};
if (upstream == NULL_ADDRESS)
return ALLOCATION_ERROR_NULL_UPSTREAM;
allocator_acquire_res_t result = create_page(request);
arena_page_mptr_t currpage =
!result.threw ? result.address : NULL_ADDRESS;
allocation_result_t result = create_page(upstream, capacity);
page_mptr_t currpage = !result.threw ? result.address : NULL_ADDRESS;
malunal_size_t reserved = 0;
if (currpage != NULL_ADDRESS) {
reserved += currpage->size;
while (reserved > size) {
result = create_page(request);
if (result.threw)
break;
currpage = currpage->next = result.address;
reserved += currpage->size;
}
}
return (allocator_t) {
.vtable = {
.acquire = (allocator_acquire_pfn_t)&arena_acquire,
.reacquire = (allocator_reacquire_pfn_t)&arena_reacquire,
.release = (allocator_release_pfn_t)&arena_release
},
.upstream = upstream,
.context = currpage
};
allocator->upstream = upstream;
allocator->context = currpage;
allocator->allocated = currpage->used;
allocator->reserved = currpage->size;
return ALLOCATION_ERROR_SUCCESS;
}
allocator_acquire_res_t
arena_acquire(allocator_acquire_req_t request) {
if (request.allocator == NULL_ADDRESS) {
return (allocator_acquire_res_t) {
.threw = 1,
.exception = ALLOCATOR_EXCEPTION_NULL_ALLOCATOR
};
}
static
allocation_error_t
arena_finalize(impl_mptr_t allocator) {
delete_page(allocator->upstream, allocator->context);
}
if (request.size > platform_page_size()) {
return (allocator_acquire_res_t) {
.threw = 1,
.exception = ALLOCATOR_EXCEPTION_OUT_OF_MEMORY
static
allocation_result_t
arena_acquire(
impl_mptr_t allocator,
malunal_size_t size
) {
if (allocator->context == NULL_ADDRESS)
return (allocation_result_t) {
.threw = 1,
.error = ALLOCATION_ERROR_NULL_CONTEXT
};
}
arena_page_mptr_t page = request.allocator->context;
while (page != NULL_ADDRESS) {
struct ArenaPage* page = allocator->context;
if (size > page->size)
return (allocation_result_t) {
.threw = 1,
.error = ALLOCATION_ERROR_OUT_OF_MEMORY
};
while (page->next != NULL_ADDRESS) {
malunal_uint32_t remaining = page->size - page->used;
if (request.size <= remaining)
return page_acquire(request);
if (size <= remaining)
return page_acquire(page, size);
page = page->next;
continue;
}
allocator_acquire_res_t result =
create_page((allocator_acquire_req_t) {
.allocator = request.allocator->upstream,
.size = platform_page_size()
});
allocation_result_t result =
create_page(allocator->upstream, page->size);
page = page->next = !result.threw
? result.address
: NULL_ADDRESS;
return page != NULL_ADDRESS
? page_acquire(request)
: (allocator_acquire_res_t) {
.threw = 1,
.exception = ALLOCATOR_EXCEPTION_OUT_OF_MEMORY
? page_acquire(page, size)
: (allocation_result_t) {
.threw = 1,
.error = ALLOCATION_ERROR_OUT_OF_MEMORY
};
}
allocator_acquire_res_t
arena_reacquire(allocator_reacquire_req_t request) {
return arena_acquire((allocator_acquire_req_t) {
.allocator = request.allocator,
.size = request.newsz
});
static
allocation_result_t
arena_reacquire(
impl_mptr_t allocator,
malunal_mptr_t address,
malunal_size_t oldsize,
malunal_size_t newsize
) {
MALUNAL_UNUSED(address);
MALUNAL_UNUSED(oldsize);
return arena_acquire(allocator, newsize);
}
allocator_exception_t
arena_release(allocator_release_req_t request) {
if (request.allocator == NULL_ADDRESS)
return ALLOCATOR_EXCEPTION_NULL_ALLOCATOR;
if (request.allocator->context == NULL_ADDRESS)
return ALLOCATOR_EXCEPTION_NULL_CONTEXT;
allocation_error_t
arena_release(
impl_mptr_t allocator,
malunal_mptr_t address,
malunal_size_t size
) {
MALUNAL_UNUSED(allocator);
MALUNAL_UNUSED(address);
MALUNAL_UNUSED(size);
}
allocator_exception_t
allocation_error_t
arena_reset(allocator_mptr_t arena) {
if (arena == NULL_ADDRESS)
return ALLOCATOR_EXCEPTION_NULL_ALLOCATOR;
return ALLOCATION_ERROR_NULL_ALLOCATOR;
arena_page_mptr_t page = arena->context;
impl_mptr_t impl = (impl_mptr_t)arena;
page_mptr_t page = impl->context;
while (page != NULL_ADDRESS) {
page->used = 0;
page->used = sizeof(page_t);
page = page->next;
}
}
allocator_exception_t
arena_free(allocator_mptr_t arena) {
if (arena == NULL_ADDRESS)
return ALLOCATOR_EXCEPTION_NULL_ALLOCATOR;
delete_page(arena->upstream, arena->context);
static const
virtual_table_t arena_vtable = {
.initialize = (allocator_initialize_pfn_t)&arena_initialize,
.finalize = (allocator_finalize_pfn_t)&arena_finalize,
.acquire = (allocator_acquire_pfn_t)&arena_acquire,
.reacquire = (allocator_reacquire_pfn_t)&arena_reacquire,
.release = (allocator_release_pfn_t)&arena_release
};
allocator_t
arena_allocator() {
implementation_t implementation = {
.vtable = &arena_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;
}
+75
View File
@@ -0,0 +1,75 @@
/**
* @file internal.h
* @brief This header contains the definition of the internal allocator
* implementation and function pointers for the allocator virtual
* function table.
* @author John Christman (sorakatadzuma@gmail.com)
* @copyright Malunal Studios, LLC.
*/
#include "malunal/allocators.h"
#ifndef MALUNAL_ALLOCATORS_INTERNAL_HEADER
#define MALUNAL_ALLOCATORS_INTERNAL_HEADER
typedef allocation_error_t
(*allocator_initialize_pfn_t)(
allocator_mptr_t allocator,
allocator_mptr_t upstream,
malunal_size_t capacity
);
typedef allocation_error_t
(*allocator_finalize_pfn_t)(
allocator_mptr_t allocator
);
typedef allocation_result_t
(*allocator_acquire_pfn_t)(
allocator_mptr_t allocator,
malunal_size_t size
);
typedef allocation_result_t
(*allocator_reacquire_pfn_t)(
allocator_mptr_t allocator,
malunal_mptr_t address,
malunal_size_t oldsize,
malunal_size_t newsize
);
typedef allocation_error_t
(*allocator_release_pfn_t)(
allocator_mptr_t allocator,
malunal_mptr_t address,
malunal_size_t size
);
typedef struct {
allocator_initialize_pfn_t const initialize;
allocator_finalize_pfn_t const finalize;
allocator_acquire_pfn_t const acquire;
allocator_reacquire_pfn_t const reacquire;
allocator_release_pfn_t const release;
} virtual_table_t;
typedef virtual_table_t* vtable_mptr_t;
typedef const virtual_table_t* vtable_iptr_t;
typedef struct {
vtable_iptr_t const vtable;
allocator_mptr_t upstream;
malunal_mptr_t context;
malunal_size_t allocated;
malunal_size_t reserved;
malunal_size_t acquires;
malunal_size_t releases;
} implementation_t;
typedef implementation_t* impl_mptr_t;
typedef const implementation_t* impl_iptr_t;
#endif /* MALUNAL_ALLOCATORS_INTERNAL_HEADER */
+109
View File
@@ -0,0 +1,109 @@
#include <stdlib.h>
#include <memory.h>
#include "internal.h"
static
allocation_error_t
libc_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
libc_finalize(impl_mptr_t allocator) {
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
) {
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;
}
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_t
libc_allocator() {
implementation_t implementation = {
.vtable = &libc_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;
}
+132
View File
@@ -0,0 +1,132 @@
#include "malunal/config.h"
#if MALUNAL_PLATFORM_LINUX
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include "internal.h"
static
malunal_size_t
g_platform_page_size = 0;
malunal_size_t
platform_page_size() {
return g_platform_page_size == 0
? g_platform_page_size = sysconf(_SC_PAGE_SIZE)
: g_platform_page_size;
}
static
allocation_error_t
linux_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
linux_finalize(impl_mptr_t allocator) {
MALUNAL_UNUSED(allocator);
return ALLOCATION_ERROR_SUCCESS;
}
static
allocation_result_t
linux_acquire(
impl_mptr_t allocator,
malunal_size_t size
) {
MALUNAL_UNUSED(allocator);
const malunal_int32_t memops = PROT_READ | PROT_WRITE;
const malunal_int32_t memprms = MAP_PRIVATE | MAP_ANONYMOUS;
malunal_mptr_t ptr = mmap(0, size, memops, memprms, -1, 0);
if (ptr == MAP_FAILED || ptr == 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 = ptr
};
}
static
allocation_error_t
linux_release(
impl_mptr_t allocator,
malunal_mptr_t address,
malunal_size_t size
) {
MALUNAL_UNUSED(allocator);
munmap(address, size);
allocator->allocated -= size;
allocator->reserved -= size;
allocator->releases += 1;
return ALLOCATION_ERROR_SUCCESS;
}
static
allocation_result_t
linux_reacquire(
impl_mptr_t allocator,
malunal_mptr_t address,
malunal_size_t oldsize,
malunal_size_t newsize
) {
allocation_result_t result = linux_acquire(allocator, newsize);
if (result.threw)
return result;
result.address = memcpy(result.address, address, oldsize);
linux_release(allocator, address, oldsize);
return result;
}
static const
virtual_table_t linux_vtable = {
.initialize = (allocator_initialize_pfn_t)&linux_initialize,
.finalize = (allocator_finalize_pfn_t)&linux_finalize,
.acquire = (allocator_acquire_pfn_t)&linux_acquire,
.reacquire = (allocator_reacquire_pfn_t)&linux_reacquire,
.release = (allocator_release_pfn_t)&linux_release
};
allocator_t
linux_allocator() {
implementation_t implementation = {
.vtable = &linux_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;
}
#endif /* MALUNAL_PLATFORM_LINUX */
+134
View File
@@ -0,0 +1,134 @@
#include "malunal/config.h"
#if MALUNAL_PLATFORM_WIN32
#include <stdlib.h>
#include <string.h>
#include <memoryapi.h>
#include "internal.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
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
) {
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 (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
win32_release(
impl_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;
}
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_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;
}
#endif /* MALUNAL_PLATFORM_WIN32 */