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
23#include <idfxx/error.hpp>
24#include <idfxx/flags.hpp>
25
26#include <cstddef>
27#include <cstdint>
28#include <esp_heap_caps.h>
29#include <limits>
30#include <type_traits>
31
69
70template<>
72
73// ---- Heap info queries ------------------------------------------------------
74
75namespace idfxx::memory {
76
93
101[[nodiscard]] inline size_t total_size(flags<caps> c) noexcept {
103}
104
112[[nodiscard]] inline size_t free_size(flags<caps> c) noexcept {
114}
115
126
140
148[[nodiscard]] inline info get_info(flags<caps> c) noexcept {
149 multi_heap_info_t raw{};
151 return {
152 .total_free_bytes = raw.total_free_bytes,
153 .total_allocated_bytes = raw.total_allocated_bytes,
154 .largest_free_block = raw.largest_free_block,
155 .minimum_free_bytes = raw.minimum_free_bytes,
156 .allocated_blocks = raw.allocated_blocks,
157 .free_blocks = raw.free_blocks,
158 .total_blocks = raw.total_blocks,
159 };
160}
161
162// ---- Heap walking -----------------------------------------------------------
163
172
177struct block {
178 void* ptr;
179 size_t size;
180 bool used;
181};
182
195template<typename F>
198 auto& fn = *static_cast<std::remove_reference_t<F>*>(user_data);
200 };
202}
203
214template<typename F>
215void walk(F&& walker) {
217 auto& fn = *static_cast<std::remove_reference_t<F>*>(user_data);
219 };
221}
222
223// ---- Heap integrity checking ------------------------------------------------
224
238
243
244// ---- Heap dump --------------------------------------------------------------
245
255inline void dump(flags<caps> c) noexcept {
257}
258
260inline void dump() noexcept {
262}
263
264} // namespace idfxx::memory
265
266// ---- Heap allocation --------------------------------------------------------
267
268namespace idfxx {
269
278[[nodiscard]] inline void* malloc(size_t size, flags<memory::caps> c) noexcept {
279 return heap_caps_malloc(size, to_underlying(c));
280}
281
291[[nodiscard]] inline void* calloc(size_t n, size_t size, flags<memory::caps> c) noexcept {
292 return heap_caps_calloc(n, size, to_underlying(c));
293}
294
307[[nodiscard]] inline void* realloc(void* ptr, size_t size, flags<memory::caps> c) noexcept {
308 return heap_caps_realloc(ptr, size, to_underlying(c));
309}
310
319inline void free(void* ptr) noexcept {
320 heap_caps_free(ptr);
321}
322
332[[nodiscard]] inline void* aligned_alloc(size_t alignment, size_t size, flags<memory::caps> c) noexcept {
334}
335
346[[nodiscard]] inline void* aligned_calloc(size_t alignment, size_t n, size_t size, flags<memory::caps> c) noexcept {
348}
349
350// ---- STL-compatible allocators ----------------------------------------------
351
366template<typename T, flags<memory::caps> Caps, size_t Alignment = 0>
368 using value_type = T;
375 template<typename U>
379
381 caps_allocator() = default;
382
388 template<typename U>
390
405 [[nodiscard]] T* allocate(size_t n) {
406 if (n > std::numeric_limits<size_t>::max() / sizeof(T)) {
407 raise_no_mem();
408 }
409 void* p;
410 if constexpr (Alignment == 0) {
411 p = heap_caps_malloc(n * sizeof(T), to_underlying(Caps));
412 } else {
413 static_assert((Alignment & (Alignment - 1)) == 0, "Alignment must be a power of two");
415 }
416 if (!p) {
417 raise_no_mem();
418 }
419 return static_cast<T*>(p);
420 }
421
427 void deallocate(T* p, size_t) noexcept { heap_caps_free(p); }
428};
429
437template<typename T, typename U, flags<memory::caps> Caps, size_t Alignment>
438constexpr bool
442
453template<typename T>
455
468template<typename T>
470
481template<typename T>
483
493template<typename T, size_t Alignment>
495
507template<typename T, size_t Alignment>
509
519template<typename T, size_t Alignment>
521
// end of idfxx_core_memory // end of idfxx_core
524
525} // namespace idfxx
IDFXX error handling.
Type-safe bitflags from scoped enums.
size_t total_size(flags< caps > c) noexcept
Returns the total size of heap regions matching the given capabilities.
Definition memory.hpp:101
void free(void *ptr) noexcept
Frees memory previously allocated by heap allocation functions.
Definition memory.hpp:319
void * realloc(void *ptr, size_t size, flags< memory::caps > c) noexcept
Reallocates memory from heap regions matching the given capabilities.
Definition memory.hpp:307
void walk(flags< caps > c, F &&walker)
Walk all heap blocks in regions matching the given capabilities.
Definition memory.hpp:196
size_t largest_free_block
Size of the largest contiguous free block.
Definition memory.hpp:87
void * malloc(size_t size, flags< memory::caps > c) noexcept
Allocates memory from heap regions matching the given capabilities.
Definition memory.hpp:278
constexpr bool operator==(const caps_allocator< T, Caps, Alignment > &, const caps_allocator< U, Caps, Alignment > &) noexcept
Equality comparison for caps_allocator.
Definition memory.hpp:439
size_t largest_free_block(flags< caps > c) noexcept
Returns the largest free block in heap regions matching the given capabilities.
Definition memory.hpp:123
void * ptr
Pointer to the block data.
Definition memory.hpp:178
intptr_t end
End address of the heap region.
Definition memory.hpp:170
size_t total_blocks
Total number of blocks (allocated + free).
Definition memory.hpp:91
size_t free_blocks
Number of free blocks.
Definition memory.hpp:90
bool used
True if allocated, false if free.
Definition memory.hpp:180
caps_allocator< T, memory::caps::dma > dma_allocator
STL-compatible allocator for DMA-capable memory.
Definition memory.hpp:482
info get_info(flags< caps > c) noexcept
Returns detailed heap statistics for regions matching the given capabilities.
Definition memory.hpp:148
caps_allocator< T, memory::caps::dma, Alignment > aligned_dma_allocator
STL-compatible aligned allocator for DMA-capable memory.
Definition memory.hpp:520
size_t total_allocated_bytes
Total allocated bytes across matching regions.
Definition memory.hpp:86
caps
Memory capability flags for heap allocations.
Definition memory.hpp:48
bool check_integrity(flags< caps > c, bool print_errors=false) noexcept
Check integrity of heaps with the given capabilities, or all heaps.
Definition memory.hpp:235
void * aligned_alloc(size_t alignment, size_t size, flags< memory::caps > c) noexcept
Allocates aligned memory from heap regions matching the given capabilities.
Definition memory.hpp:332
size_t size
Size of the block in bytes.
Definition memory.hpp:179
void * calloc(size_t n, size_t size, flags< memory::caps > c) noexcept
Allocates zero-initialized memory from heap regions matching the given capabilities.
Definition memory.hpp:291
size_t free_size(flags< caps > c) noexcept
Returns the current free size of heap regions matching the given capabilities.
Definition memory.hpp:112
intptr_t start
Start address of the heap region.
Definition memory.hpp:169
size_t total_free_bytes
Total free bytes across matching regions.
Definition memory.hpp:85
size_t allocated_blocks
Number of allocated blocks.
Definition memory.hpp:89
caps_allocator< T, memory::caps::spiram, Alignment > aligned_spiram_allocator
STL-compatible aligned allocator for external PSRAM (SPI RAM).
Definition memory.hpp:508
size_t minimum_free_bytes
Minimum free bytes since boot (high-water mark).
Definition memory.hpp:88
size_t minimum_free_size(flags< caps > c) noexcept
Returns the minimum free size since boot for heap regions matching the given capabilities.
Definition memory.hpp:137
caps_allocator< T, memory::caps::spiram > spiram_allocator
STL-compatible allocator for external PSRAM (SPI RAM).
Definition memory.hpp:469
void dump() noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition memory.hpp:260
void * aligned_calloc(size_t alignment, size_t n, size_t size, flags< memory::caps > c) noexcept
Allocates aligned, zero-initialized memory from heap regions matching the given capabilities.
Definition memory.hpp:346
caps_allocator< T, memory::caps::dram, Alignment > aligned_dram_allocator
STL-compatible aligned allocator for internal DRAM.
Definition memory.hpp:494
caps_allocator< T, memory::caps::dram > dram_allocator
STL-compatible allocator for internal DRAM.
Definition memory.hpp:454
@ cache_aligned
Cache-line aligned.
@ iram
IRAM with unaligned access.
@ dma_desc_ahb
AHB DMA descriptor capable.
@ default_heap
Default heap (same as malloc)
@ dma_desc_axi
AXI DMA descriptor capable.
@ retention
Retention DMA accessible.
@ rtc
RTC fast memory.
@ dma
DMA-capable memory.
@ access_32bit
32-bit aligned access
@ internal
Internal memory.
@ spiram
External PSRAM.
@ access_8bit
8/16/32-bit access
@ dram
Internal DRAM (8-bit accessible)
std::expected< T, std::error_code > result
result type wrapping a value or error code.
Definition error.hpp:120
constexpr auto to_underlying(flags< E > f) noexcept
Returns the underlying integral value of a flags object.
Definition flags.hpp:292
Rebind the allocator to a different type.
Definition memory.hpp:376
caps_allocator< U, Caps, Alignment > other
The rebound allocator type.
Definition memory.hpp:377
STL-compatible allocator for capability-based memory regions.
Definition memory.hpp:367
caps_allocator()=default
Default constructor.
T * allocate(size_t n)
Allocates memory for n objects of type T.
Definition memory.hpp:405
T value_type
The type of object to allocate.
Definition memory.hpp:368
void deallocate(T *p, size_t) noexcept
Deallocates memory previously allocated by this allocator.
Definition memory.hpp:427
constexpr caps_allocator(const caps_allocator< U, Caps, Alignment > &) noexcept
Rebinding copy constructor.
Definition memory.hpp:389
Information about a single heap block, passed to walk callbacks.
Definition memory.hpp:177
Heap region statistics.
Definition memory.hpp:84
Information about a heap region, passed to walk callbacks.
Definition memory.hpp:168