|
|
|
@ -65,26 +65,10 @@ private:
|
|
|
|
|
*/
|
|
|
|
|
template <typename TYPE, uint SIZE>
|
|
|
|
|
struct PersistentStorageArray : BasePersistentStorageArray {
|
|
|
|
|
TYPE storage[SIZE]; ///< Memory to for the storage array
|
|
|
|
|
TYPE *prev_storage; ///< Memory to store "old" states so we can revert them on the performance of test cases for commands etc.
|
|
|
|
|
|
|
|
|
|
/** Simply construct the array */
|
|
|
|
|
PersistentStorageArray() : prev_storage(nullptr)
|
|
|
|
|
{
|
|
|
|
|
memset(this->storage, 0, sizeof(this->storage));
|
|
|
|
|
}
|
|
|
|
|
using StorageType = std::array<TYPE, SIZE>;
|
|
|
|
|
|
|
|
|
|
/** And free all data related to it */
|
|
|
|
|
~PersistentStorageArray()
|
|
|
|
|
{
|
|
|
|
|
free(this->prev_storage);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Resets all values to zero. */
|
|
|
|
|
void ResetToZero()
|
|
|
|
|
{
|
|
|
|
|
memset(this->storage, 0, sizeof(this->storage));
|
|
|
|
|
}
|
|
|
|
|
StorageType storage{}; ///< Memory for the storage array
|
|
|
|
|
std::unique_ptr<StorageType> prev_storage{}; ///< Temporary memory to store previous state so it can be reverted, e.g. for command tests.
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Stores some value at a given position.
|
|
|
|
@ -104,10 +88,9 @@ struct PersistentStorageArray : BasePersistentStorageArray {
|
|
|
|
|
|
|
|
|
|
/* We do not have made a backup; lets do so */
|
|
|
|
|
if (AreChangesPersistent()) {
|
|
|
|
|
assert(this->prev_storage == nullptr);
|
|
|
|
|
} else if (this->prev_storage == nullptr) {
|
|
|
|
|
this->prev_storage = MallocT<TYPE>(SIZE);
|
|
|
|
|
memcpy(this->prev_storage, this->storage, sizeof(this->storage));
|
|
|
|
|
assert(!this->prev_storage);
|
|
|
|
|
} else if (!this->prev_storage) {
|
|
|
|
|
this->prev_storage = std::make_unique<StorageType>(this->storage);
|
|
|
|
|
|
|
|
|
|
/* We only need to register ourselves when we made the backup
|
|
|
|
|
* as that is the only time something will have changed */
|
|
|
|
@ -132,10 +115,9 @@ struct PersistentStorageArray : BasePersistentStorageArray {
|
|
|
|
|
|
|
|
|
|
void ClearChanges()
|
|
|
|
|
{
|
|
|
|
|
if (this->prev_storage != nullptr) {
|
|
|
|
|
memcpy(this->storage, this->prev_storage, sizeof(this->storage));
|
|
|
|
|
free(this->prev_storage);
|
|
|
|
|
this->prev_storage = nullptr;
|
|
|
|
|
if (this->prev_storage) {
|
|
|
|
|
this->storage = *this->prev_storage;
|
|
|
|
|
this->prev_storage.reset();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|