47 void init(PxU32 maxElements, PxU32 elementSize,
const char* typeName)
53 mMemory =
reinterpret_cast<PxU8*
>(PX_ALLOC(
sizeof(PxU8)*elementSize*maxElements, typeName?typeName:
"SceneSim Pool"));
54 PX_ASSERT(elementSize*maxElements>=
sizeof(
void*));
62 PX_FORCE_INLINE PxU8* allocateMemory(PxU32 maxElements, PxU32 elementSize)
66 PxU8* recycled =
reinterpret_cast<PxU8*
>(mFirstFree);
68 void** recycled32 =
reinterpret_cast<void**
>(recycled);
69 mFirstFree = *recycled32;
75 if(mNbElements==maxElements)
78 const PxU32 freeIndex = mNbElements++;
79 return mMemory + freeIndex * elementSize;
83 void deallocateMemory(PxU32 maxElements, PxU32 elementSize, PxU8* element)
86 PX_ASSERT(element>=mMemory && element<mMemory + maxElements * elementSize);
87 PX_UNUSED(elementSize);
88 PX_UNUSED(maxElements);
90 void** recycled32 =
reinterpret_cast<void**
>(element);
91 *recycled32 = mFirstFree;
98 return mMemory < p.mMemory;
103 return mMemory > p.mMemory;
117 : mMaxElements (maxElements)
118 , mElementSize (elementSize)
119 , mActivePoolIndex (0)
120 , mPools (
"MyPoolManagerPools")
121 , mNeedsSorting (
true)
122 , mTypeName (typeName)
125 tmp.init(maxElements, elementSize, mTypeName);
126 mPools.pushBack(tmp);
131 const PxU32 nbPools = mPools.size();
132 for(PxU32 i=0;i<nbPools;i++)
136 void preAllocate(PxU32 n)
141 const PxU32 nbPools = mPools.size();
142 const PxU32 maxElements = mMaxElements;
143 const PxU32 elementSize = mElementSize;
144 PxU32 availableSpace = nbPools * maxElements;
146 while(n>availableSpace)
149 tmp.init(maxElements, elementSize, mTypeName);
150 mPools.pushBack(tmp);
152 availableSpace += maxElements;
158 PX_ASSERT(mActivePoolIndex<mPools.size());
159 PxU8* memory = mPools[mActivePoolIndex].allocateMemory(mMaxElements, mElementSize);
160 return memory ? memory : searchForMemory();
163 void deallocateMemory(PxU8* element)
169 PxSort(mPools.begin(), mPools.size());
171 const PxU32 maxElements = mMaxElements;
172 const PxU32 elementSize = mElementSize;
173 const PxU32 slabSize = maxElements * elementSize;
174 const PxU32 nbPools = mPools.size();
178 int last = int(nbPools-1);
182 const int mid = (first+last)>>1;
185 if(contains(candidate.mMemory, slabSize, element))
187 candidate.deallocateMemory(maxElements, elementSize, element);
191 mActivePoolIndex = PxU32(mid);
193 mNeedsSorting =
false;
197 if(candidate.mMemory<element)
210 PxU8* searchForMemory()
212 const PxU32 nbPools = mPools.size();
213 const PxU32 activePoolIndex = mActivePoolIndex;
214 const PxU32 maxElements = mMaxElements;
215 const PxU32 elementSize = mElementSize;
218 for(PxU32 i=0;i<nbPools;i++)
220 if(i==activePoolIndex)
223 PxU8* memory = mPools[i].allocateMemory(maxElements, elementSize);
226 mActivePoolIndex = i;
231 mActivePoolIndex = nbPools;
232 mNeedsSorting =
true;
235 tmp.init(maxElements, elementSize, mTypeName);
238 return newPool.allocateMemory(maxElements, elementSize);
243 PX_FORCE_INLINE bool contains(PxU8* memory,
const PxU32 slabSize, PxU8* element)
245 return element>=memory && element<memory+slabSize;
250 const PxU32 mMaxElements;
251 const PxU32 mElementSize;
252 PxU32 mActivePoolIndex;
256 const char* mTypeName;
265 PreallocatingPool(PxU32 maxElements,
const char* typeName) : mPool(maxElements,
sizeof(T), typeName)
275 mPool.preAllocate(n);
280 return reinterpret_cast<T*
>(mPool.allocateMemory());
285 T* t =
reinterpret_cast<T*
>(mPool.allocateMemory());
292 T* t =
reinterpret_cast<T*
>(mPool.allocateMemory());
293 return t ? PX_PLACEMENT_NEW(t, T()) : NULL;
299 T* t =
reinterpret_cast<T*
>(mPool.allocateMemory());
300 return t ? PX_PLACEMENT_NEW(t, T(a)) : NULL;
303 template<
class A1,
class A2>
306 T* t =
reinterpret_cast<T*
>(mPool.allocateMemory());
307 return t ? PX_PLACEMENT_NEW(t, T(a,b)) : NULL;
310 template<
class A1,
class A2,
class A3>
311 PX_INLINE T* construct(A1& a, A2& b, A3& c)
313 T* t =
reinterpret_cast<T*
>(mPool.allocateMemory());
314 return t ? PX_PLACEMENT_NEW(t, T(a,b,c)) : NULL;
317 template<
class A1,
class A2,
class A3,
class A4>
318 PX_INLINE T* construct(A1& a, A2& b, A3& c, A4& d)
320 T* t =
reinterpret_cast<T*
>(mPool.allocateMemory());
321 return t ? PX_PLACEMENT_NEW(t, T(a,b,c,d)) : NULL;
324 template<
class A1,
class A2,
class A3,
class A4,
class A5>
325 PX_INLINE T* construct(A1& a, A2& b, A3& c, A4& d, A5& e)
327 T* t =
reinterpret_cast<T*
>(mPool.allocateMemory());
328 return t ? PX_PLACEMENT_NEW(t, T(a,b,c,d,e)) : NULL;
336 return PX_PLACEMENT_NEW(t, T());
343 return PX_PLACEMENT_NEW(t, T(a));
346 template<
class A1,
class A2>
347 PX_INLINE T* construct(T* t, A1& a, A2& b)
350 return PX_PLACEMENT_NEW(t, T(a,b));
353 template<
class A1,
class A2,
class A3>
354 PX_INLINE T* construct(T* t, A1& a, A2& b, A3& c)
357 return PX_PLACEMENT_NEW(t, T(a,b,c));
360 template<
class A1,
class A2,
class A3,
class A4>
361 PX_INLINE T* construct(T* t, A1& a, A2& b, A3& c, A4& d)
364 return PX_PLACEMENT_NEW(t, T(a,b,c,d));
367 template<
class A1,
class A2,
class A3,
class A4,
class A5>
368 PX_INLINE T* construct(T* t, A1& a, A2& b, A3& c, A4& d, A5& e)
371 return PX_PLACEMENT_NEW(t, T(a,b,c,d,e));
379 mPool.deallocateMemory(
reinterpret_cast<PxU8*
>(p));
383 PX_INLINE void releasePreallocated(T*
const p)
386 mPool.deallocateMemory(
reinterpret_cast<PxU8*
>(p));