This commit is contained in:
J. Nick Koston
2025-06-27 16:36:32 -05:00
parent 95ef131285
commit 90736f367a
2 changed files with 8 additions and 11 deletions

View File

@@ -134,16 +134,13 @@ class BLEEvent {
} }
// Destructor to clean up heap allocations // Destructor to clean up heap allocations
~BLEEvent() { this->cleanup_heap_data(); } ~BLEEvent() { this->release(); }
// Default constructor for pre-allocation in pool // Default constructor for pre-allocation in pool
BLEEvent() : type_(GAP) {} BLEEvent() : type_(GAP) {}
// Invoked on return to EventPool // Invoked on return to EventPool - clean up any heap-allocated data
void clear() { this->cleanup_heap_data(); } void release() {
// Clean up any heap-allocated data
void cleanup_heap_data() {
if (this->type_ == GAP) { if (this->type_ == GAP) {
return; return;
} }
@@ -164,19 +161,19 @@ class BLEEvent {
// Load new event data for reuse (replaces previous event data) // Load new event data for reuse (replaces previous event data)
void load_gap_event(esp_gap_ble_cb_event_t e, esp_ble_gap_cb_param_t *p) { void load_gap_event(esp_gap_ble_cb_event_t e, esp_ble_gap_cb_param_t *p) {
this->cleanup_heap_data(); this->release();
this->type_ = GAP; this->type_ = GAP;
this->init_gap_data_(e, p); this->init_gap_data_(e, p);
} }
void load_gattc_event(esp_gattc_cb_event_t e, esp_gatt_if_t i, esp_ble_gattc_cb_param_t *p) { void load_gattc_event(esp_gattc_cb_event_t e, esp_gatt_if_t i, esp_ble_gattc_cb_param_t *p) {
this->cleanup_heap_data(); this->release();
this->type_ = GATTC; this->type_ = GATTC;
this->init_gattc_data_(e, i, p); this->init_gattc_data_(e, i, p);
} }
void load_gatts_event(esp_gatts_cb_event_t e, esp_gatt_if_t i, esp_ble_gatts_cb_param_t *p) { void load_gatts_event(esp_gatts_cb_event_t e, esp_gatt_if_t i, esp_ble_gatts_cb_param_t *p) {
this->cleanup_heap_data(); this->release();
this->type_ = GATTS; this->type_ = GATTS;
this->init_gatts_data_(e, i, p); this->init_gatts_data_(e, i, p);
} }

View File

@@ -11,7 +11,7 @@ namespace esphome {
// Event Pool - On-demand pool of objects to avoid heap fragmentation // Event Pool - On-demand pool of objects to avoid heap fragmentation
// 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
// @tparam T The type of objects managed by the pool (must have a clear() method) // @tparam T The type of objects managed by the pool (must have a release() method)
// @tparam SIZE The maximum number of objects in the pool (1-255, limited by uint8_t) // @tparam SIZE The maximum number of objects in the pool (1-255, limited by uint8_t)
template<class T, uint8_t SIZE> class EventPool { template<class T, uint8_t SIZE> class EventPool {
public: public:
@@ -66,7 +66,7 @@ template<class T, uint8_t SIZE> class EventPool {
void release(T *event) { void release(T *event) {
if (event != nullptr) { if (event != nullptr) {
// Clean up the event's allocated memory // Clean up the event's allocated memory
event->clear(); event->release();
this->free_list_.push(event); this->free_list_.push(event);
} }
} }