From 86012e10eaa9692cb16379bcfb8166bf26a81d5e Mon Sep 17 00:00:00 2001 From: Charles Pigott Date: Sat, 21 Apr 2018 14:56:02 +0100 Subject: [PATCH] Fix: Remove need to instantiate SmallStack's pool object by making it a singleton method --- src/core/smallstack_type.hpp | 28 ++++++++++++++++------------ src/station.cpp | 3 --- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/core/smallstack_type.hpp b/src/core/smallstack_type.hpp index 31edba0817..06b5aaafa6 100644 --- a/src/core/smallstack_type.hpp +++ b/src/core/smallstack_type.hpp @@ -195,10 +195,10 @@ public: inline void Push(const Titem &item) { if (this->value != Tinvalid) { - ThreadMutexLocker lock(_pool.GetMutex()); - Tindex new_item = _pool.Create(); + ThreadMutexLocker lock(SmallStack::GetPool().GetMutex()); + Tindex new_item = SmallStack::GetPool().Create(); if (new_item != Tmax_size) { - PooledSmallStack &pushed = _pool.Get(new_item); + PooledSmallStack &pushed = SmallStack::GetPool().Get(new_item); pushed.value = this->value; pushed.next = this->next; pushed.branch_count = 0; @@ -218,16 +218,16 @@ public: if (this->next == Tmax_size) { this->value = Tinvalid; } else { - ThreadMutexLocker lock(_pool.GetMutex()); - PooledSmallStack &popped = _pool.Get(this->next); + ThreadMutexLocker lock(SmallStack::GetPool().GetMutex()); + PooledSmallStack &popped = SmallStack::GetPool().Get(this->next); this->value = popped.value; if (popped.branch_count == 0) { - _pool.Destroy(this->next); + SmallStack::GetPool().Destroy(this->next); } else { --popped.branch_count; /* We can't use Branch() here as we already have the mutex.*/ if (popped.next != Tmax_size) { - ++(_pool.Get(popped.next).branch_count); + ++(SmallStack::GetPool().Get(popped.next).branch_count); } } /* Accessing popped here is no problem as the pool will only set @@ -257,11 +257,11 @@ public: { if (item == Tinvalid || item == this->value) return true; if (this->next != Tmax_size) { - ThreadMutexLocker lock(_pool.GetMutex()); + ThreadMutexLocker lock(SmallStack::GetPool().GetMutex()); const SmallStack *in_list = this; do { in_list = static_cast( - static_cast(&_pool.Get(in_list->next))); + static_cast(&SmallStack::GetPool().Get(in_list->next))); if (in_list->value == item) return true; } while (in_list->next != Tmax_size); } @@ -269,7 +269,11 @@ public: } protected: - static SmallStackPool _pool; + static SmallStackPool &GetPool() + { + static SmallStackPool pool; + return pool; + } /** * Create a branch in the pool if necessary. @@ -277,8 +281,8 @@ protected: inline void Branch() { if (this->next != Tmax_size) { - ThreadMutexLocker lock(_pool.GetMutex()); - ++(_pool.Get(this->next).branch_count); + ThreadMutexLocker lock(SmallStack::GetPool().GetMutex()); + ++(SmallStack::GetPool().Get(this->next).branch_count); } } }; diff --git a/src/station.cpp b/src/station.cpp index 456262dea4..f86286f3d9 100644 --- a/src/station.cpp +++ b/src/station.cpp @@ -35,9 +35,6 @@ StationPool _station_pool("Station"); INSTANTIATE_POOL_METHODS(Station) -typedef StationIDStack::SmallStackPool StationIDStackPool; -template<> StationIDStackPool StationIDStack::_pool = StationIDStackPool(); - BaseStation::~BaseStation() { free(this->name);