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
32namespace idfxx {
33
65
66template<>
67inline constexpr bool enable_flags_operators<memory_caps> = true;
68
85
96
107
118
132
141 multi_heap_info_t info{};
143 return {
144 .total_free_bytes = info.total_free_bytes,
145 .total_allocated_bytes = info.total_allocated_bytes,
146 .largest_free_block = info.largest_free_block,
147 .minimum_free_bytes = info.minimum_free_bytes,
148 .allocated_blocks = info.allocated_blocks,
149 .free_blocks = info.free_blocks,
150 .total_blocks = info.total_blocks,
151 };
152}
153
154// ---- Heap allocation --------------------------------------------------------
155
164[[nodiscard]] inline void* heap_malloc(size_t size, flags<memory_caps> caps) noexcept {
165 return heap_caps_malloc(size, to_underlying(caps));
166}
167
177[[nodiscard]] inline void* heap_calloc(size_t n, size_t size, flags<memory_caps> caps) noexcept {
178 return heap_caps_calloc(n, size, to_underlying(caps));
179}
180
193[[nodiscard]] inline void* heap_realloc(void* ptr, size_t size, flags<memory_caps> caps) noexcept {
194 return heap_caps_realloc(ptr, size, to_underlying(caps));
195}
196
205inline void heap_free(void* ptr) noexcept {
206 heap_caps_free(ptr);
207}
208
218[[nodiscard]] inline void* heap_aligned_alloc(size_t alignment, size_t size, flags<memory_caps> caps) noexcept {
220}
221
232[[nodiscard]] inline void*
233heap_aligned_calloc(size_t alignment, size_t n, size_t size, flags<memory_caps> caps) noexcept {
235}
236
251template<typename T, flags<memory_caps> Caps, size_t Alignment = 0>
253 using value_type = T;
260 template<typename U>
264
266 caps_allocator() = default;
267
273 template<typename U>
275
290 [[nodiscard]] T* allocate(size_t n) {
291 if (n > std::numeric_limits<size_t>::max() / sizeof(T)) {
292 raise_no_mem();
293 }
294 void* p;
295 if constexpr (Alignment == 0) {
296 p = heap_caps_malloc(n * sizeof(T), to_underlying(Caps));
297 } else {
298 static_assert((Alignment & (Alignment - 1)) == 0, "Alignment must be a power of two");
300 }
301 if (!p) {
302 raise_no_mem();
303 }
304 return static_cast<T*>(p);
305 }
306
312 void deallocate(T* p, size_t) noexcept { heap_caps_free(p); }
313};
314
322template<typename T, typename U, flags<memory_caps> Caps, size_t Alignment>
323constexpr bool
327
338template<typename T>
340
354template<typename T>
356
367template<typename T>
369
379template<typename T, size_t Alignment>
381
393template<typename T, size_t Alignment>
395
405template<typename T, size_t Alignment>
407
408// ---- Heap walking -----------------------------------------------------------
409
418
424 void* ptr;
425 size_t size;
426 bool used;
427};
428
441template<typename F>
444 auto& fn = *static_cast<std::remove_reference_t<F>*>(user_data);
445 return fn(
447 );
448 };
450}
451
462template<typename F>
465 auto& fn = *static_cast<std::remove_reference_t<F>*>(user_data);
466 return fn(
468 );
469 };
471}
472
473// ---- Heap integrity checking ------------------------------------------------
474
486
497
498// ---- Heap dump --------------------------------------------------------------
499
511
518inline void heap_dump_all() noexcept {
520}
521
// end of idfxx_core_memory // end of idfxx_core
524
525} // namespace idfxx
IDFXX error handling.
Type-safe bitflags from scoped enums.
caps_allocator< T, memory_caps::dram > dram_allocator
STL-compatible allocator for internal DRAM.
Definition memory.hpp:339
heap_info get_heap_info(flags< memory_caps > caps) noexcept
Returns detailed heap statistics for regions matching the given capabilities.
Definition memory.hpp:140
void * heap_malloc(size_t size, flags< memory_caps > caps) noexcept
Allocates memory from heap regions matching the given capabilities.
Definition memory.hpp:164
caps_allocator< T, memory_caps::spiram, Alignment > aligned_spiram_allocator
STL-compatible aligned allocator for external PSRAM (SPI RAM).
Definition memory.hpp:394
constexpr bool operator==(const caps_allocator< T, Caps, Alignment > &, const caps_allocator< U, Caps, Alignment > &) noexcept
Equality comparison for caps_allocator.
Definition memory.hpp:324
size_t heap_largest_free_block(flags< memory_caps > caps) noexcept
Returns the largest free block in heap regions matching the given capabilities.
Definition memory.hpp:115
void heap_dump_all() noexcept
Dump the structure of all heaps.
Definition memory.hpp:518
void heap_dump(flags< memory_caps > caps) noexcept
Dump the structure of all heaps with matching capabilities.
Definition memory.hpp:508
caps_allocator< T, memory_caps::dram, Alignment > aligned_dram_allocator
STL-compatible aligned allocator for internal DRAM.
Definition memory.hpp:380
void heap_free(void *ptr) noexcept
Frees memory previously allocated by heap allocation functions.
Definition memory.hpp:205
size_t heap_free_size(flags< memory_caps > caps) noexcept
Returns the current free size of heap regions matching the given capabilities.
Definition memory.hpp:104
bool heap_check_integrity(flags< memory_caps > caps, bool print_errors=false) noexcept
Check integrity of all heaps with the given capabilities.
Definition memory.hpp:483
bool heap_check_integrity_all(bool print_errors=false) noexcept
Check integrity of all heaps.
Definition memory.hpp:494
size_t heap_total_size(flags< memory_caps > caps) noexcept
Returns the total size of heap regions matching the given capabilities.
Definition memory.hpp:93
caps_allocator< T, memory_caps::dma, Alignment > aligned_dma_allocator
STL-compatible aligned allocator for DMA-capable memory.
Definition memory.hpp:406
void heap_walk(flags< memory_caps > caps, F &&walker)
Walk all heap blocks in regions matching the given capabilities.
Definition memory.hpp:442
memory_caps
Memory capability flags for heap allocations.
Definition memory.hpp:48
void * heap_calloc(size_t n, size_t size, flags< memory_caps > caps) noexcept
Allocates zero-initialized memory from heap regions matching the given capabilities.
Definition memory.hpp:177
caps_allocator< T, memory_caps::dma > dma_allocator
STL-compatible allocator for DMA-capable memory.
Definition memory.hpp:368
caps_allocator< T, memory_caps::spiram > spiram_allocator
STL-compatible allocator for external PSRAM (SPI RAM).
Definition memory.hpp:355
void heap_walk_all(F &&walker)
Walk all heap blocks across all heaps.
Definition memory.hpp:463
void * heap_realloc(void *ptr, size_t size, flags< memory_caps > caps) noexcept
Reallocates memory from heap regions matching the given capabilities.
Definition memory.hpp:193
void * heap_aligned_alloc(size_t alignment, size_t size, flags< memory_caps > caps) noexcept
Allocates aligned memory from heap regions matching the given capabilities.
Definition memory.hpp:218
size_t heap_minimum_free_size(flags< memory_caps > caps) noexcept
Returns the minimum free size since boot for heap regions matching the given capabilities.
Definition memory.hpp:129
void * heap_aligned_calloc(size_t alignment, size_t n, size_t size, flags< memory_caps > caps) noexcept
Allocates aligned, zero-initialized memory from heap regions matching the given capabilities.
Definition memory.hpp:233
@ 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.
@ exec
Executable memory.
@ 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:346
Rebind the allocator to a different type.
Definition memory.hpp:261
caps_allocator< U, Caps, Alignment > other
The rebound allocator type.
Definition memory.hpp:262
STL-compatible allocator for capability-based memory regions.
Definition memory.hpp:252
caps_allocator()=default
Default constructor.
T * allocate(size_t n)
Allocates memory for n objects of type T.
Definition memory.hpp:290
T value_type
The type of object to allocate.
Definition memory.hpp:253
void deallocate(T *p, size_t) noexcept
Deallocates memory previously allocated by this allocator.
Definition memory.hpp:312
constexpr caps_allocator(const caps_allocator< U, Caps, Alignment > &) noexcept
Rebinding copy constructor.
Definition memory.hpp:274
Information about a single heap block, passed to heap walk callbacks.
Definition memory.hpp:423
size_t size
Size of the block in bytes.
Definition memory.hpp:425
void * ptr
Pointer to the block data.
Definition memory.hpp:424
bool used
True if allocated, false if free.
Definition memory.hpp:426
Heap region statistics.
Definition memory.hpp:76
size_t largest_free_block
Size of the largest contiguous free block.
Definition memory.hpp:79
size_t minimum_free_bytes
Minimum free bytes since boot (high-water mark).
Definition memory.hpp:80
size_t total_allocated_bytes
Total allocated bytes across matching regions.
Definition memory.hpp:78
size_t total_free_bytes
Total free bytes across matching regions.
Definition memory.hpp:77
size_t allocated_blocks
Number of allocated blocks.
Definition memory.hpp:81
size_t free_blocks
Number of free blocks.
Definition memory.hpp:82
size_t total_blocks
Total number of blocks (allocated + free).
Definition memory.hpp:83
Information about a heap region, passed to heap walk callbacks.
Definition memory.hpp:414
intptr_t end
End address of the heap region.
Definition memory.hpp:416
intptr_t start
Start address of the heap region.
Definition memory.hpp:415