Skip to content

Commit 1068018

Browse files
committed
best.dict: make Box
1 parent 527a66c commit 1068018

File tree

2 files changed

+13
-21
lines changed

2 files changed

+13
-21
lines changed

lib/dictBuilder/cover.rs

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,17 @@ pub(super) struct COVER_epoch_info_t {
9292
pub(super) num: u32,
9393
pub(super) size: u32,
9494
}
95+
9596
pub(super) struct COVER_best_t {
9697
pub(super) mutex: Mutex<()>,
9798
pub(super) cond: Condvar,
9899
pub(super) liveJobs: size_t,
99-
pub(super) dict: *mut core::ffi::c_void,
100+
pub(super) dict: Box<[u8]>,
100101
pub(super) dictSize: size_t,
101102
pub(super) parameters: ZDICT_cover_params_t,
102103
pub(super) compressedSize: size_t,
103104
}
105+
104106
#[repr(C)]
105107
struct COVER_tryParameters_data_t<'a, 'b> {
106108
ctx: *const COVER_ctx_t<'a>,
@@ -948,7 +950,7 @@ pub(super) unsafe fn COVER_checkTotalCompressedSize(
948950

949951
pub(super) fn COVER_best_init(best: &mut COVER_best_t) {
950952
best.liveJobs = 0;
951-
best.dict = core::ptr::null_mut();
953+
best.dict = Box::default();
952954
best.dictSize = 0;
953955
best.compressedSize = -(1 as core::ffi::c_int) as size_t;
954956
best.parameters = ZDICT_cover_params_t::default();
@@ -964,9 +966,7 @@ pub(super) fn COVER_best_wait(best: &mut COVER_best_t) {
964966

965967
pub(super) unsafe fn COVER_best_destroy(best: &mut COVER_best_t) {
966968
COVER_best_wait(best);
967-
if !(best.dict).is_null() {
968-
free(best.dict);
969-
}
969+
drop(core::mem::take(&mut best.dict));
970970
}
971971

972972
pub(super) fn COVER_best_start(best: &mut COVER_best_t) {
@@ -987,20 +987,12 @@ pub(super) unsafe fn COVER_best_finish(
987987
best.liveJobs = (best.liveJobs).wrapping_sub(1);
988988
liveJobs = best.liveJobs;
989989
if compressedSize < best.compressedSize {
990-
if (best.dict).is_null() || best.dictSize < dictSize {
991-
if !(best.dict).is_null() {
992-
free(best.dict);
993-
}
994-
best.dict = malloc(dictSize);
995-
if (best.dict).is_null() {
996-
best.compressedSize = Error::GENERIC.to_error_code();
997-
best.dictSize = 0;
998-
best.cond.notify_one();
999-
return;
1000-
}
990+
if best.dictSize < dictSize {
991+
drop(core::mem::take(&mut best.dict));
992+
best.dict = Box::from(vec![0u8; dictSize]);
1001993
}
1002994
if !dict.is_null() {
1003-
memcpy(best.dict, dict, dictSize);
995+
memcpy(best.dict.as_mut_ptr().cast(), dict, dictSize);
1004996
best.dictSize = dictSize;
1005997
best.parameters = parameters;
1006998
best.compressedSize = compressedSize;
@@ -1258,7 +1250,7 @@ pub unsafe extern "C" fn ZDICT_optimizeTrainFromBuffer_cover(
12581250
mutex: Mutex::new(()),
12591251
cond: Condvar::new(),
12601252
liveJobs: 0,
1261-
dict: core::ptr::null_mut::<core::ffi::c_void>(),
1253+
dict: Box::default(),
12621254
dictSize: 0,
12631255
parameters: ZDICT_cover_params_t::default(),
12641256
compressedSize: 0,
@@ -1412,7 +1404,7 @@ pub unsafe extern "C" fn ZDICT_optimizeTrainFromBuffer_cover(
14121404
return compressedSize;
14131405
}
14141406
*parameters = best.parameters;
1415-
memcpy(dictBuffer, best.dict, dictSize);
1407+
memcpy(dictBuffer, best.dict.as_ptr().cast(), dictSize);
14161408
COVER_best_destroy(&mut best);
14171409
POOL_free(pool);
14181410
dictSize

lib/dictBuilder/fastcover.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ pub unsafe extern "C" fn ZDICT_optimizeTrainFromBuffer_fastCover(
704704
mutex: Mutex::new(()),
705705
cond: Condvar::new(),
706706
liveJobs: 0,
707-
dict: core::ptr::null_mut::<core::ffi::c_void>(),
707+
dict: Box::default(),
708708
dictSize: 0,
709709
parameters: ZDICT_cover_params_t::default(),
710710
compressedSize: 0,
@@ -884,7 +884,7 @@ pub unsafe extern "C" fn ZDICT_optimizeTrainFromBuffer_fastCover(
884884
return compressedSize;
885885
}
886886
FASTCOVER_convertToFastCoverParams(best.parameters, parameters, f, accel);
887-
memcpy(dictBuffer, best.dict, dictSize);
887+
memcpy(dictBuffer, best.dict.as_ptr().cast(), dictSize);
888888
COVER_best_destroy(&mut best);
889889
POOL_free(pool);
890890
dictSize

0 commit comments

Comments
 (0)