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.

67 lines
1.9 KiB

  1. //========= Copyright �, Valve LLC, All rights reserved. ======================
  2. //
  3. // Purpose: Defines a buffer pool used to group small allocations
  4. //
  5. //=============================================================================
  6. #ifndef BUFFERPOOL_H
  7. #define BUFFERPOOL_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "tier1/utlbuffer.h"
  12. #include "tier1/utlvector.h"
  13. namespace GCSDK
  14. {
  15. //----------------------------------------------------------------------------
  16. // Purpose: Defines buffers that can be used to group lots of small allocs
  17. // together to improve performance. The buffers will naturally grow over time
  18. // to accommodate the largest consumers of the feature.
  19. //----------------------------------------------------------------------------
  20. class CBufferPool
  21. {
  22. public:
  23. CBufferPool( const char *pchName, const GCConVar &cvMaxSizeMB, const GCConVar &cvInitBufferSize, int nFlags = 0 );
  24. ~CBufferPool();
  25. CUtlBuffer *GetBuffer();
  26. void ReturnBuffer( CUtlBuffer *pBuffer );
  27. static void DumpPools();
  28. private:
  29. static CUtlVector<CBufferPool *> sm_vecBufferPools;
  30. const GCConVar &m_cvMaxSizeMB;
  31. const GCConVar &m_cvInitBufferSize;
  32. int m_nFlags;
  33. int32 m_nBuffersInUse;
  34. int32 m_nHighWatermark;
  35. int32 m_nBuffersTotal;
  36. size_t m_cubFree;
  37. CUtlVector<CUtlBuffer *> m_vecFreeBuffers;
  38. CUtlConstString m_sName;
  39. };
  40. //thread safe version of the above which synchronizes access at the buffer allocate/release
  41. class CBufferPoolMT
  42. {
  43. public:
  44. CBufferPoolMT( const char *pchName, const GCConVar &cvMaxSizeMB, const GCConVar &cvInitBufferSize, int nFlags = 0 ) :
  45. m_BufferPool( pchName, cvMaxSizeMB, cvInitBufferSize, nFlags )
  46. {}
  47. CUtlBuffer *GetBuffer() { AUTO_LOCK( m_mutex ); return m_BufferPool.GetBuffer(); }
  48. void ReturnBuffer( CUtlBuffer *pBuffer ) { AUTO_LOCK( m_mutex ); m_BufferPool.ReturnBuffer( pBuffer ); }
  49. private:
  50. CBufferPool m_BufferPool;
  51. CThreadFastMutex m_mutex;
  52. };
  53. } // namespace GCSDK
  54. #endif // BUFFERPOOL_H