Arduino DLNA Server
Loading...
Searching...
No Matches
Allocator.h
Go to the documentation of this file.
1#pragma once
2#include <stdlib.h>
3#include <memory>
4#include <limits>
5#include <assert.h>
6#include <type_traits>
7
8#ifdef ESP32
9#include <esp_heap_caps.h>
10#endif
11#include "dlna_config.h"
12
13namespace tiny_dlna {
14
23template <typename T>
25 public:
26 using value_type = T;
27 using pointer = T*;
28 using const_pointer = const T*;
29 using reference = T&;
30 using const_reference = const T&;
31 using size_type = std::size_t;
32 using difference_type = std::ptrdiff_t;
33 using is_always_equal = std::true_type;
35
39 AllocatorPSRAM() noexcept {}
40
46 template <typename U>
48
56 if (n==0) return nullptr;
57 // if (n > std::numeric_limits<size_type>::max() / sizeof(T))
58 // throw std::bad_alloc();
59 // in Arduino excepitons are disabled!
60 assert(n <= std::numeric_limits<size_type>::max() / sizeof(T));
61
62#ifdef ESP32
63 pointer p = static_cast<pointer>(
64 heap_caps_malloc(n * sizeof(T), MALLOC_CAP_SPIRAM));
65 if (p == nullptr) {
66 p = static_cast<pointer>(malloc(n * sizeof(T)));
67 }
68 assert(p);
69#else
70 pointer p = static_cast<pointer>(malloc(n * sizeof(T)));
71 if (!p) throw std::bad_alloc();
72#endif
73 return p;
74 }
75
81 void deallocate(pointer p, size_type) noexcept {
82#ifdef ESP32
83 if (p) heap_caps_free(p);
84#else
85 if (p) free(p);
86#endif
87 }
88
93 template <typename U>
94 struct rebind {
96 };
97
98 // Allocators compare equal (stateless)
99 template <typename U>
100 bool operator==(const AllocatorPSRAM<U>&) const noexcept { return true; }
101 template <typename U>
102 bool operator!=(const AllocatorPSRAM<U>&) const noexcept { return false; }
103};
104
105// Alias for a std::string that stores its dynamic buffer in PSRAM
106using stringPSRAM = std::basic_string<char, std::char_traits<char>, AllocatorPSRAM<char>>;
107
108
109
110} // namespace tiny_dlna
Custom allocator that uses ESP32's PSRAM for memory allocation.
Definition: Allocator.h:24
std::size_t size_type
Definition: Allocator.h:31
bool operator!=(const AllocatorPSRAM< U > &) const noexcept
Definition: Allocator.h:102
const T & const_reference
Definition: Allocator.h:30
std::true_type is_always_equal
Definition: Allocator.h:33
const T * const_pointer
Definition: Allocator.h:28
T value_type
Definition: Allocator.h:26
pointer allocate(size_type n)
Allocate memory from PSRAM.
Definition: Allocator.h:55
void deallocate(pointer p, size_type) noexcept
Deallocate memory.
Definition: Allocator.h:81
AllocatorPSRAM(const AllocatorPSRAM< U > &) noexcept
Copy constructor from another allocator type.
Definition: Allocator.h:47
std::true_type propagate_on_container_move_assignment
Definition: Allocator.h:34
AllocatorPSRAM() noexcept
Default constructor.
Definition: Allocator.h:39
T * pointer
Definition: Allocator.h:27
bool operator==(const AllocatorPSRAM< U > &) const noexcept
Definition: Allocator.h:100
std::ptrdiff_t difference_type
Definition: Allocator.h:32
T & reference
Definition: Allocator.h:29
Definition: Allocator.h:13
std::basic_string< char, std::char_traits< char >, AllocatorPSRAM< char > > stringPSRAM
Definition: Allocator.h:106
Rebind allocator to another type.
Definition: Allocator.h:94