Skip to content

Commit

Permalink
bring back WeeAlloc; README edit; make the test compile on non-wasm t…
Browse files Browse the repository at this point in the history
…argets; unsafe fn
  • Loading branch information
Michael Benfield committed Dec 4, 2023
1 parent e3fa50c commit 3953ba2
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 17 deletions.
63 changes: 55 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions mini-alloc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ wasm-bindgen-test = "0.3.0"

[dependencies]
cfg-if = "1.0.0"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
wee_alloc = "0.4.5"
6 changes: 4 additions & 2 deletions mini-alloc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ thread.
Also, `core::arch::wasm32::memory_grow` must never be called by any code outside
this crate.

On targets other than wasm32, `MiniAlloc` simply forwards to the allocator from
another crate, `wee_alloc::WeeAlloc`.

Use it like this:

```rust
#[cfg(target_arch = "wasm32")]
#[global_allocator]
static ALLOC: mini_alloc::MiniAlloc = mini_alloc::MiniAlloc;
static ALLOC: mini_alloc::MiniAlloc = mini_alloc::MiniAlloc::INIT;
```
14 changes: 9 additions & 5 deletions mini-alloc/src/imp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ pub struct MiniAlloc;
/// This is not a valid implementation of [`Sync`] but is ok in single-threaded WASM.
unsafe impl Sync for MiniAlloc {}

impl MiniAlloc {
pub const INIT: Self = MiniAlloc;
}

unsafe impl GlobalAlloc for MiniAlloc {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
alloc_impl(layout).unwrap_or(core::ptr::null_mut())
Expand All @@ -35,12 +39,12 @@ const PAGE_SIZE: usize = 1 << 16;
/// instructions than the positive offset.
static mut STATE: Option<(NonZero, usize)> = None;

fn alloc_impl(layout: Layout) -> Option<*mut u8> {
let (neg_offset, neg_bound) = unsafe { &mut STATE }.get_or_insert_with(|| {
let heap_base = unsafe { &__heap_base } as *const u8 as usize;
unsafe fn alloc_impl(layout: Layout) -> Option<*mut u8> {
let (neg_offset, neg_bound) = STATE.get_or_insert_with(|| {
let heap_base = &__heap_base as *const u8 as usize;
let bound = PAGE_SIZE * wasm32::memory_size(0);
(
unsafe { NonZero::new_unchecked(heap_base.wrapping_neg()) },
NonZero::new_unchecked(heap_base.wrapping_neg()),
bound.wrapping_neg(),
)
});
Expand All @@ -55,7 +59,7 @@ fn alloc_impl(layout: Layout) -> Option<*mut u8> {
}
*neg_bound -= PAGE_SIZE * pages_needed;
}
*neg_offset = unsafe { NonZero::new_unchecked(next_neg_offset) };
*neg_offset = NonZero::new_unchecked(next_neg_offset);
Some(neg_aligned.wrapping_neg() as *mut u8)
}

Expand Down
2 changes: 2 additions & 0 deletions mini-alloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ cfg_if! {
if #[cfg(target_arch = "wasm32")] {
mod imp;
pub use imp::MiniAlloc;
} else {
pub use wee_alloc::WeeAlloc as MiniAlloc;
}
}
4 changes: 2 additions & 2 deletions mini-alloc/tests/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

extern crate alloc;

use alloc::vec::Vec;

#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::*;

Expand All @@ -17,6 +15,8 @@ static ALLOC: mini_alloc::MiniAlloc = mini_alloc::MiniAlloc;
#[cfg(target_arch = "wasm32")]
#[wasm_bindgen_test]
fn vec_test() {
use alloc::vec::Vec;

let p1 = Vec::<u8>::with_capacity(700);
let p2 = Vec::<u8>::with_capacity(65536);
let p3 = Vec::<u8>::with_capacity(700000);
Expand Down

0 comments on commit 3953ba2

Please sign in to comment.