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.

176 lines
4.4 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "basetypes.h"
  8. #include "traceinit.h"
  9. #include "utlvector.h"
  10. #include "sys.h"
  11. #include "common.h"
  12. // memdbgon must be the last include file in a .cpp file!!!
  13. #include "tier0/memdbgon.h"
  14. //-----------------------------------------------------------------------------
  15. // Purpose:
  16. //-----------------------------------------------------------------------------
  17. class CInitTracker
  18. {
  19. public:
  20. enum
  21. {
  22. NUM_LISTS = 4,
  23. };
  24. //-----------------------------------------------------------------------------
  25. // Purpose:
  26. //-----------------------------------------------------------------------------
  27. class InitFunc
  28. {
  29. public:
  30. const char *initname;
  31. const char *shutdownname;
  32. int referencecount;
  33. int sequence;
  34. bool warningprinted;
  35. // Profiling data
  36. float inittime;
  37. float shutdowntime;
  38. };
  39. CInitTracker( void );
  40. ~CInitTracker( void );
  41. void Init( const char *init, const char *shutdown, int listnum );
  42. void Shutdown( const char *shutdown, int listnum );
  43. private:
  44. int m_nNumFuncs[ NUM_LISTS ];
  45. CUtlVector < InitFunc * > m_Funcs[ NUM_LISTS ];
  46. };
  47. static CInitTracker g_InitTracker;
  48. //-----------------------------------------------------------------------------
  49. // Purpose:
  50. //-----------------------------------------------------------------------------
  51. CInitTracker::CInitTracker( void )
  52. {
  53. for ( int l = 0; l < NUM_LISTS; l++ )
  54. {
  55. m_nNumFuncs[ l ] = 0;
  56. }
  57. }
  58. //-----------------------------------------------------------------------------
  59. // Purpose:
  60. //-----------------------------------------------------------------------------
  61. CInitTracker::~CInitTracker( void )
  62. {
  63. for ( int l = 0; l < NUM_LISTS; l++ )
  64. {
  65. //assert( m_nNumFuncs[l] == 0 );
  66. for ( int i = 0; i < m_nNumFuncs[l]; i++ )
  67. {
  68. InitFunc *f = m_Funcs[ l ][ i ];
  69. if ( f->referencecount )
  70. {
  71. Msg( "Missing shutdown function for %s : %s\n", f->initname, f->shutdownname );
  72. }
  73. delete f;
  74. }
  75. m_Funcs[ l ].RemoveAll();
  76. m_nNumFuncs[ l ] = 0;
  77. }
  78. }
  79. //-----------------------------------------------------------------------------
  80. // Purpose:
  81. // Input : *init -
  82. // *shutdown -
  83. //-----------------------------------------------------------------------------
  84. void CInitTracker::Init( const char *init, const char *shutdown, int listnum )
  85. {
  86. InitFunc *f = new InitFunc;
  87. Assert( f );
  88. f->initname = init;
  89. f->shutdownname = shutdown;
  90. f->sequence = m_nNumFuncs[ listnum ];
  91. f->referencecount = 1;
  92. f->warningprinted = false;
  93. f->inittime = 0.0; //Sys_FloatTime();
  94. f->shutdowntime = 0.0;
  95. m_Funcs[ listnum ].AddToHead( f );
  96. m_nNumFuncs[ listnum ]++;
  97. }
  98. //-----------------------------------------------------------------------------
  99. // Purpose:
  100. // Input : *shutdown -
  101. //-----------------------------------------------------------------------------
  102. void CInitTracker::Shutdown( const char *shutdown, int listnum )
  103. {
  104. if( !m_nNumFuncs[ listnum ] )
  105. {
  106. Msg( "Mismatched shutdown function %s\n", shutdown );
  107. return;
  108. }
  109. int i = 0;
  110. InitFunc *f = NULL;
  111. for ( i = 0; i < m_nNumFuncs[ listnum ]; i++ )
  112. {
  113. f = m_Funcs[ listnum ][ i ];
  114. if ( f->referencecount )
  115. break;
  116. }
  117. if ( f && f->referencecount && stricmp( f->shutdownname, shutdown ) )
  118. {
  119. if ( !f->warningprinted )
  120. {
  121. f->warningprinted = true;
  122. //Msg( "Shutdown function %s called out of order, expecting %s\n", shutdown, f->shutdownname );
  123. }
  124. }
  125. for ( i = 0; i < m_nNumFuncs[ listnum ]; i++ )
  126. {
  127. f = m_Funcs[ listnum ][ i ];
  128. if ( !stricmp( f->shutdownname, shutdown ) )
  129. {
  130. Assert( f->referencecount );
  131. //f->shutdowntime = Sys_FloatTime();
  132. f->referencecount--;
  133. return;
  134. }
  135. }
  136. Msg( "Shutdown function %s not in list!!!\n", shutdown );
  137. }
  138. //-----------------------------------------------------------------------------
  139. // Purpose:
  140. // Input : *i -
  141. // *s -
  142. //-----------------------------------------------------------------------------
  143. void TraceInit( const char *i, const char *s, int listnum )
  144. {
  145. g_InitTracker.Init( i, s, listnum );
  146. COM_TimestampedLog( "%s", i );
  147. }
  148. //-----------------------------------------------------------------------------
  149. // Purpose:
  150. // Input : *s -
  151. //-----------------------------------------------------------------------------
  152. void TraceShutdown( const char *s, int listnum )
  153. {
  154. g_InitTracker.Shutdown( s, listnum );
  155. }