From 104658e43a8f59d07986f3fbeeb8446caa8a5029 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 16 Jun 2025 15:16:15 +0200 Subject: [PATCH] ble pool --- esphome/components/esp32_ble/ble_event_pool.h | 37 +++++++++---------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/esphome/components/esp32_ble/ble_event_pool.h b/esphome/components/esp32_ble/ble_event_pool.h index 36f9f64de9..0e3b64037e 100644 --- a/esphome/components/esp32_ble/ble_event_pool.h +++ b/esphome/components/esp32_ble/ble_event_pool.h @@ -30,28 +30,27 @@ template class BLEEventPool { BLEEvent *allocate() { // Try to get from free list first BLEEvent *event = this->free_list_.pop(); + if (event != nullptr) + return event; - if (event == nullptr) { - // Need to create a new event - if (this->total_created_ >= SIZE) { - // Pool is at capacity - return nullptr; - } - - // Use internal RAM for better performance - RAMAllocator allocator(RAMAllocator::ALLOC_INTERNAL); - event = allocator.allocate(1); - - if (event == nullptr) { - // Memory allocation failed - return nullptr; - } - - // Placement new to construct the object - new (event) BLEEvent(); - this->total_created_.fetch_add(1, std::memory_order_relaxed); + // Need to create a new event + if (this->total_created_ >= SIZE) { + // Pool is at capacity + return nullptr; } + // Use internal RAM for better performance + RAMAllocator allocator(RAMAllocator::ALLOC_INTERNAL); + event = allocator.allocate(1); + + if (event == nullptr) { + // Memory allocation failed + return nullptr; + } + + // Placement new to construct the object + new (event) BLEEvent(); + this->total_created_.fetch_add(1, std::memory_order_relaxed); return event; }