RavEngine
Loading...
Searching...
No Matches
PxBroadcast.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_BROADCAST_H
30#define PX_BROADCAST_H
31
32#include "foundation/PxInlineArray.h"
33#include "foundation/PxSimpleTypes.h"
34#include "foundation/PxErrorCallback.h"
35
36#if !PX_DOXYGEN
37namespace physx
38{
39#endif
40
49{
50 public:
59 virtual void onAllocation(size_t size, const char* typeName, const char* filename, int line,
60 void* allocatedMemory) = 0;
61
66 virtual void onDeallocation(void* allocatedMemory) = 0;
67
68 protected:
69 virtual ~PxAllocationListener()
70 {
71 }
72};
73
80template <class Listener, class Base>
81class PxBroadcast : public Base
82{
83 public:
84 static const uint32_t MAX_NB_LISTENERS = 16;
85
90 {
91 }
92
101 void registerListener(Listener& listener)
102 {
103 if(mListeners.size() < MAX_NB_LISTENERS)
104 mListeners.pushBack(&listener);
105 }
106
115 void deregisterListener(Listener& listener)
116 {
117 mListeners.findAndReplaceWithLast(&listener);
118 }
119
125 uint32_t getNbListeners() const
126 {
127 return mListeners.size();
128 }
129
136 Listener& getListener(uint32_t index)
137 {
138 PX_ASSERT(index <= mListeners.size());
139 return *mListeners[index];
140 }
141
142 protected:
143 virtual ~PxBroadcast()
144 {
145 }
146
148};
149
154class PxBroadcastingAllocator : public PxBroadcast<PxAllocationListener, PxAllocatorCallback>
155{
156 PX_NOCOPY(PxBroadcastingAllocator)
157
158 public:
162 PxBroadcastingAllocator(PxAllocatorCallback& allocator, PxErrorCallback& error) : mAllocator(allocator), mError(error)
163 {
164 mListeners.clear();
165 }
166
171 {
172 mListeners.clear();
173 }
174
190 void* allocate(size_t size, const char* typeName, const char* filename, int line)
191 {
192 void* mem = mAllocator.allocate(size, typeName, filename, line);
193
194 if(!mem)
195 {
196 mError.reportError(PxErrorCode::eABORT, "User allocator returned NULL.", __FILE__, __LINE__);
197 return NULL;
198 }
199
200 if((size_t(mem) & 15))
201 {
202 mError.reportError(PxErrorCode::eABORT, "Allocations must be 16-byte aligned.", __FILE__, __LINE__);
203 return NULL;
204 }
205
206 for(uint32_t i = 0; i < mListeners.size(); i++)
207 mListeners[i]->onAllocation(size, typeName, filename, line, mem);
208
209 return mem;
210 }
211
220 void deallocate(void* ptr)
221 {
222 for(uint32_t i = 0; i < mListeners.size(); i++)
223 {
224 mListeners[i]->onDeallocation(ptr);
225 }
226 mAllocator.deallocate(ptr);
227 }
228
229 private:
230 PxAllocatorCallback& mAllocator;
231 PxErrorCallback& mError;
232};
233
238class PxBroadcastingErrorCallback : public PxBroadcast<PxErrorCallback, PxErrorCallback>
239{
241 public:
246 {
247 registerListener(errorCallback);
248 }
249
254 {
255 mListeners.clear();
256 }
257
265 void reportError(PxErrorCode::Enum code, const char* message, const char* file, int line)
266 {
267 for(uint32_t i = 0; i < mListeners.size(); i++)
268 mListeners[i]->reportError(code, message, file, line);
269 }
270};
271#if !PX_DOXYGEN
272} // namespace physx
273#endif
274
275#endif
276
Abstract listener class that listens to allocation and deallocation events from the foundation memory...
Definition PxBroadcast.h:49
virtual void onDeallocation(void *allocatedMemory)=0
callback when memory is deallocated.
virtual void onAllocation(size_t size, const char *typeName, const char *filename, int line, void *allocatedMemory)=0
callback when memory is allocated.
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.
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
PX_INLINE bool findAndReplaceWithLast(const T &a)
Definition PxArray.h:368
Broadcast class implementation, registering listeners.
Definition PxBroadcast.h:82
uint32_t getNbListeners() const
Get number of registered listeners.
Definition PxBroadcast.h:125
void registerListener(Listener &listener)
Register new listener.
Definition PxBroadcast.h:101
Listener & getListener(uint32_t index)
Get an existing listener from given index.
Definition PxBroadcast.h:136
PxBroadcast()
The default constructor.
Definition PxBroadcast.h:89
void deregisterListener(Listener &listener)
Deregister an existing listener.
Definition PxBroadcast.h:115
Abstract base class for an application defined memory allocator that allows an external listener to a...
Definition PxBroadcast.h:155
void * allocate(size_t size, const char *typeName, const char *filename, int line)
Allocates size bytes of memory, which must be 16-byte aligned.
Definition PxBroadcast.h:190
void deallocate(void *ptr)
Frees memory previously allocated by allocate().
Definition PxBroadcast.h:220
virtual ~PxBroadcastingAllocator()
The default constructor.
Definition PxBroadcast.h:170
PxBroadcastingAllocator(PxAllocatorCallback &allocator, PxErrorCallback &error)
The default constructor.
Definition PxBroadcast.h:162
Abstract base class for an application defined error callback that allows an external listener to rep...
Definition PxBroadcast.h:239
void reportError(PxErrorCode::Enum code, const char *message, const char *file, int line)
Reports an error code.
Definition PxBroadcast.h:265
PxBroadcastingErrorCallback(PxErrorCallback &errorCallback)
The default constructor.
Definition PxBroadcast.h:245
virtual ~PxBroadcastingErrorCallback()
The default destructor.
Definition PxBroadcast.h:253
User defined interface class. Used by the library to emit debug information.
Definition PxErrorCallback.h:51
virtual void reportError(PxErrorCode::Enum code, const char *message, const char *file, int line)=0
Reports an error code.
Definition PxInlineArray.h:43
Sorts an array of objects in ascending order, assuming that the predicate implements the < operator:
Definition PxBoxController.h:39
Enum
Definition PxErrors.h:52
@ eABORT
An unrecoverable error, execution should be halted and log output flushed.
Definition PxErrors.h:76