arduino-audio-tools
Loading...
Searching...
No Matches
Allocator.h
Go to the documentation of this file.
1#pragma once
2#include <stdlib.h>
3#include <string.h>
4
5#include "AudioToolsConfig.h"
7// Some top level functions: stop(), checkMemory()
9
10namespace audio_tools {
11
25class Allocator {
26 public:
27 // creates an object
28 template <class T>
29 T* create() {
30 void* addr = allocate(sizeof(T));
31 // call constructor
32 T* ref = new (addr) T();
33 return ref;
34 }
35
37 template <class T>
38 void remove(T* obj) {
39 if (obj == nullptr) return;
40 obj->~T();
41 free((void*)obj);
42 }
43
44 // creates an array of objects
45 template <class T>
46 T* createArray(int len) {
47 void* addr = allocate(sizeof(T) * len);
48 T* addrT = (T*)addr;
49 // call constructor
50#ifndef NO_INPLACE_INIT_SUPPORT
51 for (int j = 0; j < len; j++) new (addrT + j) T();
52#else
53 T default_value;
54 for (int j = 0; j < len; j++) {
55 memcpy((uint8_t*)addr + (j * sizeof(T)), &default_value, sizeof(T));
56 }
57#endif
58 return (T*)addr;
59 }
60
61 // deletes an array of objects
62 template <class T>
63 void removeArray(T* obj, int len) {
64 if (obj == nullptr) return;
65 for (int j = 0; j < len; j++) {
66 obj[j].~T();
67 }
68 free((void*)obj);
69 }
70
72 virtual void* allocate(size_t size) {
73 void* result = do_allocate(size);
74 if (result == nullptr) {
75 LOGE("Allocateation failed for %zu bytes", size);
76 stop();
77 } else {
78 LOGD("Allocated %zu", size);
79 }
80 return result;
81 }
82
84 virtual void free(void* memory) {
85 if (memory != nullptr) ::free(memory);
86 }
87
88 protected:
89 virtual void* do_allocate(size_t size) {
90 return calloc(1, size == 0 ? 1 : size);
91 }
92};
93
101class AllocatorExt : public Allocator {
102 void* do_allocate(size_t size) {
103 void* result = nullptr;
104 if (size == 0) size = 1;
105#if defined(USE_PSRAM) && defined(ARDUINO)
106 result = ps_malloc(size);
107#endif
108 if (result == nullptr) result = malloc(size);
109 if (result == nullptr) {
110 LOGE("allocateation failed for %zu bytes", size);
111 stop();
112 }
113 // initialize object
114 memset(result, 0, size);
115 return result;
116 }
117};
118
119#if (defined(ESP32)) && defined(ARDUINO)
120
129class AllocatorESP32 : public Allocator {
130 public:
131 AllocatorESP32(int caps = MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL) {
132 this->caps = caps;
133 }
134 void* do_allocate(size_t size) {
135 void* result = nullptr;
136 if (size == 0) size = 1;
137 result = heap_caps_calloc(1, size, caps);
138 if (result == nullptr) {
139 LOGE("alloc failed for %zu bytes", size);
140 stop();
141 }
142 return result;
143 }
144
145 protected:
146 int caps = 0;
147};
148
149static AllocatorESP32 DefaultESP32AllocatorRAM;
150
151#endif
152
153#if defined(USE_PSRAM) && defined(ARDUINO)
154
163class AllocatorPSRAM : public Allocator {
164 void* do_allocate(size_t size) {
165 if (size == 0) size = 1;
166 void* result = nullptr;
167 result = ps_calloc(1, size);
168 if (result == nullptr) {
169 LOGE("allocateation failed for %zu bytes", size);
170 stop();
171 }
172 return result;
173 }
174};
175
176#endif
177
178#if defined(IS_ZEPHYR)
179#include <zephyr/kernel.h>
180
187 public:
188 void* do_allocate(size_t size) override {
189 if (size == 0) size = 1;
190 void* result = k_malloc(size);
191 if (result != nullptr) memset(result, 0, size);
192 return result;
193 }
194
195 void free(void* memory) override {
196 if (memory != nullptr) k_free(memory);
197 }
198};
201
202#else // defined(ZEPHYR)
203// all non zephyr cases
206#endif
207
210
211} // namespace audio_tools
#define LOGD(...)
Definition AudioLoggerIDF.h:27
#define LOGE(...)
Definition AudioLoggerIDF.h:30
Memory allocateator which uses ps_malloc (on the ESP32) and if this fails it resorts to malloc.
Definition Allocator.h:101
Memory allocateator which uses malloc.
Definition Allocator.h:25
T * createArray(int len)
Definition Allocator.h:46
virtual void free(void *memory)
frees memory
Definition Allocator.h:84
virtual void * allocate(size_t size)
Allocates memory.
Definition Allocator.h:72
void removeArray(T *obj, int len)
Definition Allocator.h:63
virtual void * do_allocate(size_t size)
Definition Allocator.h:89
T * create()
Definition Allocator.h:29
void remove(T *obj)
deletes an object
Definition Allocator.h:38
Zephyr allocator using the kernel heap (k_malloc / k_free). This is the standard allocator for Zephyr...
Definition Allocator.h:186
void * do_allocate(size_t size) override
Definition Allocator.h:188
void free(void *memory) override
frees memory
Definition Allocator.h:195
void stop()
stops any further processing by spinning in an endless loop
Definition AudioRuntime.h:71
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition LMSEchoCancellationStream.h:6
static TAllocatorExt DefaultAllocator
Definition Allocator.h:208
static TAllocatorSTD DefaultAllocatorRAM
Definition Allocator.h:209