summaryrefslogtreecommitdiff
path: root/src/core/pool_type.hpp
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2009-10-21 19:38:50 +0000
committerrubidium <rubidium@openttd.org>2009-10-21 19:38:50 +0000
commit1528b64a68f3144b9b321efefb22800a52e64dd4 (patch)
tree36eb5db202cd81e10859db92ac04407db8e51522 /src/core/pool_type.hpp
parent0ebe525e5bb4c0953bc60350c0dfeaf40112920f (diff)
downloadopenttd-1528b64a68f3144b9b321efefb22800a52e64dd4.tar.xz
(svn r17839) -Codechange: implement the concept of 'caching' pool items to pools, that is instead of 'free' push the unused items in a linked list and use them on 'malloc'. Also add the concept of zeroing, or actually not zeroing, on new for pool items.
Diffstat (limited to 'src/core/pool_type.hpp')
-rw-r--r--src/core/pool_type.hpp19
1 files changed, 17 insertions, 2 deletions
diff --git a/src/core/pool_type.hpp b/src/core/pool_type.hpp
index 728678515..58a58f6ac 100644
--- a/src/core/pool_type.hpp
+++ b/src/core/pool_type.hpp
@@ -18,8 +18,11 @@
* @tparam Tindex Type of the index for this pool
* @tparam Tgrowth_step Size of growths; if the pool is full increase the size by this amount
* @tparam Tmax_size Maximum size of the pool
+ * @tparam Tcache Whether to perform 'alloc' caching, i.e. don't actually free/malloc just reuse the memory
+ * @tparam Tzero Whether to zero the memory
+ * @warning when Tcache is enabled *all* instances of this pool's item must be of the same size.
*/
-template <class Titem, typename Tindex, size_t Tgrowth_step, size_t Tmax_size>
+template <class Titem, typename Tindex, size_t Tgrowth_step, size_t Tmax_size, bool Tcache = false, bool Tzero = true>
struct Pool {
static const size_t MAX_SIZE = Tmax_size; ///< Make template parameter accessible from outside
@@ -75,7 +78,7 @@ struct Pool {
* Base class for all PoolItems
* @tparam Tpool The pool this item is going to be part of
*/
- template <struct Pool<Titem, Tindex, Tgrowth_step, Tmax_size> *Tpool>
+ template <struct Pool<Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero> *Tpool>
struct PoolItem {
Tindex index; ///< Index of this pool item
@@ -253,6 +256,18 @@ private:
static const size_t NO_FREE_ITEM = MAX_UVALUE(size_t); ///< Contant to indicate we can't allocate any more items
/**
+ * Helper struct to cache 'freed' PoolItems so we
+ * do not need to allocate them again.
+ */
+ struct AllocCache {
+ /** The next in our 'cache' */
+ AllocCache *next;
+ };
+
+ /** Cache of freed pointers */
+ AllocCache *alloc_cache;
+
+ /**
* Makes given index valid
* @param size size of item
* @param index index of item