From e97670e3ae29a87c14e8866d11c05b5511480ef1 Mon Sep 17 00:00:00 2001 From: Dirk Ziegelmeier Date: Sun, 13 Oct 2024 22:11:38 +0200 Subject: [PATCH] Simplify static pool implementation --- include/cpp_event_framework/StaticPool.hxx | 26 +++++----------------- 1 file changed, 6 insertions(+), 20 deletions(-) diff --git a/include/cpp_event_framework/StaticPool.hxx b/include/cpp_event_framework/StaticPool.hxx index dcd77d7..b8d3ef9 100644 --- a/include/cpp_event_framework/StaticPool.hxx +++ b/include/cpp_event_framework/StaticPool.hxx @@ -49,7 +49,6 @@ private: std::array pool_mem_ = {}; MutexType mutex_; QueueElement* first_ = nullptr; - QueueElement* last_ = nullptr; const char* name_ = nullptr; std::atomic fill_level_ = NumElements; @@ -59,12 +58,13 @@ public: * * @param name Pool name (logging) */ - explicit StaticPool(const char* name) : first_(&pool_mem_.at(0)), last_(first_), name_(name) + explicit StaticPool(const char* name) : first_(&pool_mem_.at(0)), name_(name) { + auto prev = first_; for (size_t i = 1; i < NumElements; i++) { - last_->next = &pool_mem_.at(i); - last_ = last_->next; + prev->next = &pool_mem_.at(i); + prev = prev->next; } } @@ -87,10 +87,6 @@ public: auto* result = first_; first_ = result->next; - if (first_ == nullptr) - { - last_ = nullptr; - } fill_level_--; return result; } @@ -103,18 +99,8 @@ public: auto ptr = static_cast(p); std::scoped_lock lock(mutex_); - if (first_ == nullptr) - { - first_ = ptr; - } - - if (last_ != nullptr) - { - last_->next = ptr; - } - last_ = ptr; - ptr->next = nullptr; - + ptr->next = first_; + first_ = ptr; fill_level_++; assert(FillLevel() <= NumElements); }