1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
|
/* $Id$ */
/** @file oldpool.h Base for the old pool. */
#ifndef OLDPOOL_H
#define OLDPOOL_H
#include "core/math_func.hpp"
/* The function that is called after a new block is added
start_item is the first item of the new made block */
typedef void OldMemoryPoolNewBlock(uint start_item);
/* The function that is called before a block is cleaned up */
typedef void OldMemoryPoolCleanBlock(uint start_item, uint end_item);
/**
* Stuff for dynamic vehicles. Use the wrappers to access the OldMemoryPool
* please try to avoid manual calls!
*/
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) :
name(name), max_blocks(max_blocks), block_size_bits(block_size_bits),
new_block_proc(new_block_proc), clean_block_proc(clean_block_proc), current_blocks(0),
total_items(0), cleaning_pool(false), item_size(item_size), first_free_index(0), blocks(NULL) {}
const char *name; ///< Name of the pool (just for debugging)
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
/** Pointer to a function that is called after a new block is added */
OldMemoryPoolNewBlock *new_block_proc;
/** Pointer to a function that is called to clean a block */
OldMemoryPoolCleanBlock *clean_block_proc;
uint current_blocks; ///< How many blocks we have in our pool
uint total_items; ///< How many items we now have in this pool
bool cleaning_pool; ///< Are we currently cleaning the pool?
public:
const uint item_size; ///< How many bytes one block is
uint first_free_index; ///< The index of the first free pool item in this pool
byte **blocks; ///< An array of blocks (one block hold all the items)
/**
* 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);
}
/**
* 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
{
return this->total_items;
}
/**
* 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
{
return this->current_blocks < this->max_blocks;
}
/**
* Get the maximum number of allocable blocks.
* @return the numebr of blocks
*/
inline uint GetBlockCount() const
{
return this->current_blocks;
}
/**
* Get the name of this pool.
* @return the name
*/
inline const char *GetName() const
{
return this->name;
}
/**
* Is the pool in the cleaning phase?
* @return true if it is
*/
inline bool CleaningPool() const
{
return this->cleaning_pool;
}
};
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) {}
/**
* 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
{
assert(index < this->GetSize());
return (T*)(this->blocks[index >> this->block_size_bits] +
(index & ((1 << this->block_size_bits) - 1)) * this->item_size);
}
};
/**
* 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) {
t = new (t) T();
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);
delete t;
}
}
/**
* 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;
}
};
/**
* 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.
* @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.
*/
virtual ~PoolItem()
{
}
/**
* 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()
{
}
/**
* An overriden version of new that allocates memory on the pool.
* @param size the size of the variable (unused)
* @pre CanAllocateItem()
* @return the memory that is 'allocated'
*/
void *operator new(size_t size)
{
return AllocateRaw();
}
/**
* 'Free' the memory allocated by the overriden new.
* @param p the memory to 'free'
* @note we only update Tpool->first_free_index
*/
void operator delete(void *p)
{
Tpool->UpdateFirstFreeIndex(((T*)p)->index);
}
/**
* 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'
*/
void *operator new(size_t size, int index)
{
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
* @note we only update Tpool->first_free_index
*/
void operator delete(void *p, int index)
{
Tpool->UpdateFirstFreeIndex(index);
}
/**
* 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.
* @note we only update Tpool->first_free_index
*/
void operator delete(void *p, T *pn)
{
Tpool->UpdateFirstFreeIndex(pn->index);
}
/**
* Get item with given index
* @param index item to get
*/
static FORCEINLINE T *Get(uint index)
{
return Tpool->Get(index);
}
/**
* Get item with given index
* @param index item to get
* @return NULL for invalid items
*/
static FORCEINLINE T *GetIfValid(uint index)
{
if (index >= Tpool->GetSize()) return NULL;
T *item = Tpool->Get(index);
return item->IsValid() ? item : NULL;
}
/**
* Returns size of the pool (in number of items)
* @return size of the pool
*/
static FORCEINLINE uint GetPoolSize()
{
return Tpool->GetSize();
}
/**
* Tests if given ID belongs to valid pool item
* @return is given ID valid?
*/
static FORCEINLINE bool IsValidID(uint index)
{
return index < Tpool->GetSize() && Tpool->Get(index)->IsValid();
}
private:
static T *AllocateSafeRaw(uint &first);
protected:
/**
* Allocate a pool item; possibly allocate a new block in the pool.
* @pre CanAllocateItem()
* @return the allocated pool item.
*/
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
* @pre CanAllocateItem()
* @return the allocated pool item.
*/
static inline T *AllocateRaw(uint &first)
{
if (first >= Tpool->GetSize() && !Tpool->AddBlockToPool()) return NULL;
return AllocateSafeRaw(first);
}
/**
* Are we cleaning this pool?
* @return true if we are
*/
static inline bool CleaningPool()
{
return Tpool->CleaningPool();
}
public:
static bool CanAllocateItem(uint count = 1);
};
#define OLD_POOL_ENUM(name, type, block_size_bits, max_blocks) \
enum { \
name##_POOL_BLOCK_SIZE_BITS = block_size_bits, \
name##_POOL_MAX_BLOCKS = max_blocks \
};
#define DECLARE_OLD_POOL(name, type, block_size_bits, max_blocks) \
OLD_POOL_ENUM(name, type, block_size_bits, max_blocks) \
extern OldMemoryPool<type> _##name##_pool;
#define DEFINE_OLD_POOL_GENERIC(name, type) \
OldMemoryPool<type> _##name##_pool( \
#name, name##_POOL_MAX_BLOCKS, name##_POOL_BLOCK_SIZE_BITS, sizeof(type), \
PoolNewBlock<type, &_##name##_pool>, PoolCleanBlock<type, &_##name##_pool>); \
template type *PoolItem<type, type##ID, &_##name##_pool>::AllocateSafeRaw(uint &first); \
template bool PoolItem<type, type##ID, &_##name##_pool>::CanAllocateItem(uint count);
#endif /* OLDPOOL_H */
|