diff options
author | frosch <frosch@openttd.org> | 2013-12-23 18:07:57 +0000 |
---|---|---|
committer | frosch <frosch@openttd.org> | 2013-12-23 18:07:57 +0000 |
commit | 82eb9d13df0b12ab3125b81f362f4dcd2f907c43 (patch) | |
tree | 7e151c868c00ab29d051fdc3ebbce0f88bd22f8e | |
parent | 00c05e79b19546ccde2aa3ddb7726523224e3df9 (diff) | |
download | openttd-82eb9d13df0b12ab3125b81f362f4dcd2f907c43.tar.xz |
(svn r26171) -Codechange: Make TemporaryStorageArray keep track of assigned registers. Also make clearing the array 'cheaper'.
-rw-r--r-- | src/newgrf_storage.h | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/src/newgrf_storage.h b/src/newgrf_storage.h index cdbb041b4..a353e2186 100644 --- a/src/newgrf_storage.h +++ b/src/newgrf_storage.h @@ -135,11 +135,15 @@ struct PersistentStorageArray : BaseStorageArray { 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 : BaseStorageArray { if (pos >= SIZE) return; this->storage[pos] = value; + this->init[pos] = this->init_key; AddChangedStorage(this); } @@ -166,12 +171,23 @@ struct TemporaryStorageArray : BaseStorageArray { /* 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; + } } }; |