Changeset - r27874:c6ae6b5e2114
[Not reviewed]
master
0 3 0
Peter Nelson - 12 months ago 2023-09-08 19:34:20
peter1138@openttd.org
Codechange: Use std::array and std::unique_ptr for PersistentStorageArrays.

This (mostly) avoids the need for manual memory management and copying.
3 files changed with 11 insertions and 29 deletions:
0 comments (0 inline, 0 general)
src/newgrf_storage.h
Show inline comments
 
@@ -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 : BasePers
 

	
 
		/* 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 : BasePers
 

	
 
	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();
 
		}
 
	}
 
};
src/saveload/industry_sl.cpp
Show inline comments
 
@@ -246,7 +246,7 @@ struct INDYChunkHandler : ChunkHandler {
 
				/* Store the old persistent storage. The GRFID will be added later. */
 
				assert(PersistentStorage::CanAllocateItem());
 
				i->psa = new PersistentStorage(0, 0, 0);
 
				memcpy(i->psa->storage, _old_ind_persistent_storage.storage, sizeof(_old_ind_persistent_storage.storage));
 
				std::copy(std::begin(_old_ind_persistent_storage.storage), std::end(_old_ind_persistent_storage.storage), std::begin(i->psa->storage));
 
			}
 
			if (IsSavegameVersionBefore(SLV_INDUSTRY_CARGO_REORGANISE)) LoadMoveAcceptsProduced(i);
 
			Industry::IncIndustryTypeCount(i->type);
src/saveload/station_sl.cpp
Show inline comments
 
@@ -426,7 +426,7 @@ public:
 
			/* Store the old persistent storage. The GRFID will be added later. */
 
			assert(PersistentStorage::CanAllocateItem());
 
			st->airport.psa = new PersistentStorage(0, 0, 0);
 
			memcpy(st->airport.psa->storage, _old_st_persistent_storage.storage, sizeof(_old_st_persistent_storage.storage));
 
			std::copy(std::begin(_old_st_persistent_storage.storage), std::end(_old_st_persistent_storage.storage), std::begin(st->airport.psa->storage));
 
		}
 

	
 
		size_t num_cargo = this->GetNumCargo();
0 comments (0 inline, 0 general)