memory.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. BROTLI_INTERNAL void BrotliWipeOutMemoryManager(MemoryManager* m);
  47. /*
  48. Dynamically grows array capacity to at least the requested size
  49. M: MemoryManager
  50. T: data type
  51. A: array
  52. C: capacity
  53. R: requested size
  54. */
  55. #define BROTLI_ENSURE_CAPACITY(M, T, A, C, R) { \
  56. if (C < (R)) { \
  57. size_t _new_size = (C == 0) ? (R) : C; \
  58. T* new_array; \
  59. while (_new_size < (R)) _new_size *= 2; \
  60. new_array = BROTLI_ALLOC((M), T, _new_size); \
  61. if (!BROTLI_IS_OOM(M) && C != 0) \
  62. memcpy(new_array, A, C * sizeof(T)); \
  63. BROTLI_FREE((M), A); \
  64. A = new_array; \
  65. C = _new_size; \
  66. } \
  67. }
  68. /*
  69. Appends value and dynamically grows array capacity when needed
  70. M: MemoryManager
  71. T: data type
  72. A: array
  73. C: array capacity
  74. S: array size
  75. V: value to append
  76. */
  77. #define BROTLI_ENSURE_CAPACITY_APPEND(M, T, A, C, S, V) { \
  78. (S)++; \
  79. BROTLI_ENSURE_CAPACITY(M, T, A, C, S); \
  80. A[(S) - 1] = (V); \
  81. }
  82. #if defined(__cplusplus) || defined(c_plusplus)
  83. } /* extern "C" */
  84. #endif
  85. #endif /* BROTLI_ENC_MEMORY_H_ */