-
Notifications
You must be signed in to change notification settings - Fork 296
Add initial wasm memory grow/current intrinsics #361
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
extern "C" { | ||
#[link_name = "llvm.wasm.grow.memory.i32"] | ||
fn llvm_grow_memory(pages: i32) -> i32; | ||
#[link_name = "llvm.wasm.current.memory.i32"] | ||
fn llvm_current_memory() -> i32; | ||
} | ||
|
||
/// Corresponding intrinsic to wasm's [`current_memory` instruction][instr] | ||
/// | ||
/// This function, when called, will return the current memory size in units of | ||
/// pages. | ||
/// | ||
/// [instr]: https://github.com/WebAssembly/design/blob/master/Semantics.md#resizing | ||
#[inline] | ||
pub unsafe fn current_memory() -> i32 { | ||
llvm_current_memory() | ||
} | ||
|
||
/// Corresponding intrinsic to wasm's [`grow_memory` instruction][instr] | ||
/// | ||
/// This function, when called, will attempt to grow the default linear memory | ||
/// by the specified number of pages. If memory is successfully grown then the | ||
/// previous size of memory, in pages, is returned. If memory cannot be grown | ||
/// then -1 is returned. | ||
/// | ||
/// [instr]: https://github.com/WebAssembly/design/blob/master/Semantics.md#resizing | ||
#[inline] | ||
pub unsafe fn grow_memory(delta: i32) -> i32 { | ||
llvm_grow_memory(delta) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
//! A simple slab allocator for pages in wasm | ||
|
||
#![feature(stdsimd)] | ||
#![cfg(target_arch = "wasm32")] | ||
|
||
extern crate stdsimd; | ||
|
||
use std::ptr; | ||
|
||
use stdsimd::arch::wasm32::*; | ||
|
||
static mut HEAD: *mut *mut u8 = 0 as _; | ||
|
||
#[no_mangle] | ||
pub unsafe extern "C" fn page_alloc() -> *mut u8 { | ||
if !HEAD.is_null() { | ||
let next = *HEAD; | ||
let ret = HEAD; | ||
HEAD = next as *mut _; | ||
return ret as *mut u8; | ||
} | ||
|
||
let ret = grow_memory(1); | ||
|
||
// if we failed to allocate a page then return null | ||
if ret == -1 { | ||
return ptr::null_mut(); | ||
} | ||
|
||
((ret as u32) * page_size()) as *mut u8 | ||
} | ||
|
||
#[no_mangle] | ||
pub unsafe extern "C" fn page_free(page: *mut u8) { | ||
let page = page as *mut *mut u8; | ||
*page = HEAD as *mut u8; | ||
HEAD = page; | ||
} | ||
|
||
#[no_mangle] | ||
pub unsafe extern "C" fn memory_used() -> usize { | ||
(page_size() * (current_memory() as u32)) as usize | ||
} | ||
|
||
fn page_size() -> u32 { | ||
64 * 1024 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer if we would just disable the
detect
module in the platforms in which it is not used. I can do this in the MIPS PR since I've cleaned up the detect module there quite a bit.