sys_assert.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #ifndef __SYS_ASSERT_H__
  21. #define __SYS_ASSERT_H__
  22. /*
  23. ================================================================================================
  24. Getting assert() to work as we want on all platforms and code analysis tools can be tricky.
  25. ================================================================================================
  26. */
  27. bool AssertFailed( const char *file, int line, const char *expression );
  28. // tell PC-Lint that assert failed won't return, which means it can assume the conditions
  29. // are true for subsequent analysis.
  30. //lint -function( exit, AssertFailed )
  31. //====================== assert in debug mode =======================
  32. #if defined( _DEBUG ) || defined( _lint )
  33. #undef assert
  34. // idassert is useful for cases where some external library (think MFC, etc.)
  35. // decides it's a good idea to redefine assert on us
  36. #define idassert( x ) (void)( ( !!( x ) ) || ( AssertFailed( __FILE__, __LINE__, #x ) ) )
  37. // We have the code analysis tools on the 360 compiler,
  38. // so let it know what our asserts are.
  39. // The VS ultimate editions also get it on win32, but not x86
  40. #define assert( x ) __analysis_assume( x ) ; idassert( x )
  41. #define verify( x ) ( ( x ) ? true : ( AssertFailed( __FILE__, __LINE__, #x ), false ) )
  42. #else // _DEBUG
  43. //====================== assert in release mode =======================
  44. #define idassert( x ) { (( void )0); }
  45. #undef assert
  46. #define assert( x ) idassert( x )
  47. #define verify( x ) ( ( x ) ? true : false )
  48. #endif // _DEBUG
  49. //=====================================================================
  50. #define idreleaseassert( x ) (void)( ( !!( x ) ) || ( AssertFailed( __FILE__, __LINE__, #x ) ) );
  51. #define release_assert( x ) idreleaseassert( x )
  52. #define assert_2_byte_aligned( ptr ) assert( ( ((UINT_PTR)(ptr)) & 1 ) == 0 )
  53. #define assert_4_byte_aligned( ptr ) assert( ( ((UINT_PTR)(ptr)) & 3 ) == 0 )
  54. #define assert_8_byte_aligned( ptr ) assert( ( ((UINT_PTR)(ptr)) & 7 ) == 0 )
  55. #define assert_16_byte_aligned( ptr ) assert( ( ((UINT_PTR)(ptr)) & 15 ) == 0 )
  56. #define assert_32_byte_aligned( ptr ) assert( ( ((UINT_PTR)(ptr)) & 31 ) == 0 )
  57. #define assert_64_byte_aligned( ptr ) assert( ( ((UINT_PTR)(ptr)) & 63 ) == 0 )
  58. #define assert_128_byte_aligned( ptr ) assert( ( ((UINT_PTR)(ptr)) & 127 ) == 0 )
  59. #define assert_aligned_to_type_size( ptr ) assert( ( ((UINT_PTR)(ptr)) & ( sizeof( (ptr)[0] ) - 1 ) ) == 0 )
  60. #if !defined( __TYPEINFOGEN__ ) && !defined( _lint ) // pcLint has problems with assert_offsetof()
  61. template<bool> struct compile_time_assert_failed;
  62. template<> struct compile_time_assert_failed<true> {};
  63. template<int x> struct compile_time_assert_test {};
  64. #define compile_time_assert_join2( a, b ) a##b
  65. #define compile_time_assert_join( a, b ) compile_time_assert_join2(a,b)
  66. #define compile_time_assert( x ) typedef compile_time_assert_test<sizeof(compile_time_assert_failed<(bool)(x)>)> compile_time_assert_join(compile_time_assert_typedef_, __LINE__)
  67. #define assert_sizeof( type, size ) compile_time_assert( sizeof( type ) == size )
  68. #define assert_sizeof_8_byte_multiple( type ) compile_time_assert( ( sizeof( type ) & 7 ) == 0 )
  69. #define assert_sizeof_16_byte_multiple( type ) compile_time_assert( ( sizeof( type ) & 15 ) == 0 )
  70. #define assert_offsetof( type, field, offset ) compile_time_assert( offsetof( type, field ) == offset )
  71. #define assert_offsetof_8_byte_multiple( type, field ) compile_time_assert( ( offsetof( type, field ) & 7 ) == 0 )
  72. #define assert_offsetof_16_byte_multiple( type, field ) compile_time_assert( ( offsetof( type, field ) & 15 ) == 0 )
  73. #else
  74. #define compile_time_assert( x )
  75. #define assert_sizeof( type, size )
  76. #define assert_sizeof_8_byte_multiple( type )
  77. #define assert_sizeof_16_byte_multiple( type )
  78. #define assert_offsetof( type, field, offset )
  79. #define assert_offsetof_8_byte_multiple( type, field )
  80. #define assert_offsetof_16_byte_multiple( type, field )
  81. #endif
  82. // useful for verifying that an array of items has the same number of elements in it as an enum type
  83. #define verify_array_size( _array_name_, _max_enum_ ) \
  84. compile_time_assert( sizeof( _array_name_ ) == ( _max_enum_ ) * sizeof( _array_name_[ 0 ] ) )
  85. // ai debugging macros (designed to limit ai interruptions to non-ai programmers)
  86. #ifdef _DEBUG
  87. //#define DEBUGAI // NOTE: uncomment for full ai debugging
  88. #endif
  89. #ifdef DEBUGAI
  90. #define ASSERTAI( x ) assert( x )
  91. #define VERIFYAI( x ) verify( x )
  92. #else // DEBUGAI
  93. #define ASSERTAI( x )
  94. #define VERIFYAI( x ) ( ( x ) ? true : false )
  95. #endif // DEBUGAI
  96. #endif // !__SYS_ASSERT_H__