RavEngine
Loading...
Searching...
No Matches
SnippetImmUtils.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
27#include "PxPhysXConfig.h"
28#include "foundation/PxArray.h"
29#include "foundation/PxUserAllocated.h"
30#include "collision/PxCollisionDefs.h"
31#include "solver/PxSolverDefs.h"
32
33namespace physx
34{
35namespace SnippetImmUtils
36{
37
39 {
40 struct AllocationPage : PxUserAllocated
41 {
42 static const PxU32 PageSize = 32 * 1024;
43 PxU8 mPage[PageSize];
44
45 PxU32 currentIndex;
46
47 AllocationPage() : currentIndex(0){}
48
49 PxU8* allocate(const PxU32 size)
50 {
51 PxU32 alignedSize = (size + 15)&(~15);
52 if ((currentIndex + alignedSize) < PageSize)
53 {
54 PxU8* ret = &mPage[currentIndex];
55 currentIndex += alignedSize;
56 return ret;
57 }
58 return NULL;
59 }
60 };
61
62 AllocationPage* currentPage;
63
64 PxArray<AllocationPage*> mAllocatedBlocks;
65 PxU32 mCurrentIndex;
66
67 public:
68 BlockBasedAllocator() : currentPage(NULL), mCurrentIndex(0)
69 {
70 }
71
72 virtual PxU8* allocate(const PxU32 byteSize)
73 {
74 if (currentPage)
75 {
76 PxU8* data = currentPage->allocate(byteSize);
77 if (data)
78 return data;
79 }
80
81 if (mCurrentIndex < mAllocatedBlocks.size())
82 {
83 currentPage = mAllocatedBlocks[mCurrentIndex++];
84 currentPage->currentIndex = 0;
85 return currentPage->allocate(byteSize);
86 }
87 currentPage = PX_NEW(AllocationPage);
88 mAllocatedBlocks.pushBack(currentPage);
89 mCurrentIndex = mAllocatedBlocks.size();
90
91 return currentPage->allocate(byteSize);
92 }
93
94 void release()
95 {
96 for (PxU32 a = 0; a < mAllocatedBlocks.size(); ++a)
97 PX_DELETE(mAllocatedBlocks[a]);
98 mAllocatedBlocks.clear();
99 currentPage = NULL;
100 mCurrentIndex = 0;
101 }
102
103 void reset()
104 {
105 currentPage = NULL;
106 mCurrentIndex = 0;
107 }
108
109 virtual ~BlockBasedAllocator()
110 {
111 release();
112 }
113 };
114
116 {
117 BlockBasedAllocator mAllocator[2];
118 PxU32 currIdx;
119
120 public:
121
122 TestCacheAllocator() : currIdx(0)
123 {
124 }
125
126 virtual PxU8* allocateCacheData(const PxU32 byteSize)
127 {
128 return mAllocator[currIdx].allocate(byteSize);
129 }
130
131 void release() { currIdx = 1 - currIdx; mAllocator[currIdx].release(); }
132
133 void reset() { currIdx = 1 - currIdx; mAllocator[currIdx].reset(); }
134
135 virtual ~TestCacheAllocator(){}
136 };
137
139 {
140 BlockBasedAllocator mConstraintAllocator;
141 BlockBasedAllocator mFrictionAllocator[2];
142
143 PxU32 currIdx;
144 public:
145
146 TestConstraintAllocator() : currIdx(0)
147 {
148 }
149 virtual PxU8* reserveConstraintData(const PxU32 byteSize){ return mConstraintAllocator.allocate(byteSize); }
150 virtual PxU8* reserveFrictionData(const PxU32 byteSize){ return mFrictionAllocator[currIdx].allocate(byteSize); }
151
152 void release() { currIdx = 1 - currIdx; mConstraintAllocator.release(); mFrictionAllocator[currIdx].release(); }
153
154 virtual ~TestConstraintAllocator() {}
155 };
156
157}
158}
Definition PxArray.h:53
PX_FORCE_INLINE uint32_t size() const
Definition PxArray.h:242
PX_INLINE void clear()
Definition PxArray.h:250
PX_FORCE_INLINE T & pushBack(const T &a)
Definition PxArray.h:296
A callback class to allocate memory to cache information used in contact generation.
Definition PxCollisionDefs.h:44
Definition PxSolverDefs.h:227
Definition PxUserAllocated.h:43
Definition SnippetImmUtils.h:39
Definition SnippetImmUtils.h:116
virtual PxU8 * allocateCacheData(const PxU32 byteSize)
Allocates cache data for contact generation. This data is stored inside PxCache objects....
Definition SnippetImmUtils.h:126
Definition SnippetImmUtils.h:139
virtual PxU8 * reserveConstraintData(const PxU32 byteSize)
Allocates constraint data. It is the application's responsibility to release this memory after PxSolv...
Definition SnippetImmUtils.h:149
virtual PxU8 * reserveFrictionData(const PxU32 byteSize)
Allocates friction data. Friction data can be retained by the application for a given pair and provid...
Definition SnippetImmUtils.h:150
Sorts an array of objects in ascending order, assuming that the predicate implements the < operator:
Definition PxBoxController.h:39