1use super::cluster::HistogramPair;
2use super::command::Command;
3use super::histogram::{ContextType, HistogramCommand, HistogramDistance, HistogramLiteral};
4use super::interface::StaticCommand;
5use super::s16;
6use super::util::floatX;
7use super::v8;
8use super::PDF;
9pub use alloc::Allocator;
10
11use super::entropy_encode::HuffmanTree;
12use super::hash_to_binary_tree::ZopfliNode;
13#[cfg(feature = "std")]
14use alloc_stdlib::StandardAlloc;
15pub trait BrotliAlloc:
35 Allocator<u8>
36 + Allocator<u16>
37 + Allocator<i32>
38 + Allocator<u32>
39 + Allocator<u64>
40 + Allocator<Command>
41 + Allocator<super::util::floatX>
42 + Allocator<v8>
43 + Allocator<s16>
44 + Allocator<PDF>
45 + Allocator<StaticCommand>
46 + Allocator<HistogramLiteral>
47 + Allocator<HistogramCommand>
48 + Allocator<HistogramDistance>
49 + Allocator<HistogramPair>
50 + Allocator<ContextType>
51 + Allocator<HuffmanTree>
52 + Allocator<ZopfliNode>
53{
54}
55
56#[cfg(feature = "std")]
57impl BrotliAlloc for StandardAlloc {}
58
59pub struct CombiningAllocator<
60 AllocU8: Allocator<u8>,
61 AllocU16: Allocator<u16>,
62 AllocI32: Allocator<i32>,
63 AllocU32: Allocator<u32>,
64 AllocU64: Allocator<u64>,
65 AllocCommand: Allocator<Command>,
66 AllocFloatX: Allocator<floatX>,
67 AllocV8: Allocator<v8>,
68 AllocS16: Allocator<s16>,
69 AllocPDF: Allocator<PDF>,
70 AllocStaticCommand: Allocator<StaticCommand>,
71 AllocHistogramLiteral: Allocator<HistogramLiteral>,
72 AllocHistogramCommand: Allocator<HistogramCommand>,
73 AllocHistogramDistance: Allocator<HistogramDistance>,
74 AllocHistogramPair: Allocator<HistogramPair>,
75 AllocContextType: Allocator<ContextType>,
76 AllocHuffmanTree: Allocator<HuffmanTree>,
77 AllocZopfliNode: Allocator<ZopfliNode>,
78> {
79 alloc_u8: AllocU8,
80 alloc_u16: AllocU16,
81 alloc_i32: AllocI32,
82 alloc_u32: AllocU32,
83 alloc_u64: AllocU64,
84 alloc_c: AllocCommand,
85 alloc_f: AllocFloatX,
86 alloc_f32x8: AllocV8,
87 alloc_i16x16: AllocS16,
88 alloc_pdf: AllocPDF,
89 alloc_sc: AllocStaticCommand,
90 alloc_hl: AllocHistogramLiteral,
91 alloc_hc: AllocHistogramCommand,
92 alloc_hd: AllocHistogramDistance,
93 alloc_hp: AllocHistogramPair,
94 alloc_ct: AllocContextType,
95 alloc_ht: AllocHuffmanTree,
96 alloc_zn: AllocZopfliNode,
97}
98
99impl<
100 AllocU8: Allocator<u8>,
101 AllocU16: Allocator<u16>,
102 AllocI32: Allocator<i32>,
103 AllocU32: Allocator<u32>,
104 AllocU64: Allocator<u64>,
105 AllocCommand: Allocator<Command>,
106 AllocFloatX: Allocator<floatX>,
107 AllocV8: Allocator<v8>,
108 AllocS16: Allocator<s16>,
109 AllocPDF: Allocator<PDF>,
110 AllocStaticCommand: Allocator<StaticCommand>,
111 AllocHistogramLiteral: Allocator<HistogramLiteral>,
112 AllocHistogramCommand: Allocator<HistogramCommand>,
113 AllocHistogramDistance: Allocator<HistogramDistance>,
114 AllocHistogramPair: Allocator<HistogramPair>,
115 AllocContextType: Allocator<ContextType>,
116 AllocHuffmanTree: Allocator<HuffmanTree>,
117 AllocZopfliNode: Allocator<ZopfliNode>,
118 >
119 CombiningAllocator<
120 AllocU8,
121 AllocU16,
122 AllocI32,
123 AllocU32,
124 AllocU64,
125 AllocCommand,
126 AllocFloatX,
127 AllocV8,
128 AllocS16,
129 AllocPDF,
130 AllocStaticCommand,
131 AllocHistogramLiteral,
132 AllocHistogramCommand,
133 AllocHistogramDistance,
134 AllocHistogramPair,
135 AllocContextType,
136 AllocHuffmanTree,
137 AllocZopfliNode,
138 >
139{
140 pub fn new(
141 alloc_u8: AllocU8,
142 alloc_u16: AllocU16,
143 alloc_i32: AllocI32,
144 alloc_u32: AllocU32,
145 alloc_u64: AllocU64,
146 alloc_c: AllocCommand,
147 alloc_f: AllocFloatX,
148 alloc_f32x8: AllocV8,
149 alloc_i16x16: AllocS16,
150 alloc_pdf: AllocPDF,
151 alloc_sc: AllocStaticCommand,
152 alloc_hl: AllocHistogramLiteral,
153 alloc_hc: AllocHistogramCommand,
154 alloc_hd: AllocHistogramDistance,
155 alloc_hp: AllocHistogramPair,
156 alloc_ct: AllocContextType,
157 alloc_ht: AllocHuffmanTree,
158 alloc_zn: AllocZopfliNode,
159 ) -> Self {
160 CombiningAllocator {
161 alloc_u8,
162 alloc_u16,
163 alloc_i32,
164 alloc_u32,
165 alloc_u64,
166 alloc_c,
167 alloc_f,
168 alloc_f32x8,
169 alloc_i16x16,
170 alloc_pdf,
171 alloc_sc,
172 alloc_hl,
173 alloc_hc,
174 alloc_hd,
175 alloc_hp,
176 alloc_ct,
177 alloc_ht,
178 alloc_zn,
179 }
180 }
181}
182
183impl<
184 AllocU8: Allocator<u8>,
185 AllocU16: Allocator<u16>,
186 AllocI32: Allocator<i32>,
187 AllocU32: Allocator<u32>,
188 AllocU64: Allocator<u64>,
189 AllocCommand: Allocator<Command>,
190 AllocFloatX: Allocator<floatX>,
191 AllocV8: Allocator<v8>,
192 AllocS16: Allocator<s16>,
193 AllocPDF: Allocator<PDF>,
194 AllocStaticCommand: Allocator<StaticCommand>,
195 AllocHistogramLiteral: Allocator<HistogramLiteral>,
196 AllocHistogramCommand: Allocator<HistogramCommand>,
197 AllocHistogramDistance: Allocator<HistogramDistance>,
198 AllocHistogramPair: Allocator<HistogramPair>,
199 AllocContextType: Allocator<ContextType>,
200 AllocHuffmanTree: Allocator<HuffmanTree>,
201 AllocZopfliNode: Allocator<ZopfliNode>,
202 > BrotliAlloc
203 for CombiningAllocator<
204 AllocU8,
205 AllocU16,
206 AllocI32,
207 AllocU32,
208 AllocU64,
209 AllocCommand,
210 AllocFloatX,
211 AllocV8,
212 AllocS16,
213 AllocPDF,
214 AllocStaticCommand,
215 AllocHistogramLiteral,
216 AllocHistogramCommand,
217 AllocHistogramDistance,
218 AllocHistogramPair,
219 AllocContextType,
220 AllocHuffmanTree,
221 AllocZopfliNode,
222 >
223{
224}
225
226impl<
227 AllocU8: Allocator<u8> + Default,
228 AllocU16: Allocator<u16> + Default,
229 AllocI32: Allocator<i32> + Default,
230 AllocU32: Allocator<u32> + Default,
231 AllocU64: Allocator<u64> + Default,
232 AllocCommand: Allocator<Command> + Default,
233 AllocFloatX: Allocator<floatX> + Default,
234 AllocV8: Allocator<v8> + Default,
235 AllocS16: Allocator<s16> + Default,
236 AllocPDF: Allocator<PDF> + Default,
237 AllocStaticCommand: Allocator<StaticCommand> + Default,
238 AllocHistogramLiteral: Allocator<HistogramLiteral> + Default,
239 AllocHistogramCommand: Allocator<HistogramCommand> + Default,
240 AllocHistogramDistance: Allocator<HistogramDistance> + Default,
241 AllocHistogramPair: Allocator<HistogramPair> + Default,
242 AllocContextType: Allocator<ContextType> + Default,
243 AllocHuffmanTree: Allocator<HuffmanTree> + Default,
244 AllocZopfliNode: Allocator<ZopfliNode> + Default,
245 > Default
246 for CombiningAllocator<
247 AllocU8,
248 AllocU16,
249 AllocI32,
250 AllocU32,
251 AllocU64,
252 AllocCommand,
253 AllocFloatX,
254 AllocV8,
255 AllocS16,
256 AllocPDF,
257 AllocStaticCommand,
258 AllocHistogramLiteral,
259 AllocHistogramCommand,
260 AllocHistogramDistance,
261 AllocHistogramPair,
262 AllocContextType,
263 AllocHuffmanTree,
264 AllocZopfliNode,
265 >
266{
267 fn default() -> Self {
268 CombiningAllocator {
269 alloc_u8: AllocU8::default(),
270 alloc_u16: AllocU16::default(),
271 alloc_i32: AllocI32::default(),
272 alloc_u32: AllocU32::default(),
273 alloc_u64: AllocU64::default(),
274 alloc_c: AllocCommand::default(),
275 alloc_f: AllocFloatX::default(),
276 alloc_f32x8: AllocV8::default(),
277 alloc_i16x16: AllocS16::default(),
278 alloc_pdf: AllocPDF::default(),
279 alloc_sc: AllocStaticCommand::default(),
280 alloc_hl: AllocHistogramLiteral::default(),
281 alloc_hc: AllocHistogramCommand::default(),
282 alloc_hd: AllocHistogramDistance::default(),
283 alloc_hp: AllocHistogramPair::default(),
284 alloc_ct: AllocContextType::default(),
285 alloc_ht: AllocHuffmanTree::default(),
286 alloc_zn: AllocZopfliNode::default(),
287 }
288 }
289}
290
291impl<
292 AllocU8: Allocator<u8> + Clone,
293 AllocU16: Allocator<u16> + Clone,
294 AllocI32: Allocator<i32> + Clone,
295 AllocU32: Allocator<u32> + Clone,
296 AllocU64: Allocator<u64> + Clone,
297 AllocCommand: Allocator<Command> + Clone,
298 AllocFloatX: Allocator<floatX> + Clone,
299 AllocV8: Allocator<v8> + Clone,
300 AllocS16: Allocator<s16> + Clone,
301 AllocPDF: Allocator<PDF> + Clone,
302 AllocStaticCommand: Allocator<StaticCommand> + Clone,
303 AllocHistogramLiteral: Allocator<HistogramLiteral> + Clone,
304 AllocHistogramCommand: Allocator<HistogramCommand> + Clone,
305 AllocHistogramDistance: Allocator<HistogramDistance> + Clone,
306 AllocHistogramPair: Allocator<HistogramPair> + Clone,
307 AllocContextType: Allocator<ContextType> + Clone,
308 AllocHuffmanTree: Allocator<HuffmanTree> + Clone,
309 AllocZopfliNode: Allocator<ZopfliNode> + Clone,
310 > Clone
311 for CombiningAllocator<
312 AllocU8,
313 AllocU16,
314 AllocI32,
315 AllocU32,
316 AllocU64,
317 AllocCommand,
318 AllocFloatX,
319 AllocV8,
320 AllocS16,
321 AllocPDF,
322 AllocStaticCommand,
323 AllocHistogramLiteral,
324 AllocHistogramCommand,
325 AllocHistogramDistance,
326 AllocHistogramPair,
327 AllocContextType,
328 AllocHuffmanTree,
329 AllocZopfliNode,
330 >
331{
332 fn clone(&self) -> Self {
333 CombiningAllocator {
334 alloc_u8: self.alloc_u8.clone(),
335 alloc_u16: self.alloc_u16.clone(),
336 alloc_i32: self.alloc_i32.clone(),
337 alloc_u32: self.alloc_u32.clone(),
338 alloc_u64: self.alloc_u64.clone(),
339 alloc_c: self.alloc_c.clone(),
340 alloc_f: self.alloc_f.clone(),
341 alloc_f32x8: self.alloc_f32x8.clone(),
342 alloc_i16x16: self.alloc_i16x16.clone(),
343 alloc_pdf: self.alloc_pdf.clone(),
344 alloc_sc: self.alloc_sc.clone(),
345 alloc_hl: self.alloc_hl.clone(),
346 alloc_hc: self.alloc_hc.clone(),
347 alloc_hd: self.alloc_hd.clone(),
348 alloc_hp: self.alloc_hp.clone(),
349 alloc_ct: self.alloc_ct.clone(),
350 alloc_ht: self.alloc_ht.clone(),
351 alloc_zn: self.alloc_zn.clone(),
352 }
353 }
354}
355
356impl<
357 AllocU8: Allocator<u8> + Copy,
358 AllocU16: Allocator<u16> + Copy,
359 AllocI32: Allocator<i32> + Copy,
360 AllocU32: Allocator<u32> + Copy,
361 AllocU64: Allocator<u64> + Copy,
362 AllocCommand: Allocator<Command> + Copy,
363 AllocFloatX: Allocator<floatX> + Copy,
364 AllocV8: Allocator<v8> + Copy,
365 AllocS16: Allocator<s16> + Copy,
366 AllocPDF: Allocator<PDF> + Copy,
367 AllocStaticCommand: Allocator<StaticCommand> + Copy,
368 AllocHistogramLiteral: Allocator<HistogramLiteral> + Copy,
369 AllocHistogramCommand: Allocator<HistogramCommand> + Copy,
370 AllocHistogramDistance: Allocator<HistogramDistance> + Copy,
371 AllocHistogramPair: Allocator<HistogramPair> + Copy,
372 AllocContextType: Allocator<ContextType> + Copy,
373 AllocHuffmanTree: Allocator<HuffmanTree> + Copy,
374 AllocZopfliNode: Allocator<ZopfliNode> + Copy,
375 > Copy
376 for CombiningAllocator<
377 AllocU8,
378 AllocU16,
379 AllocI32,
380 AllocU32,
381 AllocU64,
382 AllocCommand,
383 AllocFloatX,
384 AllocV8,
385 AllocS16,
386 AllocPDF,
387 AllocStaticCommand,
388 AllocHistogramLiteral,
389 AllocHistogramCommand,
390 AllocHistogramDistance,
391 AllocHistogramPair,
392 AllocContextType,
393 AllocHuffmanTree,
394 AllocZopfliNode,
395 >
396{
397}
398
399macro_rules! implement_allocator {
400 ($bound_name: ty,
401 $type_name: ty,
402 $sub_type_name: ty,
403 $local_name: ident) => {
404 impl<
405 AllocU8: Allocator<u8>,
406 AllocU16: Allocator<u16>,
407 AllocI32: Allocator<i32>,
408 AllocU32: Allocator<u32>,
409 AllocU64: Allocator<u64>,
410 AllocCommand: Allocator<Command>,
411 AllocFloatX: Allocator<floatX>,
412 AllocV8: Allocator<v8>,
413 AllocS16: Allocator<s16>,
414 AllocPDF: Allocator<PDF>,
415 AllocStaticCommand: Allocator<StaticCommand>,
416 AllocHistogramLiteral: Allocator<HistogramLiteral>,
417 AllocHistogramCommand: Allocator<HistogramCommand>,
418 AllocHistogramDistance: Allocator<HistogramDistance>,
419 AllocHistogramPair: Allocator<HistogramPair>,
420 AllocContextType: Allocator<ContextType>,
421 AllocHuffmanTree: Allocator<HuffmanTree>,
422 AllocZopfliNode: Allocator<ZopfliNode>,
423 > Allocator<$type_name>
424 for CombiningAllocator<
425 AllocU8,
426 AllocU16,
427 AllocI32,
428 AllocU32,
429 AllocU64,
430 AllocCommand,
431 AllocFloatX,
432 AllocV8,
433 AllocS16,
434 AllocPDF,
435 AllocStaticCommand,
436 AllocHistogramLiteral,
437 AllocHistogramCommand,
438 AllocHistogramDistance,
439 AllocHistogramPair,
440 AllocContextType,
441 AllocHuffmanTree,
442 AllocZopfliNode,
443 >
444 {
445 type AllocatedMemory = $sub_type_name;
446 fn alloc_cell(
447 &mut self,
448 size: usize,
449 ) -> <Self as Allocator<$type_name>>::AllocatedMemory {
450 self.$local_name.alloc_cell(size)
451 }
452 fn free_cell(&mut self, data: <Self as Allocator<$type_name>>::AllocatedMemory) {
453 self.$local_name.free_cell(data)
454 }
455 }
456 };
457}
458
459implement_allocator!(AllocU8, u8, AllocU8::AllocatedMemory, alloc_u8);
460implement_allocator!(AllocU16, u16, AllocU16::AllocatedMemory, alloc_u16);
461
462implement_allocator!(AllocI32, i32, AllocI32::AllocatedMemory, alloc_i32);
463implement_allocator!(AllocU32, u32, AllocU32::AllocatedMemory, alloc_u32);
464implement_allocator!(AllocU64, u64, AllocU64::AllocatedMemory, alloc_u64);
465implement_allocator!(
466 AllocCommand,
467 Command,
468 AllocCommand::AllocatedMemory,
469 alloc_c
470);
471implement_allocator!(AllocFloatX, floatX, AllocFloatX::AllocatedMemory, alloc_f);
472implement_allocator!(AllocV8, v8, AllocV8::AllocatedMemory, alloc_f32x8);
473implement_allocator!(AllocS16, s16, AllocS16::AllocatedMemory, alloc_i16x16);
474implement_allocator!(AllocPDF, PDF, AllocPDF::AllocatedMemory, alloc_pdf);
475implement_allocator!(
476 AllocStaticCommand,
477 StaticCommand,
478 AllocStaticCommand::AllocatedMemory,
479 alloc_sc
480);
481implement_allocator!(
482 AllocHistogramLiteral,
483 HistogramLiteral,
484 AllocHistogramLiteral::AllocatedMemory,
485 alloc_hl
486);
487implement_allocator!(
488 AllocHistogramCommand,
489 HistogramCommand,
490 AllocHistogramCommand::AllocatedMemory,
491 alloc_hc
492);
493implement_allocator!(
494 AllocHistogramDistance,
495 HistogramDistance,
496 AllocHistogramDistance::AllocatedMemory,
497 alloc_hd
498);
499implement_allocator!(
500 AllocHistogramPair,
501 HistogramPair,
502 AllocHistogramPair::AllocatedMemory,
503 alloc_hp
504);
505implement_allocator!(
506 AllocContextType,
507 ContextType,
508 AllocContextType::AllocatedMemory,
509 alloc_ct
510);
511implement_allocator!(
512 AllocHuffmanTree,
513 HuffmanTree,
514 AllocHuffmanTree::AllocatedMemory,
515 alloc_ht
516);
517implement_allocator!(
518 AllocZopfliNode,
519 ZopfliNode,
520 AllocZopfliNode::AllocatedMemory,
521 alloc_zn
522);