mirror of
https://github.com/esphome/esphome.git
synced 2026-06-24 14:01:01 +00:00
[core] Support allocating ring buffer in internal memory (#16187)
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user