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