2
-----------------------------------------------------------------------------
3
This source file is part of OGRE
4
(Object-oriented Graphics Rendering Engine)
5
For the latest info, see http://www.ogre3d.org/
7
Copyright (c) 2000-2006 The OGRE Team
8
Also see acknowledgements in Readme.html
10
This program is free software; you can redistribute it and/or modify it under
11
the terms of the GNU Lesser General Public License as published by the Free Software
12
Foundation; either version 2 of the License, or (at your option) any later
15
This program is distributed in the hope that it will be useful, but WITHOUT
16
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
19
You should have received a copy of the GNU Lesser General Public License along with
20
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21
Place - Suite 330, Boston, MA 02111-1307, USA, or go to
22
http://www.gnu.org/copyleft/lesser.txt.
23
-----------------------------------------------------------------------------
26
#ifndef __OgrePagingLandScapePoolSet_H__
27
#define __OgrePagingLandScapePoolSet_H__
35
/** A collection of pre-allocated object instance of a same base class
37
template <class T> class PoolSet
41
typedef std::list<T*> ActivePoolableList;
42
typedef std::list<T*> FreePoolableList;
43
typedef std::vector<T*> PoolablePool;
49
poolSize The initial size of the object pool. Estimate of the number of objects
50
which will be required, and pass it using this parameter. The set will
51
preallocate this number to avoid memory fragmentation. The default behaviour
52
once this pool has run out is to double it.
54
PoolSet::setAutoextend
56
PoolSet(unsigned int poolSize = 0, bool autoExtendPool = true):
57
mAutoExtendPool(autoExtendPool),
58
mAutoExtendFactor(2.0f),
61
assert (mFreePoolables.empty());
62
assert (mActivePoolables.empty());
63
assert (mPoolablePool.empty());
65
setPoolSize(poolSize);
72
assert (mPoolSize == 0 && mPoolablePool.empty());
75
empty totally the pool.
80
for (typename PoolablePool::iterator i = mPoolablePool.begin();
81
i != mPoolablePool.end(); ++i)
85
mPoolablePool.clear();
86
mFreePoolables.clear();
87
mActivePoolables.clear();
90
/** Creates a new pooled object and adds it to this set.
92
Behavior once the query pool has been exhausted depends on the
93
PoolSet::setAutoextendPool option.
95
On success, a pointer to a newly created Poolable is
98
On failure (i.e. no more space and can't autoextend),
99
<b>NULL</b> is returned.
101
PoolSet::setAutoextend
105
if (mFreePoolables.empty())
117
T* newPoolable = mFreePoolables.front();
118
mFreePoolables.pop_front();
119
mActivePoolables.push_back(newPoolable);
124
Extend the pool Size by a mAutoExtendFactor factor (default 2.0f)
128
unsigned int newSize = (unsigned int)(mPoolSize * mAutoExtendFactor);
129
if (newSize <= mPoolSize)
130
newSize = mPoolSize + 1;
131
setPoolSize (newSize);
134
Extend the pool Size by this factor (default is 2.0f)
136
void setExtendFactor(const float factor)
138
assert(factor >= 1.0f);// otherwise you'll make it smaller
139
mAutoExtendFactor = factor;
141
/** Removes a pooled object from the set.
143
This version is more efficient than removing by index.
145
void removePoolable(T * const pPoolable)
147
mActivePoolables.remove (pPoolable);
148
mFreePoolables.push_back (pPoolable);
150
/** Returns the number of active object which currently make up this set.
152
size_t getActivePoolablesSize(void) const
154
return static_cast< size_t >(mActivePoolables.size());
156
ActivePoolableList &getActivePoolables(void)
158
return mActivePoolables;
160
/** Tells the set whether to allow automatic extension of the pool of objects.
162
A PoolSet stores a pool of pre-constructed queries which are used as needed when
163
a new object is requested. This allows applications to create / remove objects efficiently
164
without incurring construction / destruction costs. This method allows you to configure
165
the behavior when a new object is requested but the object pool has been exhausted.
167
The default behavior is to allow the pool to extend (typically this allocates double the current
168
pool of queries when the pool is expended), equivalent to calling this method with
169
autoExtend = true. If you set the parameter to false however, any attempt to create a new pooled object
170
when the pool has expired will simply fail silently, returning a null pointer.
171
@param autoextend true to double the pool every time it runs out, false to fail silently.
173
void setAutoextend(bool autoextend)
175
mAutoExtendPool = autoextend;
177
/** Returns true if the object pool automatically extends.
179
PoolSet::setAutoextend
181
bool getAutoextend(void) const
183
return mAutoExtendPool;
185
/** Adjusts the size of the pool of object available in this set.
187
See the PoolSet::setAutoextend method for full details of the query pool. This method adjusts
188
the preallocated size of the pool. If you try to reduce the size of the pool, the set has the option
189
of ignoring you if too many objects are already in use. Bear in mind that calling this method will
190
incur significant construction / destruction calls so should be avoided in time-critical code. The same
191
goes for auto-extension, try to avoid it by estimating the pool size correctly up-front.
193
size The new size for the pool.
195
void setPoolSize(const unsigned int size)
197
// Never shrink below size()
198
const size_t currSize = mPoolablePool.size();
203
for (size_t i = currSize; i < size; ++i)
205
// Add new items to the queue
206
mFreePoolables.push_back (mPoolablePool[i]);
211
/** Returns the current size of the object pool.
213
The current size of the object pool.
215
PoolSet::setAutoextend
217
unsigned int getPoolSize(void) const
219
return static_cast< unsigned int >(mPoolablePool.size());
221
/** Empties this set of all Actives object (but do not delete them).
225
// Insert actives into free list
226
mFreePoolables.insert(mFreePoolables.end(), mActivePoolables.begin(), mActivePoolables.end());
228
// Remove all active instances
229
mActivePoolables.clear();
233
/** Active object list.
235
This is a linked list of pointers to objects in the object pool.
237
This allows very fast insertions and deletions from anywhere in the list to activate / deactivate objects
238
(required for particle systems etc) as well as reuse of Poolable instances in the pool
239
without construction & destruction which avoids memory thrashing.
241
ActivePoolableList mActivePoolables;
242
/** Free object queue.
244
This contains a list of the objects free for use as new instances
245
as required by the set. Poolable instances are pre-constructed up to the estimated size in the
246
mPoolablePool vector and are referenced on this deque at startup. As they get used this deque
247
reduces, as they get released back to to the set they get added back to the deque.
249
FreePoolableList mFreePoolables;
250
/** Pool of objects instances for use and reuse in the active query list.
252
This vector will be preallocated with the estimated size of the set,and will extend as required.
254
PoolablePool mPoolablePool;
255
/// The number of query in the pool.
256
unsigned int mPoolSize;
257
/// Flag indicating whether to auto extend pool
258
bool mAutoExtendPool;
259
// by how much we will auto extend current pool
260
Real mAutoExtendFactor;
262
/// method for increasing pool size
263
void increasePool(unsigned int size)
266
const size_t oldSize = mPoolablePool.size();
269
mPoolablePool.reserve(size);
270
mPoolablePool.resize(size);
272
// Create new queries
273
for (size_t i = oldSize; i < size; ++i)
274
mPoolablePool[i] = allocate();
277
virtual T* allocate() = 0;
278
virtual void deallocate(T* p) = 0;
284
#endif//__OgrePagingLandScapePoolSet_H__