10#include "os_helpers.h"
17#ifdef HAVE_MEMORY_PROFILING
18#define MP_LOG_PREFIX "==MP "
32 mem_utils_ctx =
mem_init(heap_start, heap_size);
33#ifdef HAVE_MEMORY_PROFILING
34 PRINTF(MP_LOG_PREFIX
"init;0x%p;%u\n", heap_start, heap_size);
36 return mem_utils_ctx != NULL;
56#ifdef HAVE_MEMORY_PROFILING
58 PRINTF(MP_LOG_PREFIX
"persist;%u;0x%p;%s:%u\n", size, ptr, file, line);
61 PRINTF(MP_LOG_PREFIX
"alloc;%u;0x%p;%s:%u\n", size, ptr, file, line);
83#ifdef HAVE_MEMORY_PROFILING
84 if (ptr != NULL && size == 0) {
85 PRINTF(MP_LOG_PREFIX
"free;0x%p;%s:%u\n", ptr, file, line);
87 else if (new_ptr != NULL) {
89 PRINTF(MP_LOG_PREFIX
"alloc;%u;0x%p;%s:%u\n", size, new_ptr, file, line);
91 else if (ptr != new_ptr) {
92 PRINTF(MP_LOG_PREFIX
"free;0x%p;%s:%u\n", ptr, file, line);
93 PRINTF(MP_LOG_PREFIX
"alloc;%u;0x%p;%s:%u\n", size, new_ptr, file, line);
111#ifdef HAVE_MEMORY_PROFILING
112 PRINTF(MP_LOG_PREFIX
"free;0x%p;%s:%u\n", ptr, file, line);
128bool mem_utils_calloc(
void **buffer, uint16_t size,
bool permanent,
const char *file,
int line)
131 if (*buffer != NULL) {
132 PRINTF(
"Buffer already allocated, freeing it before reallocating\n");
138 PRINTF(
"Requested buffer size is zero\n");
142 if ((*buffer =
mem_utils_alloc(size, permanent, file, line)) == NULL) {
143 PRINTF(
"Memory allocation failed for buffer of size %u\n", size);
146 explicit_bzero(*buffer, size);
159 if (*buffer != NULL) {
176 size_t length = strlen(src) + 1;
179 memcpy(dst, src, length);
void * mem_utils_realloc(void *ptr, size_t size, const char *file, int line)
Internal implementation of memory reallocation.
void mem_utils_free_and_null(void **buffer, const char *file, int line)
Internal implementation of safe free with nullification.
bool mem_utils_init(void *heap_start, size_t heap_size)
Initializes the App heap buffer.
void * mem_utils_alloc(size_t size, bool permanent, const char *file, int line)
Internal implementation of memory allocation.
void mem_utils_free(void *ptr, const char *file, int line)
Internal implementation of memory free.
bool mem_utils_calloc(void **buffer, uint16_t size, bool permanent, const char *file, int line)
Internal implementation of memory allocation of a zero-initialized buffer freeing any existing alloca...
char * mem_utils_strdup(const char *src, const char *file, int line)
Internal implementation of string duplication.
void * mem_realloc(mem_ctx_t ctx, void *ptr, size_t size)
Reallocates a buffer to a new size.
mem_ctx_t mem_init(void *heap_start, size_t heap_size)
initializes the heap for this allocator
void * mem_alloc(mem_ctx_t ctx, size_t nb_bytes)
allocates a buffer of the given size, if possible
void mem_free(mem_ctx_t ctx, void *ptr)
frees the given buffer
Dynamic memory allocation API.
void * mem_ctx_t
type shared externally