idfxx 1.0.0
Modern C++23 components for ESP-IDF
Loading...
Searching...
No Matches
memory.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 Chris Leishman
3
4#pragma once
5
21#include <cstddef>
22#include <cstdint>
23#include <esp_heap_caps.h>
24#include <esp_system.h>
25#include <new>
26
27namespace idfxx {
28
42enum class memory_type : uint32_t {
43 internal = MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT,
44 spiram = MALLOC_CAP_SPIRAM,
45};
46
58template<typename T>
60 using value_type = T;
63 dram_allocator() = default;
64
70 template<typename U>
71 constexpr dram_allocator(const dram_allocator<U>&) noexcept {}
72
84 [[nodiscard]] T* allocate(size_t n) {
85 void* p = heap_caps_malloc(n * sizeof(T), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
86 if (!p) {
87#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
88 throw std::bad_alloc();
89#else
90 esp_system_abort("dram_allocator: allocation failed");
91#endif
92 }
93 return static_cast<T*>(p);
94 }
95
101 void deallocate(T* p, size_t) noexcept { heap_caps_free(p); }
102};
103
111template<typename T, typename U>
113 return true;
114}
115
123template<typename T, typename U>
125 return false;
126}
127
141template<typename T>
143 using value_type = T;
146 spiram_allocator() = default;
147
153 template<typename U>
154 constexpr spiram_allocator(const spiram_allocator<U>&) noexcept {}
155
167 [[nodiscard]] T* allocate(size_t n) {
168 void* p = heap_caps_malloc(n * sizeof(T), MALLOC_CAP_SPIRAM);
169 if (!p) {
170#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
171 throw std::bad_alloc();
172#else
173 esp_system_abort("spiram_allocator: allocation failed");
174#endif
175 }
176 return static_cast<T*>(p);
177 }
178
184 void deallocate(T* p, size_t) noexcept { heap_caps_free(p); }
185};
186
194template<typename T, typename U>
196 return true;
197}
198
206template<typename T, typename U>
208 return false;
209}
210
221template<typename T>
223 using value_type = T;
226 dma_allocator() = default;
227
233 template<typename U>
234 constexpr dma_allocator(const dma_allocator<U>&) noexcept {}
235
247 [[nodiscard]] T* allocate(size_t n) {
248 void* p = heap_caps_malloc(n * sizeof(T), MALLOC_CAP_DMA);
249 if (!p) {
250#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
251 throw std::bad_alloc();
252#else
253 esp_system_abort("dma_allocator: allocation failed");
254#endif
255 }
256 return static_cast<T*>(p);
257 }
258
264 void deallocate(T* p, size_t) noexcept { heap_caps_free(p); }
265};
266
274template<typename T, typename U>
276 return true;
277}
278
286template<typename T, typename U>
288 return false;
289}
290
// end of idfxx_core_memory // end of idfxx_core
293
294} // namespace idfxx
bool operator!=(const dram_allocator< T > &, const dram_allocator< U > &)
Inequality comparison for dram_allocator.
Definition memory.hpp:124
bool operator==(const dram_allocator< T > &, const dram_allocator< U > &)
Equality comparison for dram_allocator.
Definition memory.hpp:112
memory_type
Memory region type for heap allocations.
Definition memory.hpp:42
@ internal
Internal DRAM (default)
@ spiram
External PSRAM.
STL-compatible allocator for DMA-capable memory.
Definition memory.hpp:222
dma_allocator()=default
Default constructor.
constexpr dma_allocator(const dma_allocator< U > &) noexcept
Rebinding copy constructor.
Definition memory.hpp:234
T * allocate(size_t n)
Allocates DMA-capable memory for n objects of type T.
Definition memory.hpp:247
T value_type
The type of object to allocate.
Definition memory.hpp:223
void deallocate(T *p, size_t) noexcept
Deallocates memory previously allocated by this allocator.
Definition memory.hpp:264
STL-compatible allocator for internal DRAM.
Definition memory.hpp:59
constexpr dram_allocator(const dram_allocator< U > &) noexcept
Rebinding copy constructor.
Definition memory.hpp:71
T * allocate(size_t n)
Allocates memory for n objects of type T from internal DRAM.
Definition memory.hpp:84
dram_allocator()=default
Default constructor.
void deallocate(T *p, size_t) noexcept
Deallocates memory previously allocated by this allocator.
Definition memory.hpp:101
T value_type
The type of object to allocate.
Definition memory.hpp:60
STL-compatible allocator for external PSRAM (SPI RAM).
Definition memory.hpp:142
T value_type
The type of object to allocate.
Definition memory.hpp:143
void deallocate(T *p, size_t) noexcept
Deallocates memory previously allocated by this allocator.
Definition memory.hpp:184
spiram_allocator()=default
Default constructor.
constexpr spiram_allocator(const spiram_allocator< U > &) noexcept
Rebinding copy constructor.
Definition memory.hpp:154
T * allocate(size_t n)
Allocates memory for n objects of type T from external PSRAM.
Definition memory.hpp:167