diff options
Diffstat (limited to 'c/enc/memory.h')
-rw-r--r-- | c/enc/memory.h | 26 |
1 files changed, 25 insertions, 1 deletions
diff --git a/c/enc/memory.h b/c/enc/memory.h index babf1f8..2d56e97 100644 --- a/c/enc/memory.h +++ b/c/enc/memory.h @@ -9,8 +9,10 @@ #ifndef BROTLI_ENC_MEMORY_H_ #define BROTLI_ENC_MEMORY_H_ +#include <string.h> /* memcpy */ + +#include "../common/platform.h" #include <brotli/types.h> -#include "./port.h" #if defined(__cplusplus) || defined(c_plusplus) extern "C" { @@ -56,6 +58,28 @@ BROTLI_INTERNAL void BrotliFree(MemoryManager* m, void* p); BROTLI_INTERNAL void BrotliWipeOutMemoryManager(MemoryManager* m); +/* +Dynamically grows array capacity to at least the requested size +M: MemoryManager +T: data type +A: array +C: capacity +R: requested size +*/ +#define BROTLI_ENSURE_CAPACITY(M, T, A, C, R) { \ + if (C < (R)) { \ + size_t _new_size = (C == 0) ? (R) : C; \ + T* new_array; \ + while (_new_size < (R)) _new_size *= 2; \ + new_array = BROTLI_ALLOC((M), T, _new_size); \ + if (!BROTLI_IS_OOM(M) && C != 0) \ + memcpy(new_array, A, C * sizeof(T)); \ + BROTLI_FREE((M), A); \ + A = new_array; \ + C = _new_size; \ + } \ +} + #if defined(__cplusplus) || defined(c_plusplus) } /* extern "C" */ #endif |