RavEngine
Loading...
Searching...
No Matches
CmIDPool.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 CM_ID_POOL_H
30#define CM_ID_POOL_H
31
32#include "foundation/Px.h"
33#include "foundation/PxArray.h"
34#include "foundation/PxUserAllocated.h"
35
36namespace physx
37{
38namespace Cm
39{
40 template<class FreeBuffer>
42 {
43 protected:
44 PxU32 mCurrentID;
45 FreeBuffer mFreeIDs;
46 public:
47 IDPoolBase() : mCurrentID(0) {}
48
49 void freeID(PxU32 id)
50 {
51 // Allocate on first call
52 // Add released ID to the array of free IDs
53 if(id == (mCurrentID - 1))
54 --mCurrentID;
55 else
56 mFreeIDs.pushBack(id);
57 }
58
59 void freeAll()
60 {
61 mCurrentID = 0;
62 mFreeIDs.clear();
63 }
64
65 PxU32 getNewID()
66 {
67 // If recycled IDs are available, use them
68 const PxU32 size = mFreeIDs.size();
69 if(size)
70 {
71 // Recycle last ID
72 return mFreeIDs.popBack();
73 }
74 // Else create a new ID
75 return mCurrentID++;
76 }
77
78 PxU32 getNumUsedID() const
79 {
80 return mCurrentID - mFreeIDs.size();
81 }
82
83 PxU32 getMaxID() const
84 {
85 return mCurrentID;
86 }
87
88 };
89
90 //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
91 //defer the free process until it is safe to do so
92 template<class FreeBuffer>
93 class DeferredIDPoolBase : public IDPoolBase<FreeBuffer>
94 {
95 FreeBuffer mDeferredFreeIDs;
96 public:
97 //release an index into the deferred list
98 void deferredFreeID(PxU32 id)
99 {
100 mDeferredFreeIDs.pushBack(id);
101 }
102
103 //release the deferred indices into the free list
104 void processDeferredIds()
105 {
106 const PxU32 deferredFreeIDCount = mDeferredFreeIDs.size();
107 for(PxU32 a = 0; a < deferredFreeIDCount;++a)
108 {
109 IDPoolBase<FreeBuffer>::freeID(mDeferredFreeIDs[a]);
110 }
111 mDeferredFreeIDs.clear();
112 }
113
114 //release all indices
115 void freeAll()
116 {
117 mDeferredFreeIDs.clear();
119 }
120
121 PxU32 getNumUsedID() const
122 {
123 return IDPoolBase<FreeBuffer>::getNumUsedID() - mDeferredFreeIDs.size();
124 }
125
126 FreeBuffer& getDeferredFreeIDs() { return mDeferredFreeIDs; }
127 };
128
129 //This is spu friendly fixed size array
130 template <typename T, uint32_t N>
132 {
133 T mArr[N];
134 PxU32 mSize;
135 public:
136
137 InlineFixedArray() : mSize(0)
138 {
139 }
140
142
143 void pushBack(const T& t)
144 {
145 PX_ASSERT(mSize < N);
146 mArr[mSize++] = t;
147 }
148
149 T popBack()
150 {
151 PX_ASSERT(mSize > 0);
152 return mArr[--mSize];
153 }
154
155 void clear() { mSize = 0; }
156
157 T& operator [] (PxU32 index) { PX_ASSERT(index < N); return mArr[index]; }
158
159 const T& operator [] (PxU32 index) const { PX_ASSERT(index < N); return mArr[index]; }
160
161 PxU32 size() const { return mSize; }
162 };
163
164 //Fix size IDPool
165 template<PxU32 Capacity>
166 class InlineIDPool : public IDPoolBase<InlineFixedArray<PxU32, Capacity> >
167 {
168 public:
169 PxU32 getNumRemainingIDs()
170 {
171 return Capacity - this->getNumUsedID();
172 }
173 };
174
175 //Dynamic resize IDPool
176 class IDPool : public IDPoolBase<PxArray<PxU32> >
177 {
178 };
179
180
181 //This class is used to recycle indices. It supports deferred release, so that until processDeferredIds is called,
182 //released indices will not be reallocated. This class will fail if the calling code request more id than the InlineDeferredIDPoll
183 //has. It is the calling code's responsibility to ensure that this does not happen.
184 template<PxU32 Capacity>
185 class InlineDeferredIDPool : public DeferredIDPoolBase<InlineFixedArray<PxU32, Capacity> >
186 {
187 public:
188 PxU32 getNumRemainingIDs()
189 {
190 return Capacity - IDPoolBase< InlineFixedArray<PxU32, Capacity> >::getNumUsedID();
191 }
192 };
193
194 //Dynamic resize DeferredIDPool
195 class DeferredIDPool : public DeferredIDPoolBase<PxArray<PxU32> >
196 {
197
198 };
199
200} // namespace Cm
201
202}
203
204#endif
Definition CmIDPool.h:94
Definition CmIDPool.h:196
Definition CmIDPool.h:42
Definition CmIDPool.h:177
Definition CmIDPool.h:186
Definition CmIDPool.h:132
Definition CmIDPool.h:167
Definition PxUserAllocated.h:43
Sorts an array of objects in ascending order, assuming that the predicate implements the < operator:
Definition PxBoxController.h:39