TinyRobotics
Loading...
Searching...
No Matches
AllocatorPSRAM.h
1#pragma once
2#include "assert.h"
3
4#ifdef ESP32
5#include <esp_heap_caps.h>
6#endif
7
8namespace tinyrobotics {
9
10/**
11 * @class AllocatorPSRAM
12 * @ingroup utils
13 * @brief Custom allocator that uses ESP32's PSRAM for memory allocation
14 * @tparam T Type of elements to allocate
15 *
16 * This allocator uses ESP32's heap_caps_malloc with MALLOC_CAP_SPIRAM flag
17 * to ensure all memory is allocated in PSRAM instead of regular RAM. If PSRAM
18 * allocation fails, it falls back to regular RAM.
19 */
20template <typename T>
21class AllocatorPSRAM {
22 public:
23 using value_type = T;
24 using pointer = T*;
25 using const_pointer = const T*;
26 using reference = T&;
27 using const_reference = const T&;
28 using size_type = std::size_t;
30
31 /**
32 * @brief Default constructor
33 */
34 AllocatorPSRAM() noexcept {}
35
36 /**
37 * @brief Copy constructor from another allocator type
38 * @tparam U Type of the other allocator
39 * @param other The other allocator
40 */
41 template <typename U>
42 AllocatorPSRAM(const AllocatorPSRAM<U>&) noexcept {}
43
44 /**
45 * @brief Allocate memory from PSRAM
46 * @param n Number of elements to allocate
47 * @return Pointer to allocated memory
48 * @throws std::bad_alloc If allocation fails or size is too large
49 */
50 pointer allocate(size_type n) {
51 // if (n > std::numeric_limits<size_type>::max() / sizeof(T))
52 // throw std::bad_alloc();
53 // in Arduino excepitons are disabled!
54 assert(n <= std::numeric_limits<size_type>::max() / sizeof(T));
55
56 pointer p = nullptr;
57#ifdef ESP32
58 p = static_cast<pointer>(
59 heap_caps_malloc(n * sizeof(T), MALLOC_CAP_SPIRAM));
60#endif
61#if defined(ARDUINO_ARCH_RP2040) && defined(RP2350_PSRAM_CS)
62 p = pmalloc(n * sizeof(T));
63#endif
64
65 if (p == nullptr) {
66 p = static_cast<pointer>(malloc(n * sizeof(T)));
67 }
68
69 // if (!p) throw std::bad_alloc();
70
71 // in Arduino excepitons are disabled!
72 assert(p);
73
74 return p;
75 }
76
77 /**
78 * @brief Deallocate memory
79 * @param p Pointer to memory to deallocate
80 * @param size Size of allocation (unused)
81 */
82 void deallocate(pointer p, size_type) noexcept {
83#ifdef ESP32
84 heap_caps_free(p);
85#else
86 free(p);
87#endif
88 }
89
90 // Required for STL allocator compatibility
91 template <typename U>
92 bool operator==(const AllocatorPSRAM<U>&) const noexcept {
93 return true;
94 }
95 template <typename U>
96 bool operator!=(const AllocatorPSRAM<U>&) const noexcept {
97 return false;
98 }
99 /**
100 * @brief Rebind allocator to another type
101 * @tparam U Type to rebind the allocator to
102 */
103 template <typename U>
104 struct rebind {
105 using other = AllocatorPSRAM<U>;
106 };
107};
108
109} // namespace tinyrobotics
Custom allocator that uses ESP32's PSRAM for memory allocation.
Definition: AllocatorPSRAM.h:21
void deallocate(pointer p, size_type) noexcept
Deallocate memory.
Definition: AllocatorPSRAM.h:82
pointer allocate(size_type n)
Allocate memory from PSRAM.
Definition: AllocatorPSRAM.h:50
AllocatorPSRAM() noexcept
Default constructor.
Definition: AllocatorPSRAM.h:34
AllocatorPSRAM(const AllocatorPSRAM< U > &) noexcept
Copy constructor from another allocator type.
Definition: AllocatorPSRAM.h:42
Rebind allocator to another type.
Definition: AllocatorPSRAM.h:104