|
TinyGPU
|
STL-compatible allocator that prefers PSRAM allocation with fallback. More...
#include <PSRAMAllocator.h>
Classes | |
| struct | rebind |
| Rebind allocator to different type. More... | |
Public Types | |
| using | value_type = T |
| The type of objects this allocator can allocate. | |
Public Member Functions | |
| PSRAMAllocator () noexcept | |
| Default constructor. | |
| template<class U > | |
| PSRAMAllocator (const PSRAMAllocator< U > &) noexcept | |
| Copy constructor from different allocator type. | |
| T * | allocate (std::size_t n) |
| Allocate memory for n objects of type T. | |
| void | deallocate (T *p, std::size_t) noexcept |
| Deallocate memory previously allocated by this allocator. | |
STL-compatible allocator that prefers PSRAM allocation with fallback.
PSRAMAllocator is a custom C++ allocator that attempts to allocate memory in external PSRAM (using heap_caps_aligned_calloc with MALLOC_CAP_SPIRAM) and falls back to regular heap allocation if PSRAM allocation fails.
This allocator is particularly useful for ESP32 platforms with external PSRAM where large buffers (like video frames) should be stored in PSRAM to preserve internal SRAM for other uses. For small, frequently-accessed buffers that need fast access, consider using RAMAllocator instead.
| T | The type of objects to allocate |
Example usage:
| using value_type = T |
The type of objects this allocator can allocate.
|
inlinenoexcept |
Default constructor.
Constructs a PSRAMAllocator. No initialization needed.
|
inlinenoexcept |
Copy constructor from different allocator type.
Allows construction from PSRAMAllocator of different type U. Required for STL allocator requirements.
| U | The type parameter of the source allocator |
| other | Source allocator (unused) |
|
inline |
Allocate memory for n objects of type T.
Attempts to allocate memory in the following priority order:
All allocated memory is zero-initialized.
| n | Number of objects of type T to allocate |
| std::bad_alloc | if allocation fails |
|
inlinenoexcept |
Deallocate memory previously allocated by this allocator.
Frees memory that was allocated by allocate(). Uses heap_caps_free which works for both PSRAM and regular heap allocations.
| p | Pointer to memory to deallocate (can be nullptr) |
| n | Number of objects (ignored, but required by allocator interface) |