2009-11-16 00:40:09 +00:00
|
|
|
/* PCSX2 - PS2 Emulator for PCs
|
2010-05-03 14:08:02 +00:00
|
|
|
* Copyright (C) 2002-2010 PCSX2 Dev Team
|
2009-11-16 00:40:09 +00:00
|
|
|
*
|
|
|
|
* PCSX2 is free software: you can redistribute it and/or modify it under the terms
|
|
|
|
* of the GNU Lesser General Public License as published by the Free Software Found-
|
|
|
|
* ation, either version 3 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE. See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with PCSX2.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2009-12-14 12:18:55 +00:00
|
|
|
#ifndef __pxFUNCTION__
|
|
|
|
#if defined(__GNUG__)
|
|
|
|
# define __pxFUNCTION__ __PRETTY_FUNCTION__
|
|
|
|
#else
|
|
|
|
# define __pxFUNCTION__ __FUNCTION__
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef wxNullChar
|
|
|
|
# define wxNullChar ((wxChar*)NULL)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// FnChar_t - function name char type; typedef'd in case it ever changes between compilers
|
|
|
|
// (ie, a compiler decides to wchar_t it instead of char/UTF8).
|
|
|
|
typedef char FnChar_t;
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
// DiagnosticOrigin
|
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
struct DiagnosticOrigin
|
|
|
|
{
|
|
|
|
const wxChar* srcfile;
|
|
|
|
const FnChar_t* function;
|
|
|
|
const wxChar* condition;
|
|
|
|
int line;
|
|
|
|
|
|
|
|
DiagnosticOrigin( const wxChar *_file, int _line, const FnChar_t *_func, const wxChar* _cond = NULL )
|
|
|
|
: srcfile( _file )
|
|
|
|
, function( _func )
|
|
|
|
, condition( _cond )
|
|
|
|
, line( _line )
|
|
|
|
{
|
|
|
|
}
|
2010-04-25 00:31:27 +00:00
|
|
|
|
2009-12-14 12:18:55 +00:00
|
|
|
wxString ToString( const wxChar* msg=NULL ) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Returns ture if the assertion is to trap into the debugger, or false if execution
|
|
|
|
// of the program should continue unimpeded.
|
|
|
|
typedef bool pxDoAssertFnType(const DiagnosticOrigin& origin, const wxChar *msg);
|
|
|
|
|
|
|
|
extern pxDoAssertFnType pxAssertImpl_LogIt;
|
|
|
|
|
|
|
|
extern pxDoAssertFnType* pxDoAssert;
|
|
|
|
|
2009-11-16 00:40:09 +00:00
|
|
|
// ----------------------------------------------------------------------------------------
|
2009-12-14 12:18:55 +00:00
|
|
|
// pxAssert / pxAssertDev
|
2009-11-16 00:40:09 +00:00
|
|
|
// ----------------------------------------------------------------------------------------
|
2009-12-07 22:39:52 +00:00
|
|
|
// Standard "nothrow" assertions. All assertions act as valid conditional statements that
|
|
|
|
// return the result of the specified conditional; useful for handling failed assertions in
|
|
|
|
// a "graceful" fashion when utilizing the "ignore" feature of assertion debugging.
|
2009-12-14 12:18:55 +00:00
|
|
|
// These macros are mostly intended for "pseudo-weak" assumptions within code, most often for
|
|
|
|
// testing threaded user interface code (threading of the UI is a prime example since often
|
|
|
|
// even very robust assertions can fail in very rare conditions, due to the complex variety
|
|
|
|
// of ways the user can invoke UI events).
|
2009-11-16 00:40:09 +00:00
|
|
|
//
|
2009-12-14 12:18:55 +00:00
|
|
|
// All macros return TRUE if the assertion succeeds, or FALSE if the assertion failed
|
|
|
|
// (thus matching the condition of the assertion itself).
|
2009-11-16 00:40:09 +00:00
|
|
|
//
|
|
|
|
// pxAssertDev is an assertion tool for Devel builds, intended for sanity checking and/or
|
2009-12-07 22:39:52 +00:00
|
|
|
// bounds checking variables in areas which are not performance critical. Another common
|
|
|
|
// use is for checking thread affinity on utility functions.
|
2009-11-16 00:40:09 +00:00
|
|
|
//
|
2009-12-14 12:18:55 +00:00
|
|
|
// Credits: These macros are based on a combination of wxASSERT, MSVCRT's assert and the
|
|
|
|
// ATL's Assertion/Assumption macros. the best of all worlds!
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
// pxAssume / pxAssumeDev / pxFail / pxFailDev
|
|
|
|
// --------------------------------------------------------------------------------------
|
2011-07-24 13:02:50 +00:00
|
|
|
// Assumptions are hints to the compiler that the condition will always be true,
|
|
|
|
// the condition should never fail under any circumstance in release builds
|
|
|
|
// or else you might get incorrect compiler generated code.
|
2009-12-14 12:18:55 +00:00
|
|
|
//
|
|
|
|
// Performance: All assumption/fail types optimize into __assume()/likely() directives in
|
2011-07-24 13:02:50 +00:00
|
|
|
// Release builds (non-dev varieties optimize as such in Devel builds as well).
|
|
|
|
// __assume(0) is a special form of __assume() which tells the compiler that the code path
|
|
|
|
// is not reachable and will cause undefined results if it is reachable...
|
2010-08-16 15:01:13 +00:00
|
|
|
//
|
|
|
|
// Having pxFail and pxFailDev translate into __assume statements is very dangerous, since
|
|
|
|
// it can lead to the compiler optimizing out code and leading to crashes in dev/release
|
2011-07-24 13:02:50 +00:00
|
|
|
// builds. To have code optimized, explicitly use pxAssume(false) or pxAssumeDev(false,msg);
|
2009-12-14 12:18:55 +00:00
|
|
|
|
|
|
|
#define pxDiagSpot DiagnosticOrigin( __TFILE__, __LINE__, __pxFUNCTION__ )
|
|
|
|
#define pxAssertSpot(cond) DiagnosticOrigin( __TFILE__, __LINE__, __pxFUNCTION__, _T(#cond) )
|
|
|
|
|
|
|
|
// pxAssertRel ->
|
|
|
|
// Special release-mode assertion. Limited use since stack traces in release mode builds
|
|
|
|
// (especially with LTCG) are highly suspect. But when troubleshooting crashes that only
|
|
|
|
// rear ugly heads in optimized builds, this is one of the few tools we have.
|
|
|
|
|
|
|
|
#define pxAssertRel(cond, msg) ( (likely(cond)) || (pxOnAssert(pxAssertSpot(cond), msg), false) )
|
2009-12-23 13:22:30 +00:00
|
|
|
#define pxAssumeRel(cond, msg) ((void) ( (!likely(cond)) && (pxOnAssert(pxAssertSpot(cond), msg), false) ))
|
2010-08-16 15:01:13 +00:00
|
|
|
#define pxFailRel(msg) pxAssertRel(false, msg)
|
2009-11-16 00:40:09 +00:00
|
|
|
|
|
|
|
#if defined(PCSX2_DEBUG)
|
|
|
|
|
2009-12-14 12:18:55 +00:00
|
|
|
# define pxAssertMsg(cond, msg) pxAssertRel(cond, msg)
|
|
|
|
# define pxAssertDev(cond, msg) pxAssertMsg(cond, msg)
|
2009-11-16 00:40:09 +00:00
|
|
|
|
2009-12-23 13:22:30 +00:00
|
|
|
# define pxAssumeMsg(cond, msg) pxAssumeRel(cond, msg)
|
|
|
|
# define pxAssumeDev(cond, msg) pxAssumeRel(cond, msg)
|
2009-11-16 00:40:09 +00:00
|
|
|
|
2010-08-16 15:01:13 +00:00
|
|
|
# define pxFail(msg) pxAssertMsg(false, msg)
|
|
|
|
# define pxFailDev(msg) pxAssertDev(false, msg)
|
2009-11-16 00:40:09 +00:00
|
|
|
|
|
|
|
#elif defined(PCSX2_DEVBUILD)
|
|
|
|
|
2011-07-24 13:02:50 +00:00
|
|
|
// Devel builds now will give you a release-mode assertion dialog window if any of the
|
|
|
|
// following macro's 'cond' field is false.
|
|
|
|
// Note: Only use pxAssume/Msg/Dev if you know what you're doing, __assume is supposed
|
|
|
|
// to be used as an optimization hint, yet many devs have been using psAssume
|
|
|
|
// thinking its the same as an assertion.
|
|
|
|
// __assume(0) is also very dangerous because it is a special case of __assume() which
|
|
|
|
// tells the compiler that the code path is not reachable, and it can cause unpredictable
|
|
|
|
// results if the code path can be reached.
|
|
|
|
// i.e. if (1) { __assume(0); something(); }
|
|
|
|
// In the above example, something() may never be called.
|
|
|
|
// __assume(0)'s real use is in optimizing stuff such as "default:" cases on a switch
|
|
|
|
// statement. See jNO_DEFAULT
|
2009-11-16 00:40:09 +00:00
|
|
|
|
2011-07-24 13:02:50 +00:00
|
|
|
# define pxAssertMsg(cond, msg) pxAssertRel(cond, msg)
|
2009-12-14 12:18:55 +00:00
|
|
|
# define pxAssertDev(cond, msg) pxAssertRel(cond, msg)
|
2009-11-16 00:40:09 +00:00
|
|
|
|
2011-07-24 13:02:50 +00:00
|
|
|
# define pxAssumeMsg(cond, msg) pxAssumeRel(cond, msg) //(__assume(cond))
|
2010-06-28 18:03:54 +00:00
|
|
|
# define pxAssumeDev(cond, msg) pxAssumeRel(cond, msg)
|
2009-11-16 00:40:09 +00:00
|
|
|
|
2011-07-24 13:02:50 +00:00
|
|
|
# define pxFail(msg) pxAssertDev(false, msg)
|
2010-08-16 15:01:13 +00:00
|
|
|
# define pxFailDev(msg) pxAssertDev(false, msg)
|
2009-11-16 00:40:09 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
// Release Builds just use __assume as an optimization, and return the conditional
|
|
|
|
// as a result (which is optimized to nil if unused).
|
|
|
|
|
2009-12-14 12:18:55 +00:00
|
|
|
# define pxAssertMsg(cond, msg) (likely(cond))
|
|
|
|
# define pxAssertDev(cond, msg) (likely(cond))
|
|
|
|
|
2009-12-23 13:22:30 +00:00
|
|
|
# define pxAssumeMsg(cond, msg) (__assume(cond))
|
2009-12-14 12:18:55 +00:00
|
|
|
# define pxAssumeDev(cond, msg) (__assume(cond))
|
|
|
|
|
2011-07-24 13:02:50 +00:00
|
|
|
# define pxFail(msg) do{} while(0)
|
|
|
|
# define pxFailDev(msg) do{} while(0)
|
2009-11-16 00:40:09 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2009-12-14 12:18:55 +00:00
|
|
|
#define pxAssert(cond) pxAssertMsg(cond, wxNullChar)
|
2009-12-23 13:22:30 +00:00
|
|
|
#define pxAssume(cond) pxAssumeMsg(cond, wxNullChar)
|
2009-12-14 12:18:55 +00:00
|
|
|
|
|
|
|
#define pxAssertRelease( cond, msg )
|
2009-11-16 00:40:09 +00:00
|
|
|
|
|
|
|
// Performs an unsigned index bounds check, and generates a debug assertion if the check fails.
|
|
|
|
// For stricter checking in Devel builds as well as debug builds (but possibly slower), use
|
|
|
|
// IndexBoundsCheckDev.
|
|
|
|
|
|
|
|
#define IndexBoundsCheck( objname, idx, sze ) pxAssertMsg( (uint)(idx) < (uint)(sze), \
|
2010-10-18 01:40:49 +00:00
|
|
|
pxsFmt( L"Array index out of bounds accessing object '%s' (index=%d, size=%d)", objname, (idx), (sze) ) )
|
2009-11-16 00:40:09 +00:00
|
|
|
|
|
|
|
#define IndexBoundsCheckDev( objname, idx, sze ) pxAssertDev( (uint)(idx) < (uint)(sze), \
|
2010-10-18 01:40:49 +00:00
|
|
|
pxsFmt( L"Array index out of bounds accessing object '%s' (index=%d, size=%d)", objname, (idx), (sze) ) )
|
2010-08-05 12:48:49 +00:00
|
|
|
|
|
|
|
#define IndexBoundsAssume( objname, idx, sze ) pxAssumeMsg( (uint)(idx) < (uint)(sze), \
|
2010-10-18 01:40:49 +00:00
|
|
|
pxsFmt( L"Array index out of bounds accessing object '%s' (index=%d, size=%d)", objname, (idx), (sze) ) )
|
2010-08-05 12:48:49 +00:00
|
|
|
|
|
|
|
#define IndexBoundsAssumeDev( objname, idx, sze ) pxAssumeDev( (uint)(idx) < (uint)(sze), \
|
2010-10-18 01:40:49 +00:00
|
|
|
pxsFmt( L"Array index out of bounds accessing object '%s' (index=%d, size=%d)", objname, (idx), (sze) ) )
|
2009-11-16 00:40:09 +00:00
|
|
|
|
|
|
|
|
2009-12-14 12:18:55 +00:00
|
|
|
extern void pxOnAssert( const DiagnosticOrigin& origin, const wxChar* msg=NULL );
|
|
|
|
extern void pxOnAssert( const DiagnosticOrigin& origin, const char* msg );
|
2009-11-16 00:40:09 +00:00
|
|
|
|
2010-08-16 15:01:13 +00:00
|
|
|
// --------------------------------------------------------------------------------------
|
2009-12-07 22:39:52 +00:00
|
|
|
// jNO_DEFAULT -- disables the default case in a switch, which improves switch optimization
|
|
|
|
// under MSVC.
|
2010-08-16 15:01:13 +00:00
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
// How it Works: pxAssumeDev turns into an __assume(0) under msvc compilers, which when specified
|
2009-12-07 22:39:52 +00:00
|
|
|
// in the 'default:' case of a switch tells the compiler that the case is unreachable, so
|
|
|
|
// that it will not generate any code, LUTs, or conditionals to handle it.
|
|
|
|
//
|
2009-12-14 12:18:55 +00:00
|
|
|
// * In debug/devel builds the default case will cause an assertion.
|
2009-12-07 22:39:52 +00:00
|
|
|
//
|
|
|
|
#ifndef jNO_DEFAULT
|
|
|
|
# define jNO_DEFAULT \
|
2009-12-14 12:18:55 +00:00
|
|
|
default: \
|
|
|
|
{ \
|
2011-07-24 13:02:50 +00:00
|
|
|
pxAssumeDev( 0, "Incorrect usage of jNO_DEFAULT detected (default case is not unreachable!)" ); \
|
2009-12-14 12:18:55 +00:00
|
|
|
break; \
|
|
|
|
}
|
2009-12-07 22:39:52 +00:00
|
|
|
#endif
|