Skip to content

Commit 131407e

Browse files
bjorn3folkertdev
authored andcommitted
Allow using the rust global allocator for memory allocations
1 parent d583a8e commit 131407e

File tree

7 files changed

+167
-48
lines changed

7 files changed

+167
-48
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,11 @@ features = ["xxh64", "xxh32"]
4040

4141

4242
[features]
43+
default = ["rust-allocator"]
4344
export-symbols = [] # whether the zlib api symbols are publicly exported
4445
semver-prefix = ["export-symbols"] # prefix all symbols in a semver-compatible way
4546
no-prefetch = [] # do not prefetch
4647
trace = []
4748
nightly = []
49+
c-allocator = []
50+
rust-allocator = []

lib/common/allocations.rs

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
use std::ptr;
22

3-
use libc::{calloc, free, malloc, size_t};
4-
53
use crate::lib::zstd::ZSTD_customMem;
64

75
#[inline]
86
pub(crate) unsafe fn ZSTD_customMalloc(
9-
size: size_t,
7+
size: usize,
108
customMem: ZSTD_customMem,
119
) -> *mut core::ffi::c_void {
1210
if customMem.customAlloc.is_some() ^ customMem.customFree.is_some() {
@@ -17,11 +15,15 @@ pub(crate) unsafe fn ZSTD_customMalloc(
1715
return f(customMem.opaque, size);
1816
}
1917

20-
malloc(size)
18+
#[cfg(feature = "rust-allocator")]
19+
return std::alloc::alloc(core::alloc::Layout::from_size_align_unchecked(size, 16)).cast();
20+
21+
#[cfg(feature = "c-allocator")]
22+
return libc::malloc(size);
2123
}
2224
#[inline]
2325
pub(crate) unsafe fn ZSTD_customCalloc(
24-
size: size_t,
26+
size: usize,
2527
customMem: ZSTD_customMem,
2628
) -> *mut core::ffi::c_void {
2729
if customMem.customAlloc.is_some() ^ customMem.customFree.is_some() {
@@ -33,15 +35,32 @@ pub(crate) unsafe fn ZSTD_customCalloc(
3335
ptr::write_bytes(ptr, 0, size);
3436
return ptr;
3537
}
36-
calloc(1, size)
38+
39+
#[cfg(feature = "rust-allocator")]
40+
return std::alloc::alloc_zeroed(core::alloc::Layout::from_size_align_unchecked(size, 16))
41+
.cast();
42+
43+
#[cfg(feature = "c-allocator")]
44+
return libc::calloc(1, size);
3745
}
3846
#[inline]
39-
pub(crate) unsafe fn ZSTD_customFree(ptr: *mut core::ffi::c_void, customMem: ZSTD_customMem) {
47+
pub(crate) unsafe fn ZSTD_customFree(
48+
ptr: *mut core::ffi::c_void,
49+
_size: usize,
50+
customMem: ZSTD_customMem,
51+
) {
4052
if !ptr.is_null() {
4153
if let Some(f) = customMem.customFree {
4254
f(customMem.opaque, ptr);
4355
} else {
44-
free(ptr);
56+
#[cfg(feature = "rust-allocator")]
57+
return std::alloc::dealloc(
58+
ptr.cast(),
59+
core::alloc::Layout::from_size_align_unchecked(_size, 16),
60+
);
61+
62+
#[cfg(feature = "c-allocator")]
63+
return libc::free(ptr);
4564
}
4665
}
4766
}

lib/common/pool.rs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,23 @@ pub unsafe fn POOL_free(ctx: *mut POOL_ctx) {
136136
ptr::drop_in_place(ptr::addr_of_mut!((*ctx).queueMutex));
137137
ptr::drop_in_place(ptr::addr_of_mut!((*ctx).queuePushCond));
138138
ptr::drop_in_place(ptr::addr_of_mut!((*ctx).queuePopCond));
139-
ZSTD_customFree((*ctx).queue as *mut core::ffi::c_void, (*ctx).customMem);
140-
ZSTD_customFree((*ctx).threads as *mut core::ffi::c_void, (*ctx).customMem);
141-
ZSTD_customFree(ctx as *mut core::ffi::c_void, (*ctx).customMem);
139+
ZSTD_customFree(
140+
(*ctx).queue as *mut core::ffi::c_void,
141+
((*ctx).queueSize).wrapping_mul(::core::mem::size_of::<POOL_job>()),
142+
(*ctx).customMem,
143+
);
144+
ZSTD_customFree(
145+
(*ctx).threads as *mut core::ffi::c_void,
146+
(*ctx)
147+
.threadCapacity
148+
.wrapping_mul(::core::mem::size_of::<JoinHandle<()>>()),
149+
(*ctx).customMem,
150+
);
151+
ZSTD_customFree(
152+
ctx as *mut core::ffi::c_void,
153+
::core::mem::size_of::<POOL_ctx>(),
154+
(*ctx).customMem,
155+
);
142156
}
143157
pub unsafe fn POOL_joinJobs(ctx: *mut POOL_ctx) {
144158
let mut guard = (*ctx).queueMutex.lock().unwrap();
@@ -181,7 +195,13 @@ unsafe fn POOL_resize_internal(ctx: *mut POOL_ctx, numThreads: size_t) -> core::
181195
(*ctx).threads as *const core::ffi::c_void,
182196
((*ctx).threadCapacity).wrapping_mul(::core::mem::size_of::<JoinHandle<()>>()),
183197
);
184-
ZSTD_customFree((*ctx).threads as *mut core::ffi::c_void, (*ctx).customMem);
198+
ZSTD_customFree(
199+
(*ctx).threads as *mut core::ffi::c_void,
200+
(*ctx)
201+
.threadCapacity
202+
.wrapping_mul(::core::mem::size_of::<JoinHandle<()>>()),
203+
(*ctx).customMem,
204+
);
185205
(*ctx).threads = threadPool;
186206
for threadId in (*ctx).threadCapacity..numThreads {
187207
let ctx = SendPoolCtxPtr(ctx);

lib/compress/zstd_compress.rs

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,8 +1251,11 @@ unsafe fn ZSTD_cwksp_create(
12511251
#[inline]
12521252
unsafe fn ZSTD_cwksp_free(ws: *mut ZSTD_cwksp, customMem: ZSTD_customMem) {
12531253
let ptr = (*ws).workspace;
1254+
let size = (*ws)
1255+
.workspaceEnd
1256+
.byte_offset_from_unsigned((*ws).workspace);
12541257
ptr::write_bytes(ws as *mut u8, 0, ::core::mem::size_of::<ZSTD_cwksp>());
1255-
ZSTD_customFree(ptr, customMem);
1258+
ZSTD_customFree(ptr, size, customMem);
12561259
}
12571260
#[inline]
12581261
unsafe fn ZSTD_cwksp_move(dst: *mut ZSTD_cwksp, src: *mut ZSTD_cwksp) {
@@ -1705,7 +1708,11 @@ pub unsafe extern "C" fn ZSTD_initStaticCCtx(
17051708
cctx
17061709
}
17071710
unsafe fn ZSTD_clearAllDicts(cctx: *mut ZSTD_CCtx) {
1708-
ZSTD_customFree((*cctx).localDict.dictBuffer, (*cctx).customMem);
1711+
ZSTD_customFree(
1712+
(*cctx).localDict.dictBuffer,
1713+
(*cctx).localDict.dictSize,
1714+
(*cctx).customMem,
1715+
);
17091716
ZSTD_freeCDict((*cctx).localDict.cdict);
17101717
ptr::write_bytes(
17111718
&mut (*cctx).localDict as *mut ZSTD_localDict as *mut u8,
@@ -1746,7 +1753,11 @@ pub unsafe extern "C" fn ZSTD_freeCCtx(cctx: *mut ZSTD_CCtx) -> size_t {
17461753
ZSTD_cwksp_owns_buffer(&(*cctx).workspace, cctx as *const core::ffi::c_void);
17471754
ZSTD_freeCCtxContent(cctx);
17481755
if cctxInWorkspace == 0 {
1749-
ZSTD_customFree(cctx as *mut core::ffi::c_void, (*cctx).customMem);
1756+
ZSTD_customFree(
1757+
cctx as *mut core::ffi::c_void,
1758+
::core::mem::size_of::<ZSTD_CCtx>(),
1759+
(*cctx).customMem,
1760+
);
17501761
}
17511762
0
17521763
}
@@ -1965,7 +1976,11 @@ pub unsafe extern "C" fn ZSTD_freeCCtxParams(params: *mut ZSTD_CCtx_params) -> s
19651976
if params.is_null() {
19661977
return 0;
19671978
}
1968-
ZSTD_customFree(params as *mut core::ffi::c_void, (*params).customMem);
1979+
ZSTD_customFree(
1980+
params as *mut core::ffi::c_void,
1981+
::core::mem::size_of::<ZSTD_CCtx_params>(),
1982+
(*params).customMem,
1983+
);
19691984
0
19701985
}
19711986
#[cfg_attr(feature = "export-symbols", export_name = crate::prefix!(ZSTD_CCtxParams_reset))]
@@ -5564,7 +5579,7 @@ pub unsafe extern "C" fn ZSTD_generateSequences(
55645579
seqCollector.maxSequences = outSeqsSize;
55655580
(*zc).seqCollector = seqCollector;
55665581
let ret = ZSTD_compress2(zc, dst, dstCapacity, src, srcSize);
5567-
ZSTD_customFree(dst, ZSTD_customMem::default());
5582+
ZSTD_customFree(dst, dstCapacity, ZSTD_customMem::default());
55685583
let err_code_1 = ret;
55695584
if ERR_isError(err_code_1) {
55705585
return err_code_1;
@@ -8657,7 +8672,6 @@ unsafe fn ZSTD_createCDict_advanced_internal(
86578672
isStatic: ZSTD_cwksp_dynamic_alloc,
86588673
};
86598674
if workspace.is_null() {
8660-
ZSTD_customFree(workspace, customMem);
86618675
return core::ptr::null_mut();
86628676
}
86638677
ZSTD_cwksp_init(&mut ws, workspace, workspaceSize, ZSTD_cwksp_dynamic_alloc);
@@ -8765,7 +8779,6 @@ pub unsafe extern "C" fn ZSTD_createCDict_advanced2(
87658779
targetLength: 0,
87668780
strategy: 0,
87678781
};
8768-
let mut cdict = core::ptr::null_mut::<ZSTD_CDict>();
87698782
if cctxParams.enableDedicatedDictSearch != 0 {
87708783
cParams = ZSTD_dedicatedDictSearch_getCParams(cctxParams.compressionLevel, dictSize);
87718784
ZSTD_overrideCParams(&mut cParams, &cctxParams.cParams);
@@ -8789,7 +8802,7 @@ pub unsafe extern "C" fn ZSTD_createCDict_advanced2(
87898802
cctxParams.cParams = cParams;
87908803
cctxParams.useRowMatchFinder =
87918804
ZSTD_resolveRowMatchFinderMode(cctxParams.useRowMatchFinder, &cParams);
8792-
cdict = ZSTD_createCDict_advanced_internal(
8805+
let cdict = ZSTD_createCDict_advanced_internal(
87938806
dictSize,
87948807
dictLoadMethod,
87958808
cctxParams.cParams,
@@ -8880,7 +8893,11 @@ pub unsafe extern "C" fn ZSTD_freeCDict(cdict: *mut ZSTD_CDict) -> size_t {
88808893
ZSTD_cwksp_owns_buffer(&(*cdict).workspace, cdict as *const core::ffi::c_void);
88818894
ZSTD_cwksp_free(&mut (*cdict).workspace, cMem);
88828895
if cdictInWorkspace == 0 {
8883-
ZSTD_customFree(cdict as *mut core::ffi::c_void, cMem);
8896+
ZSTD_customFree(
8897+
cdict as *mut core::ffi::c_void,
8898+
(*cdict).dictContentSize,
8899+
cMem,
8900+
);
88848901
}
88858902
0
88868903
}
@@ -8898,18 +8915,14 @@ pub unsafe extern "C" fn ZSTD_initStaticCDict(
88988915
ZSTD_resolveRowMatchFinderMode(ZSTD_ParamSwitch_e::ZSTD_ps_auto, &cParams);
88998916
let matchStateSize = ZSTD_sizeof_matchState(&cParams, useRowMatchFinder, 1, 0);
89008917
let neededSize = (ZSTD_cwksp_alloc_size(::core::mem::size_of::<ZSTD_CDict>()))
8901-
.wrapping_add(
8902-
if dictLoadMethod as core::ffi::c_uint
8903-
== ZSTD_dlm_byRef as core::ffi::c_int as core::ffi::c_uint
8904-
{
8905-
0
8906-
} else {
8907-
ZSTD_cwksp_alloc_size(ZSTD_cwksp_align(
8908-
dictSize,
8909-
::core::mem::size_of::<*mut core::ffi::c_void>(),
8910-
))
8911-
},
8912-
)
8918+
.wrapping_add(if dictLoadMethod == ZSTD_dlm_byRef {
8919+
0
8920+
} else {
8921+
ZSTD_cwksp_alloc_size(ZSTD_cwksp_align(
8922+
dictSize,
8923+
::core::mem::size_of::<*mut core::ffi::c_void>(),
8924+
))
8925+
})
89138926
.wrapping_add(ZSTD_cwksp_alloc_size(HUF_WORKSPACE_SIZE as size_t))
89148927
.wrapping_add(matchStateSize);
89158928
let mut cdict = core::ptr::null_mut::<ZSTD_CDict>();

lib/compress/zstdmt_compress.rs

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -294,17 +294,23 @@ unsafe fn ZSTDMT_freeBufferPool(bufPool: *mut ZSTDMT_bufferPool) {
294294
while u < (*bufPool).totalBuffers {
295295
ZSTD_customFree(
296296
(*((*bufPool).buffers).offset(u as isize)).start,
297+
(*((*bufPool).buffers).offset(u as isize)).capacity,
297298
(*bufPool).cMem,
298299
);
299300
u = u.wrapping_add(1);
300301
}
301302
ZSTD_customFree(
302303
(*bufPool).buffers as *mut core::ffi::c_void,
304+
(*bufPool).bufferSize,
303305
(*bufPool).cMem,
304306
);
305307
}
306308
core::ptr::drop_in_place(core::ptr::addr_of_mut!((*bufPool).poolMutex));
307-
ZSTD_customFree(bufPool as *mut core::ffi::c_void, (*bufPool).cMem);
309+
ZSTD_customFree(
310+
bufPool as *mut core::ffi::c_void,
311+
size_of::<ZSTDMT_bufferPool>(),
312+
(*bufPool).cMem,
313+
);
308314
}
309315
unsafe fn ZSTDMT_createBufferPool(
310316
maxNbBuffers: core::ffi::c_uint,
@@ -389,7 +395,7 @@ unsafe fn ZSTDMT_getBuffer(bufPool: *mut ZSTDMT_bufferPool) -> Buffer {
389395
{
390396
return buf;
391397
}
392-
ZSTD_customFree(buf.start, (*bufPool).cMem);
398+
ZSTD_customFree(buf.start, buf.capacity, (*bufPool).cMem);
393399
}
394400
drop(guard);
395401
let mut buffer = buffer_s {
@@ -414,7 +420,7 @@ unsafe fn ZSTDMT_releaseBuffer(bufPool: *mut ZSTDMT_bufferPool, buf: Buffer) {
414420
return;
415421
}
416422
drop(guard);
417-
ZSTD_customFree(buf.start, (*bufPool).cMem);
423+
ZSTD_customFree(buf.start, buf.capacity, (*bufPool).cMem);
418424
}
419425
unsafe fn ZSTDMT_sizeof_seqPool(seqPool: *mut ZSTDMT_seqPool) -> size_t {
420426
ZSTDMT_sizeof_bufferPool(seqPool)
@@ -478,9 +484,17 @@ unsafe fn ZSTDMT_freeCCtxPool(pool: *mut ZSTDMT_CCtxPool) {
478484
ZSTD_freeCCtx(*((*pool).cctxs).offset(cid as isize));
479485
cid += 1;
480486
}
481-
ZSTD_customFree((*pool).cctxs as *mut core::ffi::c_void, (*pool).cMem);
487+
ZSTD_customFree(
488+
(*pool).cctxs as *mut core::ffi::c_void,
489+
((*pool).totalCCtx as usize).wrapping_mul(::core::mem::size_of::<*mut ZSTD_CCtx>()),
490+
(*pool).cMem,
491+
);
482492
}
483-
ZSTD_customFree(pool as *mut core::ffi::c_void, (*pool).cMem);
493+
ZSTD_customFree(
494+
pool as *mut core::ffi::c_void,
495+
size_of::<ZSTDMT_CCtxPool>(),
496+
(*pool).cMem,
497+
);
484498
}
485499
unsafe fn ZSTDMT_createCCtxPool(
486500
nbWorkers: core::ffi::c_int,
@@ -605,6 +619,7 @@ unsafe fn ZSTDMT_serialState_reset(
605619
{
606620
ZSTD_customFree(
607621
(*serialState).ldmState.hashTable as *mut core::ffi::c_void,
622+
hashSize,
608623
cMem,
609624
);
610625
(*serialState).ldmState.hashTable =
@@ -613,6 +628,7 @@ unsafe fn ZSTDMT_serialState_reset(
613628
if ((*serialState).ldmState.bucketOffsets).is_null() || prevBucketLog < bucketLog {
614629
ZSTD_customFree(
615630
(*serialState).ldmState.bucketOffsets as *mut core::ffi::c_void,
631+
1 << prevBucketLog,
616632
cMem,
617633
);
618634
(*serialState).ldmState.bucketOffsets = ZSTD_customMalloc(numBuckets, cMem) as *mut u8;
@@ -676,12 +692,19 @@ unsafe fn ZSTDMT_serialState_free(serialState: *mut SerialState) {
676692
core::ptr::drop_in_place(core::ptr::addr_of_mut!((*serialState).cond));
677693
core::ptr::drop_in_place(core::ptr::addr_of_mut!((*serialState).ldmWindowMutex));
678694
core::ptr::drop_in_place(core::ptr::addr_of_mut!((*serialState).ldmWindowCond));
695+
let hashLog = (*serialState).params.ldmParams.hashLog;
696+
let hashSize = ((1 as size_t) << hashLog).wrapping_mul(::core::mem::size_of::<ldmEntry_t>());
697+
let bucketLog = ((*serialState).params.ldmParams.hashLog)
698+
.wrapping_sub((*serialState).params.ldmParams.bucketSizeLog);
699+
let numBuckets = 1usize << bucketLog;
679700
ZSTD_customFree(
680701
(*serialState).ldmState.hashTable as *mut core::ffi::c_void,
702+
hashSize,
681703
cMem,
682704
);
683705
ZSTD_customFree(
684706
(*serialState).ldmState.bucketOffsets as *mut core::ffi::c_void,
707+
numBuckets,
685708
cMem,
686709
);
687710
}
@@ -1066,7 +1089,11 @@ unsafe fn ZSTDMT_freeJobsTable(
10661089
));
10671090
jobNb = jobNb.wrapping_add(1);
10681091
}
1069-
ZSTD_customFree(jobTable as *mut core::ffi::c_void, cMem);
1092+
ZSTD_customFree(
1093+
jobTable as *mut core::ffi::c_void,
1094+
(nbJobs as usize).wrapping_mul(::core::mem::size_of::<ZSTDMT_jobDescription>()),
1095+
cMem,
1096+
);
10701097
}
10711098
unsafe fn ZSTDMT_createJobsTable(
10721099
nbJobsPtr: *mut u32,
@@ -1268,10 +1295,15 @@ pub unsafe fn ZSTDMT_freeCCtx(mtctx: *mut ZSTDMT_CCtx) -> size_t {
12681295
if !((*mtctx).roundBuff.buffer).is_null() {
12691296
ZSTD_customFree(
12701297
(*mtctx).roundBuff.buffer as *mut core::ffi::c_void,
1298+
(*mtctx).roundBuff.capacity,
12711299
(*mtctx).cMem,
12721300
);
12731301
}
1274-
ZSTD_customFree(mtctx as *mut core::ffi::c_void, (*mtctx).cMem);
1302+
ZSTD_customFree(
1303+
mtctx as *mut core::ffi::c_void,
1304+
::core::mem::size_of::<ZSTDMT_CCtx>(),
1305+
(*mtctx).cMem,
1306+
);
12751307
0
12761308
}
12771309
pub unsafe fn ZSTDMT_sizeof_CCtx(mtctx: *mut ZSTDMT_CCtx) -> size_t {
@@ -1560,6 +1592,7 @@ pub unsafe fn ZSTDMT_initCStream_internal(
15601592
if !((*mtctx).roundBuff.buffer).is_null() {
15611593
ZSTD_customFree(
15621594
(*mtctx).roundBuff.buffer as *mut core::ffi::c_void,
1595+
(*mtctx).roundBuff.capacity,
15631596
(*mtctx).cMem,
15641597
);
15651598
}

lib/decompress/zstd_ddict.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,12 @@ pub unsafe extern "C" fn ZSTD_freeDDict(ddict: *mut ZSTD_DDict) -> size_t {
297297
return 0;
298298
}
299299
let cMem = (*ddict).cMem;
300-
ZSTD_customFree((*ddict).dictBuffer, cMem);
301-
ZSTD_customFree(ddict as *mut core::ffi::c_void, cMem);
300+
ZSTD_customFree((*ddict).dictBuffer, (*ddict).dictSize, cMem);
301+
ZSTD_customFree(
302+
ddict as *mut core::ffi::c_void,
303+
size_of::<ZSTD_DDict>(),
304+
cMem,
305+
);
302306
0
303307
}
304308

0 commit comments

Comments
 (0)