From: nvalimak Date: Fri, 29 May 2009 14:35:20 +0000 (+0000) Subject: Fixed a few mem leaks, and C++ compatibility X-Git-Url: http://git.nguyen.vg/gitweb/?a=commitdiff_plain;ds=inline;h=181fe167b6f4ee7d383679442a371cd7c89535b6;p=SXSI%2FTextCollection.git Fixed a few mem leaks, and C++ compatibility git-svn-id: svn+ssh://idea.nguyen.vg/svn/sxsi/trunk/TextCollection@415 3cdefd35-fc62-479d-8e8d-bae585ffb9ca --- diff --git a/lzindex/hash.c b/lzindex/hash.c index 272fbee..754f327 100644 --- a/lzindex/hash.c +++ b/lzindex/hash.c @@ -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; diff --git a/lzindex/lztrie.c b/lzindex/lztrie.c index 0a4c56b..7e4453d 100644 --- a/lzindex/lztrie.c +++ b/lzindex/lztrie.c @@ -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; } diff --git a/lzindex/lztrie.h b/lzindex/lztrie.h index 691a2aa..f70c50e 100644 --- a/lzindex/lztrie.h +++ b/lzindex/lztrie.h @@ -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 diff --git a/lzindex/makefile b/lzindex/makefile index c2d688b..c943bcd 100644 --- a/lzindex/makefile +++ b/lzindex/makefile @@ -1,4 +1,4 @@ -#FLAGS = -g -lm +#FLAGS = -g FLAGS = -O9 -fomit-frame-pointer -W -Wall -Winline -DDEBUG=0 -DNDEBUG=1 all: lztrie.a diff --git a/lzindex/position.c b/lzindex/position.c index 984a8d4..d9d552d 100644 --- a/lzindex/position.c +++ b/lzindex/position.c @@ -136,6 +136,7 @@ void destroyPosition(position P) if (!P) return; free(P->SuperBlock); free(P->Offset); + free(P); } diff --git a/lzindex/test.c b/lzindex/test.c index c0d2694..55ca075 100644 --- a/lzindex/test.c +++ b/lzindex/test.c @@ -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; }