OLD | NEW |
1 /* Copyright 2013 Google Inc. All Rights Reserved. | 1 /* Copyright 2016 Google Inc. All Rights Reserved. |
2 | 2 |
3 Distributed under MIT license. | 3 Distributed under MIT license. |
4 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT | 4 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT |
5 */ | 5 */ |
6 | 6 |
7 /* Bit reading helpers */ | 7 /* Macros for memory management. */ |
8 | 8 |
9 #include "./bit_reader.h" | 9 #ifndef BROTLI_ENC_MEMORY_H_ |
| 10 #define BROTLI_ENC_MEMORY_H_ |
10 | 11 |
| 12 #include <brotli/types.h> |
11 #include "./port.h" | 13 #include "./port.h" |
12 #include "./types.h" | |
13 | 14 |
14 #if defined(__cplusplus) || defined(c_plusplus) | 15 #if defined(__cplusplus) || defined(c_plusplus) |
15 extern "C" { | 16 extern "C" { |
16 #endif | 17 #endif |
17 | 18 |
18 void BrotliInitBitReader(BrotliBitReader* const br) { | 19 #if !defined(BROTLI_ENCODER_CLEANUP_ON_OOM) && \ |
19 br->val_ = 0; | 20 !defined(BROTLI_ENCODER_EXIT_ON_OOM) |
20 br->bit_pos_ = sizeof(br->val_) << 3; | 21 #define BROTLI_ENCODER_EXIT_ON_OOM |
| 22 #endif |
| 23 |
| 24 typedef struct MemoryManager { |
| 25 brotli_alloc_func alloc_func; |
| 26 brotli_free_func free_func; |
| 27 void* opaque; |
| 28 #if !defined(BROTLI_ENCODER_EXIT_ON_OOM) |
| 29 BROTLI_BOOL is_oom; |
| 30 size_t perm_allocated; |
| 31 size_t new_allocated; |
| 32 size_t new_freed; |
| 33 void* pointers[256]; |
| 34 #endif /* BROTLI_ENCODER_EXIT_ON_OOM */ |
| 35 } MemoryManager; |
| 36 |
| 37 BROTLI_INTERNAL void BrotliInitMemoryManager( |
| 38 MemoryManager* m, brotli_alloc_func alloc_func, brotli_free_func free_func, |
| 39 void* opaque); |
| 40 |
| 41 BROTLI_INTERNAL void* BrotliAllocate(MemoryManager* m, size_t n); |
| 42 #define BROTLI_ALLOC(M, T, N) ((T*)BrotliAllocate((M), (N) * sizeof(T))) |
| 43 |
| 44 BROTLI_INTERNAL void BrotliFree(MemoryManager* m, void* p); |
| 45 #define BROTLI_FREE(M, P) { \ |
| 46 BrotliFree((M), (P)); \ |
| 47 P = NULL; \ |
21 } | 48 } |
22 | 49 |
23 int BrotliWarmupBitReader(BrotliBitReader* const br) { | 50 #if defined(BROTLI_ENCODER_EXIT_ON_OOM) |
24 size_t aligned_read_mask = (sizeof(br->val_) >> 1) - 1; | 51 #define BROTLI_IS_OOM(M) (!!0) |
25 /* Fixing alignment after unaligned BrotliFillWindow would result accumulator | 52 #else /* BROTLI_ENCODER_EXIT_ON_OOM */ |
26 overflow. If unalignment is caused by BrotliSafeReadBits, then there is | 53 #define BROTLI_IS_OOM(M) (!!(M)->is_oom) |
27 enough space in accumulator to fix aligment. */ | 54 #endif /* BROTLI_ENCODER_EXIT_ON_OOM */ |
28 if (!BROTLI_ALIGNED_READ) { | |
29 aligned_read_mask = 0; | |
30 } | |
31 if (BrotliGetAvailableBits(br) == 0) { | |
32 if (!BrotliPullByte(br)) { | |
33 return 0; | |
34 } | |
35 } | |
36 | 55 |
37 while ((((size_t)br->next_in) & aligned_read_mask) != 0) { | 56 BROTLI_INTERNAL void BrotliWipeOutMemoryManager(MemoryManager* m); |
38 if (!BrotliPullByte(br)) { | |
39 /* If we consumed all the input, we don't care about the alignment. */ | |
40 return 1; | |
41 } | |
42 } | |
43 return 1; | |
44 } | |
45 | 57 |
46 #if defined(__cplusplus) || defined(c_plusplus) | 58 #if defined(__cplusplus) || defined(c_plusplus) |
47 } /* extern "C" */ | 59 } /* extern "C" */ |
48 #endif | 60 #endif |
| 61 |
| 62 #endif /* BROTLI_ENC_MEMORY_H_ */ |
OLD | NEW |