arduino-audio-tools
Loading...
Searching...
No Matches
Allocator.h
1#pragma once
2#include <stdlib.h>
3
4#include "AudioToolsConfig.h"
5#include "AudioTools/CoreAudio/AudioLogger.h"
6#include "AudioTools/CoreAudio/AudioRuntime.h"
7
8namespace audio_tools {
9
23class Allocator {
24 public:
25 // creates an object
26 template <class T>
27 T* create() {
28 void* addr = allocate(sizeof(T));
29 // call constructor
30 T* ref = new (addr) T();
31 return ref;
32 }
33
35 template <class T>
36 void remove(T* obj) {
37 if (obj == nullptr) return;
38 obj->~T();
39 free((void*)obj);
40 }
41
42 // creates an array of objects
43 template <class T>
44 T* createArray(int len) {
45 void* addr = allocate(sizeof(T) * len);
46 T* addrT = (T*)addr;
47 // call constructor
48#ifndef NO_INPLACE_INIT_SUPPORT
49 for (int j = 0; j < len; j++) new (addrT + j) T();
50#else
51 T default_value;
52 for (int j = 0; j < len; j++) {
53 memcpy((uint8_t*)addr+(j*sizeof(T)), &default_value, sizeof(T));
54 }
55#endif
56 return (T*)addr;
57 }
58
59 // deletes an array of objects
60 template <class T>
61 void removeArray(T* obj, int len) {
62 if (obj == nullptr) return;
63 for (int j = 0; j < len; j++) {
64 obj[j].~T();
65 }
66 free((void*)obj);
67 }
68
70 virtual void* allocate(size_t size) {
71 void* result = do_allocate(size);
72 if (result == nullptr) {
73 LOGE("Allocateation failed for %zu bytes", size);
74 stop();
75 } else {
76 LOGD("Allocated %zu", size);
77 }
78 return result;
79 }
80
82 virtual void free(void* memory) {
83 if (memory != nullptr) ::free(memory);
84 }
85
86 protected:
87 virtual void* do_allocate(size_t size) {
88 return calloc(1, size == 0 ? 1 : size);
89 }
90};
91
99class AllocatorExt : public Allocator {
100 void* do_allocate(size_t size) {
101 void* result = nullptr;
102 if (size == 0) size = 1;
103#if (defined(RP2040) || defined(ESP32)) && defined(ARDUINO)
104 result = ps_malloc(size);
105#endif
106 if (result == nullptr) result = malloc(size);
107 if (result == nullptr) {
108 LOGE("allocateation failed for %zu bytes", size);
109 stop();
110 }
111 // initialize object
112 memset(result, 0, size);
113 return result;
114 }
115};
116
117
118#if (defined(ESP32)) && defined(ARDUINO)
119
128 class AllocatorESP32 : public Allocator {
129 AllocatorESP32(int caps = MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL) {
130 this->caps = caps;
131 }
132 void* do_allocate(size_t size) {
133 void* result = nullptr;
134 if (size == 0) size = 1;
135 result = heap_caps_calloc(1, size, caps);
136 if (result == nullptr) {
137 LOGE("alloc failed for %zu bytes", size);
138 stop();
139 }
140 return result;
141 }
142
143 protected:
144 int caps = 0;
145};
146
147#endif
148
149#if (defined(RP2040) || defined(ESP32)) && defined(ARDUINO)
150
159class AllocatorPSRAM : public Allocator {
160 void* do_allocate(size_t size) {
161 if (size == 0) size = 1;
162 void* result = nullptr;
163 result = ps_calloc(1, size);
164 if (result == nullptr) {
165 LOGE("allocateation failed for %zu bytes", size);
166 stop();
167 }
168 return result;
169 }
170};
171
172#endif
173
174static AllocatorExt DefaultAllocator;
175
176} // namespace audio_tools
Memory allocateator which uses ps_malloc (on the ESP32) and if this fails it resorts to malloc.
Definition Allocator.h:99
Memory allocateator which uses malloc.
Definition Allocator.h:23
virtual void free(void *memory)
frees memory
Definition Allocator.h:82
virtual void * allocate(size_t size)
Allocates memory.
Definition Allocator.h:70
void remove(T *obj)
deletes an object
Definition Allocator.h:36
void stop()
Public generic methods.
Definition AudioRuntime.h:14
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy