Changeset - r21087:647d6358127c
[Not reviewed]
master
0 1 0
frosch - 11 years ago 2013-12-23 18:07:57
frosch@openttd.org
(svn r26171) -Codechange: Make TemporaryStorageArray keep track of assigned registers. Also make clearing the array 'cheaper'.
1 file changed with 18 insertions and 2 deletions:
0 comments (0 inline, 0 general)
src/newgrf_storage.h
Show inline comments
 
@@ -135,11 +135,15 @@ struct PersistentStorageArray : BaseStor
 
template <typename TYPE, uint SIZE>
 
struct TemporaryStorageArray : BaseStorageArray {
 
	TYPE storage[SIZE]; ///< Memory to for the storage array
 
	uint16 init[SIZE];  ///< Storage has been assigned, if this equals 'init_key'.
 
	uint16 init_key;    ///< Magic key to 'init'.
 

	
 
	/** Simply construct the array */
 
	TemporaryStorageArray()
 
	{
 
		memset(this->storage, 0, sizeof(this->storage));
 
		memset(this->storage, 0, sizeof(this->storage)); // not exactly needed, but makes code analysers happy
 
		memset(this->init, 0, sizeof(this->init));
 
		this->init_key = 1;
 
	}
 

	
 
	/**
 
@@ -153,6 +157,7 @@ struct TemporaryStorageArray : BaseStora
 
		if (pos >= SIZE) return;
 

	
 
		this->storage[pos] = value;
 
		this->init[pos] = this->init_key;
 
		AddChangedStorage(this);
 
	}
 

	
 
@@ -166,12 +171,23 @@ struct TemporaryStorageArray : BaseStora
 
		/* Out of the scope of the array */
 
		if (pos >= SIZE) return 0;
 

	
 
		if (this->init[pos] != this->init_key) {
 
			/* Unassigned since last call to ClearChanges */
 
			return 0;
 
		}
 

	
 
		return this->storage[pos];
 
	}
 

	
 
	void ClearChanges(bool keep_changes)
 
	{
 
		memset(this->storage, 0, sizeof(this->storage));
 
		/* Increment init_key to invalidate all storage */
 
		this->init_key++;
 
		if (this->init_key == 0) {
 
			/* When init_key wraps around, we need to reset everything */
 
			memset(this->init, 0, sizeof(this->init));
 
			this->init_key = 1;
 
		}
 
	}
 
};
 

	
0 comments (0 inline, 0 general)