From 9db5c8ac2dac1c7a46b349ea4088ce5b0172d7c4 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 31 Mar 2026 20:42:13 -1000 Subject: [PATCH] Use xQueueCreateStatic to avoid heap allocation Storage buffer and control block are now class members, matching LockFreeQueue's static buffer approach. Removes all null handle checks. --- esphome/core/freertos_queue.h | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/esphome/core/freertos_queue.h b/esphome/core/freertos_queue.h index e65d2fbbb12..073b47eff68 100644 --- a/esphome/core/freertos_queue.h +++ b/esphome/core/freertos_queue.h @@ -14,6 +14,9 @@ * empty, full, size) but uses xQueue internally, which synchronizes via * FreeRTOS critical sections. * + * Uses xQueueCreateStatic so the queue storage lives in the object itself + * with no heap allocation, matching LockFreeQueue's static buffer approach. + * * @tparam T The type of elements stored in the queue (stored as pointers) * @tparam SIZE The maximum number of elements */ @@ -22,10 +25,12 @@ namespace esphome { template class FreeRTOSQueue { public: - FreeRTOSQueue() : dropped_count_(0) { this->handle_ = xQueueCreate(SIZE, sizeof(T *)); } + FreeRTOSQueue() : dropped_count_(0) { + this->handle_ = xQueueCreateStatic(SIZE, sizeof(T *), this->storage_, &this->queue_buf_); + } bool push(T *element) { - if (element == nullptr || this->handle_ == nullptr) + if (element == nullptr) return false; if (xQueueSend(this->handle_, &element, 0) != pdPASS) { @@ -36,9 +41,6 @@ template class FreeRTOSQueue { } T *pop() { - if (this->handle_ == nullptr) - return nullptr; - T *element; if (xQueueReceive(this->handle_, &element, 0) != pdTRUE) { return nullptr; @@ -56,25 +58,16 @@ template class FreeRTOSQueue { void increment_dropped_count() { this->dropped_count_++; } - bool empty() const { - if (this->handle_ == nullptr) - return true; - return uxQueueMessagesWaiting(this->handle_) == 0; - } + bool empty() const { return uxQueueMessagesWaiting(this->handle_) == 0; } - bool full() const { - if (this->handle_ == nullptr) - return true; - return uxQueueSpacesAvailable(this->handle_) == 0; - } + bool full() const { return uxQueueSpacesAvailable(this->handle_) == 0; } - size_t size() const { - if (this->handle_ == nullptr) - return 0; - return uxQueueMessagesWaiting(this->handle_); - } + size_t size() const { return uxQueueMessagesWaiting(this->handle_); } protected: + // Static storage for the queue - sized for SIZE pointer-sized items + uint8_t storage_[SIZE * sizeof(T *)]; + StaticQueue_t queue_buf_; QueueHandle_t handle_; volatile uint16_t dropped_count_; };