[core] Support allocating ring buffer in internal memory (#16187)

This commit is contained in:
Kevin Ahrendt
2026-05-01 08:55:08 -04:00
committed by GitHub
parent f073c1cabe
commit 3dd60c5713
2 changed files with 13 additions and 7 deletions

View File

@@ -1,11 +1,9 @@
#include "ring_buffer.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
#ifdef USE_ESP32
#include "helpers.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
namespace esphome {
@@ -19,12 +17,15 @@ RingBuffer::~RingBuffer() {
}
}
std::unique_ptr<RingBuffer> RingBuffer::create(size_t len) {
std::unique_ptr<RingBuffer> RingBuffer::create(size_t len, MemoryPreference preference) {
std::unique_ptr<RingBuffer> rb = make_unique<RingBuffer>();
rb->size_ = len;
RAMAllocator<uint8_t> allocator;
const uint8_t type = (preference == MemoryPreference::INTERNAL_FIRST) ? RAMAllocator<uint8_t>::PREFER_INTERNAL
: RAMAllocator<uint8_t>::NONE;
RAMAllocator<uint8_t> allocator(type);
rb->storage_ = allocator.allocate(rb->size_);
if (rb->storage_ == nullptr) {
return nullptr;

View File

@@ -80,7 +80,12 @@ class RingBuffer {
*/
BaseType_t reset();
static std::unique_ptr<RingBuffer> create(size_t len);
enum class MemoryPreference {
EXTERNAL_FIRST, // External RAM preferred, fall back to internal (default)
INTERNAL_FIRST, // Internal RAM preferred, fall back to external
};
static std::unique_ptr<RingBuffer> create(size_t len, MemoryPreference preference = MemoryPreference::EXTERNAL_FIRST);
protected:
/// @brief Discards data from the ring buffer.