Improve memory efficiency when total file size < total size of chunks.

Fix freeing of Zlib structures.
This commit is contained in:
Moinak Ghosh 2012-08-24 20:16:21 +05:30
parent bf149e880d
commit d4e9cd0140
2 changed files with 27 additions and 25 deletions

50
main.c
View file

@ -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 */

View file

@ -84,6 +84,8 @@ int
zlib_deinit(void **data)
{
if (*data) {
z_stream *zs = (z_stream *)(*data);
deflateEnd(zs);
slab_free(NULL, *data);
}
}