From 6a756ab3b640cb88983f9aa64ec7f34df657c623 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 16 Jun 2025 15:09:49 +0200 Subject: [PATCH] cleanup --- esphome/components/esp32_ble/ble.cpp | 4 ++-- esphome/components/esp32_ble/ble_event.h | 10 +++++++--- esphome/components/esp32_ble/ble_event_pool.h | 7 ++----- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/esphome/components/esp32_ble/ble.cpp b/esphome/components/esp32_ble/ble.cpp index a73626a1cf..b2328828ac 100644 --- a/esphome/components/esp32_ble/ble.cpp +++ b/esphome/components/esp32_ble/ble.cpp @@ -348,7 +348,7 @@ void ESP32BLE::loop() { break; } // Return the event to the pool - this->ble_event_pool_.deallocate(ble_event); + this->ble_event_pool_.release(ble_event); ble_event = this->ble_events_.pop(); } if (this->advertising_ != nullptr) { @@ -411,7 +411,7 @@ template void enqueue_ble_event(Args... args) { // This should not happen in SPSC queue with single producer ESP_LOGE(TAG, "BLE queue push failed unexpectedly"); // Return to pool - global_ble->ble_event_pool_.deallocate(event); + global_ble->ble_event_pool_.release(event); } } diff --git a/esphome/components/esp32_ble/ble_event.h b/esphome/components/esp32_ble/ble_event.h index cb70d3e018..faccb034c6 100644 --- a/esphome/components/esp32_ble/ble_event.h +++ b/esphome/components/esp32_ble/ble_event.h @@ -174,7 +174,7 @@ class BLEEvent { this->event_.gap.gap_event = e; if (p == nullptr) { - return; + return; // Invalid event, but we can't log in header file } // Copy data based on event type @@ -216,10 +216,12 @@ class BLEEvent { if (p == nullptr) { this->event_.gattc.gattc_param = nullptr; this->event_.gattc.data = nullptr; - return; + return; // Invalid event, but we can't log in header file } // Heap-allocate param + // Heap allocation is used because GATTC/GATTS events are rare (<1% of events) + // while GAP events (99%) are stored inline to minimize memory usage this->event_.gattc.gattc_param = new esp_ble_gattc_cb_param_t(*p); // Copy data for events that need it @@ -247,10 +249,12 @@ class BLEEvent { if (p == nullptr) { this->event_.gatts.gatts_param = nullptr; this->event_.gatts.data = nullptr; - return; + return; // Invalid event, but we can't log in header file } // Heap-allocate param + // Heap allocation is used because GATTC/GATTS events are rare (<1% of events) + // while GAP events (99%) are stored inline to minimize memory usage this->event_.gatts.gatts_param = new esp_ble_gatts_cb_param_t(*p); // Copy data for events that need it diff --git a/esphome/components/esp32_ble/ble_event_pool.h b/esphome/components/esp32_ble/ble_event_pool.h index d118ccf3ab..26f091536f 100644 --- a/esphome/components/esp32_ble/ble_event_pool.h +++ b/esphome/components/esp32_ble/ble_event_pool.h @@ -55,16 +55,13 @@ template class BLEEventPool { return event; } - // Return an event to the pool - void deallocate(BLEEvent *event) { + // Return an event to the pool for reuse + void release(BLEEvent *event) { if (event == nullptr) { return; } - // Events are reused - the load methods handle cleanup - // Just return to free list this->free_list_.push(event); - // Push cannot fail: pool size = queue size, and we never exceed pool size } // Get total number of events created (high water mark)