memory.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /* Copyright 2016 Google Inc. All Rights Reserved.
  2. Distributed under MIT license.
  3. See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
  4. */
  5. /* Macros for memory management. */
  6. #ifndef BROTLI_ENC_MEMORY_H_
  7. #define BROTLI_ENC_MEMORY_H_
  8. #include "../common/types.h"
  9. #include "./port.h"
  10. #if defined(__cplusplus) || defined(c_plusplus)
  11. extern "C" {
  12. #endif
  13. #if !defined(BROTLI_ENCODER_CLEANUP_ON_OOM) && \
  14. !defined(BROTLI_ENCODER_EXIT_ON_OOM)
  15. #define BROTLI_ENCODER_EXIT_ON_OOM
  16. #endif
  17. typedef struct MemoryManager {
  18. brotli_alloc_func alloc_func;
  19. brotli_free_func free_func;
  20. void* opaque;
  21. #if !defined(BROTLI_ENCODER_EXIT_ON_OOM)
  22. int is_oom;
  23. size_t perm_allocated;
  24. size_t new_allocated;
  25. size_t new_freed;
  26. void* pointers[256];
  27. #endif /* BROTLI_ENCODER_EXIT_ON_OOM */
  28. } MemoryManager;
  29. BROTLI_INTERNAL void BrotliInitMemoryManager(
  30. MemoryManager* m, brotli_alloc_func alloc_func, brotli_free_func free_func,
  31. void* opaque);
  32. BROTLI_INTERNAL void* BrotliAllocate(MemoryManager* m, size_t n);
  33. #define BROTLI_ALLOC(M, T, N) ((T*)BrotliAllocate((M), (N) * sizeof(T)))
  34. BROTLI_INTERNAL void BrotliFree(MemoryManager* m, void* p);
  35. #define BROTLI_FREE(M, P) { \
  36. BrotliFree((M), (P)); \
  37. P = NULL; \
  38. }
  39. #if defined(BROTLI_ENCODER_EXIT_ON_OOM)
  40. #define BROTLI_IS_OOM(M) (!!0)
  41. #else /* BROTLI_ENCODER_EXIT_ON_OOM */
  42. #define BROTLI_IS_OOM(M) (!!(M)->is_oom)
  43. #endif /* BROTLI_ENCODER_EXIT_ON_OOM */
  44. BROTLI_INTERNAL void BrotliWipeOutMemoryManager(MemoryManager* m);
  45. #if defined(__cplusplus) || defined(c_plusplus)
  46. } /* extern "C" */
  47. #endif
  48. #endif /* BROTLI_ENC_MEMORY_H_ */