From 00e5c1df18449992cc974b99c61a44d1385bf4a7 Mon Sep 17 00:00:00 2001 From: terkhen Date: Sun, 12 Jun 2011 20:47:45 +0000 Subject: (svn r22567) -Codechange: Store persistent storages inside a pool. --- src/newgrf_storage.h | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'src/newgrf_storage.h') diff --git a/src/newgrf_storage.h b/src/newgrf_storage.h index dc8f86799..2ea3e3d8e 100644 --- a/src/newgrf_storage.h +++ b/src/newgrf_storage.h @@ -13,6 +13,7 @@ #define NEWGRF_STORAGE_H #include "core/alloc_func.hpp" +#include "core/pool_type.hpp" /** * Base class for all NewGRF storage arrays. Nothing fancy, only here @@ -178,4 +179,40 @@ struct TemporaryStorageArray : BaseStorageArray { void AddChangedStorage(BaseStorageArray *storage); void ClearStorageChanges(bool keep_changes); + +typedef PersistentStorageArray OldPersistentStorage; + +typedef uint32 PersistentStorageID; + +struct PersistentStorage; +typedef Pool PersistentStoragePool; + +extern PersistentStoragePool _persistent_storage_pool; + +/** + * Class for pooled persistent storage of data. + * On ClearChanges that data is always zero-ed. + */ +struct PersistentStorage : PersistentStorageArray, PersistentStoragePool::PoolItem<&_persistent_storage_pool> { + uint32 grfid; ///< GRFID associated to this persistent storage. A value of zero means "default". + + /** We don't want GCC to zero our struct! It already is zeroed and has an index! */ + PersistentStorage(const uint32 new_grfid) : grfid(new_grfid) + { + this->prev_storage = NULL; + memset(this->storage, 0, sizeof(this->storage)); + } + + /** Free the memory used by the persistent storage. */ + ~PersistentStorage() + { + free(this->prev_storage); + } +}; + +assert_compile(cpp_lengthof(OldPersistentStorage, storage) == cpp_lengthof(PersistentStorage, storage)); + +#define FOR_ALL_STORAGES_FROM(var, start) FOR_ALL_ITEMS_FROM(PersistentStorage, storage_index, var, start) +#define FOR_ALL_STORAGES(var) FOR_ALL_STORAGES_FROM(var, 0) + #endif /* NEWGRF_STORAGE_H */ -- cgit v1.2.3-54-g00ecf