Arduino DLNA Server
Allocator.h
Go to the documentation of this file.
1 #pragma once
2 #include <stdlib.h>
3 
4 #include "basic/Logger.h"
5 
6 namespace tiny_dlna {
7 
21 class Allocator {
22  public:
23  // creates an object
24  template <class T>
25  T* create() {
26  void* addr = allocate(sizeof(T));
27  // call constructor
28  T* ref = new (addr) T();
29  return ref;
30  }
31 
33  template <class T>
34  void remove(T* obj) {
35  if (obj == nullptr) return;
36  obj->~T();
37  free((void*)obj);
38  }
39 
40  // creates an array of objects
41  template <class T>
42  T* createArray(int len) {
43  void* addr = allocate(sizeof(T) * len);
44  T* addrT = (T*)addr;
45  // call constructor
46  for (int j = 0; j < len; j++) new (addrT + j) T();
47  return (T*)addr;
48  }
49 
50  // deletes an array of objects
51  template <class T>
52  void removeArray(T* obj, int len) {
53  if (obj == nullptr) return;
54  for (int j = 0; j < len; j++) {
55  obj[j].~T();
56  }
57  free((void*)obj);
58  }
59 
61  virtual void* allocate(size_t size) {
62  void* result = do_allocate(size);
63  if (result == nullptr) {
64  DlnaLogger.log(DlnaError, "Allocateation failed for %zu bytes", size);
65  stop();
66  } else {
67  DlnaLogger.log(DlnaDebug, "Allocated %zu", size);
68  }
69  return result;
70  }
71 
72  void stop() {
73  while (true) delay(1000);
74  }
75 
77  virtual void free(void* memory) {
78  if (memory != nullptr) ::free(memory);
79  }
80 
81  protected:
82  virtual void* do_allocate(size_t size) {
83  return calloc(1, size == 0 ? 1 : size);
84  }
85 };
86 
94 class AllocatorExt : public Allocator {
95  void* do_allocate(size_t size) {
96  void* result = nullptr;
97  if (size == 0) size = 1;
98 #if defined(ESP32) && defined(ARDUINO)
99  result = ps_malloc(size);
100 #endif
101  if (result == nullptr) result = malloc(size);
102  if (result == nullptr) {
103  DlnaLogger.log(DlnaError, "allocateation failed for %zu bytes", size);
104  stop();
105  }
106  // initialize object
107  memset(result, 0, size);
108  return result;
109  }
110 };
111 
112 #if defined(ESP32) && defined(ARDUINO)
113 
122 class AllocatorPSRAM : public Allocator {
123  void* do_allocate(size_t size) {
124  if (size == 0) size = 1;
125  void* result = nullptr;
126  result = ps_calloc(1, size);
127  if (result == nullptr) {
128  DlnaLogger.log(DlnaError, "allocateation failed for %zu bytes", size);
129  stop();
130  }
131  return result;
132  }
133 };
134 
135 #endif
136 
137 static AllocatorExt DefaultAllocator;
138 
139 } // namespace tiny_dlna
Memory allocateator which uses ps_malloc (on the ESP32) and if this fails it resorts to malloc.
Definition: Allocator.h:94
Memory allocateator which uses malloc.
Definition: Allocator.h:21
void remove(T *obj)
deletes an object
Definition: Allocator.h:34
void removeArray(T *obj, int len)
Definition: Allocator.h:52
virtual void * do_allocate(size_t size)
Definition: Allocator.h:82
T * create()
Definition: Allocator.h:25
T * createArray(int len)
Definition: Allocator.h:42
virtual void * allocate(size_t size)
Allocates memory.
Definition: Allocator.h:61
virtual void free(void *memory)
frees memory
Definition: Allocator.h:77
void stop()
Definition: Allocator.h:72
void log(DlnaLogLevel current_level, const char *fmt...)
Print log message.
Definition: Logger.h:40
Definition: Allocator.h:6
@ DlnaDebug
Definition: Logger.h:16
@ DlnaError
Definition: Logger.h:16
LoggerClass DlnaLogger
Definition: Logger.cpp:5