From d4e9cd01403e9235227b2fb2ebda06dfcfb77dc6 Mon Sep 17 00:00:00 2001 From: Moinak Ghosh Date: Fri, 24 Aug 2012 20:16:21 +0530 Subject: [PATCH] Improve memory efficiency when total file size < total size of chunks. Fix freeing of Zlib structures. --- main.c | 50 ++++++++++++++++++++++++------------------------- zlib_compress.c | 2 ++ 2 files changed, 27 insertions(+), 25 deletions(-) diff --git a/main.c b/main.c index 641bde0..b5a5e5f 100644 --- a/main.c +++ b/main.c @@ -605,6 +605,27 @@ start_decompress(const char *filename, const char *to_filename) if (main_cancel) break; tdat->id = chunk_num; + /* + * First read length of compressed chunk. + */ + rb = Read(compfd, &tdat->len_cmp, sizeof (tdat->len_cmp)); + if (rb != sizeof (tdat->len_cmp)) { + if (rb < 0) perror("Read: "); + else + fprintf(stderr, "Incomplete chunk %d header," + "file corrupt\n", chunk_num); + UNCOMP_BAIL; + } + tdat->len_cmp = htonll(tdat->len_cmp); + + /* + * Zero compressed len means end of file. + */ + if (tdat->len_cmp == 0) { + bail = 1; + break; + } + /* * Delayed allocation. Allocate chunks if not already done. The compressed * file format does not provide any info on how many chunks are there in @@ -627,27 +648,6 @@ start_decompress(const char *filename, const char *to_filename) tdat->cmp_seg = tdat->uncompressed_chunk; } - /* - * First read length of compressed chunk. - */ - rb = Read(compfd, &tdat->len_cmp, sizeof (tdat->len_cmp)); - if (rb != sizeof (tdat->len_cmp)) { - if (rb < 0) perror("Read: "); - else - fprintf(stderr, "Incomplete chunk %d header," - "file corrupt\n", chunk_num); - UNCOMP_BAIL; - } - tdat->len_cmp = htonll(tdat->len_cmp); - - /* - * Zero compressed len means end of file. - */ - if (tdat->len_cmp == 0) { - bail = 1; - break; - } - if (tdat->len_cmp > largest_chunk) largest_chunk = tdat->len_cmp; if (tdat->len_cmp < smallest_chunk) @@ -1192,6 +1192,10 @@ start_compress(const char *filename, uint64_t chunksize, int level) sem_wait(&tdat->write_done_sem); if (main_cancel) break; + if (rbytes == 0) { /* EOF */ + bail = 1; + break; + } /* * Delayed allocation. Allocate chunks if not already done. */ @@ -1258,10 +1262,6 @@ start_compress(const char *filename, uint64_t chunksize, int level) bail = 1; perror("Read: "); COMP_BAIL; - - } else if (tdat->rbytes == 0) { /* EOF */ - bail = 1; - break; } } /* Signal the compression thread to start */ diff --git a/zlib_compress.c b/zlib_compress.c index 1ba8ead..087a3b0 100644 --- a/zlib_compress.c +++ b/zlib_compress.c @@ -84,6 +84,8 @@ int zlib_deinit(void **data) { if (*data) { + z_stream *zs = (z_stream *)(*data); + deflateEnd(zs); slab_free(NULL, *data); } }