wip
Some checks failed
📊 Benchmark the Runtime / release (pull_request) Failing after 1m16s
🚀 Lint & Test & Deploy / release (pull_request) Failing after 1m19s

This commit is contained in:
2026-01-23 14:11:27 +01:00
parent 4cb24e8ff9
commit be8161ec8d
4 changed files with 25 additions and 24 deletions

View File

@@ -125,7 +125,7 @@ pub fn nodarium_execute(_attr: TokenStream, item: TokenStream) -> TokenStream {
unsafe { unsafe {
let src = result.as_ptr() as *const u8; let src = result.as_ptr() as *const u8;
let dst = output_pos as *mut u8; let dst = output_pos as *mut u8;
// nodarium_utils::log!("writing output_pos={:?} src={:?} len_bytes={:?}", output_pos, src, len_bytes); nodarium_utils::log!("writing output_pos={:?} src={:?} len_bytes={:?}", output_pos, src, len_bytes);
dst.copy_from_nonoverlapping(src, len_bytes); dst.copy_from_nonoverlapping(src, len_bytes);
} }

View File

@@ -9,52 +9,52 @@ extern "C" {
#[allow(dead_code)] #[allow(dead_code)]
const WASM_PAGE_SIZE: usize = 64 * 1024; const WASM_PAGE_SIZE: usize = 64 * 1024;
pub struct DownwardBumpAllocator { pub struct UpwardBumpAllocator {
heap_top: AtomicUsize, heap_base: AtomicUsize,
} }
impl Default for DownwardBumpAllocator { impl Default for UpwardBumpAllocator {
fn default() -> Self { fn default() -> Self {
Self::new() Self::new()
} }
} }
impl DownwardBumpAllocator { impl UpwardBumpAllocator {
pub const fn new() -> Self { pub const fn new() -> Self {
Self { Self {
heap_top: AtomicUsize::new(0), heap_base: AtomicUsize::new(0),
} }
} }
#[allow(dead_code)] #[allow(dead_code)]
pub fn init(&self) { pub fn init(&self) {
let pages = unsafe { __wasm_memory_size() }; // Start heap at 10000 to leave space for data sections
let mem_size = pages * WASM_PAGE_SIZE; self.heap_base.store(10000, Ordering::Relaxed);
self.heap_top.store(mem_size, Ordering::Relaxed);
} }
} }
#[global_allocator] #[global_allocator]
pub static ALLOCATOR: DownwardBumpAllocator = DownwardBumpAllocator::new(); pub static ALLOCATOR: UpwardBumpAllocator = UpwardBumpAllocator::new();
unsafe impl GlobalAlloc for DownwardBumpAllocator { unsafe impl GlobalAlloc for UpwardBumpAllocator {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 { unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
let align = layout.align(); let align = layout.align();
let size = layout.size(); let size = layout.size();
let mut current = self.heap_top.load(Ordering::Relaxed); let mut current = self.heap_base.load(Ordering::Relaxed);
loop { loop {
let aligned = (current - size) & !(align - 1); let aligned = (current + align - 1) & !(align - 1);
let new_current = aligned + size;
let manual_end = unsafe { __nodarium_manual_end() }; let manual_end = unsafe { __nodarium_manual_end() };
if aligned < manual_end { if new_current > manual_end {
return core::ptr::null_mut(); return core::ptr::null_mut();
} }
match self.heap_top.compare_exchange( match self.heap_base.compare_exchange(
current, current,
aligned, new_current,
Ordering::SeqCst, Ordering::SeqCst,
Ordering::Relaxed, Ordering::Relaxed,
) { ) {

View File

@@ -31,6 +31,7 @@ pub fn read_f32(ptr: i32) -> f32 {
#[inline] #[inline]
pub fn read_i32_slice(range: (i32, i32)) -> Vec<i32> { pub fn read_i32_slice(range: (i32, i32)) -> Vec<i32> {
log!("read_i32_slice ptr: {:?}", range);
let (start, end) = range; let (start, end) = range;
assert!(end >= start); assert!(end >= start);
let byte_len = (end - start) as usize; let byte_len = (end - start) as usize;