1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-05 19:33:47 +01:00
This commit is contained in:
J. Nick Koston
2025-06-16 15:16:15 +02:00
parent e7e4b995bf
commit 104658e43a

View File

@@ -30,28 +30,27 @@ template<uint8_t SIZE> class BLEEventPool {
BLEEvent *allocate() { BLEEvent *allocate() {
// Try to get from free list first // Try to get from free list first
BLEEvent *event = this->free_list_.pop(); BLEEvent *event = this->free_list_.pop();
if (event != nullptr)
return event;
if (event == nullptr) { // Need to create a new event
// Need to create a new event if (this->total_created_ >= SIZE) {
if (this->total_created_ >= SIZE) { // Pool is at capacity
// Pool is at capacity return nullptr;
return nullptr;
}
// Use internal RAM for better performance
RAMAllocator<BLEEvent> allocator(RAMAllocator<BLEEvent>::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);
} }
// Use internal RAM for better performance
RAMAllocator<BLEEvent> allocator(RAMAllocator<BLEEvent>::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; return event;
} }