Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(rust): allocation not working #111

Merged
merged 5 commits into from
Sep 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions cli/assets/templates/rust/.cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ 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 2044 bytes of stack space, offset from 6580.
# Bump this value, 16-byte aligned, if the framebuffer gets corrupted.
"-C", "link-arg=-zstack-size=8624",

# Not working? https://github.com/rust-lang/rust/issues/46645#issuecomment-423912553
# "-C", "link-arg=--global-base=6560",
Expand Down
6 changes: 6 additions & 0 deletions cli/assets/templates/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
16 changes: 16 additions & 0 deletions cli/assets/templates/rust/src/alloc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use buddy_alloc::{BuddyAllocParam, FastAllocParam, NonThreadsafeAlloc};

// 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];
static mut HEAP: [u8; HEAP_SIZE] = [0u8; 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.as_ptr(), HEAP_SIZE, LEAF_SIZE);
NonThreadsafeAlloc::new(fast_param, buddy_param)
};
5 changes: 4 additions & 1 deletion cli/assets/templates/rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#[cfg(feature = "buddy-alloc")]
mod alloc;
mod wasm4;
use wasm4::*;

#[rustfmt::skip]
const SMILEY: [u8; 8] = [
0b11000011,
0b10000001,
Expand All @@ -13,7 +16,7 @@ const SMILEY: [u8; 8] = [
];

#[no_mangle]
fn update () {
fn update() {
unsafe { *DRAW_COLORS = 2 }
text("Hello from Rust!", 10, 10);

Expand Down
10 changes: 6 additions & 4 deletions cli/assets/templates/rust/src/wasm4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,9 @@ extern "C" {
}

/// Draws text using the built-in system font.
pub fn text(text: &str, x: i32, y: i32) {
unsafe { extern_text(text.as_ptr(), text.len(), x, y) }
pub fn text<T: AsRef<str>>(text: T, x: i32, y: i32) {
let text_ref = text.as_ref();
unsafe { extern_text(text_ref.as_ptr(), text_ref.len(), x, y) }
FaberVitale marked this conversation as resolved.
Show resolved Hide resolved
}
extern "C" {
#[link_name = "textUtf8"]
Expand Down Expand Up @@ -200,8 +201,9 @@ extern "C" {
// └───────────────────────────────────────────────────────────────────────────┘

/// Prints a message to the debug console.
pub fn trace(text: &str) {
unsafe { extern_trace(text.as_ptr(), text.len()) }
pub fn trace<T: AsRef<str>>(text: T) {
let text_ref = text.as_ref();
unsafe { extern_trace(text_ref.as_ptr(), text_ref.len()) }
}
extern "C" {
#[link_name = "traceUtf8"]
Expand Down