Skip to content

Commit

Permalink
Merge pull request #36 from fgh1999/fix-mem-base-addr
Browse files Browse the repository at this point in the history
Fix memory base type in WASI wrapper rust example
  • Loading branch information
ainozaki authored Jul 5, 2024
2 parents 16f78dd + 1cccc27 commit bacb755
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 35 deletions.
12 changes: 2 additions & 10 deletions examples/wasi-wrapper/rust/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,13 @@ fn main() {
.status()
.expect("failed to compile target into WASM");
Command::new("wasker")
.args([
"-o",
&target_obj_name,
"target/wasm32-wasi/debug/rust.wasm",
])
.args(["-o", &target_obj_name, "target/wasm32-wasi/debug/rust.wasm"])
.status()
.expect("failed to compile target into obj");

let target_lib_name = format!("lib{}.a", target_name);
Command::new("ar")
.args([
"rcs",
&target_lib_name,
&target_obj_name,
])
.args(["rcs", &target_lib_name, &target_obj_name])
.status()
.expect("failed to convert obj into lib");

Expand Down
40 changes: 15 additions & 25 deletions examples/wasi-wrapper/rust/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,28 @@ static LINEAR_MEMORY_BLOCK_NUM: Mutex<i32> = Mutex::new(0);

#[inline]
pub unsafe fn get_memory_base() -> *mut u8 {
unsafe { LINEAR_MEMORY_BASE }
LINEAR_MEMORY_BASE
}

unsafe fn alloc_memory() -> *mut u8 {
use std::alloc::{alloc, Layout};
unsafe {
LINEAR_MEMORY_BASE = alloc(
Layout::from_size_align(
(LINEAR_MEMORY_BLOCK_SIZE * LINEAR_MEMORY_BLOCK_NUM_MAX) as usize,
8,
)
.unwrap(),
);
LINEAR_MEMORY_BASE
}
LINEAR_MEMORY_BASE = alloc(
Layout::from_size_align(
(LINEAR_MEMORY_BLOCK_SIZE * LINEAR_MEMORY_BLOCK_NUM_MAX) as usize,
8,
)
.unwrap(),
);
LINEAR_MEMORY_BASE
}

// fn get_memory_block_num() -> i32 {
// LINEAR_MEMORY_BLOCK_NUM.lock().unwrap().clone()
// }
#[no_mangle]
pub extern "C" fn memory_base() -> usize {
unsafe { alloc_memory() as usize }
}

fn inc_memory_block_num(block_num: i32) -> i32 {
#[no_mangle]
pub extern "C" fn memory_grow(block_num: i32) -> i32 {
assert!(
block_num >= 0,
"block_num must be greater than or equal to 0"
Expand All @@ -44,13 +44,3 @@ fn inc_memory_block_num(block_num: i32) -> i32 {
*num += block_num;
old_val
}

#[no_mangle]
pub extern "C" fn memory_base() -> i32 {
unsafe { alloc_memory() as i32 }
}

#[no_mangle]
pub extern "C" fn memory_grow(block_num: i32) -> i32 {
inc_memory_block_num(block_num)
}

0 comments on commit bacb755

Please sign in to comment.