diff --git a/esphome/components/esp32_ble/ble.cpp b/esphome/components/esp32_ble/ble.cpp index e3c9785078..2a67cc0f8c 100644 --- a/esphome/components/esp32_ble/ble.cpp +++ b/esphome/components/esp32_ble/ble.cpp @@ -25,7 +25,7 @@ namespace esp32_ble { static const char *const TAG = "esp32_ble"; -// No longer need static allocator - using pre-allocated pool instead +// No longer need static allocator - using on-demand pool instead void ESP32BLE::setup() { global_ble = this; diff --git a/esphome/components/esp32_ble/ble_event_pool.h b/esphome/components/esp32_ble/ble_event_pool.h index d3cff28110..4b9dcd6e33 100644 --- a/esphome/components/esp32_ble/ble_event_pool.h +++ b/esphome/components/esp32_ble/ble_event_pool.h @@ -21,7 +21,7 @@ template class BLEEventPool { } // Initialize the free list - all indices are initially free - for (size_t i = 0; i < SIZE - 1; i++) { + for (uint8_t i = 0; i < SIZE - 1; i++) { this->next_free_[i] = i + 1; } this->next_free_[SIZE - 1] = INVALID_INDEX; @@ -44,14 +44,14 @@ template class BLEEventPool { // Returns INVALID_INDEX if pool is full size_t allocate() { while (true) { - size_t head = this->free_head_.load(std::memory_order_acquire); + uint8_t head = this->free_head_.load(std::memory_order_acquire); if (head == INVALID_INDEX) { // Pool is full return INVALID_INDEX; } - size_t next = this->next_free_[head]; + uint8_t next = this->next_free_[head]; // Try to update the free list head if (this->free_head_.compare_exchange_weak(head, next, std::memory_order_release, std::memory_order_acquire)) { @@ -72,11 +72,12 @@ template class BLEEventPool { // The event's reset methods handle cleanup when switching types while (true) { - size_t head = this->free_head_.load(std::memory_order_acquire); + uint8_t head = this->free_head_.load(std::memory_order_acquire); this->next_free_[index] = head; // Try to add this index back to the free list - if (this->free_head_.compare_exchange_weak(head, index, std::memory_order_release, std::memory_order_acquire)) { + if (this->free_head_.compare_exchange_weak(head, static_cast(index), std::memory_order_release, + std::memory_order_acquire)) { this->allocated_count_.fetch_sub(1, std::memory_order_relaxed); return; } @@ -117,14 +118,14 @@ template class BLEEventPool { // Get total number of events created (high water mark) size_t get_total_created() const { return this->total_created_.load(std::memory_order_relaxed); } - static constexpr size_t INVALID_INDEX = SIZE_MAX; + static constexpr uint8_t INVALID_INDEX = 0xFF; // 255, which is > MAX_BLE_QUEUE_SIZE (64) private: - BLEEvent *events_[SIZE]; // Array of pointers, allocated on demand - size_t next_free_[SIZE]; // Next free index for each slot - std::atomic free_head_; // Head of the free list - std::atomic allocated_count_; // Number of currently allocated events - std::atomic total_created_; // Total events created (high water mark) + BLEEvent *events_[SIZE]; // Array of pointers, allocated on demand + uint8_t next_free_[SIZE]; // Next free index for each slot + std::atomic free_head_; // Head of the free list + std::atomic allocated_count_; // Number of currently allocated events + std::atomic total_created_; // Total events created (high water mark) }; } // namespace esp32_ble diff --git a/esphome/components/esp32_ble/queue_index.h b/esphome/components/esp32_ble/queue_index.h index 3010310e5a..43ddba5689 100644 --- a/esphome/components/esp32_ble/queue_index.h +++ b/esphome/components/esp32_ble/queue_index.h @@ -12,11 +12,11 @@ namespace esp32_ble { // This allows us to use a pre-allocated pool of objects template class LockFreeIndexQueue { public: - static constexpr size_t INVALID_INDEX = SIZE_MAX; + static constexpr uint8_t INVALID_INDEX = 0xFF; // 255, which is > MAX_BLE_QUEUE_SIZE (64) LockFreeIndexQueue() : head_(0), tail_(0), dropped_count_(0) { // Initialize all slots to invalid - for (size_t i = 0; i < SIZE; i++) { + for (uint8_t i = 0; i < SIZE; i++) { buffer_[i] = INVALID_INDEX; } } @@ -69,10 +69,10 @@ template class LockFreeIndexQueue { } protected: - size_t buffer_[SIZE]; - std::atomic head_; - std::atomic tail_; - std::atomic dropped_count_; + uint8_t buffer_[SIZE]; + std::atomic head_; + std::atomic tail_; + std::atomic dropped_count_; // Keep this as uint32_t for larger counts }; } // namespace esp32_ble