From 948d0b4e5dbacc37168761e3c6772bdef9d19a63 Mon Sep 17 00:00:00 2001 From: Dave Vasilevsky Date: Sat, 22 Dec 2012 18:59:36 -0500 Subject: [PATCH] Add block-fraction option --- pixz.c | 9 ++++++++- pixz.h | 2 ++ write.c | 11 ++++++++--- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/pixz.c b/pixz.c index c3b0c00..bfa6c73 100644 --- a/pixz.c +++ b/pixz.c @@ -59,7 +59,8 @@ int main(int argc, char **argv) { int ch; char *optend; long optint; - while ((ch = getopt(argc, argv, "dxli:o:tvhp:0123456789")) != -1) { + double optdbl; + while ((ch = getopt(argc, argv, "dxli:o:tvhp:0123456789f:")) != -1) { switch (ch) { case 'd': op = OP_READ; break; case 'x': op = OP_EXTRACT; break; @@ -68,6 +69,12 @@ int main(int argc, char **argv) { case 'o': opath = optarg; break; case 't': tar = false; break; case 'h': usage(NULL); break; + case 'f': + optdbl = strtod(optarg, &optend); + if (*optend || optdbl <= 0) + usage("Need a positive floating-point argument to -f"); + gBlockFraction = optdbl; + break; case 'p': optint = strtol(optarg, &optend, 10); if (optint < 0 || *optend) diff --git a/pixz.h b/pixz.h index df9a398..978ab95 100644 --- a/pixz.h +++ b/pixz.h @@ -52,6 +52,8 @@ uint64_t xle64dec(const uint8_t *d); void xle64enc(uint8_t *d, uint64_t n); size_t num_threads(void); +extern double gBlockFraction; + #pragma mark INDEX diff --git a/write.c b/write.c index 2c291e6..9a337e2 100644 --- a/write.c +++ b/write.c @@ -16,6 +16,8 @@ struct io_block_t { #pragma mark GLOBALS +double gBlockFraction = 1.0; + static bool gTar = true; static size_t gBlockInSize = 0, gBlockOutSize = 0; @@ -70,7 +72,9 @@ void pixz_write(bool tar, uint32_t level) { .options = &lzma_opts }; gFilters[1] = (lzma_filter){ .id = LZMA_VLI_UNKNOWN, .options = NULL }; - gBlockInSize = lzma_opts.dict_size * 1.0; + gBlockInSize = lzma_opts.dict_size * gBlockFraction; + if (gBlockInSize <= 0) + die("Block size must be positive"); gBlockOutSize = lzma_block_buffer_bound(gBlockInSize); pipeline_create(block_create, block_free, read_thread, encode_thread); @@ -234,8 +238,9 @@ static void block_free(void *data) { static void *block_create() { io_block_t *ib = malloc(sizeof(io_block_t)); - ib->input = malloc(gBlockInSize); - ib->output = malloc(gBlockOutSize); + if (!(ib->input = malloc(gBlockInSize)) + || !(ib->output = malloc(gBlockOutSize))) + die("Can't allocate blocks"); return ib; }