1 /* Copyright 2016 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 /* Macros for memory management. */ 8 9 #ifndef BROTLI_ENC_MEMORY_H_ 10 #define BROTLI_ENC_MEMORY_H_ 11 12 #include <brotli/types.h> 13 #include "./port.h" 14 15 #if defined(__cplusplus) || defined(c_plusplus) 16 extern "C" { 17 #endif 18 19 #if !defined(BROTLI_ENCODER_CLEANUP_ON_OOM) && \ 20 !defined(BROTLI_ENCODER_EXIT_ON_OOM) 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) \ 43 ((N) ? ((T*)BrotliAllocate((M), (N) * sizeof(T))) : NULL) 44 45 BROTLI_INTERNAL void BrotliFree(MemoryManager* m, void* p); 46 #define BROTLI_FREE(M, P) { \ 47 BrotliFree((M), (P)); \ 48 P = NULL; \ 49 } 50 51 #if defined(BROTLI_ENCODER_EXIT_ON_OOM) 52 #define BROTLI_IS_OOM(M) (!!0) 53 #else /* BROTLI_ENCODER_EXIT_ON_OOM */ 54 #define BROTLI_IS_OOM(M) (!!(M)->is_oom) 55 #endif /* BROTLI_ENCODER_EXIT_ON_OOM */ 56 57 BROTLI_INTERNAL void BrotliWipeOutMemoryManager(MemoryManager* m); 58 59 #if defined(__cplusplus) || defined(c_plusplus) 60 } /* extern "C" */ 61 #endif 62 63 #endif /* BROTLI_ENC_MEMORY_H_ */ 64