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 PXPVDSDK_PXPROFILEALLOCATORWRAPPER_H |
32 | #define PXPVDSDK_PXPROFILEALLOCATORWRAPPER_H |
33 | |
34 | #include "foundation/PxPreprocessor.h" |
35 | #include "foundation/PxAllocatorCallback.h" |
36 | #include "foundation/PxErrorCallback.h" |
37 | #include "foundation/PxAssert.h" |
38 | |
39 | #include "PsArray.h" |
40 | #include "PsHashMap.h" |
41 | |
42 | namespace physx { namespace profile { |
43 | |
44 | /** |
45 | \brief Helper struct to encapsulate the user allocator callback |
46 | Useful for array and hash templates |
47 | */ |
48 | struct PxProfileAllocatorWrapper |
49 | { |
50 | PxAllocatorCallback* mUserAllocator; |
51 | |
52 | PxProfileAllocatorWrapper( PxAllocatorCallback& inUserAllocator ) |
53 | : mUserAllocator( &inUserAllocator ) |
54 | { |
55 | } |
56 | |
57 | PxProfileAllocatorWrapper( PxAllocatorCallback* inUserAllocator ) |
58 | : mUserAllocator( inUserAllocator ) |
59 | { |
60 | } |
61 | |
62 | PxAllocatorCallback& getAllocator() const |
63 | { |
64 | PX_ASSERT( NULL != mUserAllocator ); |
65 | return *mUserAllocator; |
66 | } |
67 | }; |
68 | |
69 | /** |
70 | \brief Helper class to encapsulate the reflection allocator |
71 | */ |
72 | template <typename T> |
73 | class PxProfileWrapperReflectionAllocator |
74 | { |
75 | static const char* getName() |
76 | { |
77 | #if PX_LINUX || PX_ANDROID || PX_PS4 || PX_IOS || PX_OSX || PX_EMSCRIPTEN || PX_SWITCH |
78 | return __PRETTY_FUNCTION__; |
79 | #else |
80 | return typeid(T).name(); |
81 | #endif |
82 | } |
83 | PxProfileAllocatorWrapper* mWrapper; |
84 | |
85 | public: |
86 | PxProfileWrapperReflectionAllocator(PxProfileAllocatorWrapper& inWrapper) : mWrapper( &inWrapper ) {} |
87 | PxProfileWrapperReflectionAllocator( const PxProfileWrapperReflectionAllocator& inOther ) |
88 | : mWrapper( inOther.mWrapper ) |
89 | { |
90 | } |
91 | PxProfileWrapperReflectionAllocator& operator=( const PxProfileWrapperReflectionAllocator& inOther ) |
92 | { |
93 | mWrapper = inOther.mWrapper; |
94 | return *this; |
95 | } |
96 | PxAllocatorCallback& getAllocator() { return mWrapper->getAllocator(); } |
97 | void* allocate(size_t size, const char* filename, int line) |
98 | { |
99 | #if PX_CHECKED // checked and debug builds |
100 | if(!size) |
101 | return 0; |
102 | return getAllocator().allocate(size, getName(), filename, line); |
103 | #else |
104 | return getAllocator().allocate(size, "<no allocation names in this config>" , filename, line); |
105 | #endif |
106 | } |
107 | void deallocate(void* ptr) |
108 | { |
109 | if(ptr) |
110 | getAllocator().deallocate(ptr); |
111 | } |
112 | }; |
113 | |
114 | /** |
115 | \brief Helper class to encapsulate the named allocator |
116 | */ |
117 | struct PxProfileWrapperNamedAllocator |
118 | { |
119 | PxProfileAllocatorWrapper* mWrapper; |
120 | const char* mAllocationName; |
121 | PxProfileWrapperNamedAllocator(PxProfileAllocatorWrapper& inWrapper, const char* inAllocationName) |
122 | : mWrapper( &inWrapper ) |
123 | , mAllocationName( inAllocationName ) |
124 | {} |
125 | PxProfileWrapperNamedAllocator( const PxProfileWrapperNamedAllocator& inOther ) |
126 | : mWrapper( inOther.mWrapper ) |
127 | , mAllocationName( inOther.mAllocationName ) |
128 | { |
129 | } |
130 | PxProfileWrapperNamedAllocator& operator=( const PxProfileWrapperNamedAllocator& inOther ) |
131 | { |
132 | mWrapper = inOther.mWrapper; |
133 | mAllocationName = inOther.mAllocationName; |
134 | return *this; |
135 | } |
136 | PxAllocatorCallback& getAllocator() { return mWrapper->getAllocator(); } |
137 | void* allocate(size_t size, const char* filename, int line) |
138 | { |
139 | if(!size) |
140 | return 0; |
141 | return getAllocator().allocate(size, typeName: mAllocationName, filename, line); |
142 | } |
143 | void deallocate(void* ptr) |
144 | { |
145 | if(ptr) |
146 | getAllocator().deallocate(ptr); |
147 | } |
148 | }; |
149 | |
150 | /** |
151 | \brief Helper struct to encapsulate the array |
152 | */ |
153 | template<class T> |
154 | struct PxProfileArray : public shdfnd::Array<T, PxProfileWrapperReflectionAllocator<T> > |
155 | { |
156 | typedef PxProfileWrapperReflectionAllocator<T> TAllocatorType; |
157 | |
158 | PxProfileArray( PxProfileAllocatorWrapper& inWrapper ) |
159 | : shdfnd::Array<T, TAllocatorType >( TAllocatorType( inWrapper ) ) |
160 | { |
161 | } |
162 | |
163 | PxProfileArray( const PxProfileArray< T >& inOther ) |
164 | : shdfnd::Array<T, TAllocatorType >( inOther, inOther ) |
165 | { |
166 | } |
167 | }; |
168 | |
169 | /** |
170 | \brief Helper struct to encapsulate the array |
171 | */ |
172 | template<typename TKeyType, typename TValueType, typename THashType=shdfnd::Hash<TKeyType> > |
173 | struct PxProfileHashMap : public shdfnd::HashMap<TKeyType, TValueType, THashType, PxProfileWrapperReflectionAllocator< TValueType > > |
174 | { |
175 | typedef shdfnd::HashMap<TKeyType, TValueType, THashType, PxProfileWrapperReflectionAllocator< TValueType > > THashMapType; |
176 | typedef PxProfileWrapperReflectionAllocator<TValueType> TAllocatorType; |
177 | PxProfileHashMap( PxProfileAllocatorWrapper& inWrapper ) |
178 | : THashMapType( TAllocatorType( inWrapper ) ) |
179 | { |
180 | } |
181 | }; |
182 | |
183 | /** |
184 | \brief Helper function to encapsulate the profile allocation |
185 | */ |
186 | template<typename TDataType> |
187 | inline TDataType* PxProfileAllocate( PxAllocatorCallback* inAllocator, const char* file, int inLine ) |
188 | { |
189 | PxProfileAllocatorWrapper wrapper( inAllocator ); |
190 | typedef PxProfileWrapperReflectionAllocator< TDataType > TAllocator; |
191 | TAllocator theAllocator( wrapper ); |
192 | return reinterpret_cast<TDataType*>( theAllocator.allocate( sizeof( TDataType ), file, inLine ) ); |
193 | } |
194 | |
195 | /** |
196 | \brief Helper function to encapsulate the profile allocation |
197 | */ |
198 | template<typename TDataType> |
199 | inline TDataType* PxProfileAllocate( PxAllocatorCallback& inAllocator, const char* file, int inLine ) |
200 | { |
201 | return PxProfileAllocate<TDataType>( &inAllocator, file, inLine ); |
202 | } |
203 | |
204 | /** |
205 | \brief Helper function to encapsulate the profile deallocation |
206 | */ |
207 | template<typename TDataType> |
208 | inline void PxProfileDeleteAndDeallocate( PxProfileAllocatorWrapper& inAllocator, TDataType* inDType ) |
209 | { |
210 | PX_ASSERT(inDType); |
211 | PxAllocatorCallback& allocator( inAllocator.getAllocator() ); |
212 | inDType->~TDataType(); |
213 | allocator.deallocate( ptr: inDType ); |
214 | } |
215 | |
216 | /** |
217 | \brief Helper function to encapsulate the profile deallocation |
218 | */ |
219 | template<typename TDataType> |
220 | inline void PxProfileDeleteAndDeallocate( PxAllocatorCallback& inAllocator, TDataType* inDType ) |
221 | { |
222 | PxProfileAllocatorWrapper wrapper( &inAllocator ); |
223 | PxProfileDeleteAndDeallocate( wrapper, inDType ); |
224 | } |
225 | |
226 | } } |
227 | |
228 | #define PX_PROFILE_NEW( allocator, dtype ) new (physx::profile::PxProfileAllocate<dtype>( allocator, __FILE__, __LINE__ )) dtype |
229 | #define PX_PROFILE_DELETE( allocator, obj ) physx::profile::PxProfileDeleteAndDeallocate( allocator, obj ); |
230 | |
231 | #endif // PXPVDSDK_PXPROFILEALLOCATORWRAPPER_H |
232 | |