Underscores on allocators

This commit is contained in:
flysand7 2023-08-01 11:30:32 +11:00
parent fd8f580b63
commit 2166660b93
4 changed files with 20 additions and 20 deletions

View File

@ -20,11 +20,11 @@ struct Cia_Allocator {
Cia_Allocator cia_allocator_null(); Cia_Allocator cia_allocator_null();
Cia_Allocator cia_allocator_pages(); Cia_Allocator cia_allocator_pages();
void *allocator_alloc(Cia_Allocator *alloc, u64 size, u64 alignment); void *cia_allocator_alloc(Cia_Allocator *alloc, u64 size, u64 alignment);
void allocator_free_size(Cia_Allocator *alloc, void *region_ptr, u64 region_size); void cia_allocator_free_size(Cia_Allocator *alloc, void *region_ptr, u64 region_size);
void allocator_free(Cia_Allocator *alloc, void *region_ptr); void cia_allocator_free(Cia_Allocator *alloc, void *region_ptr);
void allocator_free_all(Cia_Allocator *alloc); void cia_allocator_free_all(Cia_Allocator *alloc);
void *allocator_resize(Cia_Allocator *alloc, void *old_ptr, u64 old_size, u64 new_size, u64 alignment); void *cia_allocator_resize(Cia_Allocator *alloc, void *old_ptr, u64 old_size, u64 new_size, u64 alignment);
struct Cia_Arena typedef Cia_Arena; struct Cia_Arena typedef Cia_Arena;
struct Cia_Arena { struct Cia_Arena {

View File

@ -1,17 +1,17 @@
static void *null_allocator_proc(void *ctx, int optype, void *old_ptr, u64 old_size, u64 size, u64 alignment) { static void *_null_allocator_proc(void *ctx, int optype, void *old_ptr, u64 old_size, u64 size, u64 alignment) {
return NULL; return NULL;
} }
Cia_Allocator cia_allocator_null() { Cia_Allocator cia_allocator_null() {
Cia_Allocator allocator = { Cia_Allocator allocator = {
.ctx = NULL, .ctx = NULL,
.proc = null_allocator_proc, .proc = _null_allocator_proc,
}; };
return allocator; return allocator;
} }
static void *page_allocator_proc(void *ctx, int optype, void *old_ptr, u64 old_size, u64 size, u64 alignment) { static void *_page_allocator_proc(void *ctx, int optype, void *old_ptr, u64 old_size, u64 size, u64 alignment) {
switch(optype) { switch(optype) {
case CIA_MEM_OP_ALLOC: { case CIA_MEM_OP_ALLOC: {
void *addr; void *addr;
@ -40,28 +40,28 @@ static void *page_allocator_proc(void *ctx, int optype, void *old_ptr, u64 old_s
Cia_Allocator cia_allocator_pages() { Cia_Allocator cia_allocator_pages() {
Cia_Allocator allocator = { Cia_Allocator allocator = {
.ctx = NULL, .ctx = NULL,
.proc = page_allocator_proc, .proc = _page_allocator_proc,
}; };
return allocator; return allocator;
} }
void *allocator_alloc(Cia_Allocator *alloc, u64 size, u64 alignment) { void *cia_allocator_alloc(Cia_Allocator *alloc, u64 size, u64 alignment) {
return alloc->proc(alloc->ctx, CIA_MEM_OP_ALLOC, NULL, 0, size, alignment); return alloc->proc(alloc->ctx, CIA_MEM_OP_ALLOC, NULL, 0, size, alignment);
} }
void allocator_free_size(Cia_Allocator *alloc, void *region_ptr, u64 region_size) { void cia_allocator_free_size(Cia_Allocator *alloc, void *region_ptr, u64 region_size) {
alloc->proc(alloc->ctx, CIA_MEM_OP_FREE, region_ptr, region_size, 0, 1); alloc->proc(alloc->ctx, CIA_MEM_OP_FREE, region_ptr, region_size, 0, 1);
} }
void allocator_free(Cia_Allocator *alloc, void *region_ptr) { void cia_allocator_free(Cia_Allocator *alloc, void *region_ptr) {
alloc->proc(alloc->ctx, CIA_MEM_OP_FREE, region_ptr, 0, 0, 1); alloc->proc(alloc->ctx, CIA_MEM_OP_FREE, region_ptr, 0, 0, 1);
} }
void allocator_free_all(Cia_Allocator *alloc) { void cia_allocator_free_all(Cia_Allocator *alloc) {
alloc->proc(alloc->ctx, CIA_MEM_OP_FREE_ALL, NULL, 0, 0, 1); alloc->proc(alloc->ctx, CIA_MEM_OP_FREE_ALL, NULL, 0, 0, 1);
} }
void *allocator_resize(Cia_Allocator *alloc, void *old_ptr, u64 old_size, u64 new_size, u64 alignment) { void *cia_allocator_resize(Cia_Allocator *alloc, void *old_ptr, u64 old_size, u64 new_size, u64 alignment) {
return alloc->proc(alloc->ctx, CIA_MEM_OP_RESIZE, old_ptr, old_size, new_size, alignment); return alloc->proc(alloc->ctx, CIA_MEM_OP_RESIZE, old_ptr, old_size, new_size, alignment);
} }

View File

@ -3,7 +3,7 @@ void cia_arena_create(Cia_Arena *arena, Cia_Allocator backing_allocator, u64 buf
arena->allocator = backing_allocator; arena->allocator = backing_allocator;
arena->buffer_size = buffer_size; arena->buffer_size = buffer_size;
arena->used = 0; arena->used = 0;
arena->buffer = allocator_alloc(&arena->allocator, arena->buffer_size, 16); arena->buffer = cia_allocator_alloc(&arena->allocator, arena->buffer_size, 16);
} }
void *cia_arena_alloc(Cia_Arena *arena, u64 size) { void *cia_arena_alloc(Cia_Arena *arena, u64 size) {
@ -31,5 +31,5 @@ void cia_arena_free_all(Cia_Arena *arena) {
} }
void cia_arena_destroy(Cia_Arena *arena) { void cia_arena_destroy(Cia_Arena *arena) {
allocator_free_size(&arena->allocator, arena->buffer, arena->buffer_size); cia_allocator_free_size(&arena->allocator, arena->buffer, arena->buffer_size);
} }

View File

@ -33,14 +33,14 @@ void cia_pool_create(Cia_Pool *pool, Cia_Allocator backing_allocator, u64 buffer
pool->bucket_size = cia_size_alignf(pool->bucket_size, pool->alignment); pool->bucket_size = cia_size_alignf(pool->bucket_size, pool->alignment);
// Allocate and initialize the first buffer // Allocate and initialize the first buffer
pool->freelist_head = NULL; pool->freelist_head = NULL;
pool->first = allocator_alloc(&pool->allocator, pool->buffer_size, pool->alignment); pool->first = cia_allocator_alloc(&pool->allocator, pool->buffer_size, pool->alignment);
_pool_buffer_freelist_add(pool, (u8 *)pool->first); _pool_buffer_freelist_add(pool, (u8 *)pool->first);
} }
void *cia_pool_alloc(Cia_Pool *pool) { void *cia_pool_alloc(Cia_Pool *pool) {
// If we don't have enough free buckets, create a new buffer // If we don't have enough free buckets, create a new buffer
if(pool->freelist_head == NULL) { if(pool->freelist_head == NULL) {
void *buffer = allocator_alloc(&pool->allocator, pool->buffer_size, pool->alignment); void *buffer = cia_allocator_alloc(&pool->allocator, pool->buffer_size, pool->alignment);
_pool_buffer_freelist_add(pool, buffer); _pool_buffer_freelist_add(pool, buffer);
} }
// Remove item from free list and return it // Remove item from free list and return it
@ -59,7 +59,7 @@ void cia_pool_free(Cia_Pool *pool, void *ptr) {
void cia_pool_free_all(Cia_Pool *pool) { void cia_pool_free_all(Cia_Pool *pool) {
// Deallocate all buffers except the first one // Deallocate all buffers except the first one
for(Cia_Pool_Buffer_Header *buffer = pool->first->next; buffer != NULL; buffer = buffer->next) { for(Cia_Pool_Buffer_Header *buffer = pool->first->next; buffer != NULL; buffer = buffer->next) {
allocator_free_size(&pool->allocator, buffer, pool->buffer_size); cia_allocator_free_size(&pool->allocator, buffer, pool->buffer_size);
} }
// Reinit the first buffer // Reinit the first buffer
pool->freelist_head = NULL; pool->freelist_head = NULL;
@ -69,6 +69,6 @@ void cia_pool_free_all(Cia_Pool *pool) {
void cia_pool_destroy(Cia_Pool *pool) { void cia_pool_destroy(Cia_Pool *pool) {
// Simply deallocate all the buffers // Simply deallocate all the buffers
for(Cia_Pool_Buffer_Header *buffer = pool->first; buffer != NULL; buffer = buffer->next) { for(Cia_Pool_Buffer_Header *buffer = pool->first; buffer != NULL; buffer = buffer->next) {
allocator_free_size(&pool->allocator, buffer, pool->buffer_size); cia_allocator_free_size(&pool->allocator, buffer, pool->buffer_size);
} }
} }