28#include <esp_heap_caps.h>
49#ifdef CONFIG_HEAP_HAS_EXEC_HEAP
50 exec = MALLOC_CAP_EXEC,
55 spiram = MALLOC_CAP_SPIRAM,
58 iram = MALLOC_CAP_IRAM_8BIT,
60 rtc = MALLOC_CAP_RTCRAM,
71inline constexpr bool idfxx::enable_flags_operators<idfxx::memory::capabilities> =
true;
149 multi_heap_info_t raw{};
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,
197 auto cb = [](walker_heap_into_t heap_info, walker_block_info_t block_info,
void* user_data) ->
bool {
198 auto& fn = *
static_cast<std::remove_reference_t<F>*
>(user_data);
199 return fn(
region{heap_info.
start, heap_info.end},
block{block_info.
ptr, block_info.size, block_info.used});
216 auto cb = [](walker_heap_into_t heap_info, walker_block_info_t block_info,
void* user_data) ->
bool {
217 auto& fn = *
static_cast<std::remove_reference_t<F>*
>(user_data);
218 return fn(
region{heap_info.
start, heap_info.end},
block{block_info.
ptr, block_info.size, block_info.used});
220 heap_caps_walk_all(cb, &walker);
236 return heap_caps_check_integrity(
to_underlying(c), print_errors);
241 return heap_caps_check_integrity_all(print_errors);
261 heap_caps_dump_all();
319inline void free(
void* ptr)
noexcept {
333 return heap_caps_aligned_alloc(alignment, size,
to_underlying(c));
346[[nodiscard]]
inline void*
348 return heap_caps_aligned_calloc(alignment, n, size,
to_underlying(c));
367template<
typename T, flags<memory::capabilities> Caps,
size_t Alignment = 0>
407 if (n > std::numeric_limits<size_t>::max() /
sizeof(T)) {
411 if constexpr (Alignment == 0) {
414 static_assert((Alignment & (Alignment - 1)) == 0,
"Alignment must be a power of two");
415 p = heap_caps_aligned_alloc(Alignment, n *
sizeof(T),
to_underlying(Caps));
420 return static_cast<T*
>(p);
428 void deallocate(T* p,
size_t)
noexcept { heap_caps_free(p); }
438template<
typename T,
typename U, flags<memory::capabilities> Caps,
size_t Alignment>
493template<
typename T,
size_t Alignment>
507template<
typename T,
size_t Alignment>
519template<
typename T,
size_t Alignment>
Type-safe set of flags from a scoped enum.
Type-safe bitflags from scoped enums.
void free(void *ptr) noexcept
Frees memory previously allocated by heap allocation functions.
size_t total_size(flags< capabilities > c) noexcept
Returns the total size of heap regions matching the given capabilities.
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.
void * realloc(void *ptr, size_t size, flags< memory::capabilities > c) noexcept
Reallocates memory from heap regions matching the given capabilities.
capabilities
Memory capability flags for heap allocations.
size_t largest_free_block
Size of the largest contiguous free block.
void * ptr
Pointer to the block data.
intptr_t end
End address of the heap region.
size_t total_blocks
Total number of blocks (allocated + free).
size_t free_blocks
Number of free blocks.
void * malloc(size_t size, flags< memory::capabilities > c) noexcept
Allocates memory from heap regions matching the given capabilities.
bool used
True if allocated, false if free.
bool check_integrity(flags< capabilities > c, bool print_errors=false) noexcept
Check integrity of heaps with the given capabilities, or all heaps.
size_t total_allocated_bytes
Total allocated bytes across matching regions.
size_t free_size(flags< capabilities > c) noexcept
Returns the current free size of heap regions matching the given capabilities.
size_t size
Size of the block in bytes.
size_t largest_free_block(flags< capabilities > c) noexcept
Returns the largest free block in heap regions matching the given capabilities.
void * calloc(size_t n, size_t size, flags< memory::capabilities > c) noexcept
Allocates zero-initialized memory from heap regions matching the given capabilities.
intptr_t start
Start address of the heap region.
size_t total_free_bytes
Total free bytes across matching regions.
size_t allocated_blocks
Number of allocated blocks.
size_t minimum_free_bytes
Minimum free bytes since boot (high-water mark).
void * aligned_alloc(size_t alignment, size_t size, flags< memory::capabilities > c) noexcept
Allocates aligned memory from heap regions matching the given capabilities.
constexpr bool operator==(const allocator< T, Caps, Alignment > &, const allocator< U, Caps, Alignment > &) noexcept
Equality comparison for allocator.
size_t minimum_free_size(flags< capabilities > c) noexcept
Returns the minimum free size since boot for heap regions matching the given capabilities.
void dump() noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
void walk(flags< capabilities > c, F &&walker)
Walk all heap blocks in regions matching the given capabilities.
info get_info(flags< capabilities > c) noexcept
Returns detailed heap statistics for regions matching the given capabilities.
@ 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.
@ access_32bit
32-bit aligned access
@ internal
Internal memory.
@ access_8bit
8/16/32-bit access
@ dram
Internal DRAM (8-bit accessible)
constexpr auto to_underlying(flags< E > f) noexcept
Returns the underlying integral value of a flags object.
Rebind the allocator to a different type.
STL-compatible allocator for capability-based memory regions.
constexpr allocator(const allocator< U, Caps, Alignment > &) noexcept
Rebinding copy constructor.
void deallocate(T *p, size_t) noexcept
Deallocates memory previously allocated by this allocator.
allocator()=default
Default constructor.
T value_type
The type of object to allocate.
T * allocate(size_t n)
Allocates memory for n objects of type T.
Information about a single heap block, passed to walk callbacks.
Information about a heap region, passed to walk callbacks.