void BrotliHuffmanTreeGroupInit(BrotliState* s, HuffmanTreeGroup* group, uint32_t alphabet_size, uint32_t ntrees) { /* Pack two allocations into one */ const size_t code_size = sizeof(HuffmanCode) * (size_t)(ntrees * BROTLI_HUFFMAN_MAX_TABLE_SIZE); const size_t htree_size = sizeof(HuffmanCode*) * (size_t)ntrees; char *p = (char*)BROTLI_ALLOC(s, code_size + htree_size); group->alphabet_size = (uint16_t)alphabet_size; group->num_htrees = (uint16_t)ntrees; group->codes = (HuffmanCode*)p; group->htrees = (HuffmanCode**)(p + code_size); }
void BrotliHuffmanTreeGroupInit(BrotliState* s, HuffmanTreeGroup* group, uint32_t alphabet_size, uint32_t ntrees) { /* Pack two allocations into one */ 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 htree_size = sizeof(HuffmanCode*) * ntrees; char* p = (char*)BROTLI_ALLOC(s, code_size + htree_size); group->alphabet_size = (uint16_t)alphabet_size; group->num_htrees = (uint16_t)ntrees; group->codes = (HuffmanCode*)p; group->htrees = (HuffmanCode**)(p + code_size); }
void BrotliSplitBlock(MemoryManager* m, const Command* cmds, const size_t num_commands, const uint8_t* data, const size_t pos, const size_t mask, const BrotliEncoderParams* params, BlockSplit* literal_split, BlockSplit* insert_and_copy_split, BlockSplit* dist_split) { { size_t literals_count = CountLiterals(cmds, num_commands); uint8_t* literals = BROTLI_ALLOC(m, uint8_t, literals_count); if (BROTLI_IS_OOM(m)) return; /* Create a continuous array of literals. */ CopyLiteralsToByteArray(cmds, num_commands, data, pos, mask, literals); /* Create the block split on the array of literals. Literal histograms have alphabet size 256. */ SplitByteVectorLiteral( m, literals, literals_count, kSymbolsPerLiteralHistogram, kMaxLiteralHistograms, kLiteralStrideLength, kLiteralBlockSwitchCost, params, literal_split); if (BROTLI_IS_OOM(m)) return; BROTLI_FREE(m, literals); } { /* Compute prefix codes for commands. */ uint16_t* insert_and_copy_codes = BROTLI_ALLOC(m, uint16_t, num_commands); size_t i; if (BROTLI_IS_OOM(m)) return; for (i = 0; i < num_commands; ++i) { insert_and_copy_codes[i] = cmds[i].cmd_prefix_; } /* Create the block split on the array of command prefixes. */ SplitByteVectorCommand( m, insert_and_copy_codes, num_commands, kSymbolsPerCommandHistogram, kMaxCommandHistograms, kCommandStrideLength, kCommandBlockSwitchCost, params, insert_and_copy_split); if (BROTLI_IS_OOM(m)) return; /* TODO: reuse for distances? */ BROTLI_FREE(m, insert_and_copy_codes); } { /* Create a continuous array of distance prefixes. */ uint16_t* distance_prefixes = BROTLI_ALLOC(m, uint16_t, num_commands); size_t j = 0; size_t i; if (BROTLI_IS_OOM(m)) return; for (i = 0; i < num_commands; ++i) { const Command* cmd = &cmds[i]; if (CommandCopyLen(cmd) && cmd->cmd_prefix_ >= 128) { distance_prefixes[j++] = cmd->dist_prefix_; } } /* Create the block split on the array of distance prefixes. */ SplitByteVectorDistance( m, distance_prefixes, j, kSymbolsPerDistanceHistogram, kMaxCommandHistograms, kCommandStrideLength, kDistanceBlockSwitchCost, params, dist_split); if (BROTLI_IS_OOM(m)) return; BROTLI_FREE(m, distance_prefixes); } }