Skip to content

Commit bb79157

Browse files
bjorn3folkertdev
authored andcommitted
Move most allocator checks into ZSTD_customMalloc and ZSTD_customCalloc
1 parent d6d900b commit bb79157

File tree

5 files changed

+19
-41
lines changed

5 files changed

+19
-41
lines changed

lib/common/allocations.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ pub(crate) unsafe fn ZSTD_customMalloc(
99
size: size_t,
1010
customMem: ZSTD_customMem,
1111
) -> *mut core::ffi::c_void {
12+
if customMem.customAlloc.is_some() ^ customMem.customFree.is_some() {
13+
return core::ptr::null_mut();
14+
}
15+
1216
if let Some(f) = customMem.customAlloc {
1317
return f(customMem.opaque, size);
1418
}
@@ -20,6 +24,10 @@ pub(crate) unsafe fn ZSTD_customCalloc(
2024
size: size_t,
2125
customMem: ZSTD_customMem,
2226
) -> *mut core::ffi::c_void {
27+
if customMem.customAlloc.is_some() ^ customMem.customFree.is_some() {
28+
return core::ptr::null_mut();
29+
}
30+
2331
if let Some(f) = customMem.customAlloc {
2432
let ptr = f(customMem.opaque, size);
2533
ptr::write_bytes(ptr, 0, size);

lib/compress/zstd_compress.rs

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1604,9 +1604,6 @@ unsafe fn ZSTD_initCCtx(cctx: *mut ZSTD_CCtx, memManager: ZSTD_customMem) {
16041604
}
16051605
#[cfg_attr(feature = "export-symbols", export_name = crate::prefix!(ZSTD_createCCtx_advanced))]
16061606
pub unsafe extern "C" fn ZSTD_createCCtx_advanced(customMem: ZSTD_customMem) -> *mut ZSTD_CCtx {
1607-
if customMem.customAlloc.is_none() ^ customMem.customFree.is_none() {
1608-
return core::ptr::null_mut();
1609-
}
16101607
let cctx = ZSTD_customMalloc(::core::mem::size_of::<ZSTD_CCtx>(), customMem) as *mut ZSTD_CCtx;
16111608
if cctx.is_null() {
16121609
return core::ptr::null_mut();
@@ -1950,11 +1947,7 @@ unsafe fn ZSTD_makeCCtxParamsFromCParams(cParams: ZSTD_compressionParameters) ->
19501947
cctxParams
19511948
}
19521949
unsafe fn ZSTD_createCCtxParams_advanced(customMem: ZSTD_customMem) -> *mut ZSTD_CCtx_params {
1953-
let mut params = core::ptr::null_mut::<ZSTD_CCtx_params>();
1954-
if customMem.customAlloc.is_none() ^ customMem.customFree.is_none() {
1955-
return core::ptr::null_mut();
1956-
}
1957-
params = ZSTD_customCalloc(::core::mem::size_of::<ZSTD_CCtx_params>(), customMem)
1950+
let params = ZSTD_customCalloc(::core::mem::size_of::<ZSTD_CCtx_params>(), customMem)
19581951
as *mut ZSTD_CCtx_params;
19591952
if params.is_null() {
19601953
return core::ptr::null_mut();
@@ -8633,9 +8626,6 @@ unsafe fn ZSTD_createCDict_advanced_internal(
86338626
enableDedicatedDictSearch: core::ffi::c_int,
86348627
customMem: ZSTD_customMem,
86358628
) -> *mut ZSTD_CDict {
8636-
if customMem.customAlloc.is_none() ^ customMem.customFree.is_none() {
8637-
return core::ptr::null_mut();
8638-
}
86398629
let workspaceSize = (ZSTD_cwksp_alloc_size(::core::mem::size_of::<ZSTD_CDict>()))
86408630
.wrapping_add(ZSTD_cwksp_alloc_size(HUF_WORKSPACE_SIZE as size_t))
86418631
.wrapping_add(ZSTD_sizeof_matchState(
@@ -8644,18 +8634,14 @@ unsafe fn ZSTD_createCDict_advanced_internal(
86448634
enableDedicatedDictSearch,
86458635
0,
86468636
))
8647-
.wrapping_add(
8648-
if dictLoadMethod as core::ffi::c_uint
8649-
== ZSTD_dlm_byRef as core::ffi::c_int as core::ffi::c_uint
8650-
{
8651-
0
8652-
} else {
8653-
ZSTD_cwksp_alloc_size(ZSTD_cwksp_align(
8654-
dictSize,
8655-
::core::mem::size_of::<*mut core::ffi::c_void>(),
8656-
))
8657-
},
8658-
);
8637+
.wrapping_add(if dictLoadMethod == ZSTD_dlm_byRef {
8638+
0
8639+
} else {
8640+
ZSTD_cwksp_alloc_size(ZSTD_cwksp_align(
8641+
dictSize,
8642+
::core::mem::size_of::<*mut core::ffi::c_void>(),
8643+
))
8644+
});
86598645
let workspace = ZSTD_customMalloc(workspaceSize, customMem);
86608646
let mut ws = ZSTD_cwksp {
86618647
workspace: core::ptr::null_mut::<core::ffi::c_void>(),
@@ -8670,13 +8656,12 @@ unsafe fn ZSTD_createCDict_advanced_internal(
86708656
phase: ZSTD_cwksp_alloc_objects,
86718657
isStatic: ZSTD_cwksp_dynamic_alloc,
86728658
};
8673-
let mut cdict = core::ptr::null_mut::<ZSTD_CDict>();
86748659
if workspace.is_null() {
86758660
ZSTD_customFree(workspace, customMem);
86768661
return core::ptr::null_mut();
86778662
}
86788663
ZSTD_cwksp_init(&mut ws, workspace, workspaceSize, ZSTD_cwksp_dynamic_alloc);
8679-
cdict =
8664+
let cdict =
86808665
ZSTD_cwksp_reserve_object(&mut ws, ::core::mem::size_of::<ZSTD_CDict>()) as *mut ZSTD_CDict;
86818666
ZSTD_cwksp_move(&mut (*cdict).workspace, &mut ws);
86828667
(*cdict).customMem = customMem;
@@ -8781,9 +8766,6 @@ pub unsafe extern "C" fn ZSTD_createCDict_advanced2(
87818766
strategy: 0,
87828767
};
87838768
let mut cdict = core::ptr::null_mut::<ZSTD_CDict>();
8784-
if customMem.customAlloc.is_none() ^ customMem.customFree.is_none() {
8785-
return core::ptr::null_mut();
8786-
}
87878769
if cctxParams.enableDedicatedDictSearch != 0 {
87888770
cParams = ZSTD_dedicatedDictSearch_getCParams(cctxParams.compressionLevel, dictSize);
87898771
ZSTD_overrideCParams(&mut cParams, &cctxParams.cParams);

lib/compress/zstdmt_compress.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,7 +1130,6 @@ unsafe fn ZSTDMT_createCCtx_advanced_internal(
11301130
cMem: ZSTD_customMem,
11311131
pool: *mut ZSTD_threadPool,
11321132
) -> *mut ZSTDMT_CCtx {
1133-
let mut mtctx = core::ptr::null_mut::<ZSTDMT_CCtx>();
11341133
let mut nbJobs = nbWorkers.wrapping_add(2);
11351134
let mut initError: core::ffi::c_int = 0;
11361135
if nbWorkers < 1 {
@@ -1151,10 +1150,7 @@ unsafe fn ZSTDMT_createCCtx_advanced_internal(
11511150
256
11521151
}) as core::ffi::c_uint
11531152
};
1154-
if cMem.customAlloc.is_some() ^ cMem.customFree.is_some() {
1155-
return core::ptr::null_mut();
1156-
}
1157-
mtctx = ZSTD_customCalloc(::core::mem::size_of::<ZSTDMT_CCtx>(), cMem) as *mut ZSTDMT_CCtx;
1153+
let mtctx = ZSTD_customCalloc(::core::mem::size_of::<ZSTDMT_CCtx>(), cMem) as *mut ZSTDMT_CCtx;
11581154
if mtctx.is_null() {
11591155
return core::ptr::null_mut();
11601156
}

lib/decompress/zstd_ddict.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,6 @@ pub unsafe extern "C" fn ZSTD_createDDict_advanced(
206206
dictContentType: ZSTD_dictContentType_e,
207207
customMem: ZSTD_customMem,
208208
) -> *mut ZSTD_DDict {
209-
if customMem.customAlloc.is_none() ^ customMem.customFree.is_none() {
210-
return core::ptr::null_mut();
211-
}
212-
213209
let ddict = ZSTD_customMalloc(size_of::<ZSTD_DDict>(), customMem) as *mut ZSTD_DDict;
214210

215211
if ddict.is_null() {

lib/decompress/zstd_decompress.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -711,10 +711,6 @@ pub unsafe extern "C" fn ZSTD_initStaticDCtx(
711711
}
712712

713713
unsafe fn ZSTD_createDCtx_internal(customMem: ZSTD_customMem) -> *mut ZSTD_DCtx {
714-
if customMem.customAlloc.is_none() ^ customMem.customFree.is_none() {
715-
return core::ptr::null_mut();
716-
}
717-
718714
let alloc = ZSTD_customMalloc(::core::mem::size_of::<ZSTD_DCtx>(), customMem);
719715
let Some(dctx) = alloc.cast::<MaybeUninit<ZSTD_DCtx>>().as_mut() else {
720716
return core::ptr::null_mut();

0 commit comments

Comments
 (0)