H.264 Codec for ESP32-S3
Loading...
Searching...
No Matches
PSRAMAllocatorH264.h
Go to the documentation of this file.
1#pragma once
2
3/**
4 * @file PSRAMAllocatorH264.h
5 * @brief Header-only C++ allocator that prefers PSRAM allocation on ESP32
6 */
7
8#include <cstdlib>
9#include <cstring>
10#include <new>
11#include <type_traits>
12#include <vector>
13
14#include "H264Config.h"
15#include "h264/esp_h264_alloc.h"
16
17namespace esp_h264 {
18
19/**
20 * @class PSRAMAllocatorH264
21 * @brief STL-compatible allocator that prefers PSRAM allocation with fallback
22 *
23 * PSRAMAllocatorH264 is a custom C++ allocator that attempts to allocate memory in
24 * external PSRAM (using heap_caps_aligned_calloc with MALLOC_CAP_SPIRAM) and
25 * falls back to regular heap allocation if PSRAM allocation fails.
26 *
27 * This allocator is particularly useful for ESP32 platforms with external PSRAM
28 * where large buffers (like video frames) should be stored in PSRAM to preserve
29 * internal SRAM for other uses. For small, frequently-accessed buffers that
30 * need fast access, consider using RAMAllocatorH264 instead.
31 *
32 * @tparam T The type of objects to allocate
33 *
34 * @note On ESP-IDF platforms, uses heap_caps_aligned_calloc for PSRAM
35 * allocation
36 * @note On Arduino platforms, the heap_caps functions are shimmed to standard
37 * malloc/free
38 * @note Memory is zero-initialized (calloc behavior)
39 * @note Provides proper alignment for the allocated type
40 *
41 * Example usage:
42 * @code
43 * #include "PSRAMAllocatorH264.h"
44 *
45 * // Use PSRAMAllocatorH264 with std::vector for large buffers
46 * std::vector<uint8_t, PSRAMAllocatorH264<uint8_t>> largeBuffer;
47 * largeBuffer.resize(1024 * 1024); // Large buffer preferably in PSRAM
48 *
49 * // Or use the convenience alias
50 * PSVec<uint8_t> videoFrame; // Large video frame data in PSRAM
51 * videoFrame.resize(640 * 480 * 3);
52 *
53 * // For small buffers in fast RAM, use RAMAllocatorH264 (separate header)
54 * #include "RAMAllocatorH264.h"
55 * RAMVec<uint16_t> lookupTable; // Small lookup table in fast RAM
56 * @endcode
57 */
58template <typename T = uint8_t>
60 public:
61 /** @brief The type of objects this allocator can allocate */
62 using value_type = T;
63
64 /**
65 * @brief Default constructor
66 *
67 * Constructs a PSRAMAllocatorH264. No initialization needed.
68 */
69 PSRAMAllocatorH264() noexcept {}
70
71 /**
72 * @brief Copy constructor from different allocator type
73 *
74 * Allows construction from PSRAMAllocatorH264 of different type U.
75 * Required for STL allocator requirements.
76 *
77 * @tparam U The type parameter of the source allocator
78 * @param other Source allocator (unused)
79 */
80 template <class U>
82
83 /**
84 * @brief Allocate memory for n objects of type T
85 *
86 * Attempts to allocate memory in the following priority order:
87 * 1. PSRAM using heap_caps_aligned_calloc (if available)
88 * 2. Aligned allocation using aligned_alloc (C11)
89 * 3. Standard calloc as fallback
90 *
91 * All allocated memory is zero-initialized.
92 *
93 * @param n Number of objects of type T to allocate
94 * @return Pointer to allocated memory
95 * @throws std::bad_alloc if allocation fails
96 *
97 * @note Memory is aligned by 16
98 * @note On ESP32 with PSRAM, prefers external PSRAM allocation
99 */
101 if (n == 0) return nullptr;
104 16, n, sizeof(T), &actual_size, (uint32_t)MALLOC_CAP_SPIRAM);
105 assert(p != nullptr);
106 return static_cast<T*>(p);
107 }
108
109 /**
110 * @brief Deallocate memory previously allocated by this allocator
111 *
112 * Frees memory that was allocated by allocate(). Uses heap_caps_free
113 * which works for both PSRAM and regular heap allocations.
114 *
115 * @param p Pointer to memory to deallocate (can be nullptr)
116 * @param n Number of objects (ignored, but required by allocator interface)
117 *
118 * @note Safe to call with nullptr
119 * @note The size parameter n is ignored as heap_caps_free doesn't need it
120 */
121 void deallocate(T* p, std::size_t /*n*/) noexcept {
122 if (!p) return;
124 }
125
126 /**
127 * @brief Rebind allocator to different type
128 *
129 * Provides the rebind mechanism required by STL allocators.
130 * Allows containers to allocate different types using the same allocator.
131 *
132 * @tparam U The type to rebind to
133 */
134 template <class U>
135 struct rebind {
136 /** @brief The rebound allocator type */
138 };
139};
140
141/**
142 * @brief Convenience type alias for vectors using PSRAMAllocatorH264
143 *
144 * PSVec provides a shorter way to declare vectors that use PSRAMAllocatorH264.
145 *
146 * @tparam T The element type of the vector
147 *
148 * Example usage:
149 * @code
150 * PSVec<uint8_t> buffer; // Instead of std::vector<uint8_t,
151 * PSRAMAllocatorH264<uint8_t>> buffer.resize(1024);
152 * @endcode
153 */
154template <typename T>
155using PSVec = std::vector<T, PSRAMAllocatorH264<T>>;
156
157} // namespace esp_h264
158
159/**
160 * @brief Equality operator for PSRAMAllocatorH264 instances
161 *
162 * All PSRAMAllocatorH264 instances are considered equal regardless of their
163 * template type parameter, as required by STL allocator requirements.
164 *
165 * @tparam T First allocator's value type
166 * @tparam U Second allocator's value type
167 * @param lhs First allocator
168 * @param rhs Second allocator
169 * @return true Always returns true
170 */
171template <class T, class U>
172inline bool operator==(const esp_h264::PSRAMAllocatorH264<T>&,
173 const esp_h264::PSRAMAllocatorH264<U>&) noexcept {
174 return true;
175}
176
177/**
178 * @brief Inequality operator for PSRAMAllocatorH264 instances
179 *
180 * All PSRAMAllocatorH264 instances are considered equal, so inequality
181 * always returns false.
182 *
183 * @tparam T First allocator's value type
184 * @tparam U Second allocator's value type
185 * @param lhs First allocator
186 * @param rhs Second allocator
187 * @return false Always returns false
188 */
189template <class T, class U>
190inline bool operator!=(const esp_h264::PSRAMAllocatorH264<T>&,
191 const esp_h264::PSRAMAllocatorH264<U>&) noexcept {
192 return false;
193}
bool operator!=(const esp_h264::PSRAMAllocatorH264< T > &, const esp_h264::PSRAMAllocatorH264< U > &) noexcept
Inequality operator for PSRAMAllocatorH264 instances.
Definition: PSRAMAllocatorH264.h:190
bool operator==(const esp_h264::PSRAMAllocatorH264< T > &, const esp_h264::PSRAMAllocatorH264< U > &) noexcept
Equality operator for PSRAMAllocatorH264 instances.
Definition: PSRAMAllocatorH264.h:172
#define esp_h264_free
Free memory previously allocated.
Definition: esp_h264_alloc.h:19
Definition: H264Decoder.h:31