Skip to content

Commit 4292a34

Browse files
Snmalloc v1 maintain oct 10 2021 (#161)
* 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]>
1 parent 8457da5 commit 4292a34

File tree

8 files changed

+77
-32
lines changed

8 files changed

+77
-32
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
## Changelog
22

3+
### 0.2.28
4+
- Deprecation of `cache-friendly`
5+
- Use exposed `alloc_zeroed` from `snmalloc`
6+
- **upstream** changes of remote communication, corruption detection and compilation flag detection.
7+
38
### 0.2.27
49

510
- Reduction of libc dependency

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "snmalloc-rs"
3-
version = "0.2.27"
3+
version = "0.2.28"
44
authors = ["schrodingerzhu <[email protected]>"]
55
edition = "2018"
66
license = "MIT"
@@ -16,7 +16,7 @@ readme = "README.md"
1616
members = ["snmalloc-sys" ]
1717

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

2121
[features]
2222
default = ["snmalloc-sys/build_cmake","1mib"]

README.md

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,39 @@
11
# snmalloc-rs
2-
**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.**
2+
3+
**Notice: MinGW Build is broken and may not be fixed in a near future.
4+
See [this PR](https://github.com/microsoft/snmalloc/pull/217) in the upstream.**
35

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

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

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

1113
- Memory that is freed by the same thread that allocated it does not require any synchronising operations.
12-
- 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.
14+
- Freeing memory in a different thread to initially allocated it, does not take any locks and instead uses a novel
15+
message passing scheme to return the memory to the original allocator, where it is recycled.
1316
- The allocator uses large ranges of pages to reduce the amount of meta-data required.
1417

15-
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
16-
[bench_suite](https://github.com/SchrodingerZhu/bench_suite).
17-
There are three features defined in this crate:
18+
Some old benchmark results are available in
19+
the [`snmalloc` paper](https://github.com/microsoft/snmalloc/blob/master/snmalloc.pdf). Some recent benchmark results
20+
are listed at
21+
[bench_suite](https://github.com/SchrodingerZhu/bench_suite). There are three features defined in this crate:
1822

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

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

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

4855
## For MinGW Users
4956

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

5360
- `winpthread`
5461
- `atomic`
5562
- `stdc++`
56-
- `gcc_s`
63+
- `gcc_s`
5764

58-
**Notice:** since version `0.2.12`, we no longer require you to provide additional environment variables for `mingw` target.
65+
**Notice:** since version `0.2.12`, we no longer require you to provide additional environment variables for `mingw`
66+
target.
5967

6068
## For Android Cross-Compilation
6169

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

6877
## Changelog
6978

79+
### 0.2.28
80+
81+
- Deprecation of `cache-friendly`
82+
- Use exposed `alloc_zeroed` from `snmalloc`
83+
- **upstream** changes of remote communication, corruption detection and compilation flag detection.
84+
7085
### 0.2.27
86+
7187
- Reduction of libc dependency
7288
- **upstream** Windows 7 and windows 8 compatibility added
7389
- **upstream** Option to use C++20 standards if available
@@ -80,16 +96,11 @@ Hence, please make sure the following libs are in your `PATH`:
8096
- option of cc crate as build feature, only c compiler needed, no cmake required
8197
- Addition of dynamic local TLS option
8298

83-
8499
### 0.2.25
85100

86101
- **upstream** Apple M1 support
87102
- **upstream** Building adjust
88-
- non-allocation tracking functions
89-
90-
### 0.2.24
103+
- non-allocation tracking functions
91104

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

95106
for older versions, see [CHANGELOG](CHANGELOG.md)

snmalloc-sys/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "snmalloc-sys"
3-
version = "0.2.27"
3+
version = "0.2.28"
44
authors = ["schrodingerzhu <[email protected]>"]
55
edition = "2018"
66
license = "MIT"

snmalloc-sys/build.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,10 @@ fn main() {
117117
}
118118

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

123126
build.compile(target);
@@ -247,14 +250,13 @@ fn main() {
247250
cfg = cfg.define("SNMALLOC_QEMU_WORKAROUND", "ON")
248251
}
249252

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

258+
let mut dst = cfg.build_target(target).build();
259+
258260
dst.push("./build");
259261

260262
println!("cargo:rustc-link-lib={}", target);

snmalloc-sys/src/lib.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33

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

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

30+
/// Behaves like rust_alloc, but also ensures that the contents are set to zero before being returned.
31+
pub fn rust_alloc_zeroed(alignment: usize, size: usize) -> *mut c_void;
32+
2333
/// Re-allocate the memory at the given address with the given alignment and size.
2434
/// On success, it returns a pointer pointing to the required memory address.
2535
/// The memory content within the `new_size` will remains the same as previous.
@@ -68,6 +78,15 @@ extern "C" {
6878
mod tests {
6979
use super::*;
7080

81+
#[test]
82+
fn it_zero_allocs_correctly() {
83+
let ptr = unsafe { rust_alloc_zeroed(8, 1024) } as *mut u8 as *mut [u8; 1024];
84+
unsafe {
85+
assert!((*ptr).iter().all(|x| *x == 0));
86+
};
87+
unsafe { rust_dealloc(ptr as *mut c_void, 8, 1024) };
88+
}
89+
7190
#[test]
7291
fn it_frees_memory_malloc() {
7392
let ptr = unsafe { rust_alloc(8, 8) } as *mut u8;
@@ -77,6 +96,7 @@ mod tests {
7796
};
7897
unsafe { rust_dealloc(ptr as *mut c_void, 8, 8) };
7998
}
99+
80100
#[test]
81101
fn it_frees_memory_sn_malloc() {
82102
let ptr = unsafe { sn_malloc(8) } as *mut u8;

src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ unsafe impl GlobalAlloc for SnMalloc {
4444
ffi::rust_alloc(layout.align(), layout.size()) as _
4545
}
4646

47+
48+
/// Behaves like alloc, but also ensures that the contents are set to zero before being returned.
49+
#[inline(always)]
50+
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
51+
ffi::rust_alloc_zeroed(layout.align(), layout.size()) as _
52+
}
53+
4754
/// De-allocate the memory at the given address with the given alignment and size.
4855
/// The client must assure the following things:
4956
/// - the memory is acquired using the same allocator and the pointer points to the start position.

0 commit comments

Comments
 (0)