diff options
author | Lasse Collin <lasse.collin@tukaani.org> | 2010-03-07 13:29:28 +0200 |
---|---|---|
committer | Lasse Collin <lasse.collin@tukaani.org> | 2010-03-07 13:29:28 +0200 |
commit | 2672bcc9f85ba28ff648e092e9eb4cd9e69ce418 (patch) | |
tree | 1f3c218b5b20f5d2ec7fb364fd1c7f5f9c1a04c3 /src/xzdec/xzdec.c | |
parent | Updated THANKS. (diff) | |
download | xz-2672bcc9f85ba28ff648e092e9eb4cd9e69ce418.tar.xz |
Increase the default memory usage limit on "low-memory" systems.
Previously the default limit was always 40 % of RAM. The
new limit is a little bit more complex:
- If 40 % of RAM is at least 80 MiB, 40 % of RAM is used
as the limit.
- If 80 % of RAM is over 80 MiB, 80 MiB is used as the limit.
- Otherwise 80 % of RAM is used as the limit.
This should make it possible to decompress files created with
"xz -9" on more systems. Swapping is generally more expected
on systems with less RAM, so higher default limit on them
shouldn't cause too bad surprises in terms of heavy swapping.
Instead, the higher default limit should reduce the number of
bad surprises when it used to prevent decompression of files
created with "xz -9". The DoS prevention system shouldn't be
a DoS itself.
Note that even with the new default limit, a system with 64 MiB
RAM cannot decompress files created with "xz -9" without user
overriding the limit. This should be OK, because if xz is going
to need more memory than the system has RAM, it will run very
very slowly and thus it's good that user has to override the limit
in that case.
Diffstat (limited to '')
-rw-r--r-- | src/xzdec/xzdec.c | 42 |
1 files changed, 30 insertions, 12 deletions
diff --git a/src/xzdec/xzdec.c b/src/xzdec/xzdec.c index 2b166861..6ddf7d28 100644 --- a/src/xzdec/xzdec.c +++ b/src/xzdec/xzdec.c @@ -38,6 +38,9 @@ /// Number of bytes to use memory at maximum static uint64_t memlimit; +/// Total amount of physical RAM +static uint64_t total_ram; + /// Error messages are suppressed if this is zero, which is the case when /// --quiet has been given at least twice. static unsigned int display_errors = 2; @@ -103,14 +106,7 @@ version(void) static void memlimit_set_percentage(uint32_t percentage) { - uint64_t mem = lzma_physmem(); - - // If we cannot determine the amount of RAM, use the assumption - // set by the configure script. - if (mem == 0) - mem = (uint64_t)(ASSUME_RAM) * 1024 * 1024; - - memlimit = percentage * mem / 100; + memlimit = percentage * total_ram / 100; return; } @@ -120,15 +116,37 @@ memlimit_set_percentage(uint32_t percentage) static void memlimit_set(uint64_t new_memlimit) { - if (new_memlimit == 0) - memlimit_set_percentage(40); - else + if (new_memlimit != 0) { memlimit = new_memlimit; + } else { + memlimit = 40 * total_ram / 100; + if (memlimit < UINT64_C(80) * 1024 * 1024) { + memlimit = 80 * total_ram / 100; + if (memlimit > UINT64_C(80) * 1024 * 1024) + memlimit = UINT64_C(80) * 1024 * 1024; + } + } return; } +/// Get the total amount of physical RAM and set the memory usage limit +/// to the default value. +static void +memlimit_init(void) +{ + // If we cannot determine the amount of RAM, use the assumption + // defined by the configure script. + total_ram = lzma_physmem(); + if (total_ram == 0) + total_ram = (uint64_t)(ASSUME_RAM) * 1024 * 1024; + + memlimit_set(0); + return; +} + + /// \brief Convert a string to uint64_t /// /// This is rudely copied from src/xz/util.c and modified a little. :-( @@ -422,7 +440,7 @@ main(int argc, char **argv) // Set the default memory usage limit. This is needed before parsing // the command line arguments. - memlimit_set(0); + memlimit_init(); // Parse the command line options. parse_options(argc, argv); |