RavEngine
Loading...
Searching...
No Matches
PxPool.h
1// Redistribution and use in source and binary forms, with or without
2// modification, are permitted provided that the following conditions
3// are met:
4// * Redistributions of source code must retain the above copyright
5// notice, this list of conditions and the following disclaimer.
6// * Redistributions in binary form must reproduce the above copyright
7// notice, this list of conditions and the following disclaimer in the
8// documentation and/or other materials provided with the distribution.
9// * Neither the name of NVIDIA CORPORATION nor the names of its
10// contributors may be used to endorse or promote products derived
11// from this software without specific prior written permission.
12//
13// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
14// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24//
25// Copyright (c) 2008-2022 NVIDIA Corporation. All rights reserved.
26// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
27// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
28
29#ifndef PX_POOL_H
30#define PX_POOL_H
31
32#include "foundation/PxArray.h"
33#include "foundation/PxSort.h"
34#include "foundation/PxBasicTemplates.h"
35#include "foundation/PxInlineArray.h"
36#include "foundation/PxMemory.h"
37
38namespace physx
39{
40
44template <class T, class Alloc = typename PxAllocatorTraits<T>::Type>
45class PxPoolBase : public PxUserAllocated, public Alloc
46{
47 PX_NOCOPY(PxPoolBase)
48 protected:
49 PxPoolBase(const Alloc& alloc, uint32_t elementsPerSlab, uint32_t slabSize)
50 : Alloc(alloc), mSlabs(alloc), mElementsPerSlab(elementsPerSlab), mUsed(0), mSlabSize(slabSize), mFreeElement(0)
51 {
52 mSlabs.reserve(64);
53#if PX_CLANG
54#if PX_LINUX
55#pragma clang diagnostic push
56#pragma clang diagnostic ignored "-Wunused-local-typedef"
57#endif // PX_LINUX
58#endif // PX_CLANG
59 PX_COMPILE_TIME_ASSERT(sizeof(T) >= sizeof(size_t));
60#if PX_CLANG
61#if PX_LINUX
62#pragma clang diagnostic pop
63#endif
64#endif
65 }
66
67 public:
69 {
70 if(mUsed)
71 disposeElements();
72
73 for(void** slabIt = mSlabs.begin(), *slabEnd = mSlabs.end(); slabIt != slabEnd; ++slabIt)
74 Alloc::deallocate(*slabIt);
75 }
76
77 // Allocate space for single object
78 PX_INLINE T* allocate()
79 {
80 if(mFreeElement == 0)
81 allocateSlab();
82 T* p = reinterpret_cast<T*>(mFreeElement);
83 mFreeElement = mFreeElement->mNext;
84 mUsed++;
85
86 PxMarkSerializedMemory(p, sizeof(T));
87 return p;
88 }
89
90 // Put space for a single element back in the lists
91 PX_INLINE void deallocate(T* p)
92 {
93 if(p)
94 {
95 PX_ASSERT(mUsed);
96 mUsed--;
97 push(reinterpret_cast<FreeList*>(p));
98 }
99 }
100
101 PX_INLINE T* construct()
102 {
103 T* t = allocate();
104 return t ? PX_PLACEMENT_NEW(t, T()) : NULL;
105 }
106
107 template <class A1>
108 PX_INLINE T* construct(A1& a)
109 {
110 T* t = allocate();
111 return t ? PX_PLACEMENT_NEW(t, T(a)) : NULL;
112 }
113
114 template <class A1, class A2>
115 PX_INLINE T* construct(A1& a, A2& b)
116 {
117 T* t = allocate();
118 return t ? PX_PLACEMENT_NEW(t, T(a, b)) : NULL;
119 }
120
121 template <class A1, class A2, class A3>
122 PX_INLINE T* construct(A1& a, A2& b, A3& c)
123 {
124 T* t = allocate();
125 return t ? PX_PLACEMENT_NEW(t, T(a, b, c)) : NULL;
126 }
127
128 template <class A1, class A2, class A3>
129 PX_INLINE T* construct(A1* a, A2& b, A3& c)
130 {
131 T* t = allocate();
132 return t ? PX_PLACEMENT_NEW(t, T(a, b, c)) : NULL;
133 }
134
135 template <class A1, class A2, class A3, class A4>
136 PX_INLINE T* construct(A1& a, A2& b, A3& c, A4& d)
137 {
138 T* t = allocate();
139 return t ? PX_PLACEMENT_NEW(t, T(a, b, c, d)) : NULL;
140 }
141
142 template <class A1, class A2, class A3, class A4, class A5>
143 PX_INLINE T* construct(A1& a, A2& b, A3& c, A4& d, A5& e)
144 {
145 T* t = allocate();
146 return t ? PX_PLACEMENT_NEW(t, T(a, b, c, d, e)) : NULL;
147 }
148
149 template <class A1, class A2, class A3, class A4, class A5, class A6>
150 PX_INLINE T* construct(A1& a, A2& b, A3& c, A4& d, A5& e, A6& f)
151 {
152 T* t = allocate();
153 return t ? PX_PLACEMENT_NEW(t, T(a, b, c, d, e, f)) : NULL;
154 }
155
156 template <class A1, class A2, class A3, class A4, class A5, class A6, class A7>
157 PX_INLINE T* construct(A1& a, A2& b, A3& c, A4& d, A5& e, A6& f, A7& g)
158 {
159 T* t = allocate();
160 return t ? PX_PLACEMENT_NEW(t, T(a, b, c, d, e, f, g)) : NULL;
161 }
162
163 template <class A1, class A2, class A3, class A4, class A5, class A6, class A7, class A8>
164 PX_INLINE T* construct(A1& a, A2& b, A3& c, A4& d, A5& e, A6& f, A7& g, A8& h)
165 {
166 T* t = allocate();
167 return t ? PX_PLACEMENT_NEW(t, T(a, b, c, d, e, f, g, h)) : NULL;
168 }
169
170 PX_INLINE void destroy(T* const p)
171 {
172 if(p)
173 {
174 p->~T();
175 deallocate(p);
176 }
177 }
178
179 protected:
180 struct FreeList
181 {
182 FreeList* mNext;
183 };
184
185 // All the allocated slabs, sorted by pointer
187
188 uint32_t mElementsPerSlab;
189 uint32_t mUsed;
190 uint32_t mSlabSize;
191
192 FreeList* mFreeElement; // Head of free-list
193
194 // Helper function to get bitmap of allocated elements
195
196 void push(FreeList* p)
197 {
198 p->mNext = mFreeElement;
199 mFreeElement = p;
200 }
201
202 // Allocate a slab and segregate it into the freelist
203 void allocateSlab()
204 {
205 T* slab = reinterpret_cast<T*>(Alloc::allocate(mSlabSize, __FILE__, __LINE__));
206
207 mSlabs.pushBack(slab);
208
209 // Build a chain of nodes for the freelist
210 T* it = slab + mElementsPerSlab;
211 while(--it >= slab)
212 push(reinterpret_cast<FreeList*>(it));
213 }
214
215 /*
216 Cleanup method. Go through all active slabs and call destructor for live objects,
217 then free their memory
218 */
219 void disposeElements()
220 {
221 PxArray<void*, Alloc> freeNodes(*this);
222 while(mFreeElement)
223 {
224 freeNodes.pushBack(mFreeElement);
225 mFreeElement = mFreeElement->mNext;
226 }
227 Alloc& alloc(*this);
228 PxSort(freeNodes.begin(), freeNodes.size(), PxLess<void*>(), alloc);
229 PxSort(mSlabs.begin(), mSlabs.size(), PxLess<void*>(), alloc);
230
231 typename PxArray<void*, Alloc>::Iterator slabIt = mSlabs.begin(), slabEnd = mSlabs.end();
232 for(typename PxArray<void*, Alloc>::Iterator freeIt = freeNodes.begin(); slabIt != slabEnd; ++slabIt)
233 {
234 for(T* tIt = reinterpret_cast<T*>(*slabIt), *tEnd = tIt + mElementsPerSlab; tIt != tEnd; ++tIt)
235 {
236 if(freeIt != freeNodes.end() && *freeIt == tIt)
237 ++freeIt;
238 else
239 tIt->~T();
240 }
241 }
242 }
243};
244
245// original pool implementation
246template <class T, class Alloc = typename PxAllocatorTraits<T>::Type>
247class PxPool : public PxPoolBase<T, Alloc>
248{
249 public:
250 PxPool(const Alloc& alloc = Alloc(), uint32_t elementsPerSlab = 32)
251 : PxPoolBase<T, Alloc>(alloc, elementsPerSlab, elementsPerSlab * sizeof(T))
252 {
253 }
254};
255
256// allows specification of the slab size instead of the occupancy
257template <class T, uint32_t slabSize, class Alloc = typename PxAllocatorTraits<T>::Type>
258class PxPool2 : public PxPoolBase<T, Alloc>
259{
260 public:
261 PxPool2(const Alloc& alloc = Alloc()) : PxPoolBase<T, Alloc>(alloc, slabSize / sizeof(T), slabSize)
262 {
263 }
264};
265
266} // namespace physx
267
268#endif
269
Definition PxArray.h:53
PX_FORCE_INLINE ConstIterator end() const
Definition PxArray.h:192
PX_FORCE_INLINE uint32_t size() const
Definition PxArray.h:242
PX_FORCE_INLINE ConstIterator begin() const
Definition PxArray.h:176
PX_FORCE_INLINE T & pushBack(const T &a)
Definition PxArray.h:296
Definition PxPool.h:259
Definition PxPool.h:46
Definition PxPool.h:248
Definition PxUserAllocated.h:43
#define PX_COMPILE_TIME_ASSERT(exp)
Definition PxPreprocessor.h:428
#define PX_INLINE
Definition PxPreprocessor.h:320
Sorts an array of objects in ascending order, assuming that the predicate implements the < operator:
Definition PxBoxController.h:39
PX_INLINE void PxMarkSerializedMemory(void *ptr, PxU32 byteSize)
Definition PxMemory.h:111
Definition PxPool.h:181