arduino-audio-tools
Allocator.h
1 #pragma once
2 #include <stdlib.h>
3 
4 #include "AudioConfig.h"
5 #include "AudioTools/CoreAudio/AudioLogger.h"
6 
7 namespace audio_tools {
8 
22 class Allocator {
23  public:
24  // creates an object
25  template <class T>
26  T* create() {
27  void* addr = allocate(sizeof(T));
28  // call constructor
29  T* ref = new (addr) T();
30  return ref;
31  }
32 
34  template <class T>
35  void remove(T* obj) {
36  if (obj == nullptr) return;
37  obj->~T();
38  free((void*)obj);
39  }
40 
41  // creates an array of objects
42  template <class T>
43  T* createArray(int len) {
44  void* addr = allocate(sizeof(T) * len);
45  T* addrT = (T*)addr;
46  // call constructor
47  for (int j = 0; j < len; j++) new (addrT+j) T();
48  return (T*)addr;
49  }
50 
51  // deletes an array of objects
52  template <class T>
53  void removeArray(T* obj, int len) {
54  if (obj == nullptr) return;
55  for (int j = 0; j < len; j++) {
56  obj[j].~T();
57  }
58  free((void*)obj);
59  }
60 
62  virtual void* allocate(size_t size) {
63  void* result = do_allocate(size);
64  if (result == nullptr) {
65  LOGE("Allocateation failed for %zu bytes", size);
66  stop();
67  } else {
68  LOGD("Allocated %zu", size);
69  }
70  return result;
71  }
72 
74  virtual void free(void* memory) {
75  if (memory != nullptr) ::free(memory);
76  }
77 
78  protected:
79  virtual void* do_allocate(size_t size) {
80  return calloc(1, size == 0 ? 1 : size);
81  }
82 };
83 
91 class AllocatorExt : public Allocator {
92  void* do_allocate(size_t size) {
93  void* result = nullptr;
94  if (size == 0) size = 1;
95 #if defined(ESP32) && defined(ARDUINO)
96  result = ps_malloc(size);
97 #endif
98  if (result == nullptr) result = malloc(size);
99  if (result == nullptr) {
100  LOGE("allocateation failed for %zu bytes", size);
101  stop();
102  }
103  // initialize object
104  memset(result, 0, size);
105  return result;
106  }
107 };
108 
109 #if defined(ESP32) && defined(ARDUINO)
110 
119 class AllocatorPSRAM : public Allocator {
120  void* do_allocate(size_t size) {
121  if (size == 0) size = 1;
122  void* result = nullptr;
123  result = ps_calloc(1, size);
124  if (result == nullptr) {
125  LOGE("allocateation failed for %zu bytes", size);
126  stop();
127  }
128  return result;
129  }
130 };
131 
132 #endif
133 
134 static AllocatorExt DefaultAllocator;
135 
136 } // namespace audio_tools
Memory allocateator which uses ps_malloc (on the ESP32) and if this fails it resorts to malloc.
Definition: Allocator.h:91
Memory allocateator which uses malloc.
Definition: Allocator.h:22
virtual void * allocate(size_t size)
Allocates memory.
Definition: Allocator.h:62
virtual void free(void *memory)
frees memory
Definition: Allocator.h:74
void remove(T *obj)
deletes an object
Definition: Allocator.h:35
void stop()
Public generic methods.
Definition: AudioRuntime.h:27
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition: AudioConfig.h:868