1 | // |
2 | // Redistribution and use in source and binary forms, with or without |
3 | // modification, are permitted provided that the following conditions |
4 | // are met: |
5 | // * Redistributions of source code must retain the above copyright |
6 | // notice, this list of conditions and the following disclaimer. |
7 | // * Redistributions in binary form must reproduce the above copyright |
8 | // notice, this list of conditions and the following disclaimer in the |
9 | // documentation and/or other materials provided with the distribution. |
10 | // * Neither the name of NVIDIA CORPORATION nor the names of its |
11 | // contributors may be used to endorse or promote products derived |
12 | // from this software without specific prior written permission. |
13 | // |
14 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY |
15 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
16 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
17 | // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
18 | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
19 | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
20 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
21 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
22 | // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
23 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
24 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
25 | // |
26 | // Copyright (c) 2008-2021 NVIDIA Corporation. All rights reserved. |
27 | // Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved. |
28 | // Copyright (c) 2001-2004 NovodeX AG. All rights reserved. |
29 | |
30 | |
31 | #ifndef PX_PHYSICS_COMMON_ID_POOL |
32 | #define PX_PHYSICS_COMMON_ID_POOL |
33 | |
34 | #include "foundation/Px.h" |
35 | #include "CmPhysXCommon.h" |
36 | #include "PsArray.h" |
37 | #include "PsUserAllocated.h" |
38 | |
39 | namespace physx |
40 | { |
41 | namespace Cm |
42 | { |
43 | template<class FreeBuffer> |
44 | class IDPoolBase : public Ps::UserAllocated |
45 | { |
46 | protected: |
47 | PxU32 mCurrentID; |
48 | FreeBuffer mFreeIDs; |
49 | public: |
50 | IDPoolBase() : mCurrentID(0) {} |
51 | |
52 | void freeID(PxU32 id) |
53 | { |
54 | // Allocate on first call |
55 | // Add released ID to the array of free IDs |
56 | if(id == (mCurrentID - 1)) |
57 | --mCurrentID; |
58 | else |
59 | mFreeIDs.pushBack(id); |
60 | } |
61 | |
62 | void freeAll() |
63 | { |
64 | mCurrentID = 0; |
65 | mFreeIDs.clear(); |
66 | } |
67 | |
68 | PxU32 getNewID() |
69 | { |
70 | // If recycled IDs are available, use them |
71 | const PxU32 size = mFreeIDs.size(); |
72 | if(size) |
73 | { |
74 | // Recycle last ID |
75 | return mFreeIDs.popBack(); |
76 | } |
77 | // Else create a new ID |
78 | return mCurrentID++; |
79 | } |
80 | |
81 | PxU32 getNumUsedID() const |
82 | { |
83 | return mCurrentID - mFreeIDs.size(); |
84 | } |
85 | |
86 | PxU32 getMaxID() const |
87 | { |
88 | return mCurrentID; |
89 | } |
90 | |
91 | }; |
92 | |
93 | //This class extends IDPoolBase. This is mainly used for when it is unsafe for the application to free the id immediately so that it can |
94 | //defer the free process until it is safe to do so |
95 | template<class FreeBuffer> |
96 | class DeferredIDPoolBase : public IDPoolBase<FreeBuffer> |
97 | { |
98 | FreeBuffer mDeferredFreeIDs; |
99 | public: |
100 | //release an index into the deferred list |
101 | void deferredFreeID(PxU32 id) |
102 | { |
103 | mDeferredFreeIDs.pushBack(id); |
104 | } |
105 | |
106 | //release the deferred indices into the free list |
107 | void processDeferredIds() |
108 | { |
109 | const PxU32 deferredFreeIDCount = mDeferredFreeIDs.size(); |
110 | for(PxU32 a = 0; a < deferredFreeIDCount;++a) |
111 | { |
112 | IDPoolBase<FreeBuffer>::freeID(mDeferredFreeIDs[a]); |
113 | } |
114 | mDeferredFreeIDs.clear(); |
115 | } |
116 | |
117 | //release all indices |
118 | void freeAll() |
119 | { |
120 | mDeferredFreeIDs.clear(); |
121 | IDPoolBase<FreeBuffer>::freeAll(); |
122 | } |
123 | |
124 | PxU32 getNumUsedID() const |
125 | { |
126 | return IDPoolBase<FreeBuffer>::getNumUsedID() - mDeferredFreeIDs.size(); |
127 | } |
128 | |
129 | FreeBuffer& getDeferredFreeIDs() { return mDeferredFreeIDs; } |
130 | }; |
131 | |
132 | //This is spu friendly fixed size array |
133 | template <typename T, uint32_t N> |
134 | class InlineFixedArray |
135 | { |
136 | T mArr[N]; |
137 | PxU32 mSize; |
138 | public: |
139 | |
140 | InlineFixedArray() : mSize(0) |
141 | { |
142 | } |
143 | |
144 | ~InlineFixedArray(){} |
145 | |
146 | void pushBack(const T& t) |
147 | { |
148 | PX_ASSERT(mSize < N); |
149 | mArr[mSize++] = t; |
150 | } |
151 | |
152 | T popBack() |
153 | { |
154 | PX_ASSERT(mSize > 0); |
155 | return mArr[--mSize]; |
156 | } |
157 | |
158 | void clear() { mSize = 0; } |
159 | |
160 | T& operator [] (PxU32 index) { PX_ASSERT(index < N); return mArr[index]; } |
161 | |
162 | const T& operator [] (PxU32 index) const { PX_ASSERT(index < N); return mArr[index]; } |
163 | |
164 | PxU32 size() const { return mSize; } |
165 | }; |
166 | |
167 | //Fix size IDPool |
168 | template<PxU32 Capacity> |
169 | class InlineIDPool : public IDPoolBase<InlineFixedArray<PxU32, Capacity> > |
170 | { |
171 | public: |
172 | PxU32 getNumRemainingIDs() |
173 | { |
174 | return Capacity - this->getNumUsedID(); |
175 | } |
176 | }; |
177 | |
178 | //Dynamic resize IDPool |
179 | class IDPool : public IDPoolBase<Ps::Array<PxU32> > |
180 | { |
181 | }; |
182 | |
183 | |
184 | //This class is used to recycle indices. It supports deferred release, so that until processDeferredIds is called, |
185 | //released indices will not be reallocated. This class will fail if the calling code request more id than the InlineDeferredIDPoll |
186 | //has. It is the calling code's responsibility to ensure that this does not happen. |
187 | template<PxU32 Capacity> |
188 | class InlineDeferredIDPool : public DeferredIDPoolBase<InlineFixedArray<PxU32, Capacity> > |
189 | { |
190 | public: |
191 | PxU32 getNumRemainingIDs() |
192 | { |
193 | return Capacity - IDPoolBase< InlineFixedArray<PxU32, Capacity> >::getNumUsedID(); |
194 | } |
195 | }; |
196 | |
197 | //Dynamic resize DeferredIDPool |
198 | class DeferredIDPool : public DeferredIDPoolBase<Ps::Array<PxU32> > |
199 | { |
200 | |
201 | }; |
202 | |
203 | } // namespace Cm |
204 | |
205 | } |
206 | |
207 | #endif |
208 | |