aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


commit 6a8f0575c8979575d960d3b4c231c2becaec660d
parent 1861583fd43bd034404c00c086065a88fa5c8af5
Author: anon <anon@example.com>
Date:   Thu, 15 Oct 2026 11:36:48 +0000

Index guest allocations to avoid live-block scans during loading

Diffstat:
Mdecompiled/runtime/guest_heap.c | 140++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------
Mdecompiled/tests/guest_heap.c | 36+++++++++++++++++++++++++++++++++++-
2 files changed, 121 insertions(+), 55 deletions(-)

diff --git a/decompiled/runtime/guest_heap.c b/decompiled/runtime/guest_heap.c @@ -14,20 +14,46 @@ struct block { uint32_t address, size; int free; - struct block *next; -}; -struct arena { - struct block *blocks; - struct arena *next; + struct block *previous, *next; + struct block *free_previous, *free_next, *hash_next; }; #ifdef _WIN32 static SRWLOCK mutex = SRWLOCK_INIT; #else static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; #endif -static struct arena *arenas; +static struct block *free_blocks; +/* One bucket per aligned offset in a 1 MiB arena; collisions retain full addresses. */ +static struct block *allocated[65536]; static uint32_t next_address = 0x60000000u; +static struct block **allocation_link_locked(uint32_t address) +{ + struct block **link = &allocated[(address >> 4) & 65535]; + while (*link && (*link)->address != address) link = &(*link)->hash_next; + if (!*link) { + fprintf(stderr, "Invalid guest allocation: %08x\n", address); + abort(); + } + return link; +} + +static void remove_free_locked(struct block *block) +{ + if (block->free_previous) block->free_previous->free_next = block->free_next; + else free_blocks = block->free_next; + if (block->free_next) block->free_next->free_previous = block->free_previous; +} + +static void add_free_locked(struct block *block) +{ + block->free = 1; + block->free_previous = NULL; + block->free_next = free_blocks; + if (free_blocks) free_blocks->free_previous = block; + free_blocks = block; +} + static uint32_t guest_malloc_locked(uint32_t size) { uint64_t aligned = ((uint64_t)(size ? size : 1) + 15) & ~15ull; @@ -35,32 +61,38 @@ static uint32_t guest_malloc_locked(uint32_t size) uint32_t needed = (uint32_t)aligned; /* Reuse and split free blocks within one contiguous host allocation. */ - for (struct arena *arena = arenas; arena; arena = arena->next) { - for (struct block *block = arena->blocks; block; block = block->next) { - if (!block->free || block->size < needed) continue; - if (block->size > needed) { - struct block *remainder = malloc(sizeof(*remainder)); - if (!remainder) return 0; - *remainder = (struct block){block->address + needed, block->size - needed, 1, block->next}; - block->next = remainder; - block->size = needed; - } - block->free = 0; - return block->address; + for (struct block *block = free_blocks; block; block = block->free_next) { + if (block->size < needed) continue; + if (block->size > needed) { + struct block *remainder = calloc(1, sizeof(*remainder)); + if (!remainder) return 0; + remainder->address = block->address + needed; + remainder->size = block->size - needed; + remainder->previous = block; + remainder->next = block->next; + if (block->next) block->next->previous = remainder; + block->next = remainder; + block->size = needed; + add_free_locked(remainder); } + remove_free_locked(block); + block->free = 0; + unsigned bucket = (block->address >> 4) & 65535; + block->hash_next = allocated[bucket]; + allocated[bucket] = block; + return block->address; } /* Grow in at least 1 MiB ranges; no holes need backing host memory. */ uint32_t span = (needed + 0xffffu) & ~0xffffu; if (span < 0x100000u) span = 0x100000u; if (span > 0xefff0000u - next_address) return 0; - struct arena *arena = malloc(sizeof(*arena)); - struct block *block = malloc(sizeof(*block)); - if (!arena || !block) { free(arena); free(block); return 0; } - if (!guest_map("heap", next_address, span)) { free(arena); free(block); return 0; } - *block = (struct block){next_address, span, 1, NULL}; - *arena = (struct arena){block, arenas}; - arenas = arena; + struct block *block = calloc(1, sizeof(*block)); + if (!block) return 0; + if (!guest_map("heap", next_address, span)) { free(block); return 0; } + block->address = next_address; + block->size = span; + add_free_locked(block); next_address += span; return guest_malloc_locked(size); } @@ -68,42 +100,42 @@ static uint32_t guest_malloc_locked(uint32_t size) static void guest_free_locked(uint32_t address) { if (!address) return; - for (struct arena *arena = arenas; arena; arena = arena->next) { - for (struct block *block = arena->blocks; block; block = block->next) { - if (block->address != address || block->free) continue; - block->free = 1; - for (block = arena->blocks; block && block->next;) { - if (block->free && block->next->free) { - struct block *next = block->next; - block->size += next->size; - block->next = next->next; - free(next); - } else block = block->next; - } - return; - } + struct block **link = allocation_link_locked(address); + struct block *block = *link; + *link = block->hash_next; + + /* Only adjacent free neighbors can coalesce; arena boundaries have no links. */ + if (block->next && block->next->free) { + struct block *next = block->next; + remove_free_locked(next); + block->size += next->size; + block->next = next->next; + if (block->next) block->next->previous = block; + free(next); + } + if (block->previous && block->previous->free) { + struct block *previous = block->previous; + remove_free_locked(previous); + previous->size += block->size; + previous->next = block->next; + if (block->next) block->next->previous = previous; + free(block); + block = previous; } - fprintf(stderr, "Invalid guest free: %08x\n", address); - abort(); + add_free_locked(block); } static uint32_t guest_realloc_locked(uint32_t address, uint32_t size) { if (!address) return guest_malloc_locked(size); if (!size) { guest_free_locked(address); return 0; } - for (struct arena *arena = arenas; arena; arena = arena->next) { - for (struct block *block = arena->blocks; block; block = block->next) { - if (block->address != address || block->free) continue; - if (size <= block->size) return address; - uint32_t replacement = guest_malloc_locked(size); - if (!replacement) return 0; - memcpy((void *)guest_address(replacement), (void *)guest_address(address), block->size); - guest_free_locked(address); - return replacement; - } - } - fprintf(stderr, "Invalid guest realloc: %08x\n", address); - abort(); + struct block *block = *allocation_link_locked(address); + if (size <= block->size) return address; + uint32_t replacement = guest_malloc_locked(size); + if (!replacement) return 0; + memcpy((void *)guest_address(replacement), (void *)guest_address(address), block->size); + guest_free_locked(address); + return replacement; } /* Allocation metadata is shared by every guest thread. */ diff --git a/decompiled/tests/guest_heap.c b/decompiled/tests/guest_heap.c @@ -60,13 +60,47 @@ static void allocation_spans_multiple_guest_pages(void) guest_free(large); } +static void fragmented_allocation_lifetime(void) +{ + static uint32_t addresses[65536]; + for (unsigned i = 0; i < 65536; ++i) { + addresses[i] = guest_malloc(32); + assert(addresses[i] && !(addresses[i] & 15)); + memset((void *)guest_address(addresses[i]), 0x5a, 32); + memcpy((void *)guest_address(addresses[i]), &i, 4); + } + + for (unsigned i = 0; i < 65536; i += 2) guest_free(addresses[i]); + for (unsigned i = 0; i < 65536; i += 2) { + addresses[i] = guest_malloc(32); + assert(addresses[i]); + memset((void *)guest_address(addresses[i]), 0x5a, 32); + memcpy((void *)guest_address(addresses[i]), &i, 4); + } + + for (unsigned i = 0; i < 65536; ++i) { + const unsigned char *bytes = (void *)guest_address(addresses[i]); + unsigned value; + memcpy(&value, bytes, 4); + assert(value == i); + for (unsigned byte = 4; byte < 32; ++byte) assert(bytes[byte] == 0x5a); + } + /* An odd stride visits every allocation once, merging from both directions. */ + for (unsigned i = 0; i < 65536; ++i) guest_free(addresses[(i * 40503) & 65535]); + uint32_t joined = guest_malloc(0x100000); + assert(joined); + memset((void *)guest_address(joined), 0xa5, 0x100000); + guest_free(joined); +} + int main(void) { realloc_preserves_bytes(); free_coalesces_within_arena(); failed_realloc_preserves_allocation(); allocation_spans_multiple_guest_pages(); + fragmented_allocation_lifetime(); guest_free(0); - puts("guest heap: growth, reuse, coalescing and failed realloc passed"); + puts("guest heap: growth, reuse, coalescing, failed realloc and 65536 fragmented allocations passed"); return 0; }