Skip to content

Commit

Permalink
[core] add ring buffer destructor (esphome#7500)
Browse files Browse the repository at this point in the history
  • Loading branch information
kahrendt authored Sep 26, 2024
1 parent 21fbbc5 commit 3b1b107
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
14 changes: 12 additions & 2 deletions esphome/core/ring_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,26 @@ namespace esphome {

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> rb = make_unique<RingBuffer>();

rb->size_ = len + 1;

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) {
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);
return rb;
}
Expand Down
3 changes: 3 additions & 0 deletions esphome/core/ring_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ namespace esphome {

class RingBuffer {
public:
~RingBuffer();

/**
* @brief Reads from the ring buffer, waiting up to a specified number of ticks if necessary.
*
Expand Down Expand Up @@ -83,6 +85,7 @@ class RingBuffer {
StreamBufferHandle_t handle_;
StaticStreamBuffer_t structure_;
uint8_t *storage_;
size_t size_{0};
};

} // namespace esphome
Expand Down

0 comments on commit 3b1b107

Please sign in to comment.