memory.h 3.5 KB

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