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 if (permanent) {
58 PRINTF(MP_LOG_PREFIX "persist;%u;0x%p;%s:%u\n", size, ptr, file, line);
59 }
60 else {
61 PRINTF(MP_LOG_PREFIX "alloc;%u;0x%p;%s:%u\n", size, ptr, file, line);
62 }
63#endif
64 return ptr;
65}
66
76void *mem_utils_realloc(void *ptr, size_t size, const char *file, int line)
77{
78 UNUSED(file);
79 UNUSED(line);
80 void *new_ptr;
81 new_ptr = mem_realloc(mem_utils_ctx, ptr, size);
82
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);
86 }
87 else if (new_ptr != NULL) {
88 if (ptr == NULL) {
89 PRINTF(MP_LOG_PREFIX "alloc;%u;0x%p;%s:%u\n", size, new_ptr, file, line);
90 }
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);
94 }
95 }
96#endif
97 return new_ptr;
98}
99
107void mem_utils_free(void *ptr, const char *file, int line)
108{
109 UNUSED(file);
110 UNUSED(line);
111#ifdef HAVE_MEMORY_PROFILING
112 PRINTF(MP_LOG_PREFIX "free;0x%p;%s:%u\n", ptr, file, line);
113#endif
114 mem_free(mem_utils_ctx, ptr);
115}
116
128bool mem_utils_calloc(void **buffer, uint16_t size, bool permanent, const char *file, int line)
129{
130 // Check if the buffer is already allocated
131 if (*buffer != NULL) {
132 PRINTF("Buffer already allocated, freeing it before reallocating\n");
133 mem_utils_free(*buffer, file, line);
134 *buffer = NULL;
135 }
136 if (size == 0) {
137 // Nothing to allocate, but cleanup was done if needed
138 PRINTF("Requested buffer size is zero\n");
139 return true;
140 }
141 // Allocate the message buffer
142 if ((*buffer = mem_utils_alloc(size, permanent, file, line)) == NULL) {
143 PRINTF("Memory allocation failed for buffer of size %u\n", size);
144 return false;
145 }
146 explicit_bzero(*buffer, size);
147 return true;
148}
149
157void mem_utils_free_and_null(void **buffer, const char *file, int line)
158{
159 if (*buffer != NULL) {
160 mem_utils_free(*buffer, file, line);
161 *buffer = NULL;
162 }
163}
164
173char *mem_utils_strdup(const char *src, const char *file, int line)
174{
175 char *dst = NULL;
176 size_t length = strlen(src) + 1;
177
178 if ((dst = mem_utils_alloc(length, false, file, line)) != NULL) {
179 memcpy(dst, src, length);
180 }
181 return dst;
182}
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.
Definition mem_alloc.c:447
mem_ctx_t mem_init(void *heap_start, size_t heap_size)
initializes the heap for this allocator
Definition mem_alloc.c:323
void * mem_alloc(mem_ctx_t ctx, size_t nb_bytes)
allocates a buffer of the given size, if possible
Definition mem_alloc.c:365
void mem_free(mem_ctx_t ctx, void *ptr)
frees the given buffer
Definition mem_alloc.c:532
Dynamic memory allocation API.
void * mem_ctx_t
type shared externally
Definition mem_alloc.h:27