Skip to content

Commit e97670e

Browse files
committed
Simplify static pool implementation
1 parent 06f89d0 commit e97670e

1 file changed

Lines changed: 6 additions & 20 deletions

File tree

include/cpp_event_framework/StaticPool.hxx

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ private:
4949
std::array<QueueElement, NumElements> pool_mem_ = {};
5050
MutexType mutex_;
5151
QueueElement* first_ = nullptr;
52-
QueueElement* last_ = nullptr;
5352
const char* name_ = nullptr;
5453
std::atomic<size_t> fill_level_ = NumElements;
5554

@@ -59,12 +58,13 @@ public:
5958
*
6059
* @param name Pool name (logging)
6160
*/
62-
explicit StaticPool(const char* name) : first_(&pool_mem_.at(0)), last_(first_), name_(name)
61+
explicit StaticPool(const char* name) : first_(&pool_mem_.at(0)), name_(name)
6362
{
63+
auto prev = first_;
6464
for (size_t i = 1; i < NumElements; i++)
6565
{
66-
last_->next = &pool_mem_.at(i);
67-
last_ = last_->next;
66+
prev->next = &pool_mem_.at(i);
67+
prev = prev->next;
6868
}
6969
}
7070

@@ -87,10 +87,6 @@ public:
8787

8888
auto* result = first_;
8989
first_ = result->next;
90-
if (first_ == nullptr)
91-
{
92-
last_ = nullptr;
93-
}
9490
fill_level_--;
9591
return result;
9692
}
@@ -103,18 +99,8 @@ public:
10399
auto ptr = static_cast<QueueElement*>(p);
104100

105101
std::scoped_lock lock(mutex_);
106-
if (first_ == nullptr)
107-
{
108-
first_ = ptr;
109-
}
110-
111-
if (last_ != nullptr)
112-
{
113-
last_->next = ptr;
114-
}
115-
last_ = ptr;
116-
ptr->next = nullptr;
117-
102+
ptr->next = first_;
103+
first_ = ptr;
118104
fill_level_++;
119105
assert(FillLevel() <= NumElements);
120106
}

0 commit comments

Comments
 (0)