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 | #ifndef PX_FOUNDATION_PSFOUNDATION_H |
31 | #define PX_FOUNDATION_PSFOUNDATION_H |
32 | |
33 | #include "foundation/PxErrors.h" |
34 | #include "foundation/PxProfiler.h" |
35 | |
36 | #include "PxFoundation.h" |
37 | |
38 | #include "PsBroadcast.h" |
39 | #include "PsAllocator.h" |
40 | #include "PsTempAllocator.h" |
41 | #include "PsMutex.h" |
42 | #include "PsHashMap.h" |
43 | #include "PsUserAllocated.h" |
44 | |
45 | #include <stdarg.h> |
46 | |
47 | namespace physx |
48 | { |
49 | namespace shdfnd |
50 | { |
51 | |
52 | #if PX_VC |
53 | #pragma warning(push) |
54 | #pragma warning(disable : 4251) // class needs to have dll-interface to be used by clients of class |
55 | #endif |
56 | |
57 | class PX_FOUNDATION_API Foundation : public PxFoundation, public UserAllocated |
58 | { |
59 | PX_NOCOPY(Foundation) |
60 | |
61 | public: |
62 | typedef MutexT<Allocator> Mutex; |
63 | |
64 | typedef HashMap<const NamedAllocator*, const char*, Hash<const NamedAllocator*>, NonTrackingAllocator> AllocNameMap; |
65 | typedef Array<TempAllocatorChunk*, Allocator> AllocFreeTable; |
66 | |
67 | public: |
68 | // factory |
69 | // note, you MUST eventually call release if createInstance returned true! |
70 | static Foundation* createInstance(PxU32 version, PxErrorCallback& errc, PxAllocatorCallback& alloc); |
71 | static Foundation& getInstance(); |
72 | static void setInstance(Foundation& foundation); |
73 | void release(); |
74 | static void incRefCount(); // this call requires a foundation object to exist already |
75 | static void decRefCount(); // this call requires a foundation object to exist already |
76 | |
77 | // Begin Errors |
78 | virtual PxErrorCallback& getErrorCallback() |
79 | { |
80 | return mErrorCallback; |
81 | } // Return the user's error callback |
82 | PxErrorCallback& getInternalErrorCallback() |
83 | { |
84 | return mBroadcastingError; |
85 | } // Return the broadcasting error callback |
86 | |
87 | void registerErrorCallback(PxErrorCallback& listener); |
88 | void deregisterErrorCallback(PxErrorCallback& listener); |
89 | |
90 | virtual void setErrorLevel(PxErrorCode::Enum mask) |
91 | { |
92 | mErrorMask = mask; |
93 | } |
94 | virtual PxErrorCode::Enum getErrorLevel() const |
95 | { |
96 | return mErrorMask; |
97 | } |
98 | |
99 | void error(PxErrorCode::Enum, const char* file, int line, const char* messageFmt, ...); // Report errors with the |
100 | // broadcasting |
101 | void errorImpl(PxErrorCode::Enum, const char* file, int line, const char* messageFmt, va_list); // error callback |
102 | static PxU32 getWarnOnceTimestamp(); |
103 | |
104 | // End errors |
105 | |
106 | // Begin Allocations |
107 | virtual PxAllocatorCallback& getAllocatorCallback() |
108 | { |
109 | return mAllocatorCallback; |
110 | } // Return the user's allocator callback |
111 | PxAllocatorCallback& getAllocator() |
112 | { |
113 | return mBroadcastingAllocator; |
114 | } // Return the broadcasting allocator |
115 | |
116 | void registerAllocationListener(physx::shdfnd::AllocationListener& listener); |
117 | void deregisterAllocationListener(physx::shdfnd::AllocationListener& listener); |
118 | |
119 | virtual bool getReportAllocationNames() const |
120 | { |
121 | return mReportAllocationNames; |
122 | } |
123 | virtual void setReportAllocationNames(bool value) |
124 | { |
125 | mReportAllocationNames = value; |
126 | } |
127 | |
128 | PX_INLINE AllocNameMap& getNamedAllocMap() |
129 | { |
130 | return mNamedAllocMap; |
131 | } |
132 | PX_INLINE Mutex& getNamedAllocMutex() |
133 | { |
134 | return mNamedAllocMutex; |
135 | } |
136 | |
137 | PX_INLINE AllocFreeTable& getTempAllocFreeTable() |
138 | { |
139 | return mTempAllocFreeTable; |
140 | } |
141 | PX_INLINE Mutex& getTempAllocMutex() |
142 | { |
143 | return mTempAllocMutex; |
144 | } |
145 | // End allocations |
146 | |
147 | private: |
148 | static void destroyInstance(); |
149 | |
150 | Foundation(PxErrorCallback& errc, PxAllocatorCallback& alloc); |
151 | ~Foundation(); |
152 | |
153 | // init order is tricky here: the mutexes require the allocator, the allocator may require the error stream |
154 | PxAllocatorCallback& mAllocatorCallback; |
155 | PxErrorCallback& mErrorCallback; |
156 | |
157 | BroadcastingAllocator mBroadcastingAllocator; |
158 | BroadcastingErrorCallback mBroadcastingError; |
159 | |
160 | bool mReportAllocationNames; |
161 | |
162 | PxErrorCode::Enum mErrorMask; |
163 | Mutex mErrorMutex; |
164 | |
165 | AllocNameMap mNamedAllocMap; |
166 | Mutex mNamedAllocMutex; |
167 | |
168 | AllocFreeTable mTempAllocFreeTable; |
169 | Mutex mTempAllocMutex; |
170 | |
171 | Mutex mListenerMutex; |
172 | |
173 | static Foundation* mInstance; |
174 | static PxU32 mRefCount; |
175 | static PxU32 mWarnOnceTimestap; |
176 | }; |
177 | #if PX_VC |
178 | #pragma warning(pop) |
179 | #endif |
180 | |
181 | PX_INLINE Foundation& getFoundation() |
182 | { |
183 | return Foundation::getInstance(); |
184 | } |
185 | |
186 | PX_INLINE void setFoundationInstance(Foundation& foundation) |
187 | { |
188 | Foundation::setInstance(foundation); |
189 | } |
190 | |
191 | } // namespace shdfnd |
192 | } // namespace physx |
193 | |
194 | // shortcut macros: |
195 | // usage: Foundation::error(PX_WARN, "static friction %f is is lower than dynamic friction %d", sfr, dfr); |
196 | #define PX_WARN ::physx::PxErrorCode::eDEBUG_WARNING, __FILE__, __LINE__ |
197 | #define PX_INFO ::physx::PxErrorCode::eDEBUG_INFO, __FILE__, __LINE__ |
198 | |
199 | #if PX_DEBUG || PX_CHECKED |
200 | #define PX_WARN_ONCE(string) \ |
201 | { \ |
202 | static PxU32 timestamp = 0; \ |
203 | if(timestamp != Ps::getFoundation().getWarnOnceTimestamp()) \ |
204 | { \ |
205 | timestamp = Ps::getFoundation().getWarnOnceTimestamp(); \ |
206 | Ps::getFoundation().error(PX_WARN, string); \ |
207 | } \ |
208 | \ |
209 | } |
210 | #define PX_WARN_ONCE_IF(condition, string) \ |
211 | { \ |
212 | if(condition) \ |
213 | { \ |
214 | PX_WARN_ONCE(string) \ |
215 | } \ |
216 | \ |
217 | } |
218 | #else |
219 | #define PX_WARN_ONCE(string) ((void)0) |
220 | #define PX_WARN_ONCE_IF(condition, string) ((void)0) |
221 | #endif |
222 | |
223 | #endif |
224 | |