Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions snmalloc-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
1 change: 1 addition & 0 deletions snmalloc-rs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion snmalloc-rs/snmalloc-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
47 changes: 45 additions & 2 deletions snmalloc-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<NonNull<[u8]>, 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<u8>, layout: Layout) {
if layout.size() != 0 {
unsafe { self.dealloc(ptr.as_ptr(), layout) };
}
}

#[inline(always)]
fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, 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::*;
Expand All @@ -169,7 +199,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();
Expand Down Expand Up @@ -207,7 +237,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());
}
}

Expand All @@ -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]);
}
}
Loading