Skip to content

Commit 3f95547

Browse files
committed
cover.rs: make a bunch more functions safe
1 parent cbdaac2 commit 3f95547

File tree

1 file changed

+28
-22
lines changed

1 file changed

+28
-22
lines changed

lib/dictBuilder/cover.rs

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use core::ptr;
21
use std::cmp::Ordering;
32
use std::ops::Range;
43
use std::sync::{Condvar, Mutex};
@@ -87,13 +86,15 @@ const CLOCKS_PER_SEC: core::ffi::c_int = 1000000;
8786

8887
const COVER_DEFAULT_SPLITPOINT: core::ffi::c_double = 1.0f64;
8988
const MAP_EMPTY_VALUE: core::ffi::c_int = -(1);
89+
9090
unsafe fn COVER_map_clear(map: *mut COVER_map_t) {
9191
core::ptr::write_bytes(
9292
(*map).data as *mut u8,
9393
MAP_EMPTY_VALUE as u8,
9494
(*map).size as size_t * core::mem::size_of::<COVER_map_pair_t>(),
9595
);
9696
}
97+
9798
unsafe fn COVER_map_init(map: *mut COVER_map_t, size: u32) -> core::ffi::c_int {
9899
(*map).sizeLog = (ZSTD_highbit32(size)).wrapping_add(2);
99100
(*map).size = (1) << (*map).sizeLog;
@@ -173,6 +174,7 @@ unsafe fn COVER_map_destroy(map: *mut COVER_map_t) {
173174
(*map).data = core::ptr::null_mut();
174175
(*map).size = 0;
175176
}
177+
176178
pub(super) unsafe fn COVER_sum(
177179
samplesSizes: *const size_t,
178180
nbSamples: core::ffi::c_uint,
@@ -506,7 +508,7 @@ fn COVER_checkParameters(parameters: ZDICT_cover_params_t, maxDictSize: size_t)
506508
true
507509
}
508510

509-
unsafe fn COVER_ctx_destroy(ctx: &mut COVER_ctx_t) {
511+
fn COVER_ctx_destroy(ctx: &mut COVER_ctx_t) {
510512
drop(core::mem::take(&mut ctx.suffix));
511513
drop(core::mem::take(&mut ctx.freqs));
512514
drop(core::mem::take(&mut ctx.dmerAt));
@@ -649,7 +651,7 @@ fn COVER_ctx_init<'a>(
649651
0
650652
}
651653

652-
pub(super) unsafe fn COVER_warnOnSmallCorpus(
654+
pub(super) fn COVER_warnOnSmallCorpus(
653655
maxDictSize: size_t,
654656
nbDmers: size_t,
655657
displayLevel: core::ffi::c_int,
@@ -669,7 +671,8 @@ pub(super) unsafe fn COVER_warnOnSmallCorpus(
669671
);
670672
}
671673
}
672-
pub(super) unsafe fn COVER_computeEpochs(
674+
675+
pub(super) fn COVER_computeEpochs(
673676
maxDictSize: u32,
674677
nbDmers: u32,
675678
k: u32,
@@ -694,6 +697,7 @@ pub(super) unsafe fn COVER_computeEpochs(
694697
epochs.num = nbDmers / epochs.size;
695698
epochs
696699
}
700+
697701
unsafe fn COVER_buildDictionary(
698702
ctx: *const COVER_ctx_t,
699703
freqs: *mut u32,
@@ -892,6 +896,7 @@ pub unsafe extern "C" fn ZDICT_trainFromBuffer_cover(
892896
COVER_map_destroy(&mut activeDmers);
893897
dictionarySize
894898
}
899+
895900
pub(super) unsafe fn COVER_checkTotalCompressedSize(
896901
parameters: ZDICT_cover_params_t,
897902
samplesSizes: *const size_t,
@@ -962,40 +967,38 @@ pub(super) unsafe fn COVER_checkTotalCompressedSize(
962967
}
963968
totalCompressedSize
964969
}
965-
pub(super) unsafe fn COVER_best_init(best: &mut COVER_best_t) {
970+
971+
pub(super) fn COVER_best_init(best: &mut COVER_best_t) {
966972
best.liveJobs = 0;
967973
best.dict = core::ptr::null_mut();
968974
best.dictSize = 0;
969975
best.compressedSize = -(1 as core::ffi::c_int) as size_t;
970-
ptr::write_bytes(
971-
&mut best.parameters as *mut ZDICT_cover_params_t as *mut u8,
972-
0,
973-
::core::mem::size_of::<ZDICT_cover_params_t>(),
974-
);
976+
best.parameters = ZDICT_cover_params_t::default();
975977
}
976-
pub(super) unsafe fn COVER_best_wait(best: *mut COVER_best_t) {
977-
if best.is_null() {
978-
return;
979-
}
980-
let mut guard = (*best).mutex.lock().unwrap();
978+
979+
pub(super) fn COVER_best_wait(best: &mut COVER_best_t) {
980+
let mut guard = best.mutex.lock().unwrap();
981981
#[expect(clippy::while_immutable_condition)]
982-
while (*best).liveJobs != 0 {
983-
guard = (*best).cond.wait(guard).unwrap();
982+
while best.liveJobs != 0 {
983+
guard = best.cond.wait(guard).unwrap();
984984
}
985985
}
986+
986987
pub(super) unsafe fn COVER_best_destroy(best: &mut COVER_best_t) {
987988
COVER_best_wait(best);
988989
if !(best.dict).is_null() {
989990
free(best.dict);
990991
}
991992
}
993+
992994
pub(super) unsafe fn COVER_best_start(best: *mut COVER_best_t) {
993995
if best.is_null() {
994996
return;
995997
}
996998
let _guard = (*best).mutex.lock().unwrap();
997999
(*best).liveJobs = ((*best).liveJobs).wrapping_add(1);
9981000
}
1001+
9991002
pub(super) unsafe fn COVER_best_finish(
10001003
best: *mut COVER_best_t,
10011004
parameters: ZDICT_cover_params_t,
@@ -1035,7 +1038,8 @@ pub(super) unsafe fn COVER_best_finish(
10351038
(*best).cond.notify_all();
10361039
}
10371040
}
1038-
unsafe fn setDictSelection(buf: *mut u8, s: size_t, csz: size_t) -> COVER_dictSelection_t {
1041+
1042+
fn setDictSelection(buf: *mut u8, s: size_t, csz: size_t) -> COVER_dictSelection_t {
10391043
let mut ds = COVER_dictSelection_t {
10401044
dictContent: core::ptr::null_mut::<u8>(),
10411045
dictSize: 0,
@@ -1046,18 +1050,20 @@ unsafe fn setDictSelection(buf: *mut u8, s: size_t, csz: size_t) -> COVER_dictSe
10461050
ds.totalCompressedSize = csz;
10471051
ds
10481052
}
1049-
pub(super) unsafe fn COVER_dictSelectionError(error: size_t) -> COVER_dictSelection_t {
1053+
1054+
pub(super) fn COVER_dictSelectionError(error: size_t) -> COVER_dictSelection_t {
10501055
setDictSelection(core::ptr::null_mut(), 0, error)
10511056
}
1052-
pub(super) unsafe fn COVER_dictSelectionIsError(
1053-
selection: COVER_dictSelection_t,
1054-
) -> core::ffi::c_uint {
1057+
1058+
pub(super) fn COVER_dictSelectionIsError(selection: COVER_dictSelection_t) -> core::ffi::c_uint {
10551059
(ERR_isError(selection.totalCompressedSize) || (selection.dictContent).is_null())
10561060
as core::ffi::c_int as core::ffi::c_uint
10571061
}
1062+
10581063
pub(super) unsafe fn COVER_dictSelectionFree(selection: COVER_dictSelection_t) {
10591064
free(selection.dictContent as *mut core::ffi::c_void);
10601065
}
1066+
10611067
pub(super) unsafe fn COVER_selectDict(
10621068
customDictContent: *mut u8,
10631069
dictBufferCapacity: size_t,

0 commit comments

Comments
 (0)