Return error code in verify() and minor code cleanup

This commit is contained in:
Stella Lau
2017-07-27 17:14:05 -07:00
parent 1294a4a897
commit 8fae41c412
2 changed files with 34 additions and 28 deletions
+17 -15
View File
@@ -108,6 +108,12 @@ struct LDM_hashTable {
BYTE *bucketOffsets; // A pointer (per bucket) to the next insert position. BYTE *bucketOffsets; // A pointer (per bucket) to the next insert position.
}; };
static void HASH_destroyTable(LDM_hashTable *table) {
free(table->entries);
free(table->bucketOffsets);
free(table);
}
/** /**
* Create a hash table that can contain size elements. * Create a hash table that can contain size elements.
* The number of buckets is determined by size >> HASH_BUCKET_SIZE_LOG. * The number of buckets is determined by size >> HASH_BUCKET_SIZE_LOG.
@@ -124,9 +130,7 @@ static LDM_hashTable *HASH_createTable(U32 size) {
table->bucketOffsets = calloc(size >> HASH_BUCKET_SIZE_LOG, sizeof(BYTE)); table->bucketOffsets = calloc(size >> HASH_BUCKET_SIZE_LOG, sizeof(BYTE));
if (!table->entries || !table->bucketOffsets) { if (!table->entries || !table->bucketOffsets) {
free(table->bucketOffsets); HASH_destroyTable(table);
free(table->entries);
free(table);
return NULL; return NULL;
} }
@@ -275,13 +279,13 @@ static LDM_hashEntry *HASH_getBestEntry(const LDM_CCtx *cctx,
U64 *pBackwardMatchLength) { U64 *pBackwardMatchLength) {
LDM_hashTable *table = cctx->hashTable; LDM_hashTable *table = cctx->hashTable;
LDM_hashEntry *bucket = getBucket(table, hash); LDM_hashEntry *bucket = getBucket(table, hash);
LDM_hashEntry *cur = bucket; LDM_hashEntry *cur;
LDM_hashEntry *bestEntry = NULL; LDM_hashEntry *bestEntry = NULL;
U64 bestMatchLength = 0; U64 bestMatchLength = 0;
#if !(USE_CHECKSUM) #if !(USE_CHECKSUM)
(void)checksum; (void)checksum;
#endif #endif
for (; cur < bucket + HASH_BUCKET_SIZE; ++cur) { for (cur = bucket; cur < bucket + HASH_BUCKET_SIZE; ++cur) {
const BYTE *pMatch = cur->offset + cctx->ibase; const BYTE *pMatch = cur->offset + cctx->ibase;
// Check checksum for faster check. // Check checksum for faster check.
@@ -336,12 +340,6 @@ static void HASH_insert(LDM_hashTable *table,
table->bucketOffsets[hash] &= HASH_BUCKET_SIZE - 1; table->bucketOffsets[hash] &= HASH_BUCKET_SIZE - 1;
} }
static void HASH_destroyTable(LDM_hashTable *table) {
free(table->entries);
free(table->bucketOffsets);
free(table);
}
static void HASH_outputTableOccupancy(const LDM_hashTable *table) { static void HASH_outputTableOccupancy(const LDM_hashTable *table) {
U32 ctr = 0; U32 ctr = 0;
LDM_hashEntry *cur = table->entries; LDM_hashEntry *cur = table->entries;
@@ -360,8 +358,9 @@ static void HASH_outputTableOccupancy(const LDM_hashTable *table) {
100.0 * (double)(ctr) / table->numEntries); 100.0 * (double)(ctr) / table->numEntries);
} }
// TODO: This can be done more efficiently (but it is not that important as it // TODO: This can be done more efficiently, for example by using builtin
// is only used for computing stats). // functions (but it is not that important as it is only used for computing
// stats).
static int intLog2(U64 x) { static int intLog2(U64 x) {
int ret = 0; int ret = 0;
while (x >>= 1) { while (x >>= 1) {
@@ -371,7 +370,6 @@ static int intLog2(U64 x) {
} }
void LDM_printCompressStats(const LDM_compressStats *stats) { void LDM_printCompressStats(const LDM_compressStats *stats) {
int i = 0;
printf("=====================\n"); printf("=====================\n");
printf("Compression statistics\n"); printf("Compression statistics\n");
printf("Window size, hash table size (bytes): 2^%u, 2^%u\n", printf("Window size, hash table size (bytes): 2^%u, 2^%u\n",
@@ -395,7 +393,10 @@ void LDM_printCompressStats(const LDM_compressStats *stats) {
printf("offset histogram | match length histogram\n"); printf("offset histogram | match length histogram\n");
printf("offset/ML, num matches, %% of matches | num matches, %% of matches\n"); printf("offset/ML, num matches, %% of matches | num matches, %% of matches\n");
for (; i <= intLog2(stats->maxOffset); i++) { {
int i;
int logMaxOffset = intLog2(stats->maxOffset);
for (i = 0; i <= logMaxOffset; i++) {
printf("2^%*d: %10u %6.3f%% |2^%*d: %10u %6.3f \n", printf("2^%*d: %10u %6.3f%% |2^%*d: %10u %6.3f \n",
2, i, 2, i,
stats->offsetHistogram[i], stats->offsetHistogram[i],
@@ -406,6 +407,7 @@ void LDM_printCompressStats(const LDM_compressStats *stats) {
100.0 * (double) stats->matchLengthHistogram[i] / 100.0 * (double) stats->matchLengthHistogram[i] /
(double) stats->numMatches); (double) stats->numMatches);
} }
}
printf("\n"); printf("\n");
printf("=====================\n"); printf("=====================\n");
} }
+8 -4
View File
@@ -12,7 +12,7 @@
#include "ldm.h" #include "ldm.h"
#include "zstd.h" #include "zstd.h"
#define DECOMPRESS_AND_VERIFY // #define DECOMPRESS_AND_VERIFY
/* Compress file given by fname and output to oname. /* Compress file given by fname and output to oname.
* Returns 0 if successful, error code otherwise. * Returns 0 if successful, error code otherwise.
@@ -206,6 +206,7 @@ static int verify(const char *inpFilename, const char *decFilename) {
printf("verify : OK\n"); printf("verify : OK\n");
} else { } else {
printf("verify : NG\n"); printf("verify : NG\n");
return 1;
} }
} }
@@ -239,7 +240,7 @@ int main(int argc, const char *argv[]) {
/* Compress */ /* Compress */
{ {
if (compress(inpFilename, ldmFilename)) { if (compress(inpFilename, ldmFilename)) {
printf("Compress error"); printf("Compress error\n");
return 1; return 1;
} }
} }
@@ -250,7 +251,7 @@ int main(int argc, const char *argv[]) {
struct timeval tv1, tv2; struct timeval tv1, tv2;
gettimeofday(&tv1, NULL); gettimeofday(&tv1, NULL);
if (decompress(ldmFilename, decFilename)) { if (decompress(ldmFilename, decFilename)) {
printf("Decompress error"); printf("Decompress error\n");
return 1; return 1;
} }
gettimeofday(&tv2, NULL); gettimeofday(&tv2, NULL);
@@ -259,7 +260,10 @@ int main(int argc, const char *argv[]) {
(double) (tv2.tv_sec - tv1.tv_sec)); (double) (tv2.tv_sec - tv1.tv_sec));
} }
/* verify */ /* verify */
verify(inpFilename, decFilename); if (verify(inpFilename, decFilename)) {
printf("Verification error\n");
return 1;
}
#endif #endif
return 0; return 0;
} }