aboutsummaryrefslogtreecommitdiff
path: root/PxShared/src/foundation/include/PsBroadcast.h
blob: 86ff171df17f9ad404a39cf827c32de7bad96c64 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//  * Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
//  * Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
//  * Neither the name of NVIDIA CORPORATION nor the names of its
//    contributors may be used to endorse or promote products derived
//    from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Copyright (c) 2008-2018 NVIDIA Corporation. All rights reserved.
// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.

#ifndef PXPVDSDK_PXBROADCAST_H
#define PXPVDSDK_PXBROADCAST_H

#include "Ps.h"
#include "PsInlineArray.h"

#include "foundation/PxSimpleTypes.h"
#include "foundation/PxErrorCallback.h"

namespace physx
{
namespace shdfnd
{

/**
\brief Abstract listener class that listens to allocation and deallocation events from the
    foundation memory system.

<b>Threading:</b> All methods of this class should be thread safe as it can be called from the user thread
or the physics processing thread(s).
*/
class AllocationListener
{
  public:
	/**
	\brief callback when memory is allocated.
	\param size Size of the allocation in bytes.
	\param typeName Type this data is being allocated for.
	\param filename File the allocation came from.
	\param line the allocation came from.
	\param allocatedMemory memory that will be returned from the allocation.
	*/
	virtual void onAllocation(size_t size, const char* typeName, const char* filename, int line,
	                          void* allocatedMemory) = 0;

	/**
	\brief callback when memory is deallocated.
	\param allocatedMemory memory just before allocation.
	*/
	virtual void onDeallocation(void* allocatedMemory) = 0;

  protected:
	virtual ~AllocationListener()
	{
	}
};

/**
\brief Broadcast class implementation, registering listeners.

<b>Threading:</b> All methods of this class should be thread safe as it can be called from the user thread
or the physics processing thread(s). There is not internal locking
*/
template <class Listener, class Base>
class Broadcast : public Base
{
  public:
	static const uint32_t MAX_NB_LISTENERS = 16;

	/**
	\brief The default constructor.
	*/
	Broadcast()
	{
	}

	/**
	\brief Register new listener.

	\note It is NOT SAFE to register and deregister listeners while allocations may be taking place.
	moreover, there is no thread safety to registration/deregistration.

	\param listener Listener to register.
	*/
	void registerListener(Listener& listener)
	{
		if(mListeners.size() < MAX_NB_LISTENERS)
			mListeners.pushBack(&listener);
	}

	/**
	\brief Deregister an existing listener.

	\note It is NOT SAFE to register and deregister listeners while allocations may be taking place.
	moreover, there is no thread safety to registration/deregistration.

	\param listener Listener to deregister.
	*/
	void deregisterListener(Listener& listener)
	{
		mListeners.findAndReplaceWithLast(&listener);
	}

	/**
	\brief Get number of registered listeners.

	\return Number of listeners.
	*/
	uint32_t getNbListeners() const
	{
		return mListeners.size();
	}

	/**
	\brief Get an existing listener from given index.

	\param index Index of the listener.
	\return Listener on given index.
	*/
	Listener& getListener(uint32_t index)
	{
		PX_ASSERT(index <= mListeners.size());
		return *mListeners[index];
	}

  protected:
	virtual ~Broadcast()
	{
	}

	physx::shdfnd::InlineArray<Listener*, MAX_NB_LISTENERS, physx::shdfnd::NonTrackingAllocator> mListeners;
};

/**
\brief Abstract base class for an application defined memory allocator that allows an external listener
to audit the memory allocations.
*/
class BroadcastingAllocator : public Broadcast<AllocationListener, PxAllocatorCallback>
{
	PX_NOCOPY(BroadcastingAllocator)

  public:
	/**
	\brief The default constructor.
	*/
	BroadcastingAllocator(PxAllocatorCallback& allocator, PxErrorCallback& error) : mAllocator(allocator), mError(error)
	{
		mListeners.clear();
	}

	/**
	\brief The default constructor.
	*/
	virtual ~BroadcastingAllocator()
	{
		mListeners.clear();
	}

	/**
	\brief Allocates size bytes of memory, which must be 16-byte aligned.

	This method should never return NULL.  If you run out of memory, then
	you should terminate the app or take some other appropriate action.

	<b>Threading:</b> This function should be thread safe as it can be called in the context of the user thread
	and physics processing thread(s).

	\param size			Number of bytes to allocate.
	\param typeName		Name of the datatype that is being allocated
	\param filename		The source file which allocated the memory
	\param line			The source line which allocated the memory
	\return				The allocated block of memory.
	*/
	void* allocate(size_t size, const char* typeName, const char* filename, int line)
	{
		void* mem = mAllocator.allocate(size, typeName, filename, line);

		if(!mem)
		{
			mError.reportError(PxErrorCode::eABORT, "User allocator returned NULL.", __FILE__, __LINE__);
			return NULL;
		}

		if((reinterpret_cast<size_t>(mem) & 15))
		{
			mError.reportError(PxErrorCode::eABORT, "Allocations must be 16-byte aligned.", __FILE__, __LINE__);
			return NULL;
		}

		for(uint32_t i = 0; i < mListeners.size(); i++)
			mListeners[i]->onAllocation(size, typeName, filename, line, mem);

		return mem;
	}

	/**
	\brief Frees memory previously allocated by allocate().

	<b>Threading:</b> This function should be thread safe as it can be called in the context of the user thread
	and physics processing thread(s).

	\param ptr Memory to free.
	*/
	void deallocate(void* ptr)
	{
		for(uint32_t i = 0; i < mListeners.size(); i++)
		{
			mListeners[i]->onDeallocation(ptr);
		}
		mAllocator.deallocate(ptr);
	}

  private:
	PxAllocatorCallback& mAllocator;
	PxErrorCallback& mError;
};

/**
\brief Abstract base class for an application defined error callback that allows an external listener
to report errors.
*/
class BroadcastingErrorCallback : public Broadcast<PxErrorCallback, PxErrorCallback>
{
	PX_NOCOPY(BroadcastingErrorCallback)
  public:
	/**
	\brief The default constructor.
	*/
	BroadcastingErrorCallback(PxErrorCallback& errorCallback)
	{
		registerListener(errorCallback);
	}

	/**
	\brief The default destructor.
	*/
	virtual ~BroadcastingErrorCallback()
	{
		mListeners.clear();
	}

	/**
	\brief Reports an error code.
	\param code Error code, see #PxErrorCode
	\param message Message to display.
	\param file File error occured in.
	\param line Line number error occured on.
	*/
	void reportError(PxErrorCode::Enum code, const char* message, const char* file, int line)
	{
		for(uint32_t i = 0; i < mListeners.size(); i++)
			mListeners[i]->reportError(code, message, file, line);
	}
};
}
} // namespace physx

#endif // PXPVDSDK_PXBROADCAST_H