diff --git a/cli/assets/templates/rust/.cargo/config.toml b/cli/assets/templates/rust/.cargo/config.toml index 2f06059f..b241869a 100644 --- a/cli/assets/templates/rust/.cargo/config.toml +++ b/cli/assets/templates/rust/.cargo/config.toml @@ -10,8 +10,8 @@ rustflags = [ "-C", "link-arg=--initial-memory=65536", "-C", "link-arg=--max-memory=65536", - # Reserve 1024 bytes of stack space, offset from 6580 - "-C", "link-arg=-zstack-size=7584", + # Reserve 1228 bytes of stack space, offset from 6580 + "-C", "link-arg=-zstack-size=7808", # Not working? https://github.com/rust-lang/rust/issues/46645#issuecomment-423912553 # "-C", "link-arg=--global-base=6560", diff --git a/cli/assets/templates/rust/Cargo.toml b/cli/assets/templates/rust/Cargo.toml index 763ed1e1..74fc6559 100644 --- a/cli/assets/templates/rust/Cargo.toml +++ b/cli/assets/templates/rust/Cargo.toml @@ -3,11 +3,17 @@ name = "cart" version = "0.1.0" edition = "2018" +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [lib] crate-type = ["cdylib"] [dependencies] +buddy-alloc = { version = "0.4.1", optional = true } [profile.release] opt-level = "z" lto = true + +[features] +# use `--no-default-features` or comment out next line to disable allocator +default = ["buddy-alloc"] \ No newline at end of file diff --git a/cli/assets/templates/rust/src/alloc.rs b/cli/assets/templates/rust/src/alloc.rs new file mode 100644 index 00000000..db491de2 --- /dev/null +++ b/cli/assets/templates/rust/src/alloc.rs @@ -0,0 +1,29 @@ +use buddy_alloc::{BuddyAllocParam, FastAllocParam, NonThreadsafeAlloc}; + +extern "C" { + pub(crate) static __heap_base: usize; +} + +/// Returns a pointer with offset `__heap_base`. +/// based on https://github.com/rustwasm/wee_alloc/issues/88#issuecomment-581404618 +macro_rules! heap_ptr { + ($t:ty) => {{ + &crate::alloc::__heap_base as *const _ as *const () as *const $t + }}; +} + +pub(crate) use heap_ptr; + +// These values can be tuned +const FAST_HEAP_SIZE: usize = 4 * 1024; // 4 KB +const HEAP_SIZE: usize = 16 * 1024; // 16 KB +const LEAF_SIZE: usize = 16; + +static mut FAST_HEAP: [u8; FAST_HEAP_SIZE] = [0u8; FAST_HEAP_SIZE]; + +#[global_allocator] +static ALLOC: NonThreadsafeAlloc = unsafe { + let fast_param = FastAllocParam::new(FAST_HEAP.as_ptr(), FAST_HEAP_SIZE); + let buddy_param = BuddyAllocParam::new(heap_ptr!(u8), HEAP_SIZE, LEAF_SIZE); + NonThreadsafeAlloc::new(fast_param, buddy_param) +}; diff --git a/cli/assets/templates/rust/src/lib.rs b/cli/assets/templates/rust/src/lib.rs index f973422e..57848842 100644 --- a/cli/assets/templates/rust/src/lib.rs +++ b/cli/assets/templates/rust/src/lib.rs @@ -1,19 +1,14 @@ +#[cfg(feature = "buddy-alloc")] +mod alloc; mod wasm4; use wasm4::*; const SMILEY: [u8; 8] = [ - 0b11000011, - 0b10000001, - 0b00100100, - 0b00100100, - 0b00000000, - 0b00100100, - 0b10011001, - 0b11000011, + 0b11000011, 0b10000001, 0b00100100, 0b00100100, 0b00000000, 0b00100100, 0b10011001, 0b11000011, ]; #[no_mangle] -fn update () { +fn update() { unsafe { *DRAW_COLORS = 2 } text("Hello from Rust!", 10, 10);