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.

120 lines
3.6 KiB

  1. //========= Copyright �, Valve LLC, All rights reserved. ======================
  2. //
  3. // Purpose: Defines a buffer pool used to group small allocations
  4. //
  5. //=============================================================================
  6. #include "stdafx.h"
  7. #include "bufferpool.h"
  8. using namespace GCSDK;
  9. CUtlVector<CBufferPool *> CBufferPool::sm_vecBufferPools;
  10. //----------------------------------------------------------------------------
  11. // Purpose: Constructor
  12. //----------------------------------------------------------------------------
  13. CBufferPool::CBufferPool( const char *pchName, const GCConVar &cvMaxSizeMB, const GCConVar &cvInitBufferSize, int nFlags )
  14. : m_nBuffersInUse( 0 )
  15. , m_nBuffersTotal( 0 )
  16. , m_nHighWatermark( 0 )
  17. , m_cubFree( 0 )
  18. , m_sName( pchName )
  19. , m_cvMaxSizeMB( cvMaxSizeMB )
  20. , m_cvInitBufferSize( cvInitBufferSize )
  21. , m_nFlags( nFlags )
  22. {
  23. sm_vecBufferPools.AddToTail( this );
  24. }
  25. //----------------------------------------------------------------------------
  26. // Purpose: Destructor
  27. //----------------------------------------------------------------------------
  28. CBufferPool::~CBufferPool()
  29. {
  30. m_vecFreeBuffers.PurgeAndDeleteElements();
  31. sm_vecBufferPools.FindAndRemove( this );
  32. }
  33. //----------------------------------------------------------------------------
  34. // Purpose: Gives a bind param buffer to a query
  35. //----------------------------------------------------------------------------
  36. CUtlBuffer *CBufferPool::GetBuffer()
  37. {
  38. m_nBuffersInUse++;
  39. if ( m_vecFreeBuffers.Count() > 0 )
  40. {
  41. CUtlBuffer *pBuffer = m_vecFreeBuffers.Tail();
  42. m_vecFreeBuffers.Remove( m_vecFreeBuffers.Count() - 1 );
  43. m_cubFree -= pBuffer->Size();
  44. return pBuffer;
  45. }
  46. else
  47. {
  48. m_nBuffersTotal++;
  49. m_nHighWatermark = max( m_nBuffersTotal, m_nHighWatermark );
  50. return new CUtlBuffer( 0, m_cvInitBufferSize.GetInt(), m_nFlags );
  51. }
  52. }
  53. //----------------------------------------------------------------------------
  54. // Purpose: Returns a bind param buffer when a query is done with it
  55. //----------------------------------------------------------------------------
  56. void CBufferPool::ReturnBuffer( CUtlBuffer *pBuffer )
  57. {
  58. m_nBuffersInUse--;
  59. if ( ( int64 )( m_cubFree + pBuffer->Size() ) <= ( m_cvMaxSizeMB.GetInt() * k_nMegabyte ) )
  60. {
  61. pBuffer->Clear();
  62. m_cubFree += pBuffer->Size();
  63. m_vecFreeBuffers.AddToTail( pBuffer );
  64. }
  65. else
  66. {
  67. m_nBuffersTotal--;
  68. delete pBuffer;
  69. }
  70. }
  71. //----------------------------------------------------------------------------
  72. // Purpose: Print statistics to the console
  73. //----------------------------------------------------------------------------
  74. void CBufferPool::DumpPools()
  75. {
  76. EmitInfo( SPEW_CONSOLE, SPEW_ALWAYS, LOG_ALWAYS, "Reusable Buffer Pools:\n" );
  77. EmitInfo( SPEW_CONSOLE, SPEW_ALWAYS, LOG_ALWAYS,
  78. " Pool Buffers Free Max Buffers Total Mem (est) Free Mem\n" );
  79. EmitInfo( SPEW_CONSOLE, SPEW_ALWAYS, LOG_ALWAYS,
  80. " ------------------------------ ------------ ------------ ------------ ---------------- ----------------\n" );
  81. FOR_EACH_VEC( sm_vecBufferPools, i )
  82. {
  83. CBufferPool *pPool = sm_vecBufferPools[i];
  84. int nTotalEst = 0;
  85. if ( pPool->m_vecFreeBuffers.Count() > 0 )
  86. {
  87. nTotalEst = pPool->m_cubFree / pPool->m_vecFreeBuffers.Count() * pPool->m_nBuffersTotal;
  88. }
  89. EmitInfo( SPEW_CONSOLE, SPEW_ALWAYS, LOG_ALWAYS, " %-30s %12d %12d %12d %16s %16s\n",
  90. pPool->m_sName.Get(),
  91. pPool->m_nBuffersTotal,
  92. pPool->m_vecFreeBuffers.Count(),
  93. pPool->m_nHighWatermark,
  94. Q_pretifymem( nTotalEst, 0, true ),
  95. Q_pretifymem( pPool->m_cubFree, 0, true )
  96. );
  97. }
  98. EmitInfo( SPEW_CONSOLE, SPEW_ALWAYS, LOG_ALWAYS, "\n" );
  99. }