H.264 Codec for ESP32-S3
Loading...
Searching...
No Matches
Classes | Public Types | Public Member Functions | List of all members
PSRAMAllocatorH264< T > Class Template Reference

STL-compatible allocator that prefers PSRAM allocation with fallback. More...

#include <PSRAMAllocatorH264.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

 PSRAMAllocatorH264 () noexcept
 Default constructor.
 
template<class U >
 PSRAMAllocatorH264 (const PSRAMAllocatorH264< 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.
 

Detailed Description

template<typename T = uint8_t>
class esp_h264::PSRAMAllocatorH264< T >

STL-compatible allocator that prefers PSRAM allocation with fallback.

PSRAMAllocatorH264 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 RAMAllocatorH264 instead.

Template Parameters
TThe type of objects to allocate
Note
On ESP-IDF platforms, uses heap_caps_aligned_calloc for PSRAM allocation
On Arduino platforms, the heap_caps functions are shimmed to standard malloc/free
Memory is zero-initialized (calloc behavior)
Provides proper alignment for the allocated type

Example usage:

// Use PSRAMAllocatorH264 with std::vector for large buffers
std::vector<uint8_t, PSRAMAllocatorH264<uint8_t>> largeBuffer;
largeBuffer.resize(1024 * 1024); // Large buffer preferably in PSRAM
// Or use the convenience alias
PSVec<uint8_t> videoFrame; // Large video frame data in PSRAM
videoFrame.resize(640 * 480 * 3);
// For small buffers in fast RAM, use RAMAllocatorH264 (separate header)
RAMVec<uint16_t> lookupTable; // Small lookup table in fast RAM
Header-only C++ allocator that prefers PSRAM allocation on ESP32.
Header-only C++ allocator that uses internal RAM (DRAM) on ESP32.
std::vector< T, RAMAllocatorH264< T > > RAMVec
Convenience type alias for vectors using RAMAllocatorH264.
Definition: RAMAllocatorH264.h:150
std::vector< T, PSRAMAllocatorH264< T > > PSVec
Convenience type alias for vectors using PSRAMAllocatorH264.
Definition: PSRAMAllocatorH264.h:155

Member Typedef Documentation

◆ value_type

using value_type = T

The type of objects this allocator can allocate.

Constructor & Destructor Documentation

◆ PSRAMAllocatorH264() [1/2]

PSRAMAllocatorH264 ( )
inlinenoexcept

Default constructor.

Constructs a PSRAMAllocatorH264. No initialization needed.

◆ PSRAMAllocatorH264() [2/2]

PSRAMAllocatorH264 ( const PSRAMAllocatorH264< U > &  )
inlinenoexcept

Copy constructor from different allocator type.

Allows construction from PSRAMAllocatorH264 of different type U. Required for STL allocator requirements.

Template Parameters
UThe type parameter of the source allocator
Parameters
otherSource allocator (unused)

Member Function Documentation

◆ allocate()

T * allocate ( std::size_t  n)
inline

Allocate memory for n objects of type T.

Attempts to allocate memory in the following priority order:

  1. PSRAM using heap_caps_aligned_calloc (if available)
  2. Aligned allocation using aligned_alloc (C11)
  3. Standard calloc as fallback

All allocated memory is zero-initialized.

Parameters
nNumber of objects of type T to allocate
Returns
Pointer to allocated memory
Exceptions
std::bad_allocif allocation fails
Note
Memory is aligned by 16
On ESP32 with PSRAM, prefers external PSRAM allocation

◆ deallocate()

void deallocate ( T *  p,
std::size_t   
)
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.

Parameters
pPointer to memory to deallocate (can be nullptr)
nNumber of objects (ignored, but required by allocator interface)
Note
Safe to call with nullptr
The size parameter n is ignored as heap_caps_free doesn't need it

The documentation for this class was generated from the following file: