1 /* Copyright 2016 Google Inc. All Rights Reserved.
3 Distributed under MIT license.
4 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
7 /* Macros for memory management. */
9 #ifndef BROTLI_ENC_MEMORY_H_
10 #define BROTLI_ENC_MEMORY_H_
12 #include <string.h> /* memcpy */
14 #include "../common/platform.h"
15 #include <brotli/types.h>
17 #if defined(__cplusplus) || defined(c_plusplus)
21 #if !defined(BROTLI_ENCODER_CLEANUP_ON_OOM) && \
22 !defined(BROTLI_ENCODER_EXIT_ON_OOM)
23 #define BROTLI_ENCODER_EXIT_ON_OOM
26 typedef struct MemoryManager
{
27 brotli_alloc_func alloc_func
;
28 brotli_free_func free_func
;
30 #if !defined(BROTLI_ENCODER_EXIT_ON_OOM)
32 size_t perm_allocated
;
36 #endif /* BROTLI_ENCODER_EXIT_ON_OOM */
39 BROTLI_INTERNAL
void BrotliInitMemoryManager(
40 MemoryManager
* m
, brotli_alloc_func alloc_func
, brotli_free_func free_func
,
43 BROTLI_INTERNAL
void* BrotliAllocate(MemoryManager
* m
, size_t n
);
44 #define BROTLI_ALLOC(M, T, N) \
45 ((N) > 0 ? ((T*)BrotliAllocate((M), (N) * sizeof(T))) : NULL)
47 BROTLI_INTERNAL
void BrotliFree(MemoryManager
* m
, void* p
);
48 #define BROTLI_FREE(M, P) { \
49 BrotliFree((M), (P)); \
53 #if defined(BROTLI_ENCODER_EXIT_ON_OOM)
54 #define BROTLI_IS_OOM(M) (!!0)
55 #else /* BROTLI_ENCODER_EXIT_ON_OOM */
56 #define BROTLI_IS_OOM(M) (!!(M)->is_oom)
57 #endif /* BROTLI_ENCODER_EXIT_ON_OOM */
59 BROTLI_INTERNAL
void BrotliWipeOutMemoryManager(MemoryManager
* m
);
62 Dynamically grows array capacity to at least the requested size
69 #define BROTLI_ENSURE_CAPACITY(M, T, A, C, R) { \
71 size_t _new_size = (C == 0) ? (R) : C; \
73 while (_new_size < (R)) _new_size *= 2; \
74 new_array = BROTLI_ALLOC((M), T, _new_size); \
75 if (!BROTLI_IS_OOM(M) && C != 0) \
76 memcpy(new_array, A, C * sizeof(T)); \
77 BROTLI_FREE((M), A); \
84 Appends value and dynamically grows array capacity when needed
92 #define BROTLI_ENSURE_CAPACITY_APPEND(M, T, A, C, S, V) { \
94 BROTLI_ENSURE_CAPACITY(M, T, A, C, S); \
98 #if defined(__cplusplus) || defined(c_plusplus)
102 #endif /* BROTLI_ENC_MEMORY_H_ */