1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-20 04:02:21 +01:00
This commit is contained in:
J. Nick Koston
2025-06-16 15:15:26 +02:00
parent b35b54f2c2
commit e7e4b995bf

View File

@@ -15,7 +15,7 @@ namespace esp32_ble {
// Events are allocated on first use and reused thereafter, growing to peak usage // Events are allocated on first use and reused thereafter, growing to peak usage
template<uint8_t SIZE> class BLEEventPool { template<uint8_t SIZE> class BLEEventPool {
public: public:
BLEEventPool() : total_created_(0) {} BLEEventPool() { total_created_.store(0, std::memory_order_relaxed); }
~BLEEventPool() { ~BLEEventPool() {
// Clean up any remaining events in the free list // Clean up any remaining events in the free list
@@ -49,7 +49,7 @@ template<uint8_t SIZE> class BLEEventPool {
// Placement new to construct the object // Placement new to construct the object
new (event) BLEEvent(); new (event) BLEEvent();
this->total_created_++; this->total_created_.fetch_add(1, std::memory_order_relaxed);
} }
return event; return event;
@@ -57,22 +57,20 @@ template<uint8_t SIZE> class BLEEventPool {
// Return an event to the pool for reuse // Return an event to the pool for reuse
void release(BLEEvent *event) { void release(BLEEvent *event) {
if (event == nullptr) { if (event != nullptr) {
return;
}
this->free_list_.push(event); this->free_list_.push(event);
} }
}
// Get total number of events created (high water mark) // Get total number of events created (high water mark)
uint8_t get_total_created() const { return this->total_created_; } uint8_t get_total_created() const { return this->total_created_.load(std::memory_order_relaxed); }
// Get number of events in the free list // Get number of events in the free list
size_t get_free_count() const { return this->free_list_.size(); } size_t get_free_count() const { return this->free_list_.size(); }
private: private:
LockFreeQueue<BLEEvent, SIZE> free_list_; // Free events ready for reuse LockFreeQueue<BLEEvent, SIZE> free_list_; // Free events ready for reuse
uint8_t total_created_; // Total events created (high water mark) std::atomic<uint8_t> total_created_; // Total events created (high water mark)
}; };
} // namespace esp32_ble } // namespace esp32_ble