]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
Return error code in verify() and minor code cleanup 758/head
authorStella Lau <laus@fb.com>
Fri, 28 Jul 2017 00:14:05 +0000 (17:14 -0700)
committerStella Lau <laus@fb.com>
Fri, 28 Jul 2017 00:14:05 +0000 (17:14 -0700)
contrib/long_distance_matching/ldm.c
contrib/long_distance_matching/main.c

index c2cdb21ed56201940726e4c58b808c193f256941..4dccd0bfac680aa685cfe71c72512713b6c32ca5 100644 (file)
@@ -108,6 +108,12 @@ struct LDM_hashTable {
   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.
  * 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));
 
   if (!table->entries || !table->bucketOffsets) {
-    free(table->bucketOffsets);
-    free(table->entries);
-    free(table);
+    HASH_destroyTable(table);
     return NULL;
   }
 
@@ -275,13 +279,13 @@ static LDM_hashEntry *HASH_getBestEntry(const LDM_CCtx *cctx,
                                         U64 *pBackwardMatchLength) {
   LDM_hashTable *table = cctx->hashTable;
   LDM_hashEntry *bucket = getBucket(table, hash);
-  LDM_hashEntry *cur = bucket;
+  LDM_hashEntry *cur;
   LDM_hashEntry *bestEntry = NULL;
   U64 bestMatchLength = 0;
 #if !(USE_CHECKSUM)
   (void)checksum;
 #endif
-  for (; cur < bucket + HASH_BUCKET_SIZE; ++cur) {
+  for (cur = bucket; cur < bucket + HASH_BUCKET_SIZE; ++cur) {
     const BYTE *pMatch = cur->offset + cctx->ibase;
 
     // Check checksum for faster check.
@@ -336,12 +340,6 @@ static void HASH_insert(LDM_hashTable *table,
   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) {
   U32 ctr = 0;
   LDM_hashEntry *cur = table->entries;
@@ -360,8 +358,9 @@ static void HASH_outputTableOccupancy(const LDM_hashTable *table) {
          100.0 * (double)(ctr) / table->numEntries);
 }
 
-// TODO: This can be done more efficiently (but it is not that important as it
-// is only used for computing stats).
+// TODO: This can be done more efficiently, for example by using builtin
+// functions (but it is not that important as it is only used for computing
+// stats).
 static int intLog2(U64 x) {
   int ret = 0;
   while (x >>= 1) {
@@ -371,7 +370,6 @@ static int intLog2(U64 x) {
 }
 
 void LDM_printCompressStats(const LDM_compressStats *stats) {
-  int i = 0;
   printf("=====================\n");
   printf("Compression statistics\n");
   printf("Window size, hash table size (bytes): 2^%u, 2^%u\n",
@@ -395,16 +393,20 @@ void LDM_printCompressStats(const LDM_compressStats *stats) {
   printf("offset histogram | match length histogram\n");
   printf("offset/ML, num matches, %% of matches | num matches, %% of matches\n");
 
-  for (; i <= intLog2(stats->maxOffset); i++) {
-    printf("2^%*d: %10u    %6.3f%% |2^%*d:  %10u    %6.3f \n",
-           2, i,
-           stats->offsetHistogram[i],
-           100.0 * (double) stats->offsetHistogram[i] /
-                   (double) stats->numMatches,
-           2, i,
-           stats->matchLengthHistogram[i],
-           100.0 * (double) stats->matchLengthHistogram[i] /
-                   (double) stats->numMatches);
+  {
+    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",
+             2, i,
+             stats->offsetHistogram[i],
+             100.0 * (double) stats->offsetHistogram[i] /
+                     (double) stats->numMatches,
+             2, i,
+             stats->matchLengthHistogram[i],
+             100.0 * (double) stats->matchLengthHistogram[i] /
+                     (double) stats->numMatches);
+    }
   }
   printf("\n");
   printf("=====================\n");
index 72af540491c711c4c9d0cb8178188f3bb7e5305d..7c7086a59a742c3301908fb3d7aac9600eeee7b3 100644 (file)
@@ -12,7 +12,7 @@
 #include "ldm.h"
 #include "zstd.h"
 
-#define DECOMPRESS_AND_VERIFY
+// #define DECOMPRESS_AND_VERIFY
 
 /* Compress file given by fname and output to oname.
  * Returns 0 if successful, error code otherwise.
@@ -206,6 +206,7 @@ static int verify(const char *inpFilename, const char *decFilename) {
       printf("verify : OK\n");
     } else {
       printf("verify : NG\n");
+      return 1;
     }
   }
 
@@ -239,7 +240,7 @@ int main(int argc, const char *argv[]) {
   /* Compress */
   {
     if (compress(inpFilename, ldmFilename)) {
-        printf("Compress error");
+        printf("Compress error\n");
         return 1;
     }
   }
@@ -250,7 +251,7 @@ int main(int argc, const char *argv[]) {
     struct timeval tv1, tv2;
     gettimeofday(&tv1, NULL);
     if (decompress(ldmFilename, decFilename)) {
-        printf("Decompress error");
+        printf("Decompress error\n");
         return 1;
     }
     gettimeofday(&tv2, NULL);
@@ -259,7 +260,10 @@ int main(int argc, const char *argv[]) {
           (double) (tv2.tv_sec - tv1.tv_sec));
   }
   /* verify */
-  verify(inpFilename, decFilename);
+  if (verify(inpFilename, decFilename)) {
+    printf("Verification error\n");
+    return 1;
+  }
 #endif
   return 0;
 }