Skip to content

Commit d8a325a

Browse files
committed
Simplify Rust allocator constructors and test support
1 parent 594e743 commit d8a325a

4 files changed

Lines changed: 37 additions & 131 deletions

File tree

‎zstd-safe/src/lib.rs‎

Lines changed: 22 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -308,18 +308,18 @@ impl<'a> CCtx<'a> {
308308
/// runtime's `malloc`.
309309
pub fn try_create() -> Option<Self> {
310310
#[cfg(feature = "with-rust-allocator")]
311-
{
312-
Self::try_create_with_global_allocator()
313-
}
311+
// Safety: the callbacks satisfy zstd's allocation contract and remain
312+
// valid for the lifetime of the context.
313+
let context = unsafe {
314+
zstd_sys::ZSTD_createCCtx_advanced(
315+
crate::rust_allocator::RUST_GLOBAL_ALLOCATOR,
316+
)
317+
};
314318
#[cfg(not(feature = "with-rust-allocator"))]
315-
{
316-
// Safety: Just FFI
317-
Some(CCtx(
318-
NonNull::new(unsafe { zstd_sys::ZSTD_createCCtx() })?,
319-
PhantomData,
320-
Poison::default(),
321-
))
322-
}
319+
// Safety: zstd returns a newly owned context or null.
320+
let context = unsafe { zstd_sys::ZSTD_createCCtx() };
321+
322+
Some(Self(NonNull::new(context)?, PhantomData, Poison::default()))
323323
}
324324

325325
/// Wrap `ZSTD_createCCtx`
@@ -332,26 +332,6 @@ impl<'a> CCtx<'a> {
332332
.expect("zstd returned null pointer when creating new context")
333333
}
334334

335-
/// Tries to create a new context whose internal allocations go through
336-
/// Rust's global allocator instead of the C runtime's `malloc`.
337-
///
338-
/// Returns `None` if zstd returns a NULL pointer - may happen if
339-
/// allocation fails.
340-
#[cfg(feature = "with-rust-allocator")]
341-
fn try_create_with_global_allocator() -> Option<Self> {
342-
// Safety: Just FFI. The customMem callbacks uphold the malloc/free
343-
// contract (see `rust_allocator`).
344-
Some(CCtx(
345-
NonNull::new(unsafe {
346-
zstd_sys::ZSTD_createCCtx_advanced(
347-
crate::rust_allocator::RUST_GLOBAL_ALLOCATOR,
348-
)
349-
})?,
350-
PhantomData,
351-
Poison::default(),
352-
))
353-
}
354-
355335
/// Wraps the `ZSTD_compressCCtx()` function
356336
pub fn compress<C: WriteBuf + ?Sized>(
357337
&mut self,
@@ -1045,18 +1025,18 @@ impl<'a> DCtx<'a> {
10451025
/// runtime's `malloc`.
10461026
pub fn try_create() -> Option<Self> {
10471027
#[cfg(feature = "with-rust-allocator")]
1048-
{
1049-
Self::try_create_with_global_allocator()
1050-
}
1028+
// Safety: the callbacks satisfy zstd's allocation contract and remain
1029+
// valid for the lifetime of the context.
1030+
let context = unsafe {
1031+
zstd_sys::ZSTD_createDCtx_advanced(
1032+
crate::rust_allocator::RUST_GLOBAL_ALLOCATOR,
1033+
)
1034+
};
10511035
#[cfg(not(feature = "with-rust-allocator"))]
1052-
{
1053-
// Safety: Just FFI
1054-
Some(DCtx(
1055-
NonNull::new(unsafe { zstd_sys::ZSTD_createDCtx() })?,
1056-
PhantomData,
1057-
Poison::default(),
1058-
))
1059-
}
1036+
// Safety: zstd returns a newly owned context or null.
1037+
let context = unsafe { zstd_sys::ZSTD_createDCtx() };
1038+
1039+
Some(Self(NonNull::new(context)?, PhantomData, Poison::default()))
10601040
}
10611041

10621042
/// Creates a new decoding context.
@@ -1069,25 +1049,6 @@ impl<'a> DCtx<'a> {
10691049
.expect("zstd returned null pointer when creating new context")
10701050
}
10711051

1072-
/// Tries to create a new decompression context whose internal allocations
1073-
/// go through Rust's global allocator instead of the C runtime's `malloc`.
1074-
///
1075-
/// Returns `None` if the operation failed
1076-
#[cfg(feature = "with-rust-allocator")]
1077-
fn try_create_with_global_allocator() -> Option<Self> {
1078-
// Safety: Just FFI. The customMem callbacks uphold the malloc/free
1079-
// contract (see `rust_allocator`).
1080-
Some(DCtx(
1081-
NonNull::new(unsafe {
1082-
zstd_sys::ZSTD_createDCtx_advanced(
1083-
crate::rust_allocator::RUST_GLOBAL_ALLOCATOR,
1084-
)
1085-
})?,
1086-
PhantomData,
1087-
Poison::default(),
1088-
))
1089-
}
1090-
10911052
/// Fully decompress the given frame.
10921053
///
10931054
/// This decompress an entire frame in-memory. If you can have enough memory to store both the

‎zstd-safe/src/rust_allocator.rs‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,10 @@ pub(crate) const RUST_GLOBAL_ALLOCATOR: zstd_sys::ZSTD_customMem =
8989
///
9090
/// # Safety
9191
///
92-
/// Just FFI.
93-
#[cfg(feature = "with-rust-allocator")]
92+
/// With `ZSTD_dlm_byRef`, the caller must keep `dict_buffer` alive and
93+
/// unmodified until the dictionary is freed. A non-null result must not be
94+
/// freed while referenced by a context; free it at most once with
95+
/// `ZSTD_freeCDict`.
9496
pub(crate) unsafe fn create_cdict(
9597
dict_buffer: &[u8],
9698
compression_level: crate::CompressionLevel,
@@ -117,8 +119,10 @@ pub(crate) unsafe fn create_cdict(
117119
///
118120
/// # Safety
119121
///
120-
/// Just FFI.
121-
#[cfg(feature = "with-rust-allocator")]
122+
/// With `ZSTD_dlm_byRef`, the caller must keep `dict_buffer` alive and
123+
/// unmodified until the dictionary is freed. A non-null result must not be
124+
/// freed while referenced by a context; free it at most once with
125+
/// `ZSTD_freeDDict`.
122126
pub(crate) unsafe fn create_ddict(
123127
dict_buffer: &[u8],
124128
load_method: zstd_sys::ZSTD_dictLoadMethod_e,

‎zstd-safe/tests/support/rust_allocator.rs‎

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -6,57 +6,6 @@ use std::sync::atomic::{AtomicUsize, Ordering::Relaxed};
66
pub static LIVE_BYTES: AtomicUsize = AtomicUsize::new(0);
77
/// Bytes ever requested from the global allocator (monotonic).
88
pub static TOTAL_BYTES: AtomicUsize = AtomicUsize::new(0);
9-
/// Number of `dealloc` calls for pointers this allocator never handed out,
10-
/// i.e. memory that was allocated by C's `malloc` but freed through Rust.
11-
pub static FOREIGN_FREES: AtomicUsize = AtomicUsize::new(0);
12-
13-
/// Open-addressing table of live pointers, so `dealloc` can tell whether it
14-
/// is being handed back memory that it actually allocated. Must not allocate
15-
/// itself, hence the fixed-size static.
16-
const SLOTS: usize = 1 << 16;
17-
const EMPTY: usize = 0;
18-
const TOMBSTONE: usize = 1;
19-
// Rust 1.64 can repeat a named const containing an atomic, but cannot use
20-
// the newer inline-const array syntax. Each slot gets a distinct atomic.
21-
#[allow(clippy::declare_interior_mutable_const)]
22-
const EMPTY_SLOT: AtomicUsize = AtomicUsize::new(EMPTY);
23-
static LIVE_PTRS: [AtomicUsize; SLOTS] = [EMPTY_SLOT; SLOTS];
24-
25-
fn slot_for(ptr: usize) -> usize {
26-
(ptr >> 4).wrapping_mul(0x9E37_79B9) % SLOTS
27-
}
28-
29-
fn track(ptr: usize) {
30-
let mut i = slot_for(ptr);
31-
for _ in 0..SLOTS {
32-
let slot = &LIVE_PTRS[i];
33-
let cur = slot.load(Relaxed);
34-
if (cur == EMPTY || cur == TOMBSTONE)
35-
&& slot.compare_exchange(cur, ptr, Relaxed, Relaxed).is_ok()
36-
{
37-
return;
38-
}
39-
i = (i + 1) % SLOTS;
40-
}
41-
std::process::abort();
42-
}
43-
44-
/// Returns whether `ptr` was live (and forgets it).
45-
fn untrack(ptr: usize) -> bool {
46-
let mut i = slot_for(ptr);
47-
for _ in 0..SLOTS {
48-
let slot = &LIVE_PTRS[i];
49-
match slot.load(Relaxed) {
50-
EMPTY => return false,
51-
cur if cur == ptr => {
52-
slot.store(TOMBSTONE, Relaxed);
53-
return true;
54-
}
55-
_ => i = (i + 1) % SLOTS,
56-
}
57-
}
58-
false
59-
}
609

6110
struct CountingAllocator;
6211

@@ -66,17 +15,11 @@ unsafe impl GlobalAlloc for CountingAllocator {
6615
if !ptr.is_null() {
6716
LIVE_BYTES.fetch_add(layout.size(), Relaxed);
6817
TOTAL_BYTES.fetch_add(layout.size(), Relaxed);
69-
track(ptr as usize);
7018
}
7119
ptr
7220
}
7321

7422
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
75-
if !untrack(ptr as usize) {
76-
// Not ours: record it and leak rather than corrupt the heap.
77-
FOREIGN_FREES.fetch_add(1, Relaxed);
78-
return;
79-
}
8023
LIVE_BYTES.fetch_sub(layout.size(), Relaxed);
8124
System.dealloc(ptr, layout)
8225
}

‎zstd-safe/tests/with_rust_allocator_clones.rs‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,23 @@ mod support {
55
}
66

77
use std::sync::atomic::Ordering::Relaxed;
8-
use support::rust_allocator::{FOREIGN_FREES, LIVE_BYTES};
8+
use support::rust_allocator::{LIVE_BYTES, TOTAL_BYTES};
99
use zstd_safe::{CCtx, DCtx};
1010

1111
#[test]
1212
fn clones_are_freed_through_the_source_allocator() {
1313
let live_before = LIVE_BYTES.load(Relaxed);
14-
let foreign_before = FOREIGN_FREES.load(Relaxed);
1514
{
1615
let dctx = DCtx::create();
16+
let total_before = TOTAL_BYTES.load(Relaxed);
1717
let clone = dctx.try_clone().expect("failed to clone DCtx");
18-
drop(clone);
18+
assert!(
19+
TOTAL_BYTES.load(Relaxed) > total_before,
20+
"DCtx clone did not allocate through Rust's allocator"
21+
);
1922
drop(dctx);
23+
drop(clone);
2024
}
21-
assert_eq!(
22-
FOREIGN_FREES.load(Relaxed),
23-
foreign_before,
24-
"a DCtx clone was freed through an allocator it was not allocated with"
25-
);
2625
assert_eq!(
2726
LIVE_BYTES.load(Relaxed),
2827
live_before,
@@ -42,5 +41,4 @@ fn clones_are_freed_through_the_source_allocator() {
4241
);
4342
}
4443
assert_eq!(LIVE_BYTES.load(Relaxed), live_before);
45-
assert_eq!(FOREIGN_FREES.load(Relaxed), foreign_before);
4644
}

0 commit comments

Comments
 (0)