[core] add ring buffer destructor (#7500)

This commit is contained in:
Kevin Ahrendt 2024-09-26 17:25:20 -04:00 committed by GitHub
parent 21fbbc5fb9
commit 3b1b1071f1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 15 additions and 2 deletions

View file

@ -11,16 +11,26 @@ namespace esphome {
static const char *const TAG = "ring_buffer"; static const char *const TAG = "ring_buffer";
RingBuffer::~RingBuffer() {
if (this->handle_ != nullptr) {
vStreamBufferDelete(this->handle_);
ExternalRAMAllocator<uint8_t> allocator(ExternalRAMAllocator<uint8_t>::ALLOW_FAILURE);
allocator.deallocate(this->storage_, this->size_);
}
}
std::unique_ptr<RingBuffer> RingBuffer::create(size_t len) { std::unique_ptr<RingBuffer> RingBuffer::create(size_t len) {
std::unique_ptr<RingBuffer> rb = make_unique<RingBuffer>(); std::unique_ptr<RingBuffer> rb = make_unique<RingBuffer>();
rb->size_ = len + 1;
ExternalRAMAllocator<uint8_t> allocator(ExternalRAMAllocator<uint8_t>::ALLOW_FAILURE); ExternalRAMAllocator<uint8_t> allocator(ExternalRAMAllocator<uint8_t>::ALLOW_FAILURE);
rb->storage_ = allocator.allocate(len + 1); rb->storage_ = allocator.allocate(rb->size_);
if (rb->storage_ == nullptr) { if (rb->storage_ == nullptr) {
return nullptr; return nullptr;
} }
rb->handle_ = xStreamBufferCreateStatic(len + 1, 1, rb->storage_, &rb->structure_); rb->handle_ = xStreamBufferCreateStatic(rb->size_, 1, rb->storage_, &rb->structure_);
ESP_LOGD(TAG, "Created ring buffer with size %u", len); ESP_LOGD(TAG, "Created ring buffer with size %u", len);
return rb; return rb;
} }

View file

@ -12,6 +12,8 @@ namespace esphome {
class RingBuffer { class RingBuffer {
public: public:
~RingBuffer();
/** /**
* @brief Reads from the ring buffer, waiting up to a specified number of ticks if necessary. * @brief Reads from the ring buffer, waiting up to a specified number of ticks if necessary.
* *
@ -83,6 +85,7 @@ class RingBuffer {
StreamBufferHandle_t handle_; StreamBufferHandle_t handle_;
StaticStreamBuffer_t structure_; StaticStreamBuffer_t structure_;
uint8_t *storage_; uint8_t *storage_;
size_t size_{0};
}; };
} // namespace esphome } // namespace esphome