Skip to content

Commit af5a4f3

Browse files
committed
use vec for table
1 parent caeb28c commit af5a4f3

File tree

1 file changed

+27
-38
lines changed

1 file changed

+27
-38
lines changed

lib/dictBuilder/zdict.rs

Lines changed: 27 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ struct offsetCount_t {
4545
count: u32,
4646
}
4747

48-
#[derive(Copy, Clone, Default)]
48+
#[derive(Debug, Copy, Clone, Default)]
4949
#[repr(C)]
5050
struct DictItem {
5151
pos: u32,
@@ -514,11 +514,13 @@ unsafe fn ZDICT_removeDictItem(table: *mut DictItem, id: u32) {
514514
}
515515

516516
unsafe fn ZDICT_insertDictItem(
517-
table: *mut DictItem,
518-
maxSize: u32,
517+
table: &mut [DictItem],
519518
elt: DictItem,
520519
buffer: *const core::ffi::c_void,
521520
) {
521+
let maxSize = table.len() as u32;
522+
let table = table.as_mut_ptr();
523+
522524
// merge if possible
523525
let mut mergeId = ZDICT_tryMerge(table, elt, 0, buffer);
524526
if mergeId != 0 {
@@ -548,20 +550,19 @@ unsafe fn ZDICT_insertDictItem(
548550
(*table).pos = nextElt.wrapping_add(1);
549551
}
550552

551-
unsafe fn ZDICT_dictSize(dictList: *const DictItem) -> u32 {
553+
unsafe fn ZDICT_dictSize(dictList: &[DictItem]) -> u32 {
552554
let mut u: u32 = 0;
553555
let mut dictSize = 0u32;
554556
u = 1;
555-
while u < (*dictList).pos {
556-
dictSize = dictSize.wrapping_add((*dictList.offset(u as isize)).length);
557+
while u < dictList[0].pos {
558+
dictSize = dictSize.wrapping_add((dictList[u as usize]).length);
557559
u = u.wrapping_add(1);
558560
}
559561
dictSize
560562
}
561563

562564
unsafe fn ZDICT_trainBuffer_legacy(
563-
dictList: *mut DictItem,
564-
dictListSize: u32,
565+
dictList: &mut [DictItem],
565566
buffer: &[u8],
566567
mut bufferSize: size_t,
567568
fileSizes: *const size_t,
@@ -654,7 +655,7 @@ unsafe fn ZDICT_trainBuffer_legacy(
654655
continue;
655656
}
656657

657-
ZDICT_insertDictItem(dictList, dictListSize, solution, buffer.as_ptr().cast());
658+
ZDICT_insertDictItem(dictList, solution, buffer.as_ptr().cast());
658659
cursor += solution.length as usize;
659660

660661
if notificationLevel >= 2 && displayClock.elapsed() > refresh_rate {
@@ -1311,8 +1312,7 @@ unsafe fn ZDICT_trainFromBuffer_unsafe_legacy(
13111312
) -> size_t {
13121313
let nbSamples = samplesSizes.len() as u32;
13131314
let dictListSize = Ord::max(Ord::max(10000, nbSamples), (maxDictSize / 16) as u32);
1314-
let dictList = malloc((dictListSize as size_t).wrapping_mul(::core::mem::size_of::<DictItem>()))
1315-
as *mut DictItem;
1315+
let mut dictList = vec![DictItem::default(); dictListSize as size_t];
13161316
let selectivity = if params.selectivityLevel == 0 {
13171317
g_selectivity_default
13181318
} else {
@@ -1328,29 +1328,23 @@ unsafe fn ZDICT_trainFromBuffer_unsafe_legacy(
13281328
let notificationLevel = params.zParams.notificationLevel;
13291329

13301330
// checks
1331-
if dictList.is_null() {
1332-
return Error::memory_allocation.to_error_code();
1333-
}
13341331
if maxDictSize < ZDICT_DICTSIZE_MIN {
13351332
// requested dictionary size is too small
1336-
free(dictList as *mut core::ffi::c_void);
13371333
return Error::dstSize_tooSmall.to_error_code();
13381334
}
13391335
if samplesBuffSize < ZDICT_MIN_SAMPLES_SIZE {
13401336
// not enough source to create dictionary
1341-
free(dictList as *mut core::ffi::c_void);
13421337
return Error::dictionaryCreation_failed.to_error_code();
13431338
}
13441339

1345-
dictList.as_mut().unwrap().init();
1340+
dictList[0].init();
13461341

13471342
// The samples must be followed by the noise band.
13481343
debug_assert!(samples.len() >= samplesBuffSize + NOISELENGTH);
13491344

13501345
// build dictionary
13511346
ZDICT_trainBuffer_legacy(
1352-
dictList,
1353-
dictListSize,
1347+
&mut dictList,
13541348
samples,
13551349
samplesBuffSize,
13561350
samplesSizes.as_ptr(),
@@ -1361,44 +1355,42 @@ unsafe fn ZDICT_trainFromBuffer_unsafe_legacy(
13611355

13621356
// display best matches
13631357
if params.zParams.notificationLevel >= 3 {
1364-
let nb = Ord::min(25, (*dictList).pos);
1365-
let dictContentSize = ZDICT_dictSize(dictList);
1358+
let nb = Ord::min(25, dictList[0].pos);
1359+
let dictContentSize = ZDICT_dictSize(&dictList);
13661360
eprintln!(
13671361
"\n {} segments found, of total size {} ",
1368-
((*dictList).pos).wrapping_sub(1),
1362+
(dictList[0].pos).wrapping_sub(1),
13691363
dictContentSize,
13701364
);
13711365
eprintln!("list {} best segments ", nb.wrapping_sub(1));
13721366
for u in 1..nb {
1373-
let pos = (*dictList.offset(u as isize)).pos;
1374-
let length = (*dictList.offset(u as isize)).length;
1367+
let pos = dictList[u as usize].pos;
1368+
let length = dictList[u as usize].length;
13751369
let printedLength = Ord::min(40, length);
13761370

13771371
debug_assert!((pos + length) as size_t <= samplesBuffSize);
13781372
if pos as size_t > samplesBuffSize
13791373
|| pos.wrapping_add(length) as size_t > samplesBuffSize
13801374
{
1381-
free(dictList as *mut core::ffi::c_void);
13821375
return Error::GENERIC.to_error_code(); // should never happen
13831376
}
13841377
eprint!(
13851378
"{:3}:{:3} bytes at pos {:8}, savings {:7} bytes |",
13861379
u,
13871380
length,
13881381
pos,
1389-
(*dictList.offset(u as isize)).savings,
1382+
(dictList[u as usize]).savings,
13901383
);
13911384
ZDICT_printHex(&samples[..printedLength as usize]);
13921385
eprintln!("|");
13931386
}
13941387
}
13951388

13961389
// create dictionary
1397-
let mut dictContentSize_0 = ZDICT_dictSize(dictList);
1390+
let mut dictContentSize_0 = ZDICT_dictSize(&mut dictList);
13981391
#[expect(deprecated)]
13991392
if dictContentSize_0 < ZDICT_CONTENTSIZE_MIN {
14001393
// dictionary content too small
1401-
free(dictList as *mut core::ffi::c_void);
14021394
return Error::dictionaryCreation_failed.to_error_code();
14031395
}
14041396

@@ -1445,35 +1437,33 @@ unsafe fn ZDICT_trainFromBuffer_unsafe_legacy(
14451437
}
14461438

14471439
// limit dictionary size
1448-
let max = (*dictList).pos; // convention: dictList[0].pos contains the number of useful elements
1440+
let max = dictList[0].pos; // convention: dictList[0].pos contains the number of useful elements
14491441
let mut currentSize = 0u32;
14501442
let mut n: u32 = 1;
14511443
while n < max {
1452-
currentSize = currentSize.wrapping_add((*dictList.offset(n as isize)).length);
1444+
currentSize = currentSize.wrapping_add((dictList[n as usize]).length);
14531445
if currentSize as size_t > targetDictSize {
1454-
currentSize = currentSize.wrapping_sub((*dictList.offset(n as isize)).length);
1446+
currentSize = currentSize.wrapping_sub((dictList[n as usize]).length);
14551447
break;
14561448
} else {
14571449
n = n.wrapping_add(1);
14581450
}
14591451
}
1460-
(*dictList).pos = n;
1452+
dictList[0].pos = n;
14611453
dictContentSize_0 = currentSize;
14621454

14631455
// build dictionary content
14641456
let mut ptr = (dictBuffer as *mut u8).add(maxDictSize);
1465-
for u in 1..(*dictList).pos {
1466-
let l = (*dictList.offset(u as isize)).length;
1457+
for u in 1..dictList[0].pos {
1458+
let l = (dictList[u as usize]).length;
14671459
ptr = ptr.offset(-(l as isize));
14681460
debug_assert!(ptr >= dictBuffer as *mut u8);
14691461
if ptr < dictBuffer as *mut u8 {
1470-
free(dictList as *mut core::ffi::c_void);
14711462
return Error::GENERIC.to_error_code(); // should not happen
14721463
}
14731464
memcpy(
14741465
ptr as *mut core::ffi::c_void,
1475-
samples[(*dictList.offset(u as isize)).pos as usize..].as_ptr()
1476-
as *const core::ffi::c_void,
1466+
samples[(dictList[u as usize]).pos as usize..].as_ptr() as *const core::ffi::c_void,
14771467
l as size_t,
14781468
);
14791469
}
@@ -1487,7 +1477,6 @@ unsafe fn ZDICT_trainFromBuffer_unsafe_legacy(
14871477
params.zParams,
14881478
);
14891479

1490-
free(dictList as *mut core::ffi::c_void);
14911480
dictSize
14921481
}
14931482

0 commit comments

Comments
 (0)