Skip to content

Commit dd659cb

Browse files
committed
Add memshred feature to stress2
1 parent b894608 commit dd659cb

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

benchmarks/stress2/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ measure_allocs = ["sled/measure_allocs"]
2424
jemalloc = ["jemallocator"]
2525
logging = ["env_logger", "log", "color-backtrace"]
2626
dh = ["dhat"]
27+
memshred = []
2728

2829
[dependencies]
2930
rand = "0.7.3"

benchmarks/stress2/src/main.rs

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,47 @@ use std::{
66
thread,
77
};
88

9-
#[cfg(feature = "jemalloc")]
10-
use jemallocator::Jemalloc;
11-
129
#[cfg(feature = "dh")]
1310
use dhat::{Dhat, DhatAlloc};
1411

1512
use num_format::{Locale, ToFormattedString};
1613
use rand::{thread_rng, Rng};
1714

18-
#[global_allocator]
1915
#[cfg(feature = "jemalloc")]
20-
static ALLOCATOR: Jemalloc = Jemalloc;
16+
mod alloc {
17+
use std::alloc::Layout;
18+
use jemallocator::Jemalloc;
19+
20+
#[global_allocator]
21+
static ALLOCATOR: Jemalloc = Jemalloc;
22+
}
23+
24+
#[cfg(feature = "memshred")]
25+
mod alloc {
26+
use std::alloc::{System, Layout};
27+
28+
#[global_allocator]
29+
static ALLOCATOR: Alloc = Alloc;
30+
31+
#[derive(Default, Debug, Clone, Copy)]
32+
struct Alloc;
33+
34+
unsafe impl std::alloc::GlobalAlloc for Alloc {
35+
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
36+
let ret = System.alloc(layout);
37+
assert_ne!(ret, std::ptr::null_mut());
38+
std::ptr::write_bytes(ret, 0xa1, layout.size());
39+
ret
40+
}
41+
42+
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
43+
std::ptr::write_bytes(ptr, 0xde, layout.size());
44+
System.dealloc(ptr, layout)
45+
46+
}
47+
}
48+
}
49+
2150

2251
#[global_allocator]
2352
#[cfg(feature = "dh")]

0 commit comments

Comments
 (0)