Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
4.5 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Miscellaneous platform-specific code
  4. //
  5. //=============================================================================
  6. #ifndef MISC_H
  7. #define MISC_H
  8. // Random number utilities
  9. uint32 UNRandFast();
  10. char CHRandFast();
  11. void SetRandSeed( uint64 ulRandSeed );
  12. uint64 GetRandSeed();
  13. void RandMem(void *dest, int count);
  14. bool IsVTTAccountName( const char *szAccountName );
  15. float SafeCalcPct( uint64 ulNumerator, uint64 ulDenominator );
  16. bool BRejectDueToBacklog( int nBacklogCur, int nBacklogThreshold, int nBacklogLimit, int iItem );
  17. #define SAFE_CLOSE_HANDLE( x ) if ( INVALID_HANDLE_VALUE != ( x ) ) { CloseHandle( x ); ( x ) = INVALID_HANDLE_VALUE; }
  18. #define SAFE_DELETE( x ) if ( NULL != ( x ) ) { delete ( x ); ( x ) = NULL; }
  19. #define SAFE_CLOSE_HCONNECTION( x ) if ( 0 != ( x ) ) { CNet::BClose( ( x ) ); ( x ) = 0; }
  20. #define SAFE_RELEASE( x ) if ( NULL != ( x ) ) { ( x )->Release(); x = NULL; }
  21. #define DECLARE_STEAM_CLASS_SIMPLE( className, baseClassName ) \
  22. typedef baseClassName BaseClass; \
  23. typedef className ThisClass; \
  24. #define DECLARE_STEAM_CLASS_NOBASE( className ) \
  25. typedef className ThisClass; \
  26. // Type for our memory output debugging.
  27. class IDisplayMemPoolStats
  28. {
  29. public:
  30. virtual void Display( const char *pszClassName, uint64 ulClassSize, uint32 unPoolInstanceCount, uint64 ulPoolMemoryUsage, uint32 unPoolPeakInstanceCount, uint64 ulPoolPeakMemoryUsage ) = 0;
  31. };
  32. // A class for registering functions that will print memory usage information
  33. typedef void (*DumpMemFn_t)( IDisplayMemPoolStats * );
  34. class CDumpMemFnReg
  35. {
  36. public:
  37. static CDumpMemFnReg *sm_Head;
  38. CDumpMemFnReg( DumpMemFn_t fn )
  39. : m_fn( fn ),
  40. m_pNext( sm_Head )
  41. {
  42. sm_Head = this;
  43. }
  44. DumpMemFn_t m_fn;
  45. CDumpMemFnReg *m_pNext;
  46. };
  47. // Helper macros for creating and using CClassMemoryPool on our frequently allocated objects
  48. #define DECLARE_CLASS_MEMPOOL( className ) \
  49. private: \
  50. static CUtlMemoryPool sm_classMemPool; \
  51. public: \
  52. static void* operator new ( size_t nSize ); \
  53. static void* operator new ( size_t nSize, int nBlockUse, const char *pFileName, int nLine ); \
  54. static void operator delete ( void *pMem ); \
  55. static void DumpMemStats( IDisplayMemPoolStats *pDisplayer ); \
  56. #define IMPLEMENT_CLASS_MEMPOOL( className, initSize, growMode ) \
  57. CUtlMemoryPool className::sm_classMemPool( sizeof( className ), ( initSize ), ( growMode ), MEM_ALLOC_CLASSNAME( className ) ); \
  58. \
  59. void* className::operator new ( size_t nSize ) \
  60. { \
  61. if ( nSize != sizeof( className ) ) \
  62. { \
  63. EmitError( SPEW_CONSOLE, #className"::operator new() called on wrong size! Expected %llu, Got %llu\n", (uint64)sizeof( className ), (uint64)nSize ); \
  64. return NULL; \
  65. } \
  66. \
  67. return sm_classMemPool.Alloc(); \
  68. } \
  69. \
  70. void* className::operator new ( size_t nSize, int nBlockUse, const char *pFileName, int nLine ) \
  71. { \
  72. if ( nSize != sizeof( className ) ) \
  73. { \
  74. EmitError( SPEW_CONSOLE, #className"::operator new() called on wrong size! Expected %llu, Got %llu\n", (uint64)sizeof( className ), (uint64)nSize ); \
  75. return NULL; \
  76. } \
  77. \
  78. return sm_classMemPool.Alloc(); \
  79. } \
  80. \
  81. void className::operator delete ( void *pMem ) \
  82. { \
  83. sm_classMemPool.Free( (className *)pMem ); \
  84. } \
  85. \
  86. void className::DumpMemStats( IDisplayMemPoolStats *pDisplayer ) \
  87. { \
  88. Assert( pDisplayer ); \
  89. pDisplayer->Display \
  90. ( \
  91. #className, \
  92. sizeof( className ), \
  93. sm_classMemPool.Count(), \
  94. sm_classMemPool.Count() * sizeof( className ), \
  95. ( ( sm_classMemPool.PeakCount() + ( initSize ) ) / ( initSize ) ) * ( initSize ) , \
  96. ( ( sm_classMemPool.PeakCount() + ( initSize ) ) / ( initSize ) ) * ( initSize ) * sizeof( className ) \
  97. ); \
  98. } \
  99. \
  100. static CDumpMemFnReg s_##className##RegDumpMemory( &className::DumpMemStats );
  101. // useful macro for rendering an IP
  102. #define iptod(x) ((x)>>24&0xff), ((x)>>16&0xff), ((x)>>8&0xff), ((x)&0xff)
  103. #endif // MISC_H