Debian Codegraph @

c/enc/compound_dictionary.c

raw symbols SCIP
1 /* Copyright 2017 Google Inc. All Rights Reserved.
2
3 Distributed under MIT license.
4 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
5 */
6
7 #include "compound_dictionary.h"cxx . . $ `<file>/c/enc/compound_dictionary.h`/
8
9 #include "../common/platform.h"cxx . . $ `<file>/c/common/platform.h`/
10 #include <brotli/shared_dictionary.h>cxx . . $ `<file>/c/include/brotli/shared_dictionary.h`/
11 #include "memory.h"cxx . . $ `<file>/c/enc/memory.h`/
12
13 static PreparedDictionarycxx . . $ PreparedDictionary#* CreatePreparedDictionaryWithParamscxx . . $ CreatePreparedDictionaryWithParams(1bc0787aa7f5aba3).(MemoryManagercxx . . $ MemoryManager#* m,
14 const uint8_tcxx . . $ uint8_t#* source, size_t source_size, uint32_tcxx . . $ uint32_t# bucket_bits,
15 uint32_tcxx . . $ uint32_t# slot_bits, uint32_tcxx . . $ uint32_t# hash_bits, uint16_tcxx . . $ uint16_t# bucket_limit) {
16 /* Step 1: create "bloated" hasher. */
17 uint32_tcxx . . $ uint32_t# num_slots = 1u << slot_bits;
18 uint32_tcxx . . $ uint32_t# num_buckets = 1u << bucket_bits;
19 uint32_tcxx . . $ uint32_t# hash_shift = 64u - bucket_bits;
20 uint64_tcxx . . $ uint64_t# hash_mask = (~((uint64_tcxx . . $ uint64_t#)0U)) >> (64 - hash_bits);
21 uint32_tcxx . . $ uint32_t# slot_mask = num_slots - 1;
22 size_t alloc_size = (sizeof(uint32_tcxx . . $ uint32_t#) << slot_bits) +
23 (sizeof(uint32_tcxx . . $ uint32_t#) << slot_bits) +
24 (sizeof(uint16_tcxx . . $ uint16_t#) << bucket_bits) +
25 (sizeof(uint32_tcxx . . $ uint32_t#) << bucket_bits) +
26 (sizeof(uint32_tcxx . . $ uint32_t#) * source_size);
27 uint8_tcxx . . $ uint8_t#* flat = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
28 PreparedDictionarycxx . . $ PreparedDictionary#* result = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
29 uint16_tcxx . . $ uint16_t#* num = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
30 uint32_tcxx . . $ uint32_t#* bucket_heads = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
31 uint32_tcxx . . $ uint32_t#* next_bucket = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
32 uint32_tcxx . . $ uint32_t#* slot_offsets = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
33 uint16_tcxx . . $ uint16_t#* heads = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
34 uint32_tcxx . . $ uint32_t#* items = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
35 uint8_tcxx . . $ uint8_t#** source_ref = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
36 uint32_tcxx . . $ uint32_t# i;
37 uint32_tcxx . . $ uint32_t#* slot_size = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
38 uint32_tcxx . . $ uint32_t#* slot_limit = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
39 uint32_tcxx . . $ uint32_t# total_items = 0;
40 if (slot_bits > 16) return NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
41 if (slot_bits > bucket_bits) return NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
42 if (bucket_bits - slot_bits >= 16) return NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
43
44 flat = BROTLI_ALLOCcxx . . $ `c/enc/memory.h:51:9`!(m, uint8_t, alloc_size);
45 if (BROTLI_IS_OOMcxx . . $ `c/enc/memory.h:61:9`!(m) || BROTLI_IS_NULLcxx . . $ `c/enc/memory.h:75:9`!(flat)) return NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
46
47 slot_size = (uint32_tcxx . . $ uint32_t#*)flat;
48 slot_limit = (uint32_tcxx . . $ uint32_t#*)(&slot_size[num_slots]);
49 num = (uint16_tcxx . . $ uint16_t#*)(&slot_limit[num_slots]);
50 bucket_heads = (uint32_tcxx . . $ uint32_t#*)(&num[num_buckets]);
51 next_bucket = (uint32_tcxx . . $ uint32_t#*)(&bucket_heads[num_buckets]);
52 memsetcxx . . $ memset(d1de1e4fb215f1c6).(num, 0, num_buckets * sizeof(num[0]));
53
54 /* TODO(eustas): apply custom "store" order. */
55 for (i = 0; i + 7 < source_size; ++i) {
56 const uint64_tcxx . . $ uint64_t# h = (BROTLI_UNALIGNED_LOAD64LEcxx . . $ `c/common/platform.h:380:9`!(&source[i]) & hash_mask) *
57 kPreparedDictionaryHashMul64Longcxx . . $ kPreparedDictionaryHashMul64Long.;
58 const uint32_tcxx . . $ uint32_t# key = (uint32_tcxx . . $ uint32_t#)(h >> hash_shift);
59 uint16_tcxx . . $ uint16_t# count = num[key];
60 next_bucket[i] = (count == 0) ? ((uint32_tcxx . . $ uint32_t#)(-1)) : bucket_heads[key];
61 bucket_heads[key] = i;
62 count++;
63 if (count > bucket_limit) count = bucket_limit;
64 num[key] = count;
65 }
66
67 /* Step 2: find slot limits. */
68 for (i = 0; i < num_slots; ++i) {
69 BROTLI_BOOLcxx . . $ `c/include/brotli/types.h:49:9`! overflow = BROTLI_FALSEcxx . . $ `c/include/brotli/types.h:53:9`!;
70 slot_limit[i] = bucket_limit;
71 while (BROTLI_TRUEcxx . . $ `c/include/brotli/types.h:51:9`!) {
72 uint32_tcxx . . $ uint32_t# limit = slot_limit[i];
73 size_t j;
74 uint32_tcxx . . $ uint32_t# count = 0;
75 overflow = BROTLI_FALSEcxx . . $ `c/include/brotli/types.h:53:9`!;
76 for (j = i; j < num_buckets; j += num_slots) {
77 uint32_tcxx . . $ uint32_t# size = num[j];
78 /* Last chain may span behind 64K limit; overflow happens only if
79 we are about to use 0xFFFF+ as item offset. */
80 if (count >= 0xFFFF) {
81 overflow = BROTLI_TRUEcxx . . $ `c/include/brotli/types.h:51:9`!;
82 break;
83 }
84 if (size > limit) size = limit;
85 count += size;
86 }
87 if (!overflow) {
88 slot_size[i] = count;
89 total_items += count;
90 break;
91 }
92 slot_limit[i]--;
93 }
94 }
95
96 /* Step 3: transfer data to "slim" hasher. */
97 alloc_size = sizeof(PreparedDictionarycxx . . $ PreparedDictionary#) + (sizeof(uint32_tcxx . . $ uint32_t#) << slot_bits) +
98 (sizeof(uint16_tcxx . . $ uint16_t#) << bucket_bits) + (sizeof(uint32_tcxx . . $ uint32_t#) * total_items) +
99 sizeof(uint8_tcxx . . $ uint8_t#*);
100
101 result = (PreparedDictionarycxx . . $ PreparedDictionary#*)BROTLI_ALLOCcxx . . $ `c/enc/memory.h:51:9`!(m, uint8_t, alloc_size);
102 if (BROTLI_IS_OOMcxx . . $ `c/enc/memory.h:61:9`!(m) || BROTLI_IS_NULLcxx . . $ `c/enc/memory.h:75:9`!(result)) {
103 BROTLI_FREEcxx . . $ `c/enc/memory.h:55:9`!(m, flat);
104 return NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
105 }
106 slot_offsets = (uint32_tcxx . . $ uint32_t#*)(&result[1]);
107 heads = (uint16_tcxx . . $ uint16_t#*)(&slot_offsets[num_slots]);
108 items = (uint32_tcxx . . $ uint32_t#*)(&heads[num_buckets]);
109 source_ref = (uint8_tcxx . . $ uint8_t#**)(&items[total_items]);
110
111 result->magiccxx . . $ PreparedDictionary#magic. = kLeanPreparedDictionaryMagiccxx . . $ kLeanPreparedDictionaryMagic.;
112 result->num_itemscxx . . $ PreparedDictionary#num_items. = total_items;
113 result->source_sizecxx . . $ PreparedDictionary#source_size. = (uint32_tcxx . . $ uint32_t#)source_size;
114 result->hash_bitscxx . . $ PreparedDictionary#hash_bits. = hash_bits;
115 result->bucket_bitscxx . . $ PreparedDictionary#bucket_bits. = bucket_bits;
116 result->slot_bitscxx . . $ PreparedDictionary#slot_bits. = slot_bits;
117 BROTLI_UNALIGNED_STORE_PTRcxx . . $ BROTLI_UNALIGNED_STORE_PTR(b6570c89ea41c2e0).(source_ref, source);
118
119 total_items = 0;
120 for (i = 0; i < num_slots; ++i) {
121 slot_offsets[i] = total_items;
122 total_items += slot_size[i];
123 slot_size[i] = 0;
124 }
125 for (i = 0; i < num_buckets; ++i) {
126 uint32_tcxx . . $ uint32_t# slot = i & slot_mask;
127 uint32_tcxx . . $ uint32_t# count = num[i];
128 uint32_tcxx . . $ uint32_t# pos;
129 size_t j;
130 size_t cursor = slot_size[slot];
131 if (count > slot_limit[slot]) count = slot_limit[slot];
132 if (count == 0) {
133 heads[i] = 0xFFFF;
134 continue;
135 }
136 heads[i] = (uint16_tcxx . . $ uint16_t#)cursor;
137 cursor += slot_offsets[slot];
138 slot_size[slot] += count;
139 pos = bucket_heads[i];
140 for (j = 0; j < count; j++) {
141 items[cursor++] = pos;
142 pos = next_bucket[pos];
143 }
144 items[cursor - 1] |= 0x80000000;
145 }
146
147 BROTLI_FREEcxx . . $ `c/enc/memory.h:55:9`!(m, flat);
148 return result;
149 }
150
151 PreparedDictionarycxx . . $ PreparedDictionary#* CreatePreparedDictionarycxx . . $ CreatePreparedDictionary(27baae329f958095).(MemoryManagercxx . . $ MemoryManager#* m,
152 const uint8_tcxx . . $ uint8_t#* source, size_t source_size) {
153 uint32_tcxx . . $ uint32_t# bucket_bits = 17;
154 uint32_tcxx . . $ uint32_t# slot_bits = 7;
155 uint32_tcxx . . $ uint32_t# hash_bits = 40;
156 uint16_tcxx . . $ uint16_t# bucket_limit = 32;
157 size_t volume = 16u << bucket_bits;
158 /* Tune parameters to fit dictionary size. */
159 while (volume < source_size && bucket_bits < 22) {
160 bucket_bits++;
161 slot_bits++;
162 volume <<= 1;
163 }
164 return CreatePreparedDictionaryWithParamscxx . . $ CreatePreparedDictionaryWithParams(1bc0787aa7f5aba3).(m,
165 source, source_size, bucket_bits, slot_bits, hash_bits, bucket_limit);
166 }
167
168 void DestroyPreparedDictionarycxx . . $ DestroyPreparedDictionary(1f924a578e1c6a26).(MemoryManagercxx . . $ MemoryManager#* m,
169 PreparedDictionarycxx . . $ PreparedDictionary#* dictionary) {
170 if (!dictionary) return;
171 BROTLI_FREEcxx . . $ `c/enc/memory.h:55:9`!(m, dictionary);
172 }
173
174 BROTLI_BOOLcxx . . $ `c/include/brotli/types.h:49:9`! AttachPreparedDictionarycxx . . $ AttachPreparedDictionary(8328b58aaec025ca).(
175 CompoundDictionarycxx . . $ CompoundDictionary#* compound, const PreparedDictionarycxx . . $ PreparedDictionary#* dictionary) {
176 size_t length = 0;
177 size_t index = 0;
178
179 if (compound->num_chunkscxx . . $ CompoundDictionary#num_chunks. == SHARED_BROTLI_MAX_COMPOUND_DICTScxx . . $ `c/include/brotli/shared_dictionary.h:22:9`!) {
180 return BROTLI_FALSEcxx . . $ `c/include/brotli/types.h:53:9`!;
181 }
182
183 if (!dictionary) return BROTLI_FALSEcxx . . $ `c/include/brotli/types.h:53:9`!;
184
185 length = dictionary->source_sizecxx . . $ PreparedDictionary#source_size.;
186 index = compound->num_chunkscxx . . $ CompoundDictionary#num_chunks.;
187 compound->total_sizecxx . . $ CompoundDictionary#total_size. += length;
188 compound->chunkscxx . . $ CompoundDictionary#chunks.[index] = dictionary;
189 compound->chunk_offsetscxx . . $ CompoundDictionary#chunk_offsets.[index + 1] = compound->total_sizecxx . . $ CompoundDictionary#total_size.;
190 {
191 uint32_tcxx . . $ uint32_t#* slot_offsets = (uint32_tcxx . . $ uint32_t#*)(&dictionary[1]);
192 uint16_tcxx . . $ uint16_t#* heads = (uint16_tcxx . . $ uint16_t#*)(&slot_offsets[(size_t)1u << dictionary->slot_bitscxx . . $ PreparedDictionary#slot_bits.]);
193 uint32_tcxx . . $ uint32_t#* items = (uint32_tcxx . . $ uint32_t#*)(&heads[(size_t)1u << dictionary->bucket_bitscxx . . $ PreparedDictionary#bucket_bits.]);
194 const void* tail = (void*)&items[dictionary->num_itemscxx . . $ PreparedDictionary#num_items.];
195 if (dictionary->magiccxx . . $ PreparedDictionary#magic. == kPreparedDictionaryMagiccxx . . $ kPreparedDictionaryMagic.) {
196 compound->chunk_sourcecxx . . $ CompoundDictionary#chunk_source.[index] = (const uint8_tcxx . . $ uint8_t#*)tail;
197 } else {
198 /* dictionary->magic == kLeanPreparedDictionaryMagic */
199 compound->chunk_sourcecxx . . $ CompoundDictionary#chunk_source.[index] =
200 (const uint8_tcxx . . $ uint8_t#*)BROTLI_UNALIGNED_LOAD_PTRcxx . . $ BROTLI_UNALIGNED_LOAD_PTR(3343d82129b43479).((const uint8_tcxx . . $ uint8_t#**)tail);
201 }
202 }
203 compound->num_chunkscxx . . $ CompoundDictionary#num_chunks.++;
204 return BROTLI_TRUEcxx . . $ `c/include/brotli/types.h:51:9`!;
205 }