Fix `-Wcast-align` warnings

This commit is contained in:
Eugene Kliuchnikov 2016-10-19 16:27:57 +02:00
parent 74147a1a41
commit b1db6f149a
3 changed files with 25 additions and 23 deletions

View File

@ -851,37 +851,38 @@ static BROTLI_INLINE BROTLI_BOOL SafeReadBlockLength(
static BROTLI_NOINLINE void InverseMoveToFrontTransform( static BROTLI_NOINLINE void InverseMoveToFrontTransform(
uint8_t* v, uint32_t v_len, BrotliDecoderState* state) { uint8_t* v, uint32_t v_len, BrotliDecoderState* state) {
/* Reinitialize elements that could have been changed. */ /* Reinitialize elements that could have been changed. */
uint32_t i = 4; uint32_t i = 1;
uint32_t upper_bound = state->mtf_upper_bound; uint32_t upper_bound = state->mtf_upper_bound;
uint8_t* mtf = &state->mtf[4]; /* Make mtf[-1] addressable. */ uint32_t* mtf = &state->mtf[1]; /* Make mtf[-1] addressable. */
uint8_t* mtf_u8 = (uint8_t*)mtf;
/* Load endian-aware constant. */ /* Load endian-aware constant. */
const uint8_t b0123[4] = {0, 1, 2, 3}; const uint8_t b0123[4] = {0, 1, 2, 3};
uint32_t pattern; uint32_t pattern;
memcpy(&pattern, &b0123, 4); memcpy(&pattern, &b0123, 4);
/* Initialize list using 4 consequent values pattern. */ /* Initialize list using 4 consequent values pattern. */
*(uint32_t*)mtf = pattern; mtf[0] = pattern;
do { do {
pattern += 0x04040404; /* Advance all 4 values by 4. */ pattern += 0x04040404; /* Advance all 4 values by 4. */
*(uint32_t*)(mtf + i) = pattern; mtf[i] = pattern;
i += 4; i++;
} while (i <= upper_bound); } while (i <= upper_bound);
/* Transform the input. */ /* Transform the input. */
upper_bound = 0; upper_bound = 0;
for (i = 0; i < v_len; ++i) { for (i = 0; i < v_len; ++i) {
int index = v[i]; int index = v[i];
uint8_t value = mtf[index]; uint8_t value = mtf_u8[index];
upper_bound |= v[i]; upper_bound |= v[i];
v[i] = value; v[i] = value;
mtf[-1] = value; mtf_u8[-1] = value;
do { do {
index--; index--;
mtf[index + 1] = mtf[index]; mtf_u8[index + 1] = mtf_u8[index];
} while (index >= 0); } while (index >= 0);
} }
/* Remember amount of elements to be reinitialized. */ /* Remember amount of elements to be reinitialized. */
state->mtf_upper_bound = upper_bound; state->mtf_upper_bound = upper_bound >> 2;
} }
/* Decodes a series of Huffman table using ReadHuffmanCode function. */ /* Decodes a series of Huffman table using ReadHuffmanCode function. */
@ -2142,24 +2143,23 @@ BrotliDecoderResult BrotliDecoderDecompressStream(
{ {
uint32_t num_distance_codes = s->num_direct_distance_codes + uint32_t num_distance_codes = s->num_direct_distance_codes +
((2 * BROTLI_MAX_DISTANCE_BITS) << s->distance_postfix_bits); ((2 * BROTLI_MAX_DISTANCE_BITS) << s->distance_postfix_bits);
BROTLI_BOOL allocation_success = BROTLI_TRUE;
result = DecodeContextMap( result = DecodeContextMap(
s->num_block_types[2] << BROTLI_DISTANCE_CONTEXT_BITS, s->num_block_types[2] << BROTLI_DISTANCE_CONTEXT_BITS,
&s->num_dist_htrees, &s->dist_context_map, s); &s->num_dist_htrees, &s->dist_context_map, s);
if (result != BROTLI_DECODER_SUCCESS) { if (result != BROTLI_DECODER_SUCCESS) {
break; break;
} }
BrotliDecoderHuffmanTreeGroupInit( allocation_success &= BrotliDecoderHuffmanTreeGroupInit(
s, &s->literal_hgroup, BROTLI_NUM_LITERAL_SYMBOLS, s, &s->literal_hgroup, BROTLI_NUM_LITERAL_SYMBOLS,
s->num_literal_htrees); s->num_literal_htrees);
BrotliDecoderHuffmanTreeGroupInit( allocation_success &= BrotliDecoderHuffmanTreeGroupInit(
s, &s->insert_copy_hgroup, BROTLI_NUM_COMMAND_SYMBOLS, s, &s->insert_copy_hgroup, BROTLI_NUM_COMMAND_SYMBOLS,
s->num_block_types[1]); s->num_block_types[1]);
BrotliDecoderHuffmanTreeGroupInit( allocation_success &= BrotliDecoderHuffmanTreeGroupInit(
s, &s->distance_hgroup, num_distance_codes, s, &s->distance_hgroup, num_distance_codes,
s->num_dist_htrees); s->num_dist_htrees);
if (s->literal_hgroup.codes == 0 || if (!allocation_success) {
s->insert_copy_hgroup.codes == 0 ||
s->distance_hgroup.codes == 0) {
return SaveErrorCode(s, return SaveErrorCode(s,
BROTLI_FAILURE(BROTLI_DECODER_ERROR_ALLOC_TREE_GROUPS)); BROTLI_FAILURE(BROTLI_DECODER_ERROR_ALLOC_TREE_GROUPS));
} }

View File

@ -97,7 +97,7 @@ void BrotliDecoderStateInitWithCustomAllocators(BrotliDecoderState* s,
/* Make small negative indexes addressable. */ /* Make small negative indexes addressable. */
s->symbol_lists = &s->symbols_lists_array[BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1]; s->symbol_lists = &s->symbols_lists_array[BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1];
s->mtf_upper_bound = 255; s->mtf_upper_bound = 63;
} }
void BrotliDecoderStateMetablockBegin(BrotliDecoderState* s) { void BrotliDecoderStateMetablockBegin(BrotliDecoderState* s) {
@ -148,22 +148,24 @@ void BrotliDecoderStateCleanup(BrotliDecoderState* s) {
BROTLI_FREE(s, s->block_type_trees); BROTLI_FREE(s, s->block_type_trees);
} }
void BrotliDecoderHuffmanTreeGroupInit(BrotliDecoderState* s, BROTLI_BOOL BrotliDecoderHuffmanTreeGroupInit(BrotliDecoderState* s,
HuffmanTreeGroup* group, uint32_t alphabet_size, uint32_t ntrees) { HuffmanTreeGroup* group, uint32_t alphabet_size, uint32_t ntrees) {
/* Pack two allocations into one */ /* Pack two allocations into one */
const size_t max_table_size = kMaxHuffmanTableSize[(alphabet_size + 31) >> 5]; const size_t max_table_size = kMaxHuffmanTableSize[(alphabet_size + 31) >> 5];
const size_t code_size = sizeof(HuffmanCode) * ntrees * max_table_size; const size_t code_size = sizeof(HuffmanCode) * ntrees * max_table_size;
const size_t htree_size = sizeof(HuffmanCode*) * ntrees; const size_t htree_size = sizeof(HuffmanCode*) * ntrees;
char* p = (char*)BROTLI_ALLOC(s, code_size + htree_size); /* Pointer alignment is, hopefully, wider than sizeof(HuffmanCode). */
HuffmanCode** p = (HuffmanCode**)BROTLI_ALLOC(s, code_size + htree_size);
group->alphabet_size = (uint16_t)alphabet_size; group->alphabet_size = (uint16_t)alphabet_size;
group->num_htrees = (uint16_t)ntrees; group->num_htrees = (uint16_t)ntrees;
group->codes = (HuffmanCode*)p; group->htrees = (HuffmanCode**)p;
group->htrees = (HuffmanCode**)(p + code_size); group->codes = (HuffmanCode*)(&p[ntrees]);
return !!p;
} }
void BrotliDecoderHuffmanTreeGroupRelease( void BrotliDecoderHuffmanTreeGroupRelease(
BrotliDecoderState* s, HuffmanTreeGroup* group) { BrotliDecoderState* s, HuffmanTreeGroup* group) {
BROTLI_FREE(s, group->codes); BROTLI_FREE(s, group->htrees);
group->htrees = NULL; group->htrees = NULL;
} }

View File

@ -192,7 +192,7 @@ struct BrotliDecoderStateStruct {
/* For InverseMoveToFrontTransform */ /* For InverseMoveToFrontTransform */
uint32_t mtf_upper_bound; uint32_t mtf_upper_bound;
uint8_t mtf[256 + 4]; uint32_t mtf[64 + 1];
/* For custom dictionaries */ /* For custom dictionaries */
const uint8_t* custom_dict; const uint8_t* custom_dict;
@ -235,7 +235,7 @@ BROTLI_INTERNAL void BrotliDecoderStateCleanup(BrotliDecoderState* s);
BROTLI_INTERNAL void BrotliDecoderStateMetablockBegin(BrotliDecoderState* s); BROTLI_INTERNAL void BrotliDecoderStateMetablockBegin(BrotliDecoderState* s);
BROTLI_INTERNAL void BrotliDecoderStateCleanupAfterMetablock( BROTLI_INTERNAL void BrotliDecoderStateCleanupAfterMetablock(
BrotliDecoderState* s); BrotliDecoderState* s);
BROTLI_INTERNAL void BrotliDecoderHuffmanTreeGroupInit( BROTLI_INTERNAL BROTLI_BOOL BrotliDecoderHuffmanTreeGroupInit(
BrotliDecoderState* s, HuffmanTreeGroup* group, uint32_t alphabet_size, BrotliDecoderState* s, HuffmanTreeGroup* group, uint32_t alphabet_size,
uint32_t ntrees); uint32_t ntrees);
BROTLI_INTERNAL void BrotliDecoderHuffmanTreeGroupRelease( BROTLI_INTERNAL void BrotliDecoderHuffmanTreeGroupRelease(