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