From 933b76e1702b64867dcf86e379e5bdad7394a939 Mon Sep 17 00:00:00 2001 From: Theo Butler Date: Sat, 28 Nov 2026 08:07:06 -0700 Subject: [PATCH 1/3] fix test deallocate layout --- snmalloc-rs/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/snmalloc-rs/src/lib.rs b/snmalloc-rs/src/lib.rs index 3a7a89cb1..9d766a03c 100644 --- a/snmalloc-rs/src/lib.rs +++ b/snmalloc-rs/src/lib.rs @@ -169,7 +169,7 @@ mod tests { // Test reallocation let ptr = alloc.alloc(layout); let ptr = alloc.realloc(ptr, layout, 16); - alloc.dealloc(ptr, layout); + alloc.dealloc(ptr, Layout::from_size_align(16, 8).unwrap()); // Test large allocation let large_layout = Layout::from_size_align(1 << 20, 32).unwrap(); @@ -207,7 +207,7 @@ mod tests { let ptr = alloc.alloc(layout); let ptr = alloc.realloc(ptr, layout, 16); - alloc.dealloc(ptr, layout); + alloc.dealloc(ptr, Layout::from_size_align(16, 8).unwrap()); } } From 91e10d4f6747dfb44f10789baf9b5ac3b1e8b6bd Mon Sep 17 00:00:00 2001 From: Theo Butler Date: Sat, 28 Nov 2026 08:26:06 -0700 Subject: [PATCH 2/3] fix sn_rust_dealloc return type --- snmalloc-rs/snmalloc-sys/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snmalloc-rs/snmalloc-sys/src/lib.rs b/snmalloc-rs/snmalloc-sys/src/lib.rs index 3c2cc7b36..541ab79c7 100644 --- a/snmalloc-rs/snmalloc-sys/src/lib.rs +++ b/snmalloc-rs/snmalloc-sys/src/lib.rs @@ -18,7 +18,7 @@ extern "C" { /// - the memory is acquired using the same allocator and the pointer points to the start position. /// - `alignment` and `size` is the same as allocation /// The program may be forced to abort if the constrains are not full-filled. - pub fn sn_rust_dealloc(ptr: *mut c_void, alignment: usize, size: usize) -> c_void; + pub fn sn_rust_dealloc(ptr: *mut c_void, alignment: usize, size: usize); /// Behaves like rust_alloc, but also ensures that the contents are set to zero before being returned. pub fn sn_rust_alloc_zeroed(alignment: usize, size: usize) -> *mut c_void; From af5fd442a45e4e61e6a81c6f3dee7ccbc0678876 Mon Sep 17 00:00:00 2001 From: Theo Butler Date: Sat, 28 Nov 2026 08:55:57 -0700 Subject: [PATCH 3/3] snmalloc-rs: add opt-in Allocator API support --- .github/workflows/rust.yml | 17 +++++++++++++++ snmalloc-rs/Cargo.toml | 1 + snmalloc-rs/README.md | 1 + snmalloc-rs/src/lib.rs | 43 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 62 insertions(+) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 726fd2c93..92f4a2f05 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -71,6 +71,23 @@ jobs: - name: Run tests run: cargo test ${{ matrix.release.flag }} --all ${{ matrix.features.args }} + allocator-api-compatibility: + runs-on: ubuntu-latest + strategy: + matrix: + include: + - rust: "1.85.0" + args: "" + - rust: "1.100.0" + args: "--features allocator-api" + steps: + - name: Checkout + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - name: Select Rust toolchain + run: rustup toolchain install ${{ matrix.rust }} --profile minimal + - name: Run tests + run: cargo +${{ matrix.rust }} test -p snmalloc-rs ${{ matrix.args }} + publish-scan: runs-on: ubuntu-latest name: publish-scan diff --git a/snmalloc-rs/Cargo.toml b/snmalloc-rs/Cargo.toml index a2402874f..8317478e0 100644 --- a/snmalloc-rs/Cargo.toml +++ b/snmalloc-rs/Cargo.toml @@ -17,6 +17,7 @@ snmalloc-sys = { version = "0.7.6", path = "snmalloc-sys", default-features = fa [features] default = ["snmalloc-sys/build_cmake", "snmalloc-sys/usewait-on-address"] +allocator-api = [] build_cc = ["snmalloc-sys/build_cc"] qemu = ["snmalloc-sys/qemu"] debug = ["snmalloc-sys/debug"] diff --git a/snmalloc-rs/README.md b/snmalloc-rs/README.md index f0cd64003..b2caf18b1 100644 --- a/snmalloc-rs/README.md +++ b/snmalloc-rs/README.md @@ -36,6 +36,7 @@ There are the following features defined in this crate: - `check-loads`: Enable check loads feature. - `pageid`: Enable page ID feature. - `gwp-asan`: Enable GWP-ASan integration. Requires `SNMALLOC_GWP_ASAN_INCLUDE_PATH` and `SNMALLOC_GWP_ASAN_LIBRARY_PATH`. +- `allocator-api`: Implement `core::alloc::Allocator` (requires Rust 1.100 or newer). ## Build Configuration diff --git a/snmalloc-rs/src/lib.rs b/snmalloc-rs/src/lib.rs index 9d766a03c..928cb623b 100644 --- a/snmalloc-rs/src/lib.rs +++ b/snmalloc-rs/src/lib.rs @@ -27,6 +27,9 @@ //! ``` extern crate snmalloc_sys as ffi; +#[cfg(feature = "allocator-api")] +use core::alloc::{AllocError, Allocator}; + use core::{ alloc::{GlobalAlloc, Layout}, ptr::NonNull, @@ -149,6 +152,33 @@ unsafe impl GlobalAlloc for SnMalloc { } } +#[cfg(feature = "allocator-api")] +unsafe impl Allocator for SnMalloc { + #[inline(always)] + fn allocate(&self, layout: Layout) -> Result, AllocError> { + self.alloc_aligned(layout) + .map(|ptr| NonNull::slice_from_raw_parts(ptr, layout.size())) + .ok_or(AllocError) + } + + #[inline(always)] + unsafe fn deallocate(&self, ptr: NonNull, layout: Layout) { + if layout.size() != 0 { + unsafe { self.dealloc(ptr.as_ptr(), layout) }; + } + } + + #[inline(always)] + fn allocate_zeroed(&self, layout: Layout) -> Result, AllocError> { + if layout.size() == 0 { + return self.allocate(layout); + } + NonNull::new(unsafe { self.alloc_zeroed(layout) }) + .map(|ptr| NonNull::slice_from_raw_parts(ptr, layout.size())) + .ok_or(AllocError) + } +} + #[cfg(test)] mod tests { use super::*; @@ -233,4 +263,17 @@ mod tests { assert!(usz >= 8); } } + + #[cfg(feature = "allocator-api")] + #[test] + fn standard_vec() { + extern crate alloc; + + let mut values = alloc::vec::Vec::new_in(SnMalloc); + values.extend(0..128); + values.reserve(256); + values.truncate(7); + values.shrink_to_fit(); + assert_eq!(values.as_slice(), &[0, 1, 2, 3, 4, 5, 6]); + } }