commit ef65182c5a5d6dfd90dd8cb9eefb344a42493249
parent c814feda7979442587dd4fa148f1594482f0eb5a
Author: anon <anon@example.com>
Date: Tue, 20 Oct 2026 00:28:42 +0000
Pack small guest allocations into shared pages
Diffstat:
2 files changed, 154 insertions(+), 9 deletions(-)
diff --git a/src/runtime/guest_heap.c b/src/runtime/guest_heap.c
@@ -27,6 +27,90 @@ static struct block *free_blocks[32];
static struct block *allocated[65536];
static uint32_t next_address = 0x60000000u;
+/* Small objects share page metadata instead of one host allocation per block. */
+struct small_page {
+ uint32_t address, size, used, next_unused, first_free;
+ uint32_t allocated[128];
+ struct small_page *previous, *next;
+};
+static struct small_page *small_pages[65536];
+static struct small_page *small_available[16], *small_empty;
+
+static void remove_small_page_locked(struct small_page *page)
+{
+ if (page->previous) page->previous->next = page->next;
+ else small_available[page->size / 16 - 1] = page->next;
+ if (page->next) page->next->previous = page->previous;
+}
+
+static void add_small_page_locked(struct small_page *page)
+{
+ unsigned index = page->size / 16 - 1;
+ page->previous = NULL;
+ page->next = small_available[index];
+ if (page->next) page->next->previous = page;
+ small_available[index] = page;
+}
+
+static unsigned small_index_locked(struct small_page *page, uint32_t address)
+{
+ unsigned index = (address - page->address) / page->size;
+ if ((address - page->address) % page->size || index >= page->next_unused ||
+ !(page->allocated[index / 32] & (1u << (index % 32)))) {
+ fprintf(stderr, "Invalid guest allocation: %08x\n", address);
+ abort();
+ }
+ return index;
+}
+
+static uint32_t small_malloc_locked(uint32_t size)
+{
+ struct small_page *page = small_available[size / 16 - 1];
+ if (!page) {
+ /* Reassign completely empty pages before mapping another guest page. */
+ page = small_empty;
+ if (page) small_empty = page->next;
+ else {
+ if (next_address > 0xeffe0000u) return 0;
+ page = calloc(1, sizeof(*page));
+ if (!page) return 0;
+ if (!guest_map("small heap", next_address, 0x10000u)) { free(page); return 0; }
+ page->address = next_address;
+ small_pages[next_address >> 16] = page;
+ next_address += 0x10000u;
+ }
+ page->size = size;
+ page->used = page->next_unused = 0;
+ page->first_free = UINT32_MAX;
+ add_small_page_locked(page);
+ }
+
+ /* Freed slots link through their unused payload; new slots need no list. */
+ unsigned index;
+ if (page->first_free != UINT32_MAX) {
+ index = page->first_free;
+ page->first_free = *(uint32_t *)guest_address(page->address + index * size);
+ } else index = page->next_unused++;
+ page->allocated[index / 32] |= 1u << (index % 32);
+ if (++page->used == 0x10000u / size) remove_small_page_locked(page);
+ return page->address + index * size;
+}
+
+static void small_free_locked(struct small_page *page, uint32_t address)
+{
+ unsigned index = small_index_locked(page, address);
+ if (page->used == 0x10000u / page->size) add_small_page_locked(page);
+ page->allocated[index / 32] &= ~(1u << (index % 32));
+ *(uint32_t *)guest_address(address) = page->first_free;
+ page->first_free = index;
+ if (--page->used) return;
+
+ /* Empty pages can serve any small-object size on their next allocation. */
+ remove_small_page_locked(page);
+ page->next = small_empty;
+ small_empty = page;
+}
+
/* Free ranges of comparable sizes share a bucket. Sizes are always nonzero. */
static unsigned free_bucket(uint32_t size)
{
@@ -66,6 +150,7 @@ static uint32_t guest_malloc_locked(uint32_t size)
uint64_t aligned = ((uint64_t)(size ? size : 1) + 15) & ~15ull;
if (aligned > 0x8fff0000u) return 0;
uint32_t needed = (uint32_t)aligned;
+ if (needed <= 256) return small_malloc_locked(needed);
/* Preserve large holes by choosing the smallest suitable free range. */
struct block *block = NULL;
@@ -116,6 +201,7 @@ static uint32_t guest_malloc_locked(uint32_t size)
static void guest_free_locked(uint32_t address)
{
if (!address) return;
+ if (small_pages[address >> 16]) { small_free_locked(small_pages[address >> 16], address); return; }
struct block **link = allocation_link_locked(address);
struct block *block = *link;
*link = block->hash_next;
@@ -145,11 +231,15 @@ 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; }
- struct block *block = *allocation_link_locked(address);
- if (size <= block->size) return address;
+ uint32_t previous_size;
+ if (small_pages[address >> 16]) {
+ small_index_locked(small_pages[address >> 16], address);
+ previous_size = small_pages[address >> 16]->size;
+ } else previous_size = (*allocation_link_locked(address))->size;
+ if (size <= previous_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);
+ memcpy((void *)guest_address(replacement), (void *)guest_address(address), previous_size);
guest_free_locked(address);
return replacement;
}
diff --git a/src/tests/guest_heap.c b/src/tests/guest_heap.c
@@ -21,17 +21,17 @@ static void realloc_preserves_bytes(void)
static void free_coalesces_within_arena(void)
{
- uint32_t first = guest_malloc(32);
- uint32_t second = guest_malloc(32);
- uint32_t guard = guest_malloc(32);
- assert(first && second == first + 32 && guard == second + 32);
+ uint32_t first = guest_malloc(512);
+ uint32_t second = guest_malloc(512);
+ uint32_t guard = guest_malloc(512);
+ assert(first && second == first + 512 && guard == second + 512);
guest_free(first);
guest_free(second);
- uint32_t joined = guest_malloc(64);
+ uint32_t joined = guest_malloc(1024);
assert(joined == first);
- memset((void *)guest_address(joined), 0x11, 64);
+ memset((void *)guest_address(joined), 0x11, 1024);
guest_free(joined);
guest_free(guard);
}
@@ -115,8 +115,63 @@ static void small_allocations_preserve_large_holes(void)
guest_free(tail);
}
+static void empty_pages_reuse_different_sizes(void)
+{
+ uint32_t first = guest_malloc(16);
+ guest_free(first);
+
+ uint32_t larger = guest_malloc(256);
+ memset((void *)guest_address(larger), 0x6b, 256);
+ guest_free(larger);
+ uint32_t smaller = guest_malloc(16);
+
+ assert(larger == first && smaller == first);
+ guest_free(smaller);
+}
+
+static void nondividing_size_crosses_pages(void)
+{
+ uint32_t addresses[1400];
+ for (unsigned i = 0; i < 1400; ++i) {
+ addresses[i] = guest_malloc(48);
+ memset((void *)guest_address(addresses[i]), 0x73, 48);
+ memcpy((void *)guest_address(addresses[i]), &i, sizeof(i));
+ }
+
+ for (unsigned i = 0; i < 1400; i += 2) guest_free(addresses[i]);
+ for (unsigned i = 0; i < 1400; i += 2) {
+ addresses[i] = guest_malloc(48);
+ memset((void *)guest_address(addresses[i]), 0x73, 48);
+ memcpy((void *)guest_address(addresses[i]), &i, sizeof(i));
+ }
+
+ for (unsigned i = 0; i < 1400; ++i) {
+ unsigned value;
+ memcpy(&value, (void *)guest_address(addresses[i]), sizeof(value));
+ assert(value == i && *(unsigned char *)guest_address(addresses[i] + 47) == 0x73);
+ guest_free(addresses[i]);
+ }
+}
+
+static void realloc_between_small_sizes_preserves_bytes(void)
+{
+ uint32_t original = guest_malloc(16);
+ memcpy((void *)guest_address(original), "original bytes", 15);
+
+ uint32_t grown = guest_realloc(original, 240);
+
+ assert(grown && grown != original && !(grown & 15));
+ assert(!memcmp((void *)guest_address(grown), "original bytes", 15));
+ memset((void *)guest_address(grown), 0x47, 240);
+ assert(*(unsigned char *)guest_address(grown + 239) == 0x47);
+ guest_free(grown);
+}
+
int main(void)
{
+ empty_pages_reuse_different_sizes();
+ nondividing_size_crosses_pages();
+ realloc_between_small_sizes_preserves_bytes();
small_allocations_preserve_large_holes();
realloc_preserves_bytes();
free_coalesces_within_arena();