growable buddy memory runtime

This commit is contained in:
2026-02-13 07:59:52 -06:00
parent 36fd0a35f9
commit 1ba060668e
3 changed files with 278 additions and 22 deletions

View File

@@ -95,6 +95,9 @@
/* test the GC by forcing it before each object allocation */
// #define FORCE_GC_AT_MALLOC
#include <sys/mman.h>
#include <unistd.h>
#define POISON_HEAP
/* POISON_HEAP: Use ASan's memory poisoning to detect stale pointer access */
#ifdef POISON_HEAP
@@ -116,9 +119,6 @@
#define gc_unpoison_region(addr, size) ((void)0)
#endif
#include <sys/mman.h>
#include <unistd.h>
static inline size_t poison_page_align(size_t size) {
size_t ps = (size_t)sysconf(_SC_PAGESIZE);
return (size + ps - 1) & ~(ps - 1);
@@ -333,9 +333,8 @@ static inline objhdr_t objhdr_set_cap56 (objhdr_t h, uint64_t cap) {
#else
#define BUDDY_MIN_ORDER 9 /* 512B minimum on 32-bit */
#endif
#define BUDDY_MAX_ORDER 28 /* 256MB maximum */
#define BUDDY_LEVELS (BUDDY_MAX_ORDER - BUDDY_MIN_ORDER + 1)
#define BUDDY_POOL_SIZE (1ULL << BUDDY_MAX_ORDER)
#define BUDDY_MAX_LEVELS 40 /* supports pools up to 2^(BUDDY_MIN_ORDER+39) */
#define BUDDY_DEFAULT_POOL (1ULL << 24) /* 16MB initial pool */
typedef struct BuddyBlock {
struct BuddyBlock *next;
@@ -344,15 +343,26 @@ typedef struct BuddyBlock {
uint8_t is_free;
} BuddyBlock;
typedef struct BuddyPool {
struct BuddyPool *next;
uint8_t *base;
size_t total_size;
uint8_t max_order; /* log2(total_size) */
uint32_t alloc_count; /* outstanding allocations */
BuddyBlock *free_lists[BUDDY_MAX_LEVELS];
} BuddyPool;
typedef struct BuddyAllocator {
uint8_t *base; /* 256MB base address */
size_t total_size; /* 256MB */
BuddyBlock *free_lists[BUDDY_LEVELS];
uint8_t initialized;
BuddyPool *pools; /* linked list, newest first */
size_t next_pool_size; /* next pool doubles from this */
size_t initial_size; /* starting pool size */
size_t cap; /* 0 = no cap */
size_t total_mapped; /* sum of all pool sizes */
} BuddyAllocator;
/* Forward declarations for buddy allocator functions */
static void buddy_destroy (BuddyAllocator *b);
static size_t buddy_max_block (BuddyAllocator *b);
/* controls a host of contexts, handing out memory and scheduling */
struct JSRuntime {