diff --git a/Cargo.lock b/Cargo.lock index 6509267f..6d6fa839 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -18,7 +18,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "66f73f11dcfbf8bb763d88fb1d082fe7cca0a00d3227d9921bdbd52ce5e013e2" dependencies = [ "bytes", - "cfg-if", + "cfg-if 1.0.0", "const-hex", "derive_more", "hex-literal", @@ -92,6 +92,12 @@ version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "89b2fd2a0dcf38d7971e2194b6b6eebab45ae01067456a7fd93d5547a61b70be" +[[package]] +name = "cfg-if" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" + [[package]] name = "cfg-if" version = "1.0.0" @@ -104,7 +110,7 @@ version = "0.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a06aeb73f470f66dcdbf7223caeebb85984942f22f1adb2a088cf9668146bbbc" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "wasm-bindgen", ] @@ -114,7 +120,7 @@ version = "1.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "268f52aae268980d03dd9544c1ea591965b2735b038d6998d6e4ab37c8c24445" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "cpufeatures", "hex", "serde", @@ -294,12 +300,19 @@ version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" +[[package]] +name = "memory_units" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8452105ba047068f40ff7093dd1d9da90898e63dd61736462e9cdda6a90ad3c3" + [[package]] name = "mini-alloc" version = "0.4.2" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "wasm-bindgen-test", + "wee_alloc", ] [[package]] @@ -489,7 +502,7 @@ version = "0.4.2" dependencies = [ "alloy-primitives", "alloy-sol-types", - "cfg-if", + "cfg-if 1.0.0", "convert_case 0.6.0", "lazy_static", "proc-macro2", @@ -506,7 +519,7 @@ version = "0.4.2" dependencies = [ "alloy-primitives", "alloy-sol-types", - "cfg-if", + "cfg-if 1.0.0", "derivative", "fnv", "hex", @@ -602,7 +615,7 @@ version = "0.2.89" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0ed0d4f68a3015cc185aff4db9506a015f4b96f95303897bfa23f846db54064e" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "wasm-bindgen-macro", ] @@ -627,7 +640,7 @@ version = "0.4.39" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac36a15a220124ac510204aec1c3e5db8a22ab06fd6706d881dc6149f8ed9a12" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "js-sys", "wasm-bindgen", "web-sys", @@ -697,6 +710,40 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "wee_alloc" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dbb3b5a6b2bb17cb6ad44a2e68a43e8d2722c997da10e928665c72ec6c0a0b8e" +dependencies = [ + "cfg-if 0.1.10", + "libc", + "memory_units", + "winapi", +] + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + [[package]] name = "zeroize" version = "1.6.0" diff --git a/mini-alloc/Cargo.toml b/mini-alloc/Cargo.toml index 66b77f97..9d259514 100644 --- a/mini-alloc/Cargo.toml +++ b/mini-alloc/Cargo.toml @@ -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" diff --git a/mini-alloc/README.md b/mini-alloc/README.md index 3bdc2fe2..c90d4ed0 100644 --- a/mini-alloc/README.md +++ b/mini-alloc/README.md @@ -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; ``` diff --git a/mini-alloc/src/imp.rs b/mini-alloc/src/imp.rs index f3a5c91c..5524a4bd 100644 --- a/mini-alloc/src/imp.rs +++ b/mini-alloc/src/imp.rs @@ -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()) @@ -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(), ) }); @@ -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) } diff --git a/mini-alloc/src/lib.rs b/mini-alloc/src/lib.rs index 216615c8..69b1aa9b 100644 --- a/mini-alloc/src/lib.rs +++ b/mini-alloc/src/lib.rs @@ -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; } } diff --git a/mini-alloc/tests/misc.rs b/mini-alloc/tests/misc.rs index 19df0156..58ba2cc4 100644 --- a/mini-alloc/tests/misc.rs +++ b/mini-alloc/tests/misc.rs @@ -5,8 +5,6 @@ extern crate alloc; -use alloc::vec::Vec; - #[cfg(target_arch = "wasm32")] use wasm_bindgen_test::*; @@ -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::::with_capacity(700); let p2 = Vec::::with_capacity(65536); let p3 = Vec::::with_capacity(700000);