Alternate strategy for keeping count of memory block lists

This commit is contained in:
Cacodemon345
2025-12-18 16:34:15 +06:00
parent 82a353ee6b
commit 68c056cdb0

View File

@@ -22,6 +22,7 @@
typedef struct mem_block_t {
uint32_t offset; /*Offset into mem_block_alloc*/
uint32_t next;
uint32_t tail;
uint16_t code_block;
} mem_block_t;
@@ -39,6 +40,7 @@ codegen_allocator_init(void)
for (uint32_t c = 0; c < MEM_BLOCK_NR; c++) {
mem_blocks[c].offset = c * MEM_BLOCK_SIZE;
mem_blocks[c].code_block = BLOCK_INVALID;
mem_blocks[c].tail = 0;
if (c < MEM_BLOCK_NR - 1)
mem_blocks[c].next = c + 2;
else
@@ -70,10 +72,15 @@ codegen_allocator_allocate(mem_block_t *parent, int code_block)
block->code_block = code_block;
if (parent) {
/*Add to parent list*/
block->next = parent->next;
parent->next = block_nr;
if (parent->tail) {
mem_blocks[parent->tail - 1].next = block_nr;
parent->tail = block_nr;
}
else
parent->next = parent->tail = block_nr;
block->next = block->tail = 0;
} else
block->next = 0;
block->next = block->tail = 0;
codegen_allocator_usage++;
return block;
@@ -83,6 +90,7 @@ codegen_allocator_free(mem_block_t *block)
{
int block_nr = (((uintptr_t) block - (uintptr_t) mem_blocks) / sizeof(mem_block_t)) + 1;
block->tail = 0;
while (1) {
int next_block_nr = block->next;
codegen_allocator_usage--;