Skip to content

Commit edd8e07

Browse files
committed
zdict.rs: remove final malloc
1 parent 4ba127d commit edd8e07

File tree

1 file changed

+14
-19
lines changed

1 file changed

+14
-19
lines changed

lib/dictBuilder/zdict.rs

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::mem::MaybeUninit;
22
use std::time::{Duration, Instant};
33

4-
use libc::{free, malloc, size_t};
4+
use libc::size_t;
55

66
use crate::lib::common::bits::ZSTD_highbit32;
77
use crate::lib::common::error_private::{ERR_getErrorName, ERR_isError, Error};
@@ -28,15 +28,15 @@ use crate::lib::zdict::experimental::{
2828
use crate::lib::zdict::ZDICT_params_t;
2929
use crate::lib::zstd::*;
3030

31-
#[derive(Copy, Clone)]
31+
#[derive(Clone)]
3232
#[repr(C)]
3333
struct EStats_ress_t {
3434
/// dictionary
3535
dict: *mut ZSTD_CDict,
3636
/// working context
3737
zc: *mut ZSTD_CCtx,
3838
/// must be ZSTD_BLOCKSIZE_MAX allocated
39-
workPlace: *mut core::ffi::c_void,
39+
workPlace: Box<[MaybeUninit<u8>]>,
4040
}
4141

4242
#[derive(Copy, Clone, Default)]
@@ -575,7 +575,7 @@ fn ZDICT_trainBuffer_legacy(
575575

576576
// Note: filePos tracks borders between samples.
577577
// It's not used at this stage, but planned to become useful in a later update
578-
let mut filePos = vec![0u32; nbFiles as usize];
578+
let mut filePos = vec![0u32; nbFiles];
579579
// filePos[0] is intentionally left 0
580580
for pos in 1..nbFiles as size_t {
581581
filePos[pos] =
@@ -639,22 +639,19 @@ fn fill_noise(buffer: &mut [u8]) {
639639

640640
const MAXREPOFFSET: u32 = 1024;
641641
unsafe fn ZDICT_countEStats(
642-
esr: EStats_ress_t,
642+
esr: &mut EStats_ress_t,
643643
params: &ZSTD_parameters,
644644
countLit: &mut [u32; 256],
645645
offsetcodeCount: &mut [u32; 31],
646646
matchlengthCount: &mut [u32; 53],
647647
litlengthCount: &mut [u32; 36],
648648
repOffsets: &mut [u32; 1024],
649-
src: *const core::ffi::c_void,
650-
mut srcSize: size_t,
649+
src: &[u8],
651650
notificationLevel: u32,
652651
) {
653652
let blockSizeMax = Ord::min(1 << 17, 1 << params.cParams.windowLog);
654653
let mut cSize: size_t = 0;
655-
if srcSize > blockSizeMax {
656-
srcSize = blockSizeMax;
657-
}
654+
let srcSize = Ord::min(src.len(), blockSizeMax);
658655
let errorCode = ZSTD_compressBegin_usingCDict_deprecated(esr.zc, esr.dict);
659656
if ERR_isError(errorCode) {
660657
if notificationLevel >= 1 {
@@ -664,9 +661,9 @@ unsafe fn ZDICT_countEStats(
664661
}
665662
cSize = ZSTD_compressBlock_deprecated(
666663
esr.zc,
667-
esr.workPlace,
664+
esr.workPlace.as_mut_ptr().cast(),
668665
ZSTD_BLOCKSIZE_MAX as size_t,
669-
src,
666+
src.as_ptr().cast::<core::ffi::c_void>(),
670667
srcSize,
671668
);
672669
if ERR_isError(cSize) {
@@ -760,7 +757,7 @@ unsafe fn ZDICT_analyzeEntropy(
760757
let mut esr = EStats_ress_t {
761758
dict: core::ptr::null_mut(),
762759
zc: core::ptr::null_mut(),
763-
workPlace: core::ptr::null_mut(),
760+
workPlace: Box::default(),
764761
};
765762

766763
let eSize = analyze_entropy_internal(
@@ -777,7 +774,6 @@ unsafe fn ZDICT_analyzeEntropy(
777774

778775
ZSTD_freeCDict(esr.dict);
779776
ZSTD_freeCCtx(esr.zc);
780-
free(esr.workPlace);
781777

782778
eSize
783779
}
@@ -839,8 +835,8 @@ unsafe fn analyze_entropy_internal(
839835
ZSTD_customMem::default(),
840836
);
841837
esr.zc = ZSTD_createCCtx();
842-
esr.workPlace = malloc(ZSTD_BLOCKSIZE_MAX as size_t);
843-
if (esr.dict).is_null() || (esr.zc).is_null() || (esr.workPlace).is_null() {
838+
esr.workPlace = Box::new_uninit_slice(ZSTD_BLOCKSIZE_MAX as size_t);
839+
if (esr.dict).is_null() || (esr.zc).is_null() {
844840
if notificationLevel >= 1 {
845841
eprintln!("Not enough memory");
846842
}
@@ -851,15 +847,14 @@ unsafe fn analyze_entropy_internal(
851847
let mut pos = 0usize;
852848
for fileSize in fileSizes {
853849
ZDICT_countEStats(
854-
*esr,
850+
esr,
855851
&params,
856852
&mut countLit,
857853
&mut offcodeCount,
858854
&mut matchLengthCount,
859855
&mut litLengthCount,
860856
&mut repOffset,
861-
src[pos..].as_ptr() as *const core::ffi::c_void,
862-
*fileSize,
857+
&src[pos..][..*fileSize],
863858
notificationLevel,
864859
);
865860
pos = pos.wrapping_add(*fileSize);

0 commit comments

Comments
 (0)