From 8a5804b51da33d8e363642dab4de09eac7be766d Mon Sep 17 00:00:00 2001 From: flysand7 Date: Fri, 28 Jul 2023 22:29:12 +1100 Subject: [PATCH] Fix module deps checks, make allocators use one proc --- build.py | 12 +++--- include/cia-mem.h | 17 ++++++--- src/impl/cia-mem/allocator.c | 71 ++++++++++++++++++++++++++---------- src/impl/cia-mem/arena.c | 4 +- 4 files changed, 71 insertions(+), 33 deletions(-) diff --git a/build.py b/build.py index fed0ef8..6bb99eb 100755 --- a/build.py +++ b/build.py @@ -168,12 +168,12 @@ try: reqs_satisfied = True # Check API dependencies for req in api['reqs']: - if not (req in tinyrt_apis): - reqs_satisfied = False - break - if not (req in mod_exports): - reqs_satisfied = False - break + if req in tinyrt_apis: + continue + elif req in mod_exports: + continue + reqs_satisfied = False + break if not reqs_satisfied: print(f" -> Not exporting API '{api_name}'") else: diff --git a/include/cia-mem.h b/include/cia-mem.h index 9700267..6fa065a 100644 --- a/include/cia-mem.h +++ b/include/cia-mem.h @@ -4,19 +4,26 @@ void *cia_ptr_alignf(void *ptr, u64 alignment); void *cia_ptr_alignb(void *ptr, u64 alignment); +#define CIA_MEM_OP_ALLOC 1 +#define CIA_MEM_OP_FREE 2 +#define CIA_MEM_OP_FREE_ALL 3 +#define CIA_MEM_OP_RESIZE 4 + struct Cia_Allocator typedef Cia_Allocator; struct Cia_Allocator { void *ctx; - void *(*alloc)(void *ctx, u64 size); - void *(*alloc_a)(void *ctx, u64 size, u64 alignment); - void (*free)(void *ctx, void *ptr); - void (*free_all)(void *ctx, void *ptr); - void *(*realloc)(void *ctx, void *ptr, u64 new_size); + void *(*proc)(void *ctx, int optype, void *old_ptr, u64 old_size, u64 size, u64 alignment); }; Cia_Allocator cia_allocator_null(); Cia_Allocator cia_allocator_pages(); +void *allocator_alloc(Cia_Allocator *alloc, u64 size, u64 alignment); +void allocator_free_size(Cia_Allocator *alloc, void *region_ptr, u64 region_size); +void allocator_free(Cia_Allocator *alloc, void *region_ptr); +void allocator_free_all(Cia_Allocator *alloc); +void *allocator_resize(Cia_Allocator *alloc, void *old_ptr, u64 old_size, u64 new_size); + struct Cia_Arena typedef Cia_Arena; struct Cia_Arena { Cia_Allocator allocator; diff --git a/src/impl/cia-mem/allocator.c b/src/impl/cia-mem/allocator.c index 12ad51e..8ecc03b 100644 --- a/src/impl/cia-mem/allocator.c +++ b/src/impl/cia-mem/allocator.c @@ -1,37 +1,68 @@ +static void *null_allocator_proc(void *ctx, int optype, void *old_ptr, u64 old_size, u64 size, u64 alignment) { + return NULL; +} + Cia_Allocator cia_allocator_null() { Cia_Allocator allocator = { .ctx = NULL, - .alloc = NULL, - .alloc_a = NULL, - .free = NULL, - .free_all = NULL, - .realloc = NULL + .proc = null_allocator_proc, }; return allocator; } -static void *page_alloc(void *ctx, u64 size) { - void *addr; - _RT_Status status = _rt_mem_alloc(NULL, size, &addr); - if(status != _RT_STATUS_OK) { - return NULL; +static void *page_allocator_proc(void *ctx, int optype, void *old_ptr, u64 old_size, u64 size, u64 alignment) { + switch(optype) { + case CIA_MEM_OP_ALLOC: { + void *addr; + if(alignment > 0x1000) { + return NULL; + } + _RT_Status status = _rt_mem_alloc(NULL, size, &addr); + if(status != _RT_STATUS_OK) { + return NULL; + } + return addr; + } break; + case CIA_MEM_OP_FREE: { + _rt_mem_free(old_ptr); + } break; + case CIA_MEM_OP_FREE_ALL: { + return NULL; + } break; + case CIA_MEM_OP_RESIZE: { + return NULL; + } break; } - return addr; -} - -static void page_free(void *ptr) { - _rt_mem_free(ptr); + return NULL; } Cia_Allocator cia_allocator_pages() { Cia_Allocator allocator = { .ctx = NULL, - .alloc = page_alloc, - .alloc_a = NULL, - .free = page_free, - .free_all = NULL, - .realloc = NULL, + .proc = page_allocator_proc, }; return allocator; } + +void *allocator_alloc(Cia_Allocator *alloc, u64 size, u64 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) { + alloc->proc(alloc->ctx, CIA_MEM_OP_FREE, region_ptr, region_size, 0, 1); +} + +void allocator_free(Cia_Allocator *alloc, void *region_ptr) { + alloc->proc(alloc->ctx, CIA_MEM_OP_FREE, region_ptr, 0, 0, 1); +} + +void allocator_free_all(Cia_Allocator *alloc) { + 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) { + return alloc->proc(alloc->ctx, CIA_MEM_OP_RESIZE, old_ptr, old_size, new_size, alignment); +} + + diff --git a/src/impl/cia-mem/arena.c b/src/impl/cia-mem/arena.c index 45d2e24..ff59217 100644 --- a/src/impl/cia-mem/arena.c +++ b/src/impl/cia-mem/arena.c @@ -3,7 +3,7 @@ void cia_arena_create(Cia_Arena *arena, Cia_Allocator backing_allocator, u64 buf arena->allocator = backing_allocator; arena->buffer_size = buffer_size; arena->used = 0; - arena->buffer = arena->allocator.alloc(arena->allocator.ctx, buffer_size); + arena->buffer = allocator_alloc(&arena->allocator, arena->buffer_size, 16); } 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) { - arena->allocator.free(arena->allocator.ctx, arena->buffer); + allocator_free_size(&arena->allocator, arena->buffer, arena->buffer_size); }