implement another bump allocator for WAMR's Alloc_With_Allocator

This commit is contained in:
2026-06-05 19:11:00 +02:00
parent 7284849684
commit b6355a67f3
4 changed files with 110 additions and 28 deletions

View File

@ -3,7 +3,6 @@
#include "bh_platform.h"
#include "__WASM_ARRAY_FILE__"
#ifdef TARGET_LINUX
#include <stdio.h>
#include <string.h>
@ -34,7 +33,81 @@ void host_print(wasm_exec_env_t exec_env, const char *msg) {
#define STACK_SIZE (4 * 1024)
#define HEAP_SIZE STACK_SIZE
#define RUNTIME_POOL_SIZE 4 * STACK_SIZE
#if WASM_MEM_ALLOC_WITH_USAGE
// Bump allocator
#define WASM_LINEAR_MEMORY_SIZE (64 * 1024) // Need to match --initial-memory?
#define ALLOCATOR_POOL_SIZE (WASM_LINEAR_MEMORY_SIZE + 2 * 1024 * 1024)
#define ALIGNMENT 8
static char allocator_pool[ALLOCATOR_POOL_SIZE];
static size_t pool_offset = 0;
static size_t align_up(size_t x, size_t a) {
// a is a power of two, e.g., 8:
// x+a-1: Shift value upwards to the next alignment "bucket"
// a-1: 00000111, ~(a-1): 11111000 - Clears the lower bits/alignment
// remainder
return (x + a - 1) & ~(a - 1);
}
static size_t alloc_size(void *ptr) {
size_t header_size = align_up(sizeof(size_t), ALIGNMENT);
return *(size_t *)((char *)ptr - header_size);
}
static void *bump_alloc(unsigned int size) {
// Reserve space for a size_t header before the user pointer
size_t header_size = align_up(sizeof(size_t), ALIGNMENT);
size_t start = align_up(pool_offset, ALIGNMENT);
size_t end = start + header_size + align_up(size, ALIGNMENT);
if (end > ALLOCATOR_POOL_SIZE) {
return NULL;
}
*(size_t *)&allocator_pool[start] = size;
void *ptr = &allocator_pool[start + header_size];
pool_offset = end;
return ptr;
}
void *wamr_malloc(mem_alloc_usage_t usage, unsigned int size) {
PRINT("wamr_malloc: usage=%d size=%u\n", usage, size);
return bump_alloc(size);
}
void *wamr_realloc(mem_alloc_usage_t usage, bool full_size_mmaped, void *ptr,
unsigned int new_size) {
PRINT("wamr_realloc: usage=%d full_size_mmaped=%d ptr=%p new_size=%u\n",
usage, full_size_mmaped, ptr, new_size);
void *new_addr = bump_alloc(new_size);
if (!new_addr) {
return NULL;
}
if (ptr) {
size_t old_size = alloc_size(ptr);
memcpy(new_addr, ptr, old_size < new_size ? old_size : new_size);
}
return new_addr;
}
void wamr_free(mem_alloc_usage_t usage, void *ptr) {
PRINT("wamr_free: usage=%d ptr=%p\n", usage, ptr);
}
#else
#define RUNTIME_POOL_SIZE (4 * STACK_SIZE)
static char global_heap_buf[RUNTIME_POOL_SIZE];
#endif // WASM_MEM_ALLOC_WITH_USAGE
MAIN {
char error_buf[128];
@ -42,11 +115,18 @@ MAIN {
// Step 1: Initialize WAMR Runtime
static RuntimeInitArgs init_args;
memset(&init_args, 0, sizeof(RuntimeInitArgs));
static char global_heap_buf[RUNTIME_POOL_SIZE];
#if WASM_MEM_ALLOC_WITH_USAGE
init_args.mem_alloc_type = Alloc_With_Allocator;
init_args.mem_alloc_option.allocator.malloc_func = (void *)wamr_malloc;
init_args.mem_alloc_option.allocator.realloc_func = (void *)wamr_realloc;
init_args.mem_alloc_option.allocator.free_func = (void *)wamr_free;
#else
init_args.mem_alloc_type = Alloc_With_Pool;
init_args.mem_alloc_option.pool.heap_buf = global_heap_buf;
init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);
#endif
init_args.max_thread_num = 1;
if (!wasm_runtime_full_init(&init_args)) {
PRINT_ERROR("wasm_runtime_full_init failed.\n");