wip
This commit is contained in:
@@ -82,14 +82,14 @@ export type Pointer = {
|
|||||||
|
|
||||||
perf?: PerformanceStore;
|
perf?: PerformanceStore;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private readonly registry: NodeRegistry,
|
private readonly registry: NodeRegistry,
|
||||||
public cache?: SyncCache<Int32Array>
|
public cache?: SyncCache<Int32Array>
|
||||||
) {
|
) {
|
||||||
this.cache = undefined;
|
this.cache = undefined;
|
||||||
this.refreshView();
|
this.refreshView();
|
||||||
log.info('MemoryRuntimeExecutor initialized');
|
log.info('MemoryRuntimeExecutor initialized');
|
||||||
}
|
}
|
||||||
|
|
||||||
private refreshView(): void {
|
private refreshView(): void {
|
||||||
this.memoryView = new Int32Array(this.memory.buffer);
|
this.memoryView = new Int32Array(this.memory.buffer);
|
||||||
|
|||||||
@@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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,
|
||||||
) {
|
) {
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
Reference in New Issue
Block a user