X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=BaseTools%2FSource%2FC%2FBrotliCompress%2Fenc%2Fmemory.h;h=2bd4d0c872a6f72b82ded9aed7d5c51ed19d79c6;hp=5ef59fa52ab228608e6a6f01563248acdf17fd06;hb=dd4f667e70a3d0f8dcb09bc9ea3287187c8024f6;hpb=78af0984b45a780e45d57c22c85a1f594b969212 diff --git a/BaseTools/Source/C/BrotliCompress/enc/memory.h b/BaseTools/Source/C/BrotliCompress/enc/memory.h index 5ef59fa52a..2bd4d0c872 100644 --- a/BaseTools/Source/C/BrotliCompress/enc/memory.h +++ b/BaseTools/Source/C/BrotliCompress/enc/memory.h @@ -9,8 +9,10 @@ #ifndef BROTLI_ENC_MEMORY_H_ #define BROTLI_ENC_MEMORY_H_ -#include "../common/types.h" -#include "./port.h" +#include /* memcpy */ + +#include "../common/platform.h" +#include #if defined(__cplusplus) || defined(c_plusplus) extern "C" { @@ -39,7 +41,8 @@ BROTLI_INTERNAL void BrotliInitMemoryManager( void* opaque); BROTLI_INTERNAL void* BrotliAllocate(MemoryManager* m, size_t n); -#define BROTLI_ALLOC(M, T, N) ((T*)BrotliAllocate((M), (N) * sizeof(T))) +#define BROTLI_ALLOC(M, T, N) \ + ((N) > 0 ? ((T*)BrotliAllocate((M), (N) * sizeof(T))) : NULL) BROTLI_INTERNAL void BrotliFree(MemoryManager* m, void* p); #define BROTLI_FREE(M, P) { \ @@ -55,6 +58,43 @@ 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; \ + } \ +} + +/* +Appends value and dynamically grows array capacity when needed +M: MemoryManager +T: data type +A: array +C: array capacity +S: array size +V: value to append +*/ +#define BROTLI_ENSURE_CAPACITY_APPEND(M, T, A, C, S, V) { \ + (S)++; \ + BROTLI_ENSURE_CAPACITY(M, T, A, C, S); \ + A[(S) - 1] = (V); \ +} + #if defined(__cplusplus) || defined(c_plusplus) } /* extern "C" */ #endif