memory.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 <string.h> /* memcpy */
  9. #include <brotli/types.h>
  10. #include "../common/platform.h"
  11. #if defined(__cplusplus) || defined(c_plusplus)
  12. extern "C" {
  13. #endif
  14. #if !defined(BROTLI_ENCODER_CLEANUP_ON_OOM) && \
  15. !defined(BROTLI_ENCODER_EXIT_ON_OOM)
  16. #define BROTLI_ENCODER_EXIT_ON_OOM
  17. #endif
  18. #if !defined(BROTLI_ENCODER_EXIT_ON_OOM)
  19. #if defined(BROTLI_EXPERIMENTAL)
  20. #define BROTLI_ENCODER_MEMORY_MANAGER_SLOTS (48*1024)
  21. #else /* BROTLI_EXPERIMENTAL */
  22. #define BROTLI_ENCODER_MEMORY_MANAGER_SLOTS 256
  23. #endif /* BROTLI_EXPERIMENTAL */
  24. #else /* BROTLI_ENCODER_EXIT_ON_OOM */
  25. #define BROTLI_ENCODER_MEMORY_MANAGER_SLOTS 0
  26. #endif /* BROTLI_ENCODER_EXIT_ON_OOM */
  27. typedef struct MemoryManager {
  28. brotli_alloc_func alloc_func;
  29. brotli_free_func free_func;
  30. void* opaque;
  31. #if !defined(BROTLI_ENCODER_EXIT_ON_OOM)
  32. BROTLI_BOOL is_oom;
  33. size_t perm_allocated;
  34. size_t new_allocated;
  35. size_t new_freed;
  36. void* pointers[BROTLI_ENCODER_MEMORY_MANAGER_SLOTS];
  37. #endif /* BROTLI_ENCODER_EXIT_ON_OOM */
  38. } MemoryManager;
  39. BROTLI_INTERNAL void BrotliInitMemoryManager(
  40. MemoryManager* m, brotli_alloc_func alloc_func, brotli_free_func free_func,
  41. void* opaque);
  42. BROTLI_INTERNAL void* BrotliAllocate(MemoryManager* m, size_t n);
  43. #define BROTLI_ALLOC(M, T, N) \
  44. ((N) > 0 ? ((T*)BrotliAllocate((M), (N) * sizeof(T))) : NULL)
  45. BROTLI_INTERNAL void BrotliFree(MemoryManager* m, void* p);
  46. #define BROTLI_FREE(M, P) { \
  47. BrotliFree((M), (P)); \
  48. P = NULL; \
  49. }
  50. #if defined(BROTLI_ENCODER_EXIT_ON_OOM)
  51. #define BROTLI_IS_OOM(M) (!!0)
  52. #else /* BROTLI_ENCODER_EXIT_ON_OOM */
  53. #define BROTLI_IS_OOM(M) (!!(M)->is_oom)
  54. #endif /* BROTLI_ENCODER_EXIT_ON_OOM */
  55. /*
  56. BROTLI_IS_NULL is a fake check, BROTLI_IS_OOM does the heavy lifting.
  57. The only purpose of it is to explain static analyzers the state of things.
  58. NB: use ONLY together with BROTLI_IS_OOM
  59. AND ONLY for allocations in the current scope.
  60. */
  61. #if defined(__clang_analyzer__) && !defined(BROTLI_ENCODER_EXIT_ON_OOM)
  62. #define BROTLI_IS_NULL(A) ((A) == nullptr)
  63. #else /* defined(__clang_analyzer__) */
  64. #define BROTLI_IS_NULL(A) (!!0)
  65. #endif /* defined(__clang_analyzer__) */
  66. BROTLI_INTERNAL void BrotliWipeOutMemoryManager(MemoryManager* m);
  67. /*
  68. Dynamically grows array capacity to at least the requested size
  69. M: MemoryManager
  70. T: data type
  71. A: array
  72. C: capacity
  73. R: requested size
  74. */
  75. #define BROTLI_ENSURE_CAPACITY(M, T, A, C, R) { \
  76. if (C < (R)) { \
  77. size_t _new_size = (C == 0) ? (R) : C; \
  78. T* new_array; \
  79. while (_new_size < (R)) _new_size *= 2; \
  80. new_array = BROTLI_ALLOC((M), T, _new_size); \
  81. if (!BROTLI_IS_OOM(M) && !BROTLI_IS_NULL(new_array) && C != 0) \
  82. memcpy(new_array, A, C * sizeof(T)); \
  83. BROTLI_FREE((M), A); \
  84. A = new_array; \
  85. C = _new_size; \
  86. } \
  87. }
  88. /*
  89. Appends value and dynamically grows array capacity when needed
  90. M: MemoryManager
  91. T: data type
  92. A: array
  93. C: array capacity
  94. S: array size
  95. V: value to append
  96. */
  97. #define BROTLI_ENSURE_CAPACITY_APPEND(M, T, A, C, S, V) { \
  98. (S)++; \
  99. BROTLI_ENSURE_CAPACITY(M, T, A, C, S); \
  100. A[(S) - 1] = (V); \
  101. }
  102. /* "Bootstrap" allocations are not tracked by memory manager; should be used
  103. only to allocate MemoryManager itself (or structure containing it). */
  104. BROTLI_INTERNAL void* BrotliBootstrapAlloc(size_t size,
  105. brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque);
  106. BROTLI_INTERNAL void BrotliBootstrapFree(void* address, MemoryManager* m);
  107. #if defined(__cplusplus) || defined(c_plusplus)
  108. } /* extern "C" */
  109. #endif
  110. #endif /* BROTLI_ENC_MEMORY_H_ */