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