From 8034bae9def35924110cce5e07102b6fb8ba30fb Mon Sep 17 00:00:00 2001 From: Stephen Anthony Date: Tue, 12 Dec 2017 16:44:30 -0330 Subject: [PATCH] Added destructive resize to LinkedObjectPool/RewindManager. Fixed minor warnings in DebuggerDialog. --- src/common/LinkedObjectPool.hxx | 35 ++++++++++++++++++++++----------- src/common/RewindManager.hxx | 5 +++-- src/gui/DeveloperDialog.cxx | 22 ++------------------- 3 files changed, 28 insertions(+), 34 deletions(-) diff --git a/src/common/LinkedObjectPool.hxx b/src/common/LinkedObjectPool.hxx index b609395f4..0d42d92d5 100644 --- a/src/common/LinkedObjectPool.hxx +++ b/src/common/LinkedObjectPool.hxx @@ -59,9 +59,8 @@ class LinkedObjectPool /* Create a pool of size CAPACITY; the active list starts out empty. */ - LinkedObjectPool() : myCurrent(myList.end()) { - for(uInt32 i = 0; i < CAPACITY; ++i) - myPool.emplace_back(T()); + LinkedObjectPool() : myCurrent(myList.end()), myCapacity(0) { + resize(CAPACITY); } /** @@ -199,6 +198,22 @@ class LinkedObjectPool myPool.splice(myPool.end(), myList, std::next(myCurrent, 1), myList.end()); } + /** + Resize the pool to specified size, invalidating the list in the process + (ie, the list essentially becomes empty again). + */ + void resize(uInt32 capacity) { + if(myCapacity != capacity) // only resize when necessary + { + myList.clear(); myPool.clear(); + myCurrent = myList.end(); + myCapacity = capacity; + + for(uInt32 i = 0; i < myCapacity; ++i) + myPool.emplace_back(T()); + } + } + /** Erase entire contents of active list. */ @@ -207,18 +222,11 @@ class LinkedObjectPool myCurrent = myList.end(); } -#if 0 - /** Access the list with iterators, just as you would a normal C++ STL list */ - iter begin() { return myList.begin(); } - iter end() { return myList.end(); } - const_iter begin() const { return myList.cbegin(); } - const_iter end() const { return myList.cend(); } -#endif - uInt32 capacity() const { return CAPACITY; } + uInt32 capacity() const { return myCapacity; } uInt32 size() const { return uInt32(myList.size()); } bool empty() const { return size() == 0; } - bool full() const { return size() >= CAPACITY; } + bool full() const { return size() >= capacity(); } private: std::list myList, myPool; @@ -226,6 +234,9 @@ class LinkedObjectPool // Current position in the active list (end() indicates an invalid position) iter myCurrent; + // Total capacity of the pool + uInt32 myCapacity; + private: // Following constructors and assignment operators not supported LinkedObjectPool(const LinkedObjectPool&) = delete; diff --git a/src/common/RewindManager.hxx b/src/common/RewindManager.hxx index fbfb5a2a6..920acf49e 100644 --- a/src/common/RewindManager.hxx +++ b/src/common/RewindManager.hxx @@ -46,7 +46,7 @@ class RewindManager RewindManager(OSystem& system, StateManager& statemgr); public: - static const int NUM_INTERVALS = 7; + static constexpr int NUM_INTERVALS = 7; const uInt32 INTERVAL_CYCLES[NUM_INTERVALS] = { 76 * 262, 76 * 262 * 3, @@ -71,7 +71,7 @@ class RewindManager "10s" }; - static const int NUM_HORIZONS = 8; + static constexpr int NUM_HORIZONS = 8; const uInt64 HORIZON_CYCLES[NUM_HORIZONS] = { 76 * 262 * 60 * 3, 76 * 262 * 60 * 10, @@ -124,6 +124,7 @@ class RewindManager bool atFirst() const { return myStateList.atFirst(); } bool atLast() const { return myStateList.atLast(); } + void resize(uInt32 size) { myStateList.resize(size); } void clear() { myStateList.clear(); } /** diff --git a/src/gui/DeveloperDialog.cxx b/src/gui/DeveloperDialog.cxx index 8727bbc3d..2e1d1a651 100644 --- a/src/gui/DeveloperDialog.cxx +++ b/src/gui/DeveloperDialog.cxx @@ -972,19 +972,13 @@ void DeveloperDialog::handleSize() bool found = false; Int32 i; - // handle illegal values - if(interval == -1) - interval = 0; - if(horizon == -1) - horizon = 0; - myStateSizeLabelWidget->setValue(size); // adapt horizon and interval do { for(i = horizon; i < NUM_HORIZONS; ++i) { - if((uInt64)size * instance().state().rewindManager().INTERVAL_CYCLES[interval] + if(uInt64(size) * instance().state().rewindManager().INTERVAL_CYCLES[interval] <= instance().state().rewindManager().HORIZON_CYCLES[i]) { found = true; @@ -1023,18 +1017,12 @@ void DeveloperDialog::handleInterval() bool found = false; Int32 i; - // handle illegal values - if(interval == -1) - interval = 0; - if(horizon == -1) - horizon = 0; - // adapt horizon and size do { for(i = horizon; i < NUM_HORIZONS; ++i) { - if((uInt64)size * instance().state().rewindManager().INTERVAL_CYCLES[interval] + if(uInt64(size) * instance().state().rewindManager().INTERVAL_CYCLES[interval] <= instance().state().rewindManager().HORIZON_CYCLES[i]) { found = true; @@ -1061,12 +1049,6 @@ void DeveloperDialog::handleHorizon() bool found = false; Int32 i; - // handle illegal values - if(interval == -1) - interval = 0; - if(horizon == -1) - horizon = 0; - // adapt interval and size do {