Fixed a few mem leaks, and C++ compatibility
authornvalimak <nvalimak@3cdefd35-fc62-479d-8e8d-bae585ffb9ca>
Fri, 29 May 2009 14:35:20 +0000 (14:35 +0000)
committernvalimak <nvalimak@3cdefd35-fc62-479d-8e8d-bae585ffb9ca>
Fri, 29 May 2009 14:35:20 +0000 (14:35 +0000)
git-svn-id: svn+ssh://idea.nguyen.vg/svn/sxsi/trunk/TextCollection@415 3cdefd35-fc62-479d-8e8d-bae585ffb9ca

lzindex/hash.c
lzindex/lztrie.c
lzindex/lztrie.h
lzindex/makefile
lzindex/position.c
lzindex/test.c

index 272fbee..754f327 100644 (file)
@@ -8,9 +8,10 @@
 
 hash createHash (uint n, uint vbits, float factor)
    
-   { hash H = malloc (sizeof(struct shash));
+   { 
      int i,N;
      if (n == 0) return NULL;
+     hash H = malloc (sizeof(struct shash));
      N = n*factor; if (N <= n) N = n+1;
      H->size = (1 << bits(N-1)) - 1;
      H->bits = vbits;
index 0a4c56b..7e4453d 100644 (file)
@@ -59,6 +59,8 @@ lztrie buildLZTrie(byte *text, byte s, uint text_length)
     
     destroyTrie(T);
     LZT = createLZTrie(parent,letters,Node,n,text_length);
+
+    free(Node); Node = 0;
     return LZT;
  }
 
@@ -73,6 +75,7 @@ void destroyLZTrie(lztrie T)
     destroyNodemap(T->Node);
     destroyPosition(T->TPos);
     free(T->boost);
+    
     free(T);
  }
 
@@ -132,7 +135,7 @@ lztrie loadLZTrie (FILE *f)
     i = 1; // shortcut for first child of root
     while (i != 2*T->n-1) { // shortcut for its closing parenthesis
        T->boost[T->letters[i-rank(T->pdata->bdata,i)]] = i;
-       // shortcut for leftrankLZTrie
+      // shortcut for leftrankLZTrie
        i = findclose(T->pdata,i)+1;
     }
 
index 691a2aa..f70c50e 100644 (file)
@@ -45,11 +45,29 @@ extern "C" {
 #endif
 
        // frees LZTrie structure, including the owned data
+#ifdef __cplusplus
+extern "C" {
+void destroyLZTrie (lztrie T);
+}
+#else
 void destroyLZTrie (lztrie T);
+#endif
         // stores lztrie T on file f
+#ifdef __cplusplus
+extern "C" {
+void saveLZTrie (lztrie T, FILE *f);
+}
+#else
 void saveLZTrie (lztrie T, FILE *f);
+#endif
         // loads lztrie T from file f
+#ifdef __cplusplus
+extern "C" {
 lztrie loadLZTrie (FILE *f);
+}
+#else
+lztrie loadLZTrie (FILE *f);
+#endif
        // letter by which node i descends
 byte letterLZTrie (lztrie T, trieNode i);
        // go down by letter c, if possible
index c2d688b..c943bcd 100644 (file)
@@ -1,4 +1,4 @@
-#FLAGS = -g -lm
+#FLAGS = -g 
 FLAGS = -O9 -fomit-frame-pointer -W -Wall -Winline -DDEBUG=0 -DNDEBUG=1 
 
 all: lztrie.a
index 984a8d4..d9d552d 100644 (file)
@@ -136,6 +136,7 @@ void destroyPosition(position P)
     if (!P) return; 
     free(P->SuperBlock);
     free(P->Offset);
+    free(P);
  }
 
 
index c0d2694..55ca075 100644 (file)
@@ -20,5 +20,27 @@ int main()
             exit(0);
         }
 
+    free(newt);
+
+    FILE *fp = fopen("output.lz", "wb");
+    saveLZTrie(lz, fp);
+    fclose(fp);
+    destroyLZTrie(lz);
+
+    fp = fopen("output.lz", "rb");
+    lz = loadLZTrie(fp);
+    fclose(fp);
+
+    printf("extracting:\n");
+    extract(lz, 0, n, &newt, &l);
+    for (i = 0; i < n; ++i)
+        if (newt[i]  != text[i]) {
+            printf("texts differ at %lu\n", i);
+            exit(0);
+        }
+
+    free(newt);
+
+    destroyLZTrie(lz);
     return 0;
 }