[tor-commits] [tor/master] Remove `tor_compress_memory_level()`.

nickm at torproject.org nickm at torproject.org
Tue Apr 25 12:18:24 UTC 2017


commit cf912259ba491e51f6f211e186ff67605ff269c8
Author: Alexander Færøy <ahf at torproject.org>
Date:   Mon Apr 24 14:20:16 2017 +0200

    Remove `tor_compress_memory_level()`.
    
    This patch splits up `tor_compress_memory_level()` into static functions
    in the individual compression backends, which allows us to tune the
    values per compression backend rather than globally.
    
    See: https://bugs.torproject.org/21662
---
 src/common/compress.c      | 14 --------------
 src/common/compress.h      |  3 ---
 src/common/compress_lzma.c | 16 ++++++++++++++--
 src/common/compress_zlib.c | 16 ++++++++++++++--
 src/common/compress_zstd.c | 18 ++++++++++++++++--
 5 files changed, 44 insertions(+), 23 deletions(-)

diff --git a/src/common/compress.c b/src/common/compress.c
index 8b49af8..38b8184 100644
--- a/src/common/compress.c
+++ b/src/common/compress.c
@@ -56,20 +56,6 @@ tor_compress_is_compression_bomb(size_t size_in, size_t size_out)
   return (size_out / size_in > MAX_UNCOMPRESSION_FACTOR);
 }
 
-/** Given <b>level</b> return the memory level.  The memory level is needed for
- * the various compression backends used in Tor.
- */
-int
-tor_compress_memory_level(compression_level_t level)
-{
-  switch (level) {
-    default:
-    case HIGH_COMPRESSION: return 8;
-    case MEDIUM_COMPRESSION: return 7;
-    case LOW_COMPRESSION: return 6;
-  }
-}
-
 /** Given <b>in_len</b> bytes at <b>in</b>, compress them into a newly
  * allocated buffer, using the method described in <b>method</b>.  Store the
  * compressed string in *<b>out</b>, and its length in *<b>out_len</b>.
diff --git a/src/common/compress.h b/src/common/compress.h
index 182530f..1e6e9fd 100644
--- a/src/common/compress.h
+++ b/src/common/compress.h
@@ -47,9 +47,6 @@ tor_uncompress(char **out, size_t *out_len,
 compress_method_t detect_compression_method(const char *in, size_t in_len);
 
 int
-tor_compress_memory_level(compression_level_t level);
-
-int
 tor_compress_is_compression_bomb(size_t size_in, size_t size_out);
 
 int
diff --git a/src/common/compress_lzma.c b/src/common/compress_lzma.c
index f2952cc..ae0327f 100644
--- a/src/common/compress_lzma.c
+++ b/src/common/compress_lzma.c
@@ -26,6 +26,18 @@
 static size_t total_lzma_allocation = 0;
 
 #ifdef HAVE_LZMA
+/** Given <b>level</b> return the memory level. */
+static int
+memory_level(compression_level_t level)
+{
+  switch (level) {
+    default:
+    case HIGH_COMPRESSION: return 9;
+    case MEDIUM_COMPRESSION: return 6;
+    case LOW_COMPRESSION: return 3;
+  }
+}
+
 /** Convert a given <b>error</b> to a human readable error string. */
 static const char *
 lzma_error_str(lzma_ret error)
@@ -124,7 +136,7 @@ tor_lzma_compress(char **out, size_t *out_len,
   stream.avail_in = in_len;
 
   lzma_lzma_preset(&stream_options,
-                   tor_compress_memory_level(HIGH_COMPRESSION));
+                   memory_level(HIGH_COMPRESSION));
 
   retval = lzma_alone_encoder(&stream, &stream_options);
 
@@ -432,7 +444,7 @@ tor_lzma_compress_new(int compress,
 
   if (compress) {
     lzma_lzma_preset(&stream_options,
-                     tor_compress_memory_level(compression_level));
+                     memory_level(compression_level));
 
     retval = lzma_alone_encoder(&result->stream, &stream_options);
 
diff --git a/src/common/compress_zlib.c b/src/common/compress_zlib.c
index e1a68c2..50bdf9e 100644
--- a/src/common/compress_zlib.c
+++ b/src/common/compress_zlib.c
@@ -50,6 +50,18 @@ static size_t tor_zlib_state_size_precalc(int inflate,
 /** Total number of bytes allocated for zlib state */
 static size_t total_zlib_allocation = 0;
 
+/** Given <b>level</b> return the memory level. */
+static int
+memory_level(compression_level_t level)
+{
+  switch (level) {
+    default:
+    case HIGH_COMPRESSION: return 8;
+    case MEDIUM_COMPRESSION: return 7;
+    case LOW_COMPRESSION: return 6;
+  }
+}
+
 /** Return the 'bits' value to tell zlib to use <b>method</b>.*/
 static inline int
 method_bits(compress_method_t method, compression_level_t level)
@@ -120,7 +132,7 @@ tor_zlib_compress(char **out, size_t *out_len,
 
   if (deflateInit2(stream, Z_BEST_COMPRESSION, Z_DEFLATED,
                    method_bits(method, HIGH_COMPRESSION),
-                   tor_compress_memory_level(HIGH_COMPRESSION),
+                   memory_level(HIGH_COMPRESSION),
                    Z_DEFAULT_STRATEGY) != Z_OK) {
     //LCOV_EXCL_START -- we can only provoke failure by giving junk arguments.
     log_warn(LD_GENERAL, "Error from deflateInit2: %s",
@@ -413,7 +425,7 @@ tor_zlib_compress_new(int compress,
   out->stream.opaque = NULL;
   out->compress = compress;
   bits = method_bits(method, compression_level);
-  memlevel = tor_compress_memory_level(compression_level);
+  memlevel = memory_level(compression_level);
   if (compress) {
     if (deflateInit2(&out->stream, Z_BEST_COMPRESSION, Z_DEFLATED,
                      bits, memlevel,
diff --git a/src/common/compress_zstd.c b/src/common/compress_zstd.c
index c838cd9..664cce1 100644
--- a/src/common/compress_zstd.c
+++ b/src/common/compress_zstd.c
@@ -26,6 +26,20 @@
 /** Total number of bytes allocated for Zstandard state. */
 static size_t total_zstd_allocation = 0;
 
+#ifdef HAVE_ZSTD
+/** Given <b>level</b> return the memory level. */
+static int
+memory_level(compression_level_t level)
+{
+  switch (level) {
+    default:
+    case HIGH_COMPRESSION: return 9;
+    case MEDIUM_COMPRESSION: return 8;
+    case LOW_COMPRESSION: return 7;
+  }
+}
+#endif // HAVE_ZSTD.
+
 /** Return 1 if Zstandard compression is supported; otherwise 0. */
 int
 tor_zstd_method_supported(void)
@@ -104,7 +118,7 @@ tor_zstd_compress(char **out, size_t *out_len,
   }
 
   retval = ZSTD_initCStream(stream,
-                            tor_compress_memory_level(HIGH_COMPRESSION));
+                            memory_level(HIGH_COMPRESSION));
 
   if (ZSTD_isError(retval)) {
     log_warn(LD_GENERAL, "Zstandard stream initialization error: %s",
@@ -408,7 +422,7 @@ tor_zstd_compress_new(int compress,
     }
 
     retval = ZSTD_initCStream(result->u.compress_stream,
-                              tor_compress_memory_level(compression_level));
+                              memory_level(compression_level));
 
     if (ZSTD_isError(retval)) {
       log_warn(LD_GENERAL, "Zstandard stream initialization error: %s",





More information about the tor-commits mailing list