2016-06-13 09:01:04 +00:00
|
|
|
/* Copyright 2016 Google Inc. All Rights Reserved.
|
|
|
|
|
|
|
|
Distributed under MIT license.
|
|
|
|
See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Macros for memory management. */
|
|
|
|
|
|
|
|
#ifndef BROTLI_ENC_MEMORY_H_
|
|
|
|
#define BROTLI_ENC_MEMORY_H_
|
|
|
|
|
2017-12-12 13:33:12 +00:00
|
|
|
#include <string.h> /* memcpy */
|
|
|
|
|
2016-08-23 12:40:33 +00:00
|
|
|
#include <brotli/types.h>
|
2016-06-13 09:01:04 +00:00
|
|
|
|
2022-11-17 13:03:09 +00:00
|
|
|
#include "../common/platform.h"
|
|
|
|
|
2016-06-13 09:01:04 +00:00
|
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !defined(BROTLI_ENCODER_CLEANUP_ON_OOM) && \
|
|
|
|
!defined(BROTLI_ENCODER_EXIT_ON_OOM)
|
|
|
|
#define BROTLI_ENCODER_EXIT_ON_OOM
|
|
|
|
#endif
|
|
|
|
|
|
|
|
typedef struct MemoryManager {
|
|
|
|
brotli_alloc_func alloc_func;
|
|
|
|
brotli_free_func free_func;
|
|
|
|
void* opaque;
|
|
|
|
#if !defined(BROTLI_ENCODER_EXIT_ON_OOM)
|
2016-07-26 12:41:59 +00:00
|
|
|
BROTLI_BOOL is_oom;
|
2016-06-13 09:01:04 +00:00
|
|
|
size_t perm_allocated;
|
|
|
|
size_t new_allocated;
|
|
|
|
size_t new_freed;
|
|
|
|
void* pointers[256];
|
|
|
|
#endif /* BROTLI_ENCODER_EXIT_ON_OOM */
|
|
|
|
} MemoryManager;
|
|
|
|
|
|
|
|
BROTLI_INTERNAL void BrotliInitMemoryManager(
|
|
|
|
MemoryManager* m, brotli_alloc_func alloc_func, brotli_free_func free_func,
|
|
|
|
void* opaque);
|
|
|
|
|
|
|
|
BROTLI_INTERNAL void* BrotliAllocate(MemoryManager* m, size_t n);
|
2017-02-06 13:20:43 +00:00
|
|
|
#define BROTLI_ALLOC(M, T, N) \
|
2017-06-17 13:22:07 +00:00
|
|
|
((N) > 0 ? ((T*)BrotliAllocate((M), (N) * sizeof(T))) : NULL)
|
2016-06-13 09:01:04 +00:00
|
|
|
|
|
|
|
BROTLI_INTERNAL void BrotliFree(MemoryManager* m, void* p);
|
|
|
|
#define BROTLI_FREE(M, P) { \
|
|
|
|
BrotliFree((M), (P)); \
|
|
|
|
P = NULL; \
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(BROTLI_ENCODER_EXIT_ON_OOM)
|
|
|
|
#define BROTLI_IS_OOM(M) (!!0)
|
|
|
|
#else /* BROTLI_ENCODER_EXIT_ON_OOM */
|
|
|
|
#define BROTLI_IS_OOM(M) (!!(M)->is_oom)
|
|
|
|
#endif /* BROTLI_ENCODER_EXIT_ON_OOM */
|
|
|
|
|
2019-05-03 09:51:11 +00:00
|
|
|
/*
|
|
|
|
BROTLI_IS_NULL is a fake check, BROTLI_IS_OOM does the heavy lifting.
|
|
|
|
The only purpose of it is to explain static analyzers the state of things.
|
|
|
|
NB: use ONLY together with BROTLI_IS_OOM
|
|
|
|
AND ONLY for allocations in the current scope.
|
|
|
|
*/
|
|
|
|
#if defined(__clang_analyzer__) && !defined(BROTLI_ENCODER_EXIT_ON_OOM)
|
|
|
|
#define BROTLI_IS_NULL(A) ((A) == nullptr)
|
|
|
|
#else /* defined(__clang_analyzer__) */
|
|
|
|
#define BROTLI_IS_NULL(A) (!!0)
|
|
|
|
#endif /* defined(__clang_analyzer__) */
|
|
|
|
|
2016-06-13 09:01:04 +00:00
|
|
|
BROTLI_INTERNAL void BrotliWipeOutMemoryManager(MemoryManager* m);
|
|
|
|
|
2017-12-12 13:33:12 +00:00
|
|
|
/*
|
|
|
|
Dynamically grows array capacity to at least the requested size
|
|
|
|
M: MemoryManager
|
|
|
|
T: data type
|
|
|
|
A: array
|
|
|
|
C: capacity
|
|
|
|
R: requested size
|
|
|
|
*/
|
2019-05-03 09:51:11 +00:00
|
|
|
#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) && !BROTLI_IS_NULL(new_array) && C != 0) \
|
|
|
|
memcpy(new_array, A, C * sizeof(T)); \
|
|
|
|
BROTLI_FREE((M), A); \
|
|
|
|
A = new_array; \
|
|
|
|
C = _new_size; \
|
|
|
|
} \
|
2017-12-12 13:33:12 +00:00
|
|
|
}
|
|
|
|
|
2018-03-20 11:37:41 +00:00
|
|
|
/*
|
|
|
|
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); \
|
|
|
|
}
|
|
|
|
|
2021-07-29 20:29:43 +00:00
|
|
|
/* "Bootstrap" allocations are not tracked by memory manager; should be used
|
|
|
|
only to allocate MemoryManager itself (or structure containing it). */
|
|
|
|
BROTLI_INTERNAL void* BrotliBootstrapAlloc(size_t size,
|
|
|
|
brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque);
|
|
|
|
BROTLI_INTERNAL void BrotliBootstrapFree(void* address, MemoryManager* m);
|
|
|
|
|
2016-06-13 09:01:04 +00:00
|
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
|
|
|
} /* extern "C" */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* BROTLI_ENC_MEMORY_H_ */
|