From c7a111bd3279b3e57f001bb15e8353bf738caee3 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Thu, 9 Jun 2016 10:51:55 +1000 Subject: [PATCH] Base temporary output buffer on maximum mallocable, not maxram --- lrzip.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/lrzip.c b/lrzip.c index 1bf39a3..b72b9ee 100644 --- a/lrzip.c +++ b/lrzip.c @@ -492,13 +492,25 @@ bool read_tmpinfile(rzip_control *control, int fd_in) * a pseudo-temporary file */ static bool open_tmpoutbuf(rzip_control *control) { + i64 maxlen = control->maxram; + void *buf; + + while (42) { + round_to_page(&maxlen); + buf = malloc(maxlen); + if (buf) { + print_maxverbose("Malloced %"PRId64" for tmp_outbuf\n", maxlen); + break; + } + maxlen = maxlen / 3 * 2; + if (maxlen < 100000000) + fatal_return(("Unable to even malloc 100MB for tmp_outbuf\n"), false); + } control->flags |= FLAG_TMP_OUTBUF; - control->out_maxlen = control->maxram; /* Allocate slightly more so we can cope when the buffer overflows and * fall back to a real temporary file */ - control->tmp_outbuf = malloc(control->maxram + control->page_size); - if (unlikely(!control->tmp_outbuf)) - fatal_return(("Failed to malloc tmp_outbuf in open_tmpoutbuf\n"), false); + control->out_maxlen = maxlen - control->page_size; + control->tmp_outbuf = buf; if (!DECOMPRESS && !TEST_ONLY) control->out_ofs = control->out_len = MAGIC_LEN;\ return true;