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

@ -163,6 +163,7 @@ rec {
'';
};
# Provides clang to compile C++ to Wasm
wasi-sdk = stdenv.mkDerivation rec {
pname = "wasi-sdk";
version = "29";
@ -195,6 +196,7 @@ rec {
'';
};
# Provides the iwasm interpreter
iwasm = stdenv.mkDerivation rec {
pname = "iwasm";
version = "2.4.4";
@ -229,6 +231,7 @@ rec {
'';
};
# Provides the wamrc compiler
wamrc = stdenv.mkDerivation rec {
pname = "wamrc";
version = "2.4.4";

View File

@ -54,7 +54,7 @@ my @aot_section_variants = (
"Place AOT array in .text.wamr_aot",
"Let the linker decide where the AOT array is located",
);
my $selected_aot_variant =
my ($selected_aot_variant) =
TUI::select_from_list( "Select WAMR Array.Text Variant",
0, @aot_section_variants );
die "No AOT section variant selected" unless $selected_aot_variant;
@ -68,7 +68,7 @@ my @mmap_variants = (
"Place mmap_space in .text.wamr_mmap",
"Let the linker decide where mmap_space is located"
);
my $selected_mmap_variant =
my ($selected_mmap_variant) =
TUI::select_from_list( "Select WAMR Mmap.Text Variant", 0, @mmap_variants );
die "No variant selected" unless $selected_mmap_variant;
local $ENV{WAMR_USE_MMAP_IN_TEXT} =
@ -81,18 +81,18 @@ my @allocator_variants = (
"Pool allocator (Alloc_With_Pool)",
"Allocator with usage (Alloc_With_Allocator)",
);
my $selected_allocator_variant =
my ($selected_allocator_variant) =
TUI::select_from_list( "Select WAMR Allocator Variant",
0, @allocator_variants );
die "No allocator variant selected" unless $selected_allocator_variant;
local $ENV{WAMR_USE_ALLOCATOR} =
( $selected_allocator_variant eq $allocator_variants[0] )
? "true"
: "false";
? "false"
: "true";
# Select XIP variant
my @xip_variants = ( "Compile AOT with --xip", "Compile AOT without --xip" );
my $selected_xip_variant =
my ($selected_xip_variant) =
TUI::select_from_list( "Select WAMRC XIP Variant", 0, @xip_variants );
die "No XIP variant selected" unless $selected_xip_variant;
local $ENV{WAMR_USE_XIP} =

View File

@ -71,6 +71,11 @@ my $cmake_c_flags = join( ' ', @variant_cflags );
my @variant_cmake_flags;
push @variant_cmake_flags, '-DWAMR_BUILD_ALLOC_WITH_USAGE=1' if $use_allocator;
# Defines forwarded to the host compilation so wasm_host.c sees the same
# WASM_MEM_ALLOC_WITH_USAGE value as libiwasm.a was built with.
my @host_variant_cflags;
push @host_variant_cflags, '-DWASM_MEM_ALLOC_WITH_USAGE=1' if $use_allocator;
# ========================================================================================= #
# Compiler / linker flags
# ========================================================================================= #
@ -260,10 +265,7 @@ sub make_interp_array {
sub prepare_wasm_host {
my ( $module, $bd, $mode ) = @_;
my $template =
$use_allocator
? 'targets/wasm-host/wasm_host_allocator.c'
: 'targets/wasm-host/wasm_host_pool.c';
my $template = 'targets/wasm-host/wasm_host.c';
my $mod_c = ( $module =~ s/-/_/gr );
my ( $array_file, $array_sym, $len_sym ) =
@ -290,29 +292,26 @@ sub compile_wasm_host {
my ( $module, $bd, $target ) = @_;
if ( $target eq 'fail' ) {
Util::run(
$cross_cc, '-I./targets/wasm-host',
@cross_cflags, @wamr_inc_baremetal,
'-DTARGET_FAIL', '-c',
"$bd/module_host.c", '-o',
"$bd/system.o"
$cross_cc, '-I./targets/wasm-host',
@cross_cflags, @host_variant_cflags,
@wamr_inc_baremetal, '-DTARGET_FAIL',
'-c', "$bd/module_host.c", '-o', "$bd/system.o"
);
}
elsif ( $target eq 'linux' ) {
Util::run(
$linux_cc, '-I./targets/wasm-host',
@linux_cflags, @wamr_inc_linux,
'-DTARGET_LINUX', '-c',
"$bd/module_host.c", '-o',
"$bd/system.o"
$linux_cc, '-I./targets/wasm-host',
@linux_cflags, @host_variant_cflags,
@wamr_inc_linux, '-DTARGET_LINUX',
'-c', "$bd/module_host.c", '-o', "$bd/system.o"
);
}
elsif ( $target eq 'linux-baremetal' ) {
Util::run(
$cross_cc, '-I./targets/wasm-host',
@linux_baremetal_cflags, @wamr_inc_baremetal,
'-DTARGET_LINUX_BAREMETAL', '-c',
"$bd/module_host.c", '-o',
"$bd/system.o"
$cross_cc, '-I./targets/wasm-host',
@linux_baremetal_cflags, @host_variant_cflags,
@wamr_inc_baremetal, '-DTARGET_LINUX_BAREMETAL',
'-c', "$bd/module_host.c", '-o', "$bd/system.o"
);
}
else {

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");