Fix module deps checks, make allocators use one proc

This commit is contained in:
flysand7 2023-07-28 22:29:12 +11:00
parent 76904bd19b
commit 8a5804b51d
4 changed files with 71 additions and 33 deletions

View File

@ -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:

View File

@ -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;

View File

@ -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);
}

View File

@ -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);
}