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 if (ptr == NULL) {
112 return;
113 }
114#ifdef HAVE_MEMORY_PROFILING
115 PRINTF(MP_LOG_PREFIX "free;0x%p;%s:%u\n", ptr, file, line);
116#endif
117 mem_free(mem_utils_ctx, ptr);
118}
119
130bool mem_utils_calloc(void **buffer, uint16_t size, bool permanent, const char *file, int line)
131{
132 if (size == 0) {
133 // Nothing to allocate, but cleanup was done if needed
134 PRINTF("Requested buffer size is zero\n");
135 return true;
136 }
137 // Allocate the message buffer
138 if ((*buffer = mem_utils_alloc(size, permanent, file, line)) == NULL) {
139 PRINTF("Memory allocation failed for buffer of size %u\n", size);
140 return false;
141 }
142 explicit_bzero(*buffer, size);
143 return true;
144}
145
153void mem_utils_free_and_null(void **buffer, const char *file, int line)
154{
155 if (*buffer != NULL) {
156 mem_utils_free(*buffer, file, line);
157 *buffer = NULL;
158 }
159}
160
169char *mem_utils_strdup(const char *src, const char *file, int line)
170{
171 char *dst = NULL;
172 size_t length = strlen(src) + 1;
173
174 if ((dst = mem_utils_alloc(length, false, file, line)) != NULL) {
175 memcpy(dst, src, length);
176 }
177 return dst;
178}
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: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