Embedded SDK
Embedded SDK
Loading...
Searching...
No Matches
app_mem_utils.c
Go to the documentation of this file.
1
8#include <stdint.h>
9#include <string.h>
10#include "os_helpers.h"
11#include "os_print.h"
12#include "mem_alloc.h"
13#include "app_mem_utils.h"
14
15static mem_ctx_t mem_utils_ctx = NULL;
16
17#ifdef HAVE_MEMORY_PROFILING
18#define MP_LOG_PREFIX "==MP "
19#endif
20
30bool mem_utils_init(void *heap_start, size_t heap_size)
31{
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);
35#endif
36 return mem_utils_ctx != NULL;
37}
38
48void *mem_utils_alloc(size_t size, bool permanent, const char *file, int line)
49{
50 UNUSED(permanent);
51 UNUSED(file);
52 UNUSED(line);
53 void *ptr = NULL;
54
55 ptr = mem_alloc(mem_utils_ctx, size);
56#ifdef HAVE_MEMORY_PROFILING
57 // Only log successful allocations. A failed allocation (ptr == NULL, e.g. out of memory) must
58 // not be recorded, otherwise the profiler tracks a phantom live allocation at address 0x0 that
59 // is never freed (there is nothing to free) and reports it as a spurious memory leak.
60 if (ptr != NULL) {
61 if (permanent) {
62 PRINTF(MP_LOG_PREFIX "persist;%u;0x%p;%s:%u\n", size, ptr, file, line);
63 }
64 else {
65 PRINTF(MP_LOG_PREFIX "alloc;%u;0x%p;%s:%u\n", size, ptr, file, line);
66 }
67 }
68#endif
69 return ptr;
70}
71
81void *mem_utils_realloc(void *ptr, size_t size, const char *file, int line)
82{
83 UNUSED(file);
84 UNUSED(line);
85 void *new_ptr;
86 new_ptr = mem_realloc(mem_utils_ctx, ptr, size);
87
88#ifdef HAVE_MEMORY_PROFILING
89 if (ptr != NULL && size == 0) {
90 PRINTF(MP_LOG_PREFIX "free;0x%p;%s:%u\n", ptr, file, line);
91 }
92 else if (new_ptr != NULL) {
93 if (ptr == NULL) {
94 PRINTF(MP_LOG_PREFIX "alloc;%u;0x%p;%s:%u\n", size, new_ptr, file, line);
95 }
96 else if (ptr != new_ptr) {
97 PRINTF(MP_LOG_PREFIX "free;0x%p;%s:%u\n", ptr, file, line);
98 PRINTF(MP_LOG_PREFIX "alloc;%u;0x%p;%s:%u\n", size, new_ptr, file, line);
99 }
100 }
101#endif
102 return new_ptr;
103}
104
112void mem_utils_free(void *ptr, const char *file, int line)
113{
114 UNUSED(file);
115 UNUSED(line);
116 if (ptr == NULL) {
117 return;
118 }
119#ifdef HAVE_MEMORY_PROFILING
120 PRINTF(MP_LOG_PREFIX "free;0x%p;%s:%u\n", ptr, file, line);
121#endif
122 mem_free(mem_utils_ctx, ptr);
123}
124
135bool mem_utils_calloc(void **buffer, uint16_t size, bool permanent, const char *file, int line)
136{
137 if (size == 0) {
138 // Nothing to allocate, but cleanup was done if needed
139 PRINTF("Requested buffer size is zero\n");
140 return true;
141 }
142 // Allocate the message buffer
143 if ((*buffer = mem_utils_alloc(size, permanent, file, line)) == NULL) {
144 PRINTF("Memory allocation failed for buffer of size %u\n", size);
145 return false;
146 }
147 explicit_bzero(*buffer, size);
148 return true;
149}
150
158void mem_utils_free_and_null(void **buffer, const char *file, int line)
159{
160 if (*buffer != NULL) {
161 mem_utils_free(*buffer, file, line);
162 *buffer = NULL;
163 }
164}
165
174char *mem_utils_strdup(const char *src, const char *file, int line)
175{
176 char *dst = NULL;
177 size_t length = strlen(src) + 1;
178
179 if ((dst = mem_utils_alloc(length, false, file, line)) != NULL) {
180 memcpy(dst, src, length);
181 }
182 return dst;
183}
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.
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.
Definition mem_alloc.c:458
mem_ctx_t mem_init(void *heap_start, size_t heap_size)
initializes the heap for this allocator
Definition mem_alloc.c:327
void * mem_alloc(mem_ctx_t ctx, size_t nb_bytes)
allocates a buffer of the given size, if possible
Definition mem_alloc.c:369
void mem_free(mem_ctx_t ctx, void *ptr)
frees the given buffer
Definition mem_alloc.c:551
Dynamic memory allocation API.
void * mem_ctx_t
type shared externally
Definition mem_alloc.h:27