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.

170 lines
4.1 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // The copyright to the contents herein is the property of Valve, L.L.C.
  4. // The contents may be used and/or copied only with the written permission of
  5. // Valve, L.L.C., or in accordance with the terms and conditions stipulated in
  6. // the agreement/contract under which the contents have been supplied.
  7. //
  8. // Purpose:
  9. //
  10. // $Workfile: $
  11. // $Date: $
  12. //
  13. //-----------------------------------------------------------------------------
  14. // $Log: $
  15. //
  16. // $NoKeywords: $
  17. //=============================================================================
  18. #ifndef TSMEMPOOL_H
  19. #define TSMEMPOOL_H
  20. #ifdef _WIN32
  21. #pragma once
  22. #endif
  23. #undef new
  24. //-----------------------------------------------------------------------------
  25. // Purpose: Optimized pool memory allocator
  26. //-----------------------------------------------------------------------------
  27. class CThreadSafeMemoryPool
  28. {
  29. public:
  30. enum
  31. {
  32. GROW_NONE=0, // Don't allow new blobs.
  33. GROW_FAST=1, // New blob size is numElements * (i+1) (ie: the blocks it allocates get larger and larger each time it allocates one).
  34. GROW_SLOW=2, // New blob size is numElements.
  35. GROW_TIL_YOU_CANT=3 // GROW_SLOW til alloc fails - then STOP and dont assert!
  36. };
  37. CThreadSafeMemoryPool( int blockSize, int numElements, int growMode = GROW_FAST );
  38. ~CThreadSafeMemoryPool();
  39. void *Alloc(); // Allocate the element size you specified in the constructor.
  40. void *Alloc( unsigned int cubAlloc );
  41. void Free( void *pMem );
  42. void Free( void *pMem, int cubAlloc );
  43. // Frees everything
  44. void Clear();
  45. // display
  46. void PrintStats();
  47. size_t CubTotalSize();
  48. size_t CubSizeInUse();
  49. int Count();
  50. static void * operator new( size_t size )
  51. {
  52. CThreadSafeMemoryPool *pNode = (CThreadSafeMemoryPool *)MemAlloc_AllocAligned( size, 8, __FILE__, __LINE__
  53. #ifdef STEAM
  54. , true // new operator
  55. #endif
  56. );
  57. return pNode;
  58. }
  59. static void * operator new( size_t size, int nBlockUse, const char *pFileName, int nLine )
  60. {
  61. CThreadSafeMemoryPool *pNode = (CThreadSafeMemoryPool *)MemAlloc_AllocAligned( size, 8, pFileName, nLine
  62. #ifdef STEAM
  63. , true // new operator
  64. #endif
  65. );
  66. return pNode;
  67. }
  68. static void operator delete( void *p)
  69. {
  70. MemAlloc_FreeAligned( p
  71. #ifdef STEAM
  72. , true // new operator
  73. #endif
  74. );
  75. }
  76. static void operator delete( void *p, int nBlockUse, const char *pFileName, int nLine )
  77. {
  78. MemAlloc_FreeAligned( p
  79. #ifdef STEAM
  80. , true // new operator
  81. #endif
  82. );
  83. }
  84. #ifdef DBGFLAG_VALIDATE
  85. void Validate( CValidator &validator, const char *pchName ); // Validate our internal structures
  86. #endif // DBGFLAG_VALIDATE
  87. private:
  88. // These ain't gonna work
  89. static void * operator new[] ( size_t size );
  90. static void operator delete [] ( void *p);
  91. // CThreadSpinRWLock needs 8 byte alignment to work but we new() CThreadSafeMemoryPool
  92. // so simply place it at the start of the class to make sure it fits on the 8-byte boundary
  93. CThreadSpinRWLock m_threadRWLock;
  94. int m_nGrowMode;
  95. int m_cubBlockSize;
  96. int m_nGrowSize;
  97. void ClearNoLock();
  98. CInterlockedInt m_cBlocksInUse;
  99. size_t m_cubAllocated;
  100. struct BlockSet_t
  101. {
  102. byte *m_pubBlockSet;
  103. size_t m_cubAllocated;
  104. };
  105. CUtlVector<BlockSet_t> m_vecBlockSets;
  106. class CTSListBase *m_ptslistFreeBlocks;
  107. };
  108. //-----------------------------------------------------------------------------
  109. // Wrapper macro to make an allocator that returns particular typed allocations
  110. // and construction and destruction of objects.
  111. //-----------------------------------------------------------------------------
  112. template< class T >
  113. class CThreadSafeClassMemoryPool : public CThreadSafeMemoryPool
  114. {
  115. public:
  116. CThreadSafeClassMemoryPool(int numElements, int growMode = GROW_FAST) :
  117. CThreadSafeMemoryPool( sizeof(T), numElements, growMode ) {}
  118. T* Alloc();
  119. void Free( T *pMem );
  120. };
  121. template< class T >
  122. T* CThreadSafeClassMemoryPool<T>::Alloc()
  123. {
  124. T *pRet = (T*)CThreadSafeMemoryPool::Alloc();
  125. if ( pRet )
  126. {
  127. Construct( pRet );
  128. }
  129. return pRet;
  130. }
  131. template< class T >
  132. void CThreadSafeClassMemoryPool<T>::Free(T *pMem)
  133. {
  134. if ( pMem )
  135. {
  136. Destruct( pMem );
  137. }
  138. CThreadSafeMemoryPool::Free( pMem );
  139. }
  140. #endif // TSMEMPOOL_H