RavEngine
Loading...
Searching...
No Matches
PxProfileAllocatorWrapper.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_PROFILE_ALLOCATOR_WRAPPER_H
30#define PX_PROFILE_ALLOCATOR_WRAPPER_H
31
32#include "foundation/PxPreprocessor.h"
33#include "foundation/PxAllocatorCallback.h"
34#include "foundation/PxErrorCallback.h"
35#include "foundation/PxAssert.h"
36#include "foundation/PxHashMap.h"
37#include "foundation/PxArray.h"
38
39namespace physx { namespace profile {
40
46 {
47 PxAllocatorCallback* mUserAllocator;
48
50 : mUserAllocator( &inUserAllocator )
51 {
52 }
53
55 : mUserAllocator( inUserAllocator )
56 {
57 }
58
59 PxAllocatorCallback& getAllocator() const
60 {
61 PX_ASSERT( NULL != mUserAllocator );
62 return *mUserAllocator;
63 }
64 };
65
69 template <typename T>
71 {
72 static const char* getName()
73 {
74#if PX_LINUX || PX_OSX || PX_EMSCRIPTEN || PX_SWITCH
75 return __PRETTY_FUNCTION__;
76#else
77 return typeid(T).name();
78#endif
79 }
81
82 public:
83 PxProfileWrapperReflectionAllocator(PxProfileAllocatorWrapper& inWrapper) : mWrapper( &inWrapper ) {}
85 : mWrapper( inOther.mWrapper )
86 {
87 }
89 {
90 mWrapper = inOther.mWrapper;
91 return *this;
92 }
93 PxAllocatorCallback& getAllocator() { return mWrapper->getAllocator(); }
94 void* allocate(size_t size, const char* filename, int line)
95 {
96#if PX_CHECKED // checked and debug builds
97 if(!size)
98 return 0;
99 return getAllocator().allocate(size, getName(), filename, line);
100#else
101 return getAllocator().allocate(size, "<no allocation names in this config>", filename, line);
102#endif
103 }
104 void deallocate(void* ptr)
105 {
106 if(ptr)
107 getAllocator().deallocate(ptr);
108 }
109 };
110
115 {
117 const char* mAllocationName;
118 PxProfileWrapperNamedAllocator(PxProfileAllocatorWrapper& inWrapper, const char* inAllocationName)
119 : mWrapper( &inWrapper )
120 , mAllocationName( inAllocationName )
121 {}
123 : mWrapper( inOther.mWrapper )
124 , mAllocationName( inOther.mAllocationName )
125 {
126 }
128 {
129 mWrapper = inOther.mWrapper;
130 mAllocationName = inOther.mAllocationName;
131 return *this;
132 }
133 PxAllocatorCallback& getAllocator() { return mWrapper->getAllocator(); }
134 void* allocate(size_t size, const char* filename, int line)
135 {
136 if(!size)
137 return 0;
138 return getAllocator().allocate(size, mAllocationName, filename, line);
139 }
140 void deallocate(void* ptr)
141 {
142 if(ptr)
143 getAllocator().deallocate(ptr);
144 }
145 };
146
150 template<class T>
151 struct PxProfileArray : public PxArray<T, PxProfileWrapperReflectionAllocator<T> >
152 {
154
157 {
158 }
159
160 PxProfileArray( const PxProfileArray< T >& inOther )
161 : PxArray<T, TAllocatorType >( inOther, inOther )
162 {
163 }
164 };
165
169 template<typename TKeyType, typename TValueType, typename THashType=PxHash<TKeyType> >
170 struct PxProfileHashMap : public PxHashMap<TKeyType, TValueType, THashType, PxProfileWrapperReflectionAllocator< TValueType > >
171 {
175 : THashMapType( TAllocatorType( inWrapper ) )
176 {
177 }
178 };
179
183 template<typename TDataType>
184 inline TDataType* PxProfileAllocate( PxAllocatorCallback* inAllocator, const char* file, int inLine )
185 {
186 PxProfileAllocatorWrapper wrapper( inAllocator );
188 TAllocator theAllocator( wrapper );
189 return reinterpret_cast<TDataType*>( theAllocator.allocate( sizeof( TDataType ), file, inLine ) );
190 }
191
195 template<typename TDataType>
196 inline TDataType* PxProfileAllocate( PxAllocatorCallback& inAllocator, const char* file, int inLine )
197 {
198 return PxProfileAllocate<TDataType>( &inAllocator, file, inLine );
199 }
200
204 template<typename TDataType>
205 inline void PxProfileDeleteAndDeallocate( PxProfileAllocatorWrapper& inAllocator, TDataType* inDType )
206 {
207 PX_ASSERT(inDType);
208 PxAllocatorCallback& allocator( inAllocator.getAllocator() );
209 inDType->~TDataType();
210 allocator.deallocate( inDType );
211 }
212
216 template<typename TDataType>
217 inline void PxProfileDeleteAndDeallocate( PxAllocatorCallback& inAllocator, TDataType* inDType )
218 {
219 PxProfileAllocatorWrapper wrapper( &inAllocator );
220 PxProfileDeleteAndDeallocate( wrapper, inDType );
221 }
222
223} }
224
225#define PX_PROFILE_NEW( allocator, dtype ) new (physx::profile::PxProfileAllocate<dtype>( allocator, __FILE__, __LINE__ )) dtype
226#define PX_PROFILE_DELETE( allocator, obj ) physx::profile::PxProfileDeleteAndDeallocate( allocator, obj );
227
228#endif
229
Abstract base class for an application defined memory allocator that can be used by the Nv library.
Definition PxAllocatorCallback.h:53
virtual void deallocate(void *ptr)=0
Frees memory previously allocated by allocate().
virtual void * allocate(size_t size, const char *typeName, const char *filename, int line)=0
Allocates size bytes of memory, which must be 16-byte aligned.
Definition PxArray.h:53
Definition PxHashMap.h:78
Helper class to encapsulate the reflection allocator.
Definition PxProfileAllocatorWrapper.h:71
Sorts an array of objects in ascending order, assuming that the predicate implements the < operator:
Definition PxBoxController.h:39
Helper struct to encapsulate the user allocator callback Useful for array and hash templates.
Definition PxProfileAllocatorWrapper.h:46
Helper struct to encapsulate the array.
Definition PxProfileAllocatorWrapper.h:152
Helper struct to encapsulate the array.
Definition PxProfileAllocatorWrapper.h:171
Helper class to encapsulate the named allocator.
Definition PxProfileAllocatorWrapper.h:115