RavEngine
Loading...
Searching...
No Matches
PxAllocator.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_ALLOCATOR_H
30#define PX_ALLOCATOR_H
31
32#include "foundation/PxAllocatorCallback.h"
33#include "foundation/PxAssert.h"
34#include "foundation/PxFoundation.h"
35#include "foundation/Px.h"
36
37#if PX_VC
38#pragma warning(push)
39#pragma warning(disable : 4577)
40#endif
41
42#if PX_WINDOWS_FAMILY
43 #include <exception>
44#if(_MSC_VER >= 1923)
45 #include <typeinfo>
46#else
47 #include <typeinfo.h>
48#endif
49#endif
50#if(PX_APPLE_FAMILY)
51 #include <typeinfo>
52#endif
53
54#include <new>
55
56#if PX_VC
57#pragma warning(pop)
58#endif
59
60
61// PT: the rules are simple:
62// - PX_ALLOC/PX_ALLOCATE/PX_FREE is similar to malloc/free. Use that for POD/anything that doesn't need ctor/dtor.
63// - PX_NEW/PX_DELETE is similar to new/delete. Use that for anything that needs a ctor/dtor.
64// - Everything goes through the user allocator.
65// - Inherit from PxUserAllocated to PX_NEW something. Do it even on small classes, it's free.
66// - You cannot PX_NEW a POD. Use PX_ALLOC.
67
68#define PX_ALLOC(n, name) physx::PxAllocator().allocate(n, __FILE__, __LINE__)
69
70// PT: use this one to reduce the amount of visible reinterpret_cast
71#define PX_ALLOCATE(type, count, name) reinterpret_cast<type*>(PX_ALLOC(count*sizeof(type), name))
72
73#define PX_FREE(x) \
74 if(x) \
75 { \
76 physx::PxAllocator().deallocate(x); \
77 x = NULL; \
78 }
79
80#define PX_FREE_THIS physx::PxAllocator().deallocate(this)
81
82#define PX_NEW(T) new (physx::PxReflectionAllocator<T>(), __FILE__, __LINE__) T
83#define PX_PLACEMENT_NEW(p, T) new (p) T
84#define PX_DELETE_THIS delete this
85#define PX_DELETE(x) if(x) { delete x; x = NULL; }
86#define PX_DELETE_ARRAY(x) if(x) { delete []x; x = NULL; }
87#define PX_RELEASE(x) if(x) { x->release(); x = NULL; }
88
89#if !PX_DOXYGEN
90namespace physx
91{
92#endif
97 {
98 public:
99 PX_FORCE_INLINE PxAllocator(const char* = NULL){}
100
101 PX_FORCE_INLINE void* allocate(size_t size, const char* file, int line)
102 {
103 return size ? PxGetBroadcastAllocator()->allocate(size, "", file, line) : NULL;
104 }
105
106 PX_FORCE_INLINE void deallocate(void* ptr)
107 {
108 if(ptr)
110 }
111 };
112
113 /*
114 * Bootstrap allocator using malloc/free.
115 * Don't use unless your objects get allocated before foundation is initialized.
116 */
118 {
119 public:
120 PxRawAllocator(const char* = 0) {}
121
122 PX_FORCE_INLINE void* allocate(size_t size, const char*, int)
123 {
124 // malloc returns valid pointer for size==0, no need to check
125 return ::malloc(size);
126 }
127
128 PX_FORCE_INLINE void deallocate(void* ptr)
129 {
130 // free(0) is guaranteed to have no side effect, no need to check
131 ::free(ptr);
132 }
133 };
134
135 /*
136 \brief Virtual allocator callback used to provide run-time defined allocators to foundation types like Array or Bitmap.
137 This is used by VirtualAllocator
138 */
140 {
141 public:
143 virtual ~PxVirtualAllocatorCallback() {}
144
145 virtual void* allocate(const size_t size, const int group, const char* file, const int line) = 0;
146 virtual void deallocate(void* ptr) = 0;
147 };
148
149 /*
150 \brief Virtual allocator to be used by foundation types to provide run-time defined allocators.
151 Due to the fact that Array extends its allocator, rather than contains a reference/pointer to it, the VirtualAllocator
152 must
153 be a concrete type containing a pointer to a virtual callback. The callback may not be available at instantiation time,
154 therefore
155 methods are provided to set the callback later.
156 */
158 {
159 public:
160 PxVirtualAllocator(PxVirtualAllocatorCallback* callback = NULL, const int group = 0) : mCallback(callback), mGroup(group) {}
161
162 PX_FORCE_INLINE void* allocate(const size_t size, const char* file, const int line)
163 {
164 PX_ASSERT(mCallback);
165 if (size)
166 return mCallback->allocate(size, mGroup, file, line);
167 return NULL;
168 }
169
170 PX_FORCE_INLINE void deallocate(void* ptr)
171 {
172 PX_ASSERT(mCallback);
173 if (ptr)
174 mCallback->deallocate(ptr);
175 }
176
177 private:
179 const int mGroup;
180 PxVirtualAllocator& operator=(const PxVirtualAllocator&);
181 };
182
186 template <typename T>
188 {
189 static const char* getName()
190 {
191 if (!PxGetFoundation().getReportAllocationNames())
192 return "<allocation names disabled>";
193#if PX_GCC_FAMILY
194 return __PRETTY_FUNCTION__;
195#else
196 // name() calls malloc(), raw_name() wouldn't
197 return typeid(T).name();
198#endif
199 }
200
201 public:
203 PxReflectionAllocator(const char* = 0) {}
204
206
207 PX_FORCE_INLINE void* allocate(size_t size, const char* filename, int line)
208 {
209 return size ? PxGetBroadcastAllocator()->allocate(size, getName(), filename, line) : NULL;
210 }
211
212 PX_FORCE_INLINE void deallocate(void* ptr)
213 {
214 if (ptr)
216 }
217 };
218
219 template <typename T>
221 {
223 };
224
225#if !PX_DOXYGEN
226} // namespace physx
227#endif
228
229#endif
230
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 PxAllocator.h:97
Definition PxAllocator.h:118
Definition PxAllocator.h:188
Definition PxAllocator.h:140
Definition PxAllocator.h:158
#define PX_FORCE_INLINE
Definition PxPreprocessor.h:335
PX_C_EXPORT PX_FOUNDATION_API physx::PxFoundation &PX_CALL_CONV PxGetFoundation()
Retrieves the Foundation SDK after it has been created.
Definition FdFoundation.cpp:279
PX_C_EXPORT PX_FOUNDATION_API physx::PxAllocatorCallback *PX_CALL_CONV PxGetBroadcastAllocator()
Get the broadcasting allocator callback.
Definition FdFoundation.cpp:264
Sorts an array of objects in ascending order, assuming that the predicate implements the < operator:
Definition PxBoxController.h:39
PxEMPTY
Definition Px.h:87
Definition PxAllocator.h:221