diff options
author | Eugene Kliuchnikov <eustas@google.com> | 2017-06-13 12:52:56 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-06-13 12:52:56 +0200 |
commit | 05d5f3d77a3445b464c230b382855979e1b89fef (patch) | |
tree | 452a38544c2107493ad0ff5cb92545991748f5ab /c/dec | |
parent | 0fceb906eceefde1548c2ba2b215cc4473ca5175 (diff) | |
download | brotli-05d5f3d77a3445b464c230b382855979e1b89fef.tar.gz |
Update (#560)
Update:
* add decoder API to avoid ringbuffer reallocation
* fix MSVC warnings
* remove dead code
Diffstat (limited to 'c/dec')
-rw-r--r-- | c/dec/decode.c | 33 | ||||
-rw-r--r-- | c/dec/state.c | 4 | ||||
-rw-r--r-- | c/dec/state.h | 3 |
3 files changed, 31 insertions, 9 deletions
diff --git a/c/dec/decode.c b/c/dec/decode.c index d3951f8..53a9b55 100644 --- a/c/dec/decode.c +++ b/c/dec/decode.c @@ -39,6 +39,11 @@ extern "C" { #define HUFFMAN_TABLE_BITS 8U #define HUFFMAN_TABLE_MASK 0xff +/* We need the slack region for the following reasons: + - doing up to two 16-byte copies for fast backward copying + - inserting transformed dictionary word (5 prefix + 24 base + 8 suffix) */ +static const uint32_t kRingBufferWriteAheadSlack = 42; + static const uint8_t kCodeLengthCodeOrder[BROTLI_CODE_LENGTH_CODES] = { 1, 2, 3, 4, 0, 5, 17, 6, 16, 7, 8, 9, 10, 11, 12, 13, 14, 15, }; @@ -52,6 +57,17 @@ static const uint8_t kCodeLengthPrefixValue[16] = { 0, 4, 3, 2, 0, 4, 3, 1, 0, 4, 3, 2, 0, 4, 3, 5, }; +BROTLI_BOOL BrotliDecoderSetParameter( + BrotliDecoderState* state, BrotliDecoderParameter p, uint32_t value) { + switch (p) { + case BROTLI_DECODER_PARAM_DISABLE_RING_BUFFER_REALLOCATION: + state->canny_ringbuffer_allocation = !!value ? 0 : 1; + return BROTLI_TRUE; + + default: return BROTLI_FALSE; + } +} + BrotliDecoderState* BrotliDecoderCreateInstance( brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque) { BrotliDecoderState* state = 0; @@ -1247,17 +1263,13 @@ static void BROTLI_NOINLINE WrapRingBuffer(BrotliDecoderState* s) { */ static BROTLI_BOOL BROTLI_NOINLINE BrotliEnsureRingBuffer( BrotliDecoderState* s) { - /* We need the slack region for the following reasons: - - doing up to two 16-byte copies for fast backward copying - - inserting transformed dictionary word (5 prefix + 24 base + 8 suffix) */ - static const int kRingBufferWriteAheadSlack = 42; uint8_t* old_ringbuffer = s->ringbuffer; if (s->ringbuffer_size == s->new_ringbuffer_size) { return BROTLI_TRUE; } - s->ringbuffer = (uint8_t*)BROTLI_ALLOC(s, (size_t)(s->new_ringbuffer_size + - kRingBufferWriteAheadSlack)); + s->ringbuffer = (uint8_t*)BROTLI_ALLOC(s, (size_t)(s->new_ringbuffer_size) + + kRingBufferWriteAheadSlack); if (s->ringbuffer == 0) { /* Restore previous value. */ s->ringbuffer = old_ringbuffer; @@ -1369,8 +1381,13 @@ static void BROTLI_NOINLINE BrotliCalculateRingBufferSize( output_size += s->meta_block_remaining_len; min_size = min_size < output_size ? output_size : min_size; - while ((new_ringbuffer_size >> 1) >= min_size) { - new_ringbuffer_size >>= 1; + if (!!s->canny_ringbuffer_allocation) { + /* Reduce ring buffer size to save memory when server is unscrupulous. + In worst case memory usage might be 1.5x bigger for a short period of + ring buffer reallocation.*/ + while ((new_ringbuffer_size >> 1) >= min_size) { + new_ringbuffer_size >>= 1; + } } s->new_ringbuffer_size = new_ringbuffer_size; diff --git a/c/dec/state.c b/c/dec/state.c index b7431f2..0db73fb 100644 --- a/c/dec/state.c +++ b/c/dec/state.c @@ -87,7 +87,11 @@ void BrotliDecoderStateInitWithCustomAllocators(BrotliDecoderState* s, s->custom_dict_size = 0; s->is_last_metablock = 0; + s->is_uncompressed = 0; + s->is_metadata = 0; s->should_wrap_ringbuffer = 0; + s->canny_ringbuffer_allocation = 1; + s->window_bits = 0; s->max_distance = 0; s->dist_rb[0] = 16; diff --git a/c/dec/state.h b/c/dec/state.h index 5946124..00c373b 100644 --- a/c/dec/state.h +++ b/c/dec/state.h @@ -172,7 +172,7 @@ struct BrotliDecoderStateStruct { uint32_t space; HuffmanCode table[32]; - /* List of of symbol chains. */ + /* List of heads of symbol chains. */ uint16_t* symbol_lists; /* Storage from symbol_lists. */ uint16_t symbols_lists_array[BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1 + @@ -215,6 +215,7 @@ struct BrotliDecoderStateStruct { unsigned int is_uncompressed : 1; unsigned int is_metadata : 1; unsigned int should_wrap_ringbuffer : 1; + unsigned int canny_ringbuffer_allocation : 1; unsigned int size_nibbles : 8; uint32_t window_bits; |