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();
|
||||
|
||||
+220
-180
@@ -1,198 +1,238 @@
|
||||
#include <memory.h>
|
||||
#include "internal.h"
|
||||
#include "malunal/allocators/arena.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;
|
||||
typedef struct {
|
||||
malunal_uint32_t size;
|
||||
malunal_int8_t data[];
|
||||
};
|
||||
malunal_uint32_t used;
|
||||
malunal_mptr_t next;
|
||||
malunal_uint8_t bytes[];
|
||||
} region_t;
|
||||
|
||||
typedef region_t* region_mptr_t;
|
||||
typedef const region_t* region_iptr_t;
|
||||
|
||||
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)
|
||||
typedef struct {
|
||||
allocator_t allocator;
|
||||
region_mptr_t context;
|
||||
} impl_t;
|
||||
|
||||
typedef impl_t* impl_mptr_t;
|
||||
typedef const impl_t* impl_iptr_t;
|
||||
_Static_assert(
|
||||
sizeof(arena_allocator_t) == sizeof(impl_t),
|
||||
"Arena allocator must be the size of its implementation"
|
||||
);
|
||||
|
||||
error_t
|
||||
create_region(region_mptr_t* region) {
|
||||
allocator_mptr_t allocator = platform_allocator();
|
||||
malunal_size_t rounded = align_to_page(MALUNAL_ARENA_REGION_SIZE);
|
||||
malunal_mptr_t* address = (malunal_mptr_t*)region;
|
||||
error_t result = allocator_acquire(allocator, rounded, address);
|
||||
|
||||
if (result.domain != null)
|
||||
return result;
|
||||
|
||||
page_mptr_t page = result.address;
|
||||
page->size = rounded;
|
||||
page->used = sizeof(page_t);
|
||||
page->next = NULL_ADDRESS;
|
||||
return result;
|
||||
(*region)->size = rounded;
|
||||
(*region)->used = sizeof(region_t);
|
||||
(*region)->next = null;
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
allocation_error_t
|
||||
delete_page(
|
||||
allocator_mptr_t allocator,
|
||||
page_mptr_t page
|
||||
error_t
|
||||
delete_region(region_mptr_t region) {
|
||||
if (region == null)
|
||||
return NO_ERROR;
|
||||
|
||||
error_t result = delete_region(region->next);
|
||||
if (result.domain != null)
|
||||
return result;
|
||||
|
||||
region->next = null;
|
||||
allocator_mptr_t allocator = platform_allocator();
|
||||
return allocator_dispose(allocator, region, region->size);
|
||||
}
|
||||
|
||||
error_t
|
||||
region_acquire(
|
||||
region_mptr_t region,
|
||||
malunal_size_t size,
|
||||
malunal_mptr_t* out
|
||||
) {
|
||||
if (allocator == NULL_ADDRESS)
|
||||
return ALLOCATION_ERROR_NULL_UPSTREAM;
|
||||
|
||||
if (page == NULL_ADDRESS)
|
||||
return ALLOCATION_ERROR_FAILURE;
|
||||
|
||||
allocation_error_t err = delete_page(allocator, page->next);
|
||||
return err == ALLOCATION_ERROR_SUCCESS
|
||||
? allocator_release(allocator, page, page->size)
|
||||
: err;
|
||||
}
|
||||
|
||||
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 += size;
|
||||
return (allocation_result_t) {
|
||||
.threw = 0,
|
||||
.address = result
|
||||
};
|
||||
}
|
||||
|
||||
static
|
||||
allocation_error_t
|
||||
arena_initialize(
|
||||
impl_mptr_t allocator,
|
||||
allocator_mptr_t upstream,
|
||||
malunal_size_t capacity
|
||||
) {
|
||||
if (upstream == NULL_ADDRESS)
|
||||
return ALLOCATION_ERROR_NULL_UPSTREAM;
|
||||
|
||||
allocation_result_t result = create_page(upstream, capacity);
|
||||
page_mptr_t currpage = !result.threw ? result.address : NULL_ADDRESS;
|
||||
|
||||
allocator->upstream = upstream;
|
||||
allocator->context = currpage;
|
||||
allocator->allocated = currpage->used;
|
||||
allocator->reserved = currpage->size;
|
||||
return ALLOCATION_ERROR_SUCCESS;
|
||||
}
|
||||
|
||||
static
|
||||
allocation_error_t
|
||||
arena_finalize(impl_mptr_t allocator) {
|
||||
delete_page(allocator->upstream, allocator->context);
|
||||
}
|
||||
|
||||
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
|
||||
};
|
||||
|
||||
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 (size <= remaining)
|
||||
return page_acquire(page, size);
|
||||
page = page->next;
|
||||
continue;
|
||||
}
|
||||
|
||||
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(page, size)
|
||||
: (allocation_result_t) {
|
||||
.threw = 1,
|
||||
.error = ALLOCATION_ERROR_OUT_OF_MEMORY
|
||||
};
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
allocation_error_t
|
||||
arena_reset(allocator_mptr_t arena) {
|
||||
if (arena == NULL_ADDRESS)
|
||||
return ALLOCATION_ERROR_NULL_ALLOCATOR;
|
||||
|
||||
impl_mptr_t impl = (impl_mptr_t)arena;
|
||||
page_mptr_t page = impl->context;
|
||||
while (page != NULL_ADDRESS) {
|
||||
page->used = sizeof(page_t);
|
||||
page = page->next;
|
||||
}
|
||||
malunal_uint32_t offset =
|
||||
region->used - sizeof(region_t);
|
||||
*out = region->bytes + offset;
|
||||
region->used += size;
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
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_vtable_t arena_allocator_vtable = {
|
||||
.acquire = (allocator_acquire_pfn_t)&arena_allocator_acquire,
|
||||
.dispose = (allocator_dispose_pfn_t)&arena_allocator_dispose
|
||||
};
|
||||
|
||||
allocator_t
|
||||
arena_allocator() {
|
||||
implementation_t implementation = {
|
||||
.vtable = &arena_vtable,
|
||||
.upstream = NULL_ADDRESS,
|
||||
.context = NULL_ADDRESS,
|
||||
.allocated = 0,
|
||||
.reserved = 0,
|
||||
.acquires = 0,
|
||||
.releases = 0
|
||||
};
|
||||
error_t
|
||||
arena_allocator_size(
|
||||
arena_allocator_iptr_t allocator,
|
||||
malunal_uint32_t* out
|
||||
) {
|
||||
if (allocator == null)
|
||||
return (error_t) {
|
||||
.domain = &ERROR_DOMAIN_ALLOCATOR_T,
|
||||
.code = ALLOCATOR_ERROR_NULL_ALLOCATOR
|
||||
};
|
||||
|
||||
allocator_t result;
|
||||
memcpy(
|
||||
&result,
|
||||
&implementation,
|
||||
sizeof(implementation_t)
|
||||
);
|
||||
impl_iptr_t self = (impl_iptr_t)allocator;
|
||||
region_iptr_t region = self->context;
|
||||
malunal_size_t total = 0;
|
||||
while (region != null) {
|
||||
total += region->size;
|
||||
region = region->next;
|
||||
}
|
||||
|
||||
return result;
|
||||
*out = total;
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
error_t
|
||||
arena_allocator_used(
|
||||
arena_allocator_iptr_t allocator,
|
||||
malunal_uint32_t* out
|
||||
) {
|
||||
if (allocator == null)
|
||||
return (error_t) {
|
||||
.domain = &ERROR_DOMAIN_ALLOCATOR_T,
|
||||
.code = ALLOCATOR_ERROR_NULL_ALLOCATOR
|
||||
};
|
||||
|
||||
impl_iptr_t self = (impl_iptr_t)allocator;
|
||||
region_iptr_t region = self->context;
|
||||
malunal_size_t total = 0;
|
||||
while (region != null) {
|
||||
total += region->used;
|
||||
region = region->next;
|
||||
}
|
||||
|
||||
*out = total;
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
error_t
|
||||
arena_allocator_init(
|
||||
malunal_size_t capacity,
|
||||
arena_allocator_mptr_t allocator
|
||||
) {
|
||||
if (allocator == null)
|
||||
return (error_t) {
|
||||
.domain = &ERROR_DOMAIN_ALLOCATOR_T,
|
||||
.code = ALLOCATOR_ERROR_NULL_ALLOCATOR
|
||||
};
|
||||
|
||||
region_mptr_t region = null;
|
||||
error_t result = create_region(®ion);
|
||||
if (result.domain != null)
|
||||
return result;
|
||||
|
||||
impl_mptr_t self = (impl_mptr_t)allocator;
|
||||
self->allocator = (allocator_t){ &arena_allocator_vtable };
|
||||
self->context = region;
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
error_t
|
||||
arena_allocator_acquire(
|
||||
arena_allocator_mptr_t allocator,
|
||||
malunal_size_t size,
|
||||
malunal_mptr_t* out
|
||||
) {
|
||||
if (allocator == null)
|
||||
return (error_t) {
|
||||
.domain = &ERROR_DOMAIN_ALLOCATOR_T,
|
||||
.code = ALLOCATOR_ERROR_NULL_ALLOCATOR
|
||||
};
|
||||
|
||||
impl_mptr_t self = (impl_mptr_t)allocator;
|
||||
region_mptr_t region = self->context;
|
||||
while (region != null) {
|
||||
if (region->size - region->used >= size)
|
||||
return region_acquire(region, size, out);
|
||||
region = region->next;
|
||||
}
|
||||
|
||||
error_t result = create_region((region_mptr_t*)®ion->next);
|
||||
return result.domain == null
|
||||
? region_acquire(region->next, size, out)
|
||||
: result;
|
||||
}
|
||||
|
||||
error_t
|
||||
arena_allocator_dispose(
|
||||
arena_allocator_mptr_t allocator,
|
||||
malunal_mptr_t address,
|
||||
malunal_size_t size
|
||||
) {
|
||||
if (allocator == null)
|
||||
return (error_t) {
|
||||
.domain = &ERROR_DOMAIN_ALLOCATOR_T,
|
||||
.code = ALLOCATOR_ERROR_NULL_ALLOCATOR
|
||||
};
|
||||
|
||||
impl_mptr_t self = (impl_mptr_t)allocator;
|
||||
region_mptr_t reg = self->context;
|
||||
malunal_uint8_t* addr = address;
|
||||
malunal_uint8_t* beg = reg->bytes;
|
||||
malunal_uint8_t* end = reg->bytes + reg->size - sizeof(region_t);
|
||||
while (true) {
|
||||
if (addr < beg || addr > end)
|
||||
return (error_t) {
|
||||
.domain = &ERROR_DOMAIN_ALLOCATOR_T,
|
||||
.code = ALLOCATOR_ERROR_NOT_MY_ADDRESS
|
||||
};
|
||||
|
||||
reg = reg->next;
|
||||
if (reg == null)
|
||||
break;
|
||||
beg = reg->bytes;
|
||||
end = reg->bytes + reg->size - sizeof(region_t);
|
||||
}
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
error_t
|
||||
arena_allocator_reset(
|
||||
arena_allocator_mptr_t allocator
|
||||
) {
|
||||
if (allocator == null)
|
||||
return (error_t) {
|
||||
.domain = &ERROR_DOMAIN_ALLOCATOR_T,
|
||||
.code = ALLOCATOR_ERROR_NULL_ALLOCATOR
|
||||
};
|
||||
|
||||
impl_mptr_t self = (impl_mptr_t)allocator;
|
||||
region_mptr_t region = self->context;
|
||||
while (region != null) {
|
||||
region->used = sizeof(region_t);
|
||||
region = region->next;
|
||||
}
|
||||
}
|
||||
|
||||
error_t
|
||||
arena_allocator_free(
|
||||
arena_allocator_mptr_t allocator
|
||||
) {
|
||||
if (allocator == null)
|
||||
return (error_t) {
|
||||
.domain = &ERROR_DOMAIN_ALLOCATOR_T,
|
||||
.code = ALLOCATOR_ERROR_NULL_ALLOCATOR
|
||||
};
|
||||
|
||||
impl_mptr_t self = (impl_mptr_t)allocator;
|
||||
error_t result = delete_region(self->context);
|
||||
if (result.domain != null)
|
||||
return result;
|
||||
|
||||
self->context = null;
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,75 +0,0 @@
|
||||
/**
|
||||
* @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 */
|
||||
+32
-92
@@ -1,109 +1,49 @@
|
||||
#include <stdlib.h>
|
||||
#include <memory.h>
|
||||
#include "internal.h"
|
||||
|
||||
#include "malunal/allocator.h"
|
||||
|
||||
static
|
||||
allocation_error_t
|
||||
libc_initialize(
|
||||
impl_mptr_t allocator,
|
||||
allocator_mptr_t upstream,
|
||||
malunal_size_t capacity
|
||||
error_t
|
||||
libc_allocator_acquire(
|
||||
allocator_mptr_t allocator,
|
||||
malunal_size_t size,
|
||||
malunal_mptr_t* out
|
||||
) {
|
||||
MALUNAL_UNUSED(allocator);
|
||||
MALUNAL_UNUSED(upstream);
|
||||
MALUNAL_UNUSED(capacity);
|
||||
return ALLOCATION_ERROR_SUCCESS;
|
||||
|
||||
*out = malloc(size);
|
||||
return *out == null
|
||||
? (error_t) {
|
||||
.domain = &ERROR_DOMAIN_ALLOCATOR_T,
|
||||
.code = ALLOCATOR_ERROR_OUT_OF_MEMORY
|
||||
}
|
||||
: NO_ERROR;
|
||||
}
|
||||
|
||||
static
|
||||
allocation_error_t
|
||||
libc_finalize(impl_mptr_t allocator) {
|
||||
error_t
|
||||
libc_allocator_dispose(
|
||||
allocator_mptr_t allocator,
|
||||
malunal_mptr_t address,
|
||||
malunal_size_t size
|
||||
) {
|
||||
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
|
||||
) {
|
||||
MALUNAL_UNUSED(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;
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
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_vtable_t libc_allocator_vtable = {
|
||||
.acquire = (allocator_acquire_pfn_t)&libc_allocator_acquire,
|
||||
.dispose = (allocator_dispose_pfn_t)&libc_allocator_dispose
|
||||
};
|
||||
|
||||
allocator_t
|
||||
libc_allocator() {
|
||||
implementation_t implementation = {
|
||||
.vtable = &libc_vtable,
|
||||
.upstream = NULL_ADDRESS,
|
||||
.context = NULL_ADDRESS,
|
||||
.allocated = 0,
|
||||
.reserved = 0,
|
||||
.acquires = 0,
|
||||
.releases = 0
|
||||
};
|
||||
static const
|
||||
allocator_t libc_allocator_instance = {
|
||||
.vtable = &libc_allocator_vtable
|
||||
};
|
||||
|
||||
allocator_t result;
|
||||
memcpy(
|
||||
&result,
|
||||
&implementation,
|
||||
sizeof(implementation_t)
|
||||
);
|
||||
return result;
|
||||
allocator_mptr_t
|
||||
libc_allocator() {
|
||||
return &libc_allocator_instance;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,139 @@
|
||||
#include "malunal/allocators/linear.h"
|
||||
|
||||
typedef struct {
|
||||
allocator_t allocator;
|
||||
malunal_size_t size;
|
||||
malunal_size_t used;
|
||||
malunal_uint8_t* buffer;
|
||||
} impl_t;
|
||||
|
||||
typedef impl_t* impl_mptr_t;
|
||||
typedef const impl_t* impl_iptr_t;
|
||||
_Static_assert(
|
||||
sizeof(linear_allocator_t) == sizeof(impl_t),
|
||||
"Linear allocator must be the size of its implementation"
|
||||
);
|
||||
|
||||
static const
|
||||
allocator_vtable_t linear_allocator_vtable = {
|
||||
.acquire = (allocator_acquire_pfn_t)&linear_allocator_acquire,
|
||||
.dispose = (allocator_dispose_pfn_t)&linear_allocator_dispose
|
||||
};
|
||||
|
||||
error_t
|
||||
linear_allocator_size(
|
||||
linear_allocator_iptr_t allocator,
|
||||
malunal_uint32_t* out
|
||||
) {
|
||||
if (allocator == null)
|
||||
return (error_t) {
|
||||
.domain = &ERROR_DOMAIN_ALLOCATOR_T,
|
||||
.code = ALLOCATOR_ERROR_NULL_ALLOCATOR
|
||||
};
|
||||
|
||||
impl_mptr_t self = (impl_mptr_t)allocator;
|
||||
*out = self->size;
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
error_t
|
||||
linear_allocator_used(
|
||||
linear_allocator_iptr_t allocator,
|
||||
malunal_uint32_t* out
|
||||
) {
|
||||
if (allocator == null)
|
||||
return (error_t) {
|
||||
.domain = &ERROR_DOMAIN_ALLOCATOR_T,
|
||||
.code = ALLOCATOR_ERROR_NULL_ALLOCATOR
|
||||
};
|
||||
|
||||
impl_mptr_t self = (impl_mptr_t)allocator;
|
||||
*out = self->used;
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
error_t
|
||||
linear_allocator_init(
|
||||
malunal_size_t size,
|
||||
malunal_mptr_t buffer,
|
||||
linear_allocator_mptr_t allocator
|
||||
) {
|
||||
if (allocator == null)
|
||||
return (error_t) {
|
||||
.domain = &ERROR_DOMAIN_ALLOCATOR_T,
|
||||
.code = ALLOCATOR_ERROR_NULL_ALLOCATOR
|
||||
};
|
||||
|
||||
impl_mptr_t self = (impl_mptr_t)allocator;
|
||||
self->allocator.vtable = &linear_allocator_vtable;
|
||||
self->buffer = buffer;
|
||||
self->size = size;
|
||||
self->used = 0;
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
error_t
|
||||
linear_allocator_acquire(
|
||||
linear_allocator_mptr_t allocator,
|
||||
malunal_size_t size,
|
||||
malunal_mptr_t* out
|
||||
) {
|
||||
if (allocator == null)
|
||||
return (error_t) {
|
||||
.domain = &ERROR_DOMAIN_ALLOCATOR_T,
|
||||
.code = ALLOCATOR_ERROR_NULL_ALLOCATOR
|
||||
};
|
||||
|
||||
impl_mptr_t self = (impl_mptr_t)allocator;
|
||||
if (self->used + size > self->size)
|
||||
return (error_t) {
|
||||
.domain = &ERROR_DOMAIN_ALLOCATOR_T,
|
||||
.code = ALLOCATOR_ERROR_OUT_OF_MEMORY
|
||||
};
|
||||
|
||||
*out = self->buffer + self->used;
|
||||
self->used += size;
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
error_t
|
||||
linear_allocator_dispose(
|
||||
linear_allocator_mptr_t allocator,
|
||||
malunal_mptr_t address,
|
||||
malunal_size_t size
|
||||
) {
|
||||
if (allocator == null)
|
||||
return (error_t) {
|
||||
.domain = &ERROR_DOMAIN_ALLOCATOR_T,
|
||||
.code = ALLOCATOR_ERROR_NULL_ALLOCATOR
|
||||
};
|
||||
|
||||
impl_mptr_t self = (impl_mptr_t)allocator;
|
||||
|
||||
malunal_uint8_t* addr = address;
|
||||
malunal_uint8_t* beg = self->buffer;
|
||||
malunal_uint8_t* end = self->buffer + self->size;
|
||||
if (addr < beg || addr > end)
|
||||
return (error_t) {
|
||||
.domain = &ERROR_DOMAIN_ALLOCATOR_T,
|
||||
.code = ALLOCATOR_ERROR_NOT_MY_ADDRESS
|
||||
};
|
||||
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
error_t
|
||||
linear_allocator_reset(
|
||||
linear_allocator_mptr_t allocator
|
||||
) {
|
||||
if (allocator == null)
|
||||
return (error_t) {
|
||||
.domain = &ERROR_DOMAIN_ALLOCATOR_T,
|
||||
.code = ALLOCATOR_ERROR_NULL_ALLOCATOR
|
||||
};
|
||||
|
||||
impl_mptr_t self = (impl_mptr_t)allocator;
|
||||
|
||||
self->used = 0;
|
||||
return NO_ERROR;
|
||||
}
|
||||
+28
-88
@@ -2,10 +2,9 @@
|
||||
|
||||
#if MALUNAL_PLATFORM_LINUX
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/mman.h>
|
||||
#include "internal.h"
|
||||
#include "malunal/allocator.h"
|
||||
|
||||
|
||||
static
|
||||
@@ -19,32 +18,12 @@ platform_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
|
||||
error_t
|
||||
linux_allocator_acquire(
|
||||
allocator_mptr_t allocator,
|
||||
malunal_size_t size,
|
||||
malunal_mptr_t* out
|
||||
) {
|
||||
MALUNAL_UNUSED(allocator);
|
||||
|
||||
@@ -52,81 +31,42 @@ linux_acquire(
|
||||
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
|
||||
if (ptr == MAP_FAILED || ptr == null)
|
||||
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 = ptr
|
||||
};
|
||||
*out = ptr;
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
static
|
||||
allocation_error_t
|
||||
linux_release(
|
||||
impl_mptr_t allocator,
|
||||
malunal_mptr_t address,
|
||||
malunal_size_t size
|
||||
error_t
|
||||
linux_allocator_dispose(
|
||||
allocator_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;
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
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_vtable_t linux_allocator_vtable = {
|
||||
.acquire = (allocator_acquire_pfn_t)&linux_allocator_acquire,
|
||||
.dispose = (allocator_dispose_pfn_t)&linux_allocator_dispose
|
||||
};
|
||||
|
||||
allocator_t
|
||||
static const
|
||||
allocator_t linux_allocator_instance = {
|
||||
.vtable = &linux_allocator_vtable
|
||||
};
|
||||
|
||||
allocator_mptr_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;
|
||||
return &linux_allocator_instance;
|
||||
}
|
||||
#endif /* MALUNAL_PLATFORM_LINUX */
|
||||
|
||||
+248
@@ -0,0 +1,248 @@
|
||||
#include "malunal/allocators/pool.h"
|
||||
|
||||
typedef struct free_t free_t;
|
||||
typedef free_t* free_mptr_t;
|
||||
typedef const free_t* free_iptr_t;
|
||||
|
||||
struct free_t {
|
||||
free_mptr_t next;
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
allocator_t allocator;
|
||||
allocator_mptr_t upstream;
|
||||
|
||||
malunal_size_t stride;
|
||||
malunal_size_t count;
|
||||
malunal_size_t capacity;
|
||||
malunal_mptr_t buffer;
|
||||
free_mptr_t freelist;
|
||||
} impl_t;
|
||||
|
||||
typedef impl_t* impl_mptr_t;
|
||||
typedef const impl_t* impl_iptr_t;
|
||||
_Static_assert(
|
||||
sizeof(pool_allocator_t) == sizeof(impl_t),
|
||||
"Pool allocator must be the size of its implemenation"
|
||||
);
|
||||
|
||||
static
|
||||
error_t
|
||||
pool_allocator_acquire_impl(
|
||||
pool_allocator_mptr_t allocator,
|
||||
malunal_size_t size,
|
||||
malunal_mptr_t* out
|
||||
) {
|
||||
MALUNAL_UNUSED(size);
|
||||
return pool_allocator_acquire(allocator, out);
|
||||
}
|
||||
|
||||
static
|
||||
error_t
|
||||
pool_allocator_dispose_impl(
|
||||
pool_allocator_mptr_t allocator,
|
||||
malunal_mptr_t address,
|
||||
malunal_size_t size
|
||||
) {
|
||||
MALUNAL_UNUSED(size);
|
||||
return pool_allocator_dispose(allocator, address);
|
||||
}
|
||||
|
||||
static const
|
||||
allocator_vtable_t pool_allocator_vtable = {
|
||||
.acquire = (allocator_acquire_pfn_t)&pool_allocator_acquire_impl,
|
||||
.dispose = (allocator_dispose_pfn_t)&pool_allocator_dispose_impl
|
||||
};
|
||||
|
||||
error_t
|
||||
pool_allocator_stride(
|
||||
pool_allocator_iptr_t allocator,
|
||||
malunal_size_t* out
|
||||
) {
|
||||
if (allocator == null)
|
||||
return (error_t) {
|
||||
.domain = &ERROR_DOMAIN_ALLOCATOR_T,
|
||||
.code = ALLOCATOR_ERROR_NULL_ALLOCATOR
|
||||
};
|
||||
|
||||
impl_iptr_t self = (impl_iptr_t)allocator;
|
||||
*out = self->stride;
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
error_t
|
||||
pool_allocator_count(
|
||||
pool_allocator_iptr_t allocator,
|
||||
malunal_size_t* out
|
||||
) {
|
||||
if (allocator == null)
|
||||
return (error_t) {
|
||||
.domain = &ERROR_DOMAIN_ALLOCATOR_T,
|
||||
.code = ALLOCATOR_ERROR_NULL_ALLOCATOR
|
||||
};
|
||||
|
||||
impl_iptr_t self = (impl_iptr_t)allocator;
|
||||
*out = self->count;
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
error_t
|
||||
pool_allocator_capacity(
|
||||
pool_allocator_iptr_t allocator,
|
||||
malunal_size_t* out
|
||||
) {
|
||||
if (allocator == null)
|
||||
return (error_t) {
|
||||
.domain = &ERROR_DOMAIN_ALLOCATOR_T,
|
||||
.code = ALLOCATOR_ERROR_NULL_ALLOCATOR
|
||||
};
|
||||
|
||||
impl_iptr_t self = (impl_iptr_t)allocator;
|
||||
*out = self->capacity;
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
error_t
|
||||
pool_allocator_init(
|
||||
malunal_size_t stride,
|
||||
malunal_size_t capacity,
|
||||
allocator_mptr_t upstream,
|
||||
pool_allocator_mptr_t allocator
|
||||
) {
|
||||
if (stride < 8)
|
||||
return (error_t) {
|
||||
.domain = &ERROR_DOMAIN_ALLOCATOR_T,
|
||||
.code = ALLOCATOR_ERROR_FAILURE
|
||||
};
|
||||
|
||||
if (allocator == null)
|
||||
return (error_t) {
|
||||
.domain = &ERROR_DOMAIN_ALLOCATOR_T,
|
||||
.code = ALLOCATOR_ERROR_NULL_ALLOCATOR
|
||||
};
|
||||
|
||||
if (upstream == null)
|
||||
upstream = libc_allocator();
|
||||
|
||||
malunal_mptr_t address;
|
||||
malunal_size_t bytes = stride * capacity;
|
||||
error_t result = allocator_acquire(upstream, bytes, &address);
|
||||
if (result.domain != null)
|
||||
return result;
|
||||
|
||||
malunal_size_t index;
|
||||
malunal_uint8_t* asbytes = (malunal_uint8_t*)address;
|
||||
free_mptr_t chunk = (free_mptr_t)address;
|
||||
for (index = 1; index < capacity; index++)
|
||||
chunk = chunk->next = (free_mptr_t)(asbytes + stride * index);
|
||||
|
||||
impl_mptr_t self = (impl_mptr_t)allocator;
|
||||
self->allocator = (allocator_t){ &pool_allocator_vtable };
|
||||
self->upstream = upstream;
|
||||
self->stride = stride;
|
||||
self->count = 0;
|
||||
self->capacity = capacity;
|
||||
self->buffer = address;
|
||||
self->freelist = chunk;
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
error_t
|
||||
pool_allocator_acquire(
|
||||
pool_allocator_mptr_t allocator,
|
||||
malunal_mptr_t* out
|
||||
) {
|
||||
if (allocator == null)
|
||||
return (error_t) {
|
||||
.domain = &ERROR_DOMAIN_ALLOCATOR_T,
|
||||
.code = ALLOCATOR_ERROR_NULL_ALLOCATOR
|
||||
};
|
||||
|
||||
impl_mptr_t self = (impl_mptr_t)allocator;
|
||||
if (self->count == self->capacity)
|
||||
return (error_t) {
|
||||
.domain = &ERROR_DOMAIN_ALLOCATOR_T,
|
||||
.code = ALLOCATOR_ERROR_OUT_OF_MEMORY
|
||||
};
|
||||
|
||||
free_mptr_t prev = self->freelist;
|
||||
free_mptr_t curr = prev;
|
||||
free_mptr_t next = prev->next;
|
||||
while (next != null) {
|
||||
prev = curr;
|
||||
curr = next;
|
||||
next = next->next;
|
||||
}
|
||||
|
||||
*out = curr;
|
||||
self->count++;
|
||||
if (curr == self->freelist)
|
||||
self->freelist = null;
|
||||
else
|
||||
prev->next = null;
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
error_t
|
||||
pool_allocator_dispose(
|
||||
pool_allocator_mptr_t allocator,
|
||||
malunal_mptr_t address
|
||||
) {
|
||||
if (allocator == null)
|
||||
return (error_t) {
|
||||
.domain = &ERROR_DOMAIN_ALLOCATOR_T,
|
||||
.code = ALLOCATOR_ERROR_NULL_ALLOCATOR
|
||||
};
|
||||
|
||||
impl_mptr_t self = (impl_mptr_t)allocator;
|
||||
malunal_uint8_t* addr = address;
|
||||
malunal_uint8_t* beg = self->buffer;
|
||||
malunal_uint8_t* end = beg + self->stride * self->capacity;
|
||||
if (addr < beg || addr > end)
|
||||
return (error_t) {
|
||||
.domain = &ERROR_DOMAIN_ALLOCATOR_T,
|
||||
.code = ALLOCATOR_ERROR_NOT_MY_ADDRESS
|
||||
};
|
||||
|
||||
if (self->freelist == null) {
|
||||
self->freelist = (free_mptr_t)addr;
|
||||
self->freelist->next = null;
|
||||
self->count--;
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
free_mptr_t chunk = self->freelist;
|
||||
while (chunk->next != null)
|
||||
chunk = chunk->next;
|
||||
chunk->next = (free_mptr_t)addr;
|
||||
chunk->next->next = null;
|
||||
self->count--;
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
error_t
|
||||
pool_allocator_free(
|
||||
pool_allocator_mptr_t allocator
|
||||
) {
|
||||
if (allocator == null)
|
||||
return (error_t) {
|
||||
.domain = &ERROR_DOMAIN_ALLOCATOR_T,
|
||||
.code = ALLOCATOR_ERROR_NULL_ALLOCATOR
|
||||
};
|
||||
|
||||
impl_mptr_t self = (impl_mptr_t)allocator;
|
||||
error_t result = allocator_dispose(
|
||||
self->upstream,
|
||||
self->buffer,
|
||||
self->stride * self->capacity
|
||||
);
|
||||
if (result.domain != null)
|
||||
return result;
|
||||
|
||||
self->stride = 0;
|
||||
self->count = 0;
|
||||
self->capacity = 0;
|
||||
self->buffer = null;
|
||||
self->freelist = null;
|
||||
return NO_ERROR;
|
||||
}
|
||||
+215
@@ -0,0 +1,215 @@
|
||||
#include "malunal/allocators/stack.h"
|
||||
|
||||
typedef struct {
|
||||
allocator_t allocator;
|
||||
allocator_mptr_t upstream;
|
||||
|
||||
malunal_size_t stride;
|
||||
malunal_size_t count;
|
||||
malunal_size_t capacity;
|
||||
malunal_mptr_t buffer;
|
||||
} impl_t;
|
||||
|
||||
typedef impl_t* impl_mptr_t;
|
||||
typedef const impl_t* impl_iptr_t;
|
||||
_Static_assert(
|
||||
sizeof(stack_allocator_t) == sizeof(impl_t),
|
||||
"Arena allocator must be the size of its implementation"
|
||||
);
|
||||
|
||||
static
|
||||
error_t
|
||||
stack_allocator_acquire_impl(
|
||||
stack_allocator_mptr_t allocator,
|
||||
malunal_size_t size,
|
||||
malunal_mptr_t* out
|
||||
) {
|
||||
MALUNAL_UNUSED(size);
|
||||
return stack_allocator_acquire(allocator, out);
|
||||
}
|
||||
|
||||
static
|
||||
error_t
|
||||
stack_allocator_dispose_impl(
|
||||
stack_allocator_mptr_t allocator,
|
||||
malunal_mptr_t address,
|
||||
malunal_size_t size
|
||||
) {
|
||||
MALUNAL_UNUSED(size);
|
||||
return stack_allocator_dispose(allocator, address);
|
||||
}
|
||||
|
||||
static const
|
||||
allocator_vtable_t stack_allocator_vtable = {
|
||||
.acquire = (allocator_acquire_pfn_t)&stack_allocator_acquire_impl,
|
||||
.dispose = (allocator_dispose_pfn_t)&stack_allocator_dispose_impl
|
||||
};
|
||||
|
||||
error_t
|
||||
stack_allocator_stride(
|
||||
stack_allocator_iptr_t allocator,
|
||||
malunal_size_t* out
|
||||
) {
|
||||
if (allocator == null)
|
||||
return (error_t) {
|
||||
.domain = &ERROR_DOMAIN_ALLOCATOR_T,
|
||||
.code = ALLOCATOR_ERROR_NULL_ALLOCATOR
|
||||
};
|
||||
|
||||
impl_iptr_t self = (impl_iptr_t)allocator;
|
||||
*out = self->stride;
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
error_t
|
||||
stack_allocator_count(
|
||||
stack_allocator_iptr_t allocator,
|
||||
malunal_size_t* out
|
||||
) {
|
||||
if (allocator == null)
|
||||
return (error_t) {
|
||||
.domain = &ERROR_DOMAIN_ALLOCATOR_T,
|
||||
.code = ALLOCATOR_ERROR_NULL_ALLOCATOR
|
||||
};
|
||||
|
||||
impl_iptr_t self = (impl_iptr_t)allocator;
|
||||
*out = self->count;
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
error_t
|
||||
stack_allocator_capacity(
|
||||
stack_allocator_iptr_t allocator,
|
||||
malunal_size_t* out
|
||||
) {
|
||||
if (allocator == null)
|
||||
return (error_t) {
|
||||
.domain = &ERROR_DOMAIN_ALLOCATOR_T,
|
||||
.code = ALLOCATOR_ERROR_NULL_ALLOCATOR
|
||||
};
|
||||
|
||||
impl_iptr_t self = (impl_iptr_t)allocator;
|
||||
*out = self->capacity;
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
error_t
|
||||
stack_allocator_init(
|
||||
malunal_size_t stride,
|
||||
malunal_size_t capacity,
|
||||
allocator_mptr_t upstream,
|
||||
stack_allocator_mptr_t allocator
|
||||
) {
|
||||
if (stride < 8)
|
||||
return (error_t) {
|
||||
.domain = &ERROR_DOMAIN_ALLOCATOR_T,
|
||||
.code = ALLOCATOR_ERROR_FAILURE
|
||||
};
|
||||
|
||||
if (allocator == null)
|
||||
return (error_t) {
|
||||
.domain = &ERROR_DOMAIN_ALLOCATOR_T,
|
||||
.code = ALLOCATOR_ERROR_NULL_ALLOCATOR
|
||||
};
|
||||
|
||||
if (upstream == null)
|
||||
upstream = libc_allocator();
|
||||
|
||||
malunal_mptr_t address;
|
||||
malunal_size_t bytes = stride * capacity;
|
||||
error_t result = allocator_acquire(upstream, bytes, &address);
|
||||
if (result.domain != null)
|
||||
return result;
|
||||
|
||||
impl_mptr_t self = (impl_mptr_t)allocator;
|
||||
self->allocator = (allocator_t){ &stack_allocator_vtable };
|
||||
self->upstream = upstream;
|
||||
self->stride = stride;
|
||||
self->count = 0;
|
||||
self->capacity = capacity;
|
||||
self->buffer = address;
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
error_t
|
||||
stack_allocator_acquire(
|
||||
stack_allocator_mptr_t allocator,
|
||||
malunal_mptr_t* out
|
||||
) {
|
||||
if (allocator == null)
|
||||
return (error_t) {
|
||||
.domain = &ERROR_DOMAIN_ALLOCATOR_T,
|
||||
.code = ALLOCATOR_ERROR_NULL_ALLOCATOR
|
||||
};
|
||||
|
||||
impl_mptr_t self = (impl_mptr_t)allocator;
|
||||
if (self->count == self->capacity)
|
||||
return (error_t) {
|
||||
.domain = &ERROR_DOMAIN_ALLOCATOR_T,
|
||||
.code = ALLOCATOR_ERROR_OUT_OF_MEMORY
|
||||
};
|
||||
|
||||
malunal_uint8_t* asbytes = self->buffer;
|
||||
*out = asbytes + self->stride * self->count;
|
||||
self->count++;
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
error_t
|
||||
stack_allocator_dispose(
|
||||
stack_allocator_mptr_t allocator,
|
||||
malunal_mptr_t address
|
||||
) {
|
||||
if (allocator == null)
|
||||
return (error_t) {
|
||||
.domain = &ERROR_DOMAIN_ALLOCATOR_T,
|
||||
.code = ALLOCATOR_ERROR_NULL_ALLOCATOR
|
||||
};
|
||||
|
||||
impl_mptr_t self = (impl_mptr_t)allocator;
|
||||
malunal_uint8_t* addr = address;
|
||||
malunal_uint8_t* beg = self->buffer;
|
||||
malunal_uint8_t* end = beg + self->stride * self->capacity;
|
||||
if (addr < beg || addr > end)
|
||||
return (error_t) {
|
||||
.domain = &ERROR_DOMAIN_ALLOCATOR_T,
|
||||
.code = ALLOCATOR_ERROR_NOT_MY_ADDRESS
|
||||
};
|
||||
|
||||
malunal_uint8_t* top = self->buffer;
|
||||
top += self->stride * (self->count - 1);
|
||||
if (addr != top)
|
||||
return (error_t) {
|
||||
.domain = &ERROR_DOMAIN_ALLOCATOR_T,
|
||||
.code = ALLOCATOR_ERROR_FAILURE
|
||||
};
|
||||
|
||||
self->count--;
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
error_t
|
||||
stack_allocator_free(
|
||||
stack_allocator_mptr_t allocator
|
||||
) {
|
||||
if (allocator == null)
|
||||
return (error_t) {
|
||||
.domain = &ERROR_DOMAIN_ALLOCATOR_T,
|
||||
.code = ALLOCATOR_ERROR_NULL_ALLOCATOR
|
||||
};
|
||||
|
||||
impl_mptr_t self = (impl_mptr_t)allocator;
|
||||
error_t result = allocator_dispose(
|
||||
self->upstream,
|
||||
self->buffer,
|
||||
self->stride * self->capacity
|
||||
);
|
||||
if (result.domain != null)
|
||||
return result;
|
||||
|
||||
self->stride = 0;
|
||||
self->count = 0;
|
||||
self->capacity = 0;
|
||||
self->buffer = null;
|
||||
return NO_ERROR;
|
||||
}
|
||||
+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