mirror of
https://github.com/esphome/esphome.git
synced 2025-09-15 01:32:19 +01:00
ble pool
This commit is contained in:
@@ -25,7 +25,7 @@ namespace esp32_ble {
|
|||||||
|
|
||||||
static const char *const TAG = "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() {
|
void ESP32BLE::setup() {
|
||||||
global_ble = this;
|
global_ble = this;
|
||||||
|
@@ -21,7 +21,7 @@ template<size_t SIZE> class BLEEventPool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Initialize the free list - all indices are initially free
|
// 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_[i] = i + 1;
|
||||||
}
|
}
|
||||||
this->next_free_[SIZE - 1] = INVALID_INDEX;
|
this->next_free_[SIZE - 1] = INVALID_INDEX;
|
||||||
@@ -44,14 +44,14 @@ template<size_t SIZE> class BLEEventPool {
|
|||||||
// Returns INVALID_INDEX if pool is full
|
// Returns INVALID_INDEX if pool is full
|
||||||
size_t allocate() {
|
size_t allocate() {
|
||||||
while (true) {
|
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) {
|
if (head == INVALID_INDEX) {
|
||||||
// Pool is full
|
// Pool is full
|
||||||
return INVALID_INDEX;
|
return INVALID_INDEX;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t next = this->next_free_[head];
|
uint8_t next = this->next_free_[head];
|
||||||
|
|
||||||
// Try to update the free list 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)) {
|
if (this->free_head_.compare_exchange_weak(head, next, std::memory_order_release, std::memory_order_acquire)) {
|
||||||
@@ -72,11 +72,12 @@ template<size_t SIZE> class BLEEventPool {
|
|||||||
// The event's reset methods handle cleanup when switching types
|
// The event's reset methods handle cleanup when switching types
|
||||||
|
|
||||||
while (true) {
|
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;
|
this->next_free_[index] = head;
|
||||||
|
|
||||||
// Try to add this index back to the free list
|
// 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<uint8_t>(index), std::memory_order_release,
|
||||||
|
std::memory_order_acquire)) {
|
||||||
this->allocated_count_.fetch_sub(1, std::memory_order_relaxed);
|
this->allocated_count_.fetch_sub(1, std::memory_order_relaxed);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -117,14 +118,14 @@ template<size_t SIZE> class BLEEventPool {
|
|||||||
// Get total number of events created (high water mark)
|
// Get total number of events created (high water mark)
|
||||||
size_t get_total_created() const { return this->total_created_.load(std::memory_order_relaxed); }
|
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:
|
private:
|
||||||
BLEEvent *events_[SIZE]; // Array of pointers, allocated on demand
|
BLEEvent *events_[SIZE]; // Array of pointers, allocated on demand
|
||||||
size_t next_free_[SIZE]; // Next free index for each slot
|
uint8_t next_free_[SIZE]; // Next free index for each slot
|
||||||
std::atomic<size_t> free_head_; // Head of the free list
|
std::atomic<uint8_t> free_head_; // Head of the free list
|
||||||
std::atomic<size_t> allocated_count_; // Number of currently allocated events
|
std::atomic<uint8_t> allocated_count_; // Number of currently allocated events
|
||||||
std::atomic<size_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
|
||||||
|
@@ -12,11 +12,11 @@ namespace esp32_ble {
|
|||||||
// This allows us to use a pre-allocated pool of objects
|
// This allows us to use a pre-allocated pool of objects
|
||||||
template<size_t SIZE> class LockFreeIndexQueue {
|
template<size_t SIZE> class LockFreeIndexQueue {
|
||||||
public:
|
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) {
|
LockFreeIndexQueue() : head_(0), tail_(0), dropped_count_(0) {
|
||||||
// Initialize all slots to invalid
|
// 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;
|
buffer_[i] = INVALID_INDEX;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -69,10 +69,10 @@ template<size_t SIZE> class LockFreeIndexQueue {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
size_t buffer_[SIZE];
|
uint8_t buffer_[SIZE];
|
||||||
std::atomic<size_t> head_;
|
std::atomic<uint8_t> head_;
|
||||||
std::atomic<size_t> tail_;
|
std::atomic<uint8_t> tail_;
|
||||||
std::atomic<size_t> dropped_count_;
|
std::atomic<uint32_t> dropped_count_; // Keep this as uint32_t for larger counts
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace esp32_ble
|
} // namespace esp32_ble
|
||||||
|
Reference in New Issue
Block a user