Skip to content

Commit

Permalink
Snmalloc v1 maintain oct 10 2021 (#161)
Browse files Browse the repository at this point in the history
* bump module

Signed-off-by: Schrodinger ZHU Yifan <[email protected]>

* deprecate cache-friendly

Signed-off-by: Schrodinger ZHU Yifan <[email protected]>

* bump version and use exposed alloc_zeroed

Signed-off-by: Schrodinger ZHU Yifan <[email protected]>

* update change log

Signed-off-by: Schrodinger ZHU Yifan <[email protected]>
  • Loading branch information
SchrodingerZhu authored Oct 11, 2021
1 parent 8457da5 commit 4292a34
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 32 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## Changelog

### 0.2.28
- Deprecation of `cache-friendly`
- Use exposed `alloc_zeroed` from `snmalloc`
- **upstream** changes of remote communication, corruption detection and compilation flag detection.

### 0.2.27

- Reduction of libc dependency
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "snmalloc-rs"
version = "0.2.27"
version = "0.2.28"
authors = ["schrodingerzhu <[email protected]>"]
edition = "2018"
license = "MIT"
Expand All @@ -16,7 +16,7 @@ readme = "README.md"
members = ["snmalloc-sys" ]

[dependencies]
snmalloc-sys = {version = "0.2.27", path = "snmalloc-sys", default-features = false }
snmalloc-sys = {version = "0.2.28", path = "snmalloc-sys", default-features = false }

[features]
default = ["snmalloc-sys/build_cmake","1mib"]
Expand Down
53 changes: 32 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,32 +1,39 @@
# snmalloc-rs
**Notice: MinGW Build is broken and may not be fixed in a near future. See [this PR](https://github.com/microsoft/snmalloc/pull/217) in the upstream.**

**Notice: MinGW Build is broken and may not be fixed in a near future.
See [this PR](https://github.com/microsoft/snmalloc/pull/217) in the upstream.**

MSVC/MinGW/Linux/MacOS: [![Actions Status](https://github.com/schrodingerzhu/snmalloc-rs/workflows/Rust/badge.svg)](https://github.com/schrodingerzhu/snmalloc-rs/actions)

FreeBSD: [![Build Status](https://api.cirrus-ci.com/github/SchrodingerZhu/snmalloc-rs.svg)](https://cirrus-ci.com/github/SchrodingerZhu/snmalloc-rs)

`snmalloc-rs` provides a wrapper for [`microsoft/snmalloc`](https://github.com/microsoft/snmalloc) to make it usable as a global allocator for rust.
snmalloc is a research allocator. Its key design features are:
`snmalloc-rs` provides a wrapper for [`microsoft/snmalloc`](https://github.com/microsoft/snmalloc) to make it usable as
a global allocator for rust. snmalloc is a research allocator. Its key design features are:

- Memory that is freed by the same thread that allocated it does not require any synchronising operations.
- Freeing memory in a different thread to initially allocated it, does not take any locks and instead uses a novel message passing scheme to return the memory to the original allocator, where it is recycled.
- Freeing memory in a different thread to initially allocated it, does not take any locks and instead uses a novel
message passing scheme to return the memory to the original allocator, where it is recycled.
- The allocator uses large ranges of pages to reduce the amount of meta-data required.

Some old benchmark results are available in the [`snmalloc` paper](https://github.com/microsoft/snmalloc/blob/master/snmalloc.pdf). Some recent benchmark results are listed at
[bench_suite](https://github.com/SchrodingerZhu/bench_suite).
There are three features defined in this crate:
Some old benchmark results are available in
the [`snmalloc` paper](https://github.com/microsoft/snmalloc/blob/master/snmalloc.pdf). Some recent benchmark results
are listed at
[bench_suite](https://github.com/SchrodingerZhu/bench_suite). There are three features defined in this crate:

- `debug`: Enable the `Debug` mode in `snmalloc`.
- `1mib`: Use the `1mib` chunk configuration. From `0.2.17`, this is set as a default feature
- `16mib`: Use the `16mib` chunk configuration.
- `cache-friendly`: Make the allocator more cache friendly (setting `CACHE_FRIENDLY_OFFSET` to `64` in building the library).
- `native-cpu`: Optimize `snmalloc` for the native CPU of the host machine. (this is not a default behavior since `0.2.14`)
- `cache-friendly`: Make the allocator more cache friendly (setting `CACHE_FRIENDLY_OFFSET` to `64` in building the
library).
(**deprecated since 0.2.28**)
- `native-cpu`: Optimize `snmalloc` for the native CPU of the host machine. (this is not a default behavior
since `0.2.14`)
- `qemu`: Workaround `madvise` problem of QEMU environment
- `stats`: Enable statistics
- `local_dynamic_tls`: Workaround cannot allocate memory in static tls block
- `build_cc`: Use of cc crate instead of cmake (cmake still default) as builder (more platform agnostic)
- `usecxx20`: Enable C++20 standard if available
- `win8compat`: Improve compatibility for old Windows platforms (removing usages of `VirtualAlloc2` and other new APIs)
- `win8compat`: Improve compatibility for old Windows platforms (removing usages of `VirtualAlloc2` and other new APIs)

**To get the crates compiled, you need to choose either `1mib` or `16mib` to determine the chunk configuration**

Expand All @@ -47,27 +54,36 @@ static ALLOC: snmalloc_rs::SnMalloc = snmalloc_rs::SnMalloc;

## For MinGW Users

`mingw` version is only tested on nightly branch with MSYS environment. We are using dynamic linking method.
Hence, please make sure the following libs are in your `PATH`:
`mingw` version is only tested on nightly branch with MSYS environment. We are using dynamic linking method. Hence,
please make sure the following libs are in your `PATH`:

- `winpthread`
- `atomic`
- `stdc++`
- `gcc_s`
- `gcc_s`

**Notice:** since version `0.2.12`, we no longer require you to provide additional environment variables for `mingw` target.
**Notice:** since version `0.2.12`, we no longer require you to provide additional environment variables for `mingw`
target.

## For Android Cross-Compilation

- `ANDROID_NDK` must be provided as an environment variable
- `ANDROID_PLATFORM` can be passed as an optional environment variable
- `ANDROID_ABI` used by CMake is detected automatically
- feature `android-lld` can be used to set the linker of `snmalloc` to `lld`
- feature `android-shared-std` can be used to set the STL library of `snmalloc` to `c++_shared` (it uses `c++_static` by default)
- feature `android-shared-std` can be used to set the STL library of `snmalloc` to `c++_shared` (it uses `c++_static` by
default)

## Changelog

### 0.2.28

- Deprecation of `cache-friendly`
- Use exposed `alloc_zeroed` from `snmalloc`
- **upstream** changes of remote communication, corruption detection and compilation flag detection.

### 0.2.27

- Reduction of libc dependency
- **upstream** Windows 7 and windows 8 compatibility added
- **upstream** Option to use C++20 standards if available
Expand All @@ -80,16 +96,11 @@ Hence, please make sure the following libs are in your `PATH`:
- option of cc crate as build feature, only c compiler needed, no cmake required
- Addition of dynamic local TLS option


### 0.2.25

- **upstream** Apple M1 support
- **upstream** Building adjust
- non-allocation tracking functions

### 0.2.24
- non-allocation tracking functions

- **upstream** update to use a more efficient power of 2 check
- fix msvc support w/ crt-static

for older versions, see [CHANGELOG](CHANGELOG.md)
2 changes: 1 addition & 1 deletion snmalloc-sys/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "snmalloc-sys"
version = "0.2.27"
version = "0.2.28"
authors = ["schrodingerzhu <[email protected]>"]
edition = "2018"
license = "MIT"
Expand Down
16 changes: 9 additions & 7 deletions snmalloc-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,10 @@ fn main() {
}

if cfg!(feature = "cache-friendly") {
build.define("CACHE_FRIENDLY_OFFSET", "64");
eprintln!("cache-friendly feature flag is deprecated and no longer has any effect. \
it may be removed in a future release");
// The following code no longer works
// build.define("CACHE_FRIENDLY_OFFSET", "64");
}

build.compile(target);
Expand Down Expand Up @@ -247,14 +250,13 @@ fn main() {
cfg = cfg.define("SNMALLOC_QEMU_WORKAROUND", "ON")
}

let mut dst = if cfg!(feature = "cache-friendly") {
cfg.define("CACHE_FRIENDLY_OFFSET", "64")
.build_target(target)
.build()
} else {
cfg.build_target(target).build()
if cfg!(feature = "cache-friendly") {
eprintln!("cache-friendly feature flag is deprecated and no longer has any effect. \
it may be removed in a future release");
};

let mut dst = cfg.build_target(target).build();

dst.push("./build");

println!("cargo:rustc-link-lib={}", target);
Expand Down
20 changes: 20 additions & 0 deletions snmalloc-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@

use {core::ffi::c_void, core::usize};

#[cfg_attr(
feature = "cache-friendly",
deprecated(
since = "0.2.28",
note = "Crate `snmalloc-sys` enables cache-friendly feature flag, \
which is deprecated and no longer has any effect. \
It may be removed in a future release"))]
extern "C" {
/// Allocate the memory with the given alignment and size.
/// On success, it returns a pointer pointing to the required memory address.
Expand All @@ -20,6 +27,9 @@ extern "C" {
/// The program may be forced to abort if the constrains are not full-filled.
pub fn rust_dealloc(ptr: *mut c_void, alignment: usize, size: usize) -> c_void;

/// Behaves like rust_alloc, but also ensures that the contents are set to zero before being returned.
pub fn rust_alloc_zeroed(alignment: usize, size: usize) -> *mut c_void;

/// Re-allocate the memory at the given address with the given alignment and size.
/// On success, it returns a pointer pointing to the required memory address.
/// The memory content within the `new_size` will remains the same as previous.
Expand Down Expand Up @@ -68,6 +78,15 @@ extern "C" {
mod tests {
use super::*;

#[test]
fn it_zero_allocs_correctly() {
let ptr = unsafe { rust_alloc_zeroed(8, 1024) } as *mut u8 as *mut [u8; 1024];
unsafe {
assert!((*ptr).iter().all(|x| *x == 0));
};
unsafe { rust_dealloc(ptr as *mut c_void, 8, 1024) };
}

#[test]
fn it_frees_memory_malloc() {
let ptr = unsafe { rust_alloc(8, 8) } as *mut u8;
Expand All @@ -77,6 +96,7 @@ mod tests {
};
unsafe { rust_dealloc(ptr as *mut c_void, 8, 8) };
}

#[test]
fn it_frees_memory_sn_malloc() {
let ptr = unsafe { sn_malloc(8) } as *mut u8;
Expand Down
7 changes: 7 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ unsafe impl GlobalAlloc for SnMalloc {
ffi::rust_alloc(layout.align(), layout.size()) as _
}


/// Behaves like alloc, but also ensures that the contents are set to zero before being returned.
#[inline(always)]
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
ffi::rust_alloc_zeroed(layout.align(), layout.size()) as _
}

/// De-allocate the memory at the given address with the given alignment and size.
/// The client must assure the following things:
/// - the memory is acquired using the same allocator and the pointer points to the start position.
Expand Down

0 comments on commit 4292a34

Please sign in to comment.