2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file oldpool.h Base for the old pool. */
|
2007-03-21 15:19:33 +00:00
|
|
|
|
2006-12-03 20:03:40 +00:00
|
|
|
#ifndef OLDPOOL_H
|
|
|
|
#define OLDPOOL_H
|
2005-02-01 18:30:11 +00:00
|
|
|
|
2007-12-21 19:21:21 +00:00
|
|
|
#include "core/math_func.hpp"
|
|
|
|
|
2005-02-01 18:30:11 +00:00
|
|
|
/* The function that is called after a new block is added
|
|
|
|
start_item is the first item of the new made block */
|
2006-12-03 17:27:43 +00:00
|
|
|
typedef void OldMemoryPoolNewBlock(uint start_item);
|
2006-04-18 18:48:50 +00:00
|
|
|
/* The function that is called before a block is cleaned up */
|
2006-12-03 17:27:43 +00:00
|
|
|
typedef void OldMemoryPoolCleanBlock(uint start_item, uint end_item);
|
2005-02-01 18:30:11 +00:00
|
|
|
|
|
|
|
/**
|
2006-12-03 17:27:43 +00:00
|
|
|
* Stuff for dynamic vehicles. Use the wrappers to access the OldMemoryPool
|
2005-02-01 18:30:11 +00:00
|
|
|
* please try to avoid manual calls!
|
|
|
|
*/
|
2007-08-01 22:10:54 +00:00
|
|
|
struct OldMemoryPoolBase {
|
|
|
|
void CleanPool();
|
|
|
|
bool AddBlockToPool();
|
|
|
|
bool AddBlockIfNeeded(uint index);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
OldMemoryPoolBase(const char *name, uint max_blocks, uint block_size_bits, uint item_size,
|
|
|
|
OldMemoryPoolNewBlock *new_block_proc, OldMemoryPoolCleanBlock *clean_block_proc) :
|
2007-08-02 21:02:57 +00:00
|
|
|
name(name), max_blocks(max_blocks), block_size_bits(block_size_bits),
|
2007-08-01 22:10:54 +00:00
|
|
|
new_block_proc(new_block_proc), clean_block_proc(clean_block_proc), current_blocks(0),
|
2007-08-05 21:20:55 +00:00
|
|
|
total_items(0), cleaning_pool(false), item_size(item_size), first_free_index(0), blocks(NULL) {}
|
2007-08-01 22:10:54 +00:00
|
|
|
|
2009-01-10 00:31:47 +00:00
|
|
|
const char *name; ///< Name of the pool (just for debugging)
|
2005-02-01 18:30:11 +00:00
|
|
|
|
2007-08-02 08:47:56 +00:00
|
|
|
const uint max_blocks; ///< The max amount of blocks this pool can have
|
|
|
|
const uint block_size_bits; ///< The size of each block in bits
|
2005-02-01 18:30:11 +00:00
|
|
|
|
2006-03-09 20:37:51 +00:00
|
|
|
/// Pointer to a function that is called after a new block is added
|
2007-08-02 13:18:57 +00:00
|
|
|
OldMemoryPoolNewBlock *new_block_proc;
|
2006-04-18 18:48:50 +00:00
|
|
|
/// Pointer to a function that is called to clean a block
|
2007-08-02 13:18:57 +00:00
|
|
|
OldMemoryPoolCleanBlock *clean_block_proc;
|
2005-02-01 18:30:11 +00:00
|
|
|
|
2006-03-09 20:37:51 +00:00
|
|
|
uint current_blocks; ///< How many blocks we have in our pool
|
|
|
|
uint total_items; ///< How many items we now have in this pool
|
2005-02-01 18:30:11 +00:00
|
|
|
|
2007-08-05 21:20:55 +00:00
|
|
|
bool cleaning_pool; ///< Are we currently cleaning the pool?
|
2007-08-01 22:10:54 +00:00
|
|
|
public:
|
2007-08-02 21:02:57 +00:00
|
|
|
const uint item_size; ///< How many bytes one block is
|
2007-08-02 10:47:00 +00:00
|
|
|
uint first_free_index; ///< The index of the first free pool item in this pool
|
2006-03-09 20:37:51 +00:00
|
|
|
byte **blocks; ///< An array of blocks (one block hold all the items)
|
2007-08-01 22:10:54 +00:00
|
|
|
|
2007-11-26 22:56:30 +00:00
|
|
|
/**
|
|
|
|
* Check if the index of pool item being deleted is lower than cached first_free_index
|
|
|
|
* @param index index of pool item
|
|
|
|
* @note usage of min() will result in better code on some architectures
|
|
|
|
*/
|
|
|
|
inline void UpdateFirstFreeIndex(uint index)
|
|
|
|
{
|
|
|
|
first_free_index = min(first_free_index, index);
|
|
|
|
}
|
|
|
|
|
2007-08-02 08:47:56 +00:00
|
|
|
/**
|
|
|
|
* Get the size of this pool, i.e. the total number of items you
|
|
|
|
* can put into it at the current moment; the pool might still
|
|
|
|
* be able to increase the size of the pool.
|
|
|
|
* @return the size of the pool
|
|
|
|
*/
|
|
|
|
inline uint GetSize() const
|
2007-08-01 22:10:54 +00:00
|
|
|
{
|
|
|
|
return this->total_items;
|
|
|
|
}
|
|
|
|
|
2007-08-02 08:47:56 +00:00
|
|
|
/**
|
|
|
|
* Can this pool allocate more blocks, i.e. is the maximum amount
|
|
|
|
* of allocated blocks not yet reached?
|
|
|
|
* @return the if and only if the amount of allocable blocks is
|
|
|
|
* less than the amount of allocated blocks.
|
|
|
|
*/
|
|
|
|
inline bool CanAllocateMoreBlocks() const
|
2007-08-01 22:10:54 +00:00
|
|
|
{
|
|
|
|
return this->current_blocks < this->max_blocks;
|
|
|
|
}
|
|
|
|
|
2007-08-02 08:47:56 +00:00
|
|
|
/**
|
|
|
|
* Get the maximum number of allocable blocks.
|
|
|
|
* @return the numebr of blocks
|
|
|
|
*/
|
|
|
|
inline uint GetBlockCount() const
|
2007-08-01 22:10:54 +00:00
|
|
|
{
|
|
|
|
return this->current_blocks;
|
|
|
|
}
|
2007-08-02 08:47:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the name of this pool.
|
|
|
|
* @return the name
|
|
|
|
*/
|
|
|
|
inline const char *GetName() const
|
|
|
|
{
|
|
|
|
return this->name;
|
|
|
|
}
|
2007-08-05 21:20:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Is the pool in the cleaning phase?
|
|
|
|
* @return true if it is
|
|
|
|
*/
|
|
|
|
inline bool CleaningPool() const
|
|
|
|
{
|
|
|
|
return this->cleaning_pool;
|
|
|
|
}
|
2007-08-01 22:10:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct OldMemoryPool : public OldMemoryPoolBase {
|
|
|
|
OldMemoryPool(const char *name, uint max_blocks, uint block_size_bits, uint item_size,
|
|
|
|
OldMemoryPoolNewBlock *new_block_proc, OldMemoryPoolCleanBlock *clean_block_proc) :
|
|
|
|
OldMemoryPoolBase(name, max_blocks, block_size_bits, item_size, new_block_proc, clean_block_proc) {}
|
|
|
|
|
2007-08-02 08:47:56 +00:00
|
|
|
/**
|
|
|
|
* Get the pool entry at the given index.
|
|
|
|
* @param index the index into the pool
|
|
|
|
* @pre index < this->GetSize()
|
|
|
|
* @return the pool entry.
|
|
|
|
*/
|
|
|
|
inline T *Get(uint index) const
|
2007-08-01 22:10:54 +00:00
|
|
|
{
|
|
|
|
assert(index < this->GetSize());
|
|
|
|
return (T*)(this->blocks[index >> this->block_size_bits] +
|
2007-08-02 21:02:57 +00:00
|
|
|
(index & ((1 << this->block_size_bits) - 1)) * this->item_size);
|
2007-08-01 22:10:54 +00:00
|
|
|
}
|
2005-02-01 18:30:11 +00:00
|
|
|
};
|
|
|
|
|
2007-08-01 23:49:06 +00:00
|
|
|
/**
|
|
|
|
* Generic function to initialize a new block in a pool.
|
|
|
|
* @param start_item the first item that needs to be initialized
|
|
|
|
*/
|
|
|
|
template <typename T, OldMemoryPool<T> *Tpool>
|
|
|
|
static void PoolNewBlock(uint start_item)
|
|
|
|
{
|
|
|
|
for (T *t = Tpool->Get(start_item); t != NULL; t = (t->index + 1U < Tpool->GetSize()) ? Tpool->Get(t->index + 1U) : NULL) {
|
2007-08-02 08:47:56 +00:00
|
|
|
t = new (t) T();
|
2007-08-01 23:49:06 +00:00
|
|
|
t->index = start_item++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generic function to free a new block in a pool.
|
|
|
|
* @param start_item the first item that needs to be cleaned
|
|
|
|
* @param end_item the last item that needs to be cleaned
|
|
|
|
*/
|
|
|
|
template <typename T, OldMemoryPool<T> *Tpool>
|
|
|
|
static void PoolCleanBlock(uint start_item, uint end_item)
|
|
|
|
{
|
|
|
|
for (uint i = start_item; i <= end_item; i++) {
|
|
|
|
T *t = Tpool->Get(i);
|
2007-08-05 21:20:55 +00:00
|
|
|
delete t;
|
2007-08-01 23:49:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-14 12:07:14 +00:00
|
|
|
/**
|
|
|
|
* Template providing a predicate to allow STL containers of
|
|
|
|
* pointers to pool items to be sorted by index.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
struct PoolItemIndexLess {
|
|
|
|
/**
|
|
|
|
* The actual comparator.
|
|
|
|
* @param lhs the left hand side of the comparison.
|
|
|
|
* @param rhs the right hand side of the comparison.
|
|
|
|
* @return true if lhs' index is less than rhs' index.
|
|
|
|
*/
|
|
|
|
bool operator()(const T *lhs, const T *rhs) const
|
|
|
|
{
|
|
|
|
return lhs->index < rhs->index;
|
|
|
|
}
|
|
|
|
};
|
2007-08-01 23:49:06 +00:00
|
|
|
|
2007-08-02 08:47:56 +00:00
|
|
|
/**
|
|
|
|
* Generalization for all pool items that are saved in the savegame.
|
|
|
|
* It specifies all the mechanics to access the pool easily.
|
|
|
|
*/
|
|
|
|
template <typename T, typename Tid, OldMemoryPool<T> *Tpool>
|
|
|
|
struct PoolItem {
|
|
|
|
/**
|
|
|
|
* The pool-wide index of this object.
|
|
|
|
*/
|
|
|
|
Tid index;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* We like to have the correct class destructed.
|
2007-11-26 22:56:30 +00:00
|
|
|
* @warning It is called even for object allocated on stack,
|
|
|
|
* so it is not present in the TPool!
|
|
|
|
* Then, index is undefined, not associated with TPool in any way.
|
|
|
|
* @note The idea is to free up allocated memory etc.
|
2007-08-02 08:47:56 +00:00
|
|
|
*/
|
|
|
|
virtual ~PoolItem()
|
|
|
|
{
|
2007-11-26 22:56:30 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor of given class.
|
|
|
|
* @warning It is called even for object allocated on stack,
|
|
|
|
* so it may not be present in TPool!
|
|
|
|
* Then, index is undefined, not associated with TPool in any way.
|
|
|
|
* @note The idea is to initialize variables (except index)
|
|
|
|
*/
|
|
|
|
PoolItem()
|
|
|
|
{
|
|
|
|
|
2007-08-02 08:47:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An overriden version of new that allocates memory on the pool.
|
|
|
|
* @param size the size of the variable (unused)
|
2009-01-09 15:01:15 +00:00
|
|
|
* @pre CanAllocateItem()
|
2007-08-02 08:47:56 +00:00
|
|
|
* @return the memory that is 'allocated'
|
|
|
|
*/
|
2007-08-02 12:22:40 +00:00
|
|
|
void *operator new(size_t size)
|
2007-08-02 08:47:56 +00:00
|
|
|
{
|
|
|
|
return AllocateRaw();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 'Free' the memory allocated by the overriden new.
|
|
|
|
* @param p the memory to 'free'
|
2007-11-26 22:56:30 +00:00
|
|
|
* @note we only update Tpool->first_free_index
|
2007-08-02 08:47:56 +00:00
|
|
|
*/
|
|
|
|
void operator delete(void *p)
|
|
|
|
{
|
2007-11-26 22:56:30 +00:00
|
|
|
Tpool->UpdateFirstFreeIndex(((T*)p)->index);
|
2007-08-02 08:47:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An overriden version of new, so you can directly allocate a new object with
|
|
|
|
* the correct index when one is loading the savegame.
|
|
|
|
* @param size the size of the variable (unused)
|
|
|
|
* @param index the index of the object
|
|
|
|
* @return the memory that is 'allocated'
|
|
|
|
*/
|
2007-08-02 12:22:40 +00:00
|
|
|
void *operator new(size_t size, int index)
|
2007-08-02 08:47:56 +00:00
|
|
|
{
|
|
|
|
if (!Tpool->AddBlockIfNeeded(index)) error("%s: failed loading savegame: too many %s", Tpool->GetName(), Tpool->GetName());
|
|
|
|
|
|
|
|
return Tpool->Get(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 'Free' the memory allocated by the overriden new.
|
|
|
|
* @param p the memory to 'free'
|
|
|
|
* @param index the original parameter given to create the memory
|
2007-11-26 22:56:30 +00:00
|
|
|
* @note we only update Tpool->first_free_index
|
2007-08-02 08:47:56 +00:00
|
|
|
*/
|
|
|
|
void operator delete(void *p, int index)
|
|
|
|
{
|
2007-11-26 22:56:30 +00:00
|
|
|
Tpool->UpdateFirstFreeIndex(index);
|
2007-08-02 08:47:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An overriden version of new, so you can use the vehicle instance
|
|
|
|
* instead of a newly allocated piece of memory.
|
|
|
|
* @param size the size of the variable (unused)
|
|
|
|
* @param pn the already existing object to use as 'storage' backend
|
|
|
|
* @return the memory that is 'allocated'
|
|
|
|
*/
|
|
|
|
void *operator new(size_t size, T *pn)
|
|
|
|
{
|
|
|
|
return pn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 'Free' the memory allocated by the overriden new.
|
|
|
|
* @param p the memory to 'free'
|
|
|
|
* @param pn the pointer that was given to 'new' on creation.
|
2007-11-26 22:56:30 +00:00
|
|
|
* @note we only update Tpool->first_free_index
|
2007-08-02 08:47:56 +00:00
|
|
|
*/
|
|
|
|
void operator delete(void *p, T *pn)
|
|
|
|
{
|
2007-11-26 22:56:30 +00:00
|
|
|
Tpool->UpdateFirstFreeIndex(pn->index);
|
2007-08-02 08:47:56 +00:00
|
|
|
}
|
|
|
|
|
2007-08-08 14:18:05 +00:00
|
|
|
private:
|
2008-04-06 23:49:45 +00:00
|
|
|
static T *AllocateSafeRaw(uint &first);
|
2007-08-05 21:20:55 +00:00
|
|
|
|
2007-08-08 14:18:05 +00:00
|
|
|
protected:
|
|
|
|
/**
|
|
|
|
* Allocate a pool item; possibly allocate a new block in the pool.
|
2009-01-09 15:01:15 +00:00
|
|
|
* @pre CanAllocateItem()
|
|
|
|
* @return the allocated pool item.
|
2007-08-08 14:18:05 +00:00
|
|
|
*/
|
|
|
|
static inline T *AllocateRaw()
|
|
|
|
{
|
|
|
|
return AllocateSafeRaw(Tpool->first_free_index);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allocate a pool item; possibly allocate a new block in the pool.
|
|
|
|
* @param first the first pool item to start searching
|
2009-01-09 15:01:15 +00:00
|
|
|
* @pre CanAllocateItem()
|
|
|
|
* @return the allocated pool item.
|
2007-08-08 14:18:05 +00:00
|
|
|
*/
|
|
|
|
static inline T *AllocateRaw(uint &first)
|
|
|
|
{
|
|
|
|
if (first >= Tpool->GetSize() && !Tpool->AddBlockToPool()) return NULL;
|
|
|
|
|
|
|
|
return AllocateSafeRaw(first);
|
|
|
|
}
|
|
|
|
|
2007-08-05 21:20:55 +00:00
|
|
|
/**
|
|
|
|
* Are we cleaning this pool?
|
|
|
|
* @return true if we are
|
|
|
|
*/
|
|
|
|
static inline bool CleaningPool()
|
|
|
|
{
|
|
|
|
return Tpool->CleaningPool();
|
|
|
|
}
|
2008-04-23 20:56:08 +00:00
|
|
|
|
|
|
|
public:
|
2008-10-30 12:32:32 +00:00
|
|
|
static bool CanAllocateItem(uint count = 1);
|
2007-08-02 08:47:56 +00:00
|
|
|
};
|
|
|
|
|
2007-08-01 23:49:06 +00:00
|
|
|
|
2006-12-03 17:27:43 +00:00
|
|
|
#define OLD_POOL_ENUM(name, type, block_size_bits, max_blocks) \
|
2006-10-28 10:54:20 +00:00
|
|
|
enum { \
|
|
|
|
name##_POOL_BLOCK_SIZE_BITS = block_size_bits, \
|
|
|
|
name##_POOL_MAX_BLOCKS = max_blocks \
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2006-12-03 17:27:43 +00:00
|
|
|
#define OLD_POOL_ACCESSORS(name, type) \
|
2009-01-10 00:31:47 +00:00
|
|
|
static inline type *Get##name(uint index) { return _##name##_pool.Get(index); } \
|
2007-08-01 22:10:54 +00:00
|
|
|
static inline uint Get##name##PoolSize() { return _##name##_pool.GetSize(); }
|
2006-10-28 10:54:20 +00:00
|
|
|
|
|
|
|
|
2006-12-03 17:27:43 +00:00
|
|
|
#define DECLARE_OLD_POOL(name, type, block_size_bits, max_blocks) \
|
|
|
|
OLD_POOL_ENUM(name, type, block_size_bits, max_blocks) \
|
2007-08-01 22:10:54 +00:00
|
|
|
extern OldMemoryPool<type> _##name##_pool; \
|
2006-12-03 17:27:43 +00:00
|
|
|
OLD_POOL_ACCESSORS(name, type)
|
2006-10-28 10:54:20 +00:00
|
|
|
|
|
|
|
|
2006-12-03 17:27:43 +00:00
|
|
|
#define DEFINE_OLD_POOL(name, type, new_block_proc, clean_block_proc) \
|
2007-08-01 22:10:54 +00:00
|
|
|
OldMemoryPool<type> _##name##_pool( \
|
2006-10-28 10:54:20 +00:00
|
|
|
#name, name##_POOL_MAX_BLOCKS, name##_POOL_BLOCK_SIZE_BITS, sizeof(type), \
|
2007-08-01 22:10:54 +00:00
|
|
|
new_block_proc, clean_block_proc);
|
2006-10-28 10:54:20 +00:00
|
|
|
|
2007-08-01 23:49:06 +00:00
|
|
|
#define DEFINE_OLD_POOL_GENERIC(name, type) \
|
|
|
|
OldMemoryPool<type> _##name##_pool( \
|
|
|
|
#name, name##_POOL_MAX_BLOCKS, name##_POOL_BLOCK_SIZE_BITS, sizeof(type), \
|
2008-04-06 23:49:45 +00:00
|
|
|
PoolNewBlock<type, &_##name##_pool>, PoolCleanBlock<type, &_##name##_pool>); \
|
2009-01-09 15:01:15 +00:00
|
|
|
template type *PoolItem<type, type##ID, &_##name##_pool>::AllocateSafeRaw(uint &first); \
|
|
|
|
template bool PoolItem<type, type##ID, &_##name##_pool>::CanAllocateItem(uint count);
|
2007-08-01 23:49:06 +00:00
|
|
|
|
2006-10-28 10:54:20 +00:00
|
|
|
|
2006-12-03 17:27:43 +00:00
|
|
|
#define STATIC_OLD_POOL(name, type, block_size_bits, max_blocks, new_block_proc, clean_block_proc) \
|
|
|
|
OLD_POOL_ENUM(name, type, block_size_bits, max_blocks) \
|
|
|
|
static DEFINE_OLD_POOL(name, type, new_block_proc, clean_block_proc) \
|
|
|
|
OLD_POOL_ACCESSORS(name, type)
|
2006-10-28 10:54:20 +00:00
|
|
|
|
2006-12-03 20:03:40 +00:00
|
|
|
#endif /* OLDPOOL_H */
|