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<capabilities> c) noexcept {
103}
104
112[[nodiscard]] inline size_t free_size(flags<capabilities> c) noexcept {
114}
115
126
140
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<capabilities> 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::capabilities> 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::capabilities> 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::capabilities> 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::capabilities> c) noexcept {
334}
335
346[[nodiscard]] inline void*
347aligned_calloc(size_t alignment, size_t n, size_t size, flags<memory::capabilities> c) noexcept {
349}
350
351// ---- STL-compatible allocators ----------------------------------------------
352
367template<typename T, flags<memory::capabilities> Caps, size_t Alignment = 0>
368struct allocator {
369 using value_type = T;
376 template<typename U>
380
382 allocator() = default;
383
389 template<typename U>
390 constexpr allocator(const allocator<U, Caps, Alignment>&) noexcept {}
391
406 [[nodiscard]] T* allocate(size_t n) {
407 if (n > std::numeric_limits<size_t>::max() / sizeof(T)) {
408 raise_no_mem();
409 }
410 void* p;
411 if constexpr (Alignment == 0) {
412 p = heap_caps_malloc(n * sizeof(T), to_underlying(Caps));
413 } else {
414 static_assert((Alignment & (Alignment - 1)) == 0, "Alignment must be a power of two");
416 }
417 if (!p) {
418 raise_no_mem();
419 }
420 return static_cast<T*>(p);
421 }
422
428 void deallocate(T* p, size_t) noexcept { heap_caps_free(p); }
429};
430
438template<typename T, typename U, flags<memory::capabilities> Caps, size_t Alignment>
440 return true;
441}
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.
allocator< T, memory::capabilities::spiram > spiram_allocator
STL-compatible allocator for external PSRAM (SPI RAM).
Definition memory.hpp:469
void free(void *ptr) noexcept
Frees memory previously allocated by heap allocation functions.
Definition memory.hpp:319
size_t total_size(flags< capabilities > c) noexcept
Returns the total size of heap regions matching the given capabilities.
Definition memory.hpp:101
void * aligned_calloc(size_t alignment, size_t n, size_t size, flags< memory::capabilities > c) noexcept
Allocates aligned, zero-initialized memory from heap regions matching the given capabilities.
Definition memory.hpp:347
void * realloc(void *ptr, size_t size, flags< memory::capabilities > c) noexcept
Reallocates memory from heap regions matching the given capabilities.
Definition memory.hpp:307
capabilities
Memory capability flags for heap allocations.
Definition memory.hpp:48
size_t largest_free_block
Size of the largest contiguous free block.
Definition memory.hpp:87
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
void * malloc(size_t size, flags< memory::capabilities > c) noexcept
Allocates memory from heap regions matching the given capabilities.
Definition memory.hpp:278
bool used
True if allocated, false if free.
Definition memory.hpp:180
bool check_integrity(flags< capabilities > c, bool print_errors=false) noexcept
Check integrity of heaps with the given capabilities, or all heaps.
Definition memory.hpp:235
size_t total_allocated_bytes
Total allocated bytes across matching regions.
Definition memory.hpp:86
size_t free_size(flags< capabilities > c) noexcept
Returns the current free size of heap regions matching the given capabilities.
Definition memory.hpp:112
size_t size
Size of the block in bytes.
Definition memory.hpp:179
size_t largest_free_block(flags< capabilities > c) noexcept
Returns the largest free block in heap regions matching the given capabilities.
Definition memory.hpp:123
void * calloc(size_t n, size_t size, flags< memory::capabilities > c) noexcept
Allocates zero-initialized memory from heap regions matching the given capabilities.
Definition memory.hpp:291
intptr_t start
Start address of the heap region.
Definition memory.hpp:169
allocator< T, memory::capabilities::spiram, Alignment > aligned_spiram_allocator
STL-compatible aligned allocator for external PSRAM (SPI RAM).
Definition memory.hpp:508
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
size_t minimum_free_bytes
Minimum free bytes since boot (high-water mark).
Definition memory.hpp:88
allocator< T, memory::capabilities::dma, Alignment > aligned_dma_allocator
STL-compatible aligned allocator for DMA-capable memory.
Definition memory.hpp:520
allocator< T, memory::capabilities::dma > dma_allocator
STL-compatible allocator for DMA-capable memory.
Definition memory.hpp:482
void * aligned_alloc(size_t alignment, size_t size, flags< memory::capabilities > c) noexcept
Allocates aligned memory from heap regions matching the given capabilities.
Definition memory.hpp:332
allocator< T, memory::capabilities::dram, Alignment > aligned_dram_allocator
STL-compatible aligned allocator for internal DRAM.
Definition memory.hpp:494
constexpr bool operator==(const allocator< T, Caps, Alignment > &, const allocator< U, Caps, Alignment > &) noexcept
Equality comparison for allocator.
Definition memory.hpp:439
allocator< T, memory::capabilities::dram > dram_allocator
STL-compatible allocator for internal DRAM.
Definition memory.hpp:454
size_t minimum_free_size(flags< capabilities > c) noexcept
Returns the minimum free size since boot for heap regions matching the given capabilities.
Definition memory.hpp:137
void dump() noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition memory.hpp:260
void walk(flags< capabilities > c, F &&walker)
Walk all heap blocks in regions matching the given capabilities.
Definition memory.hpp:196
info get_info(flags< capabilities > c) noexcept
Returns detailed heap statistics for regions matching the given capabilities.
Definition memory.hpp:148
@ 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.
@ dma
DMA-capable memory.
@ access_32bit
32-bit aligned access
@ internal
Internal memory.
@ 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:377
allocator< U, Caps, Alignment > other
The rebound allocator type.
Definition memory.hpp:378
STL-compatible allocator for capability-based memory regions.
Definition memory.hpp:368
constexpr allocator(const allocator< U, Caps, Alignment > &) noexcept
Rebinding copy constructor.
Definition memory.hpp:390
void deallocate(T *p, size_t) noexcept
Deallocates memory previously allocated by this allocator.
Definition memory.hpp:428
allocator()=default
Default constructor.
T value_type
The type of object to allocate.
Definition memory.hpp:369
T * allocate(size_t n)
Allocates memory for n objects of type T.
Definition memory.hpp:406
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