Counter Strike : Global Offensive Source Code
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.

166 lines
4.0 KiB

  1. //=========== (C) Copyright 2000 Valve, L.L.C. 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_AllocAlignedFileLine( 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_AllocAlignedFileLine( 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. private:
  85. // These ain't gonna work
  86. static void * operator new[] ( size_t size );
  87. static void operator delete [] ( void *p);
  88. // CThreadSpinRWLock needs 8 byte alignment to work but we new() CThreadSafeMemoryPool
  89. // so simply place it at the start of the class to make sure it fits on the 8-byte boundary
  90. CThreadSpinRWLock m_threadRWLock;
  91. int m_nGrowMode;
  92. int m_cubBlockSize;
  93. int m_nGrowSize;
  94. void ClearNoLock();
  95. CInterlockedInt m_cBlocksInUse;
  96. size_t m_cubAllocated;
  97. struct BlockSet_t
  98. {
  99. byte *m_pubBlockSet;
  100. size_t m_cubAllocated;
  101. };
  102. CUtlVector<BlockSet_t> m_vecBlockSets;
  103. class CTSListBase *m_ptslistFreeBlocks;
  104. };
  105. //-----------------------------------------------------------------------------
  106. // Wrapper macro to make an allocator that returns particular typed allocations
  107. // and construction and destruction of objects.
  108. //-----------------------------------------------------------------------------
  109. template< class T >
  110. class CThreadSafeClassMemoryPool : public CThreadSafeMemoryPool
  111. {
  112. public:
  113. CThreadSafeClassMemoryPool(int numElements, int growMode = GROW_FAST) :
  114. CThreadSafeMemoryPool( sizeof(T), numElements, growMode ) {}
  115. T* Alloc();
  116. void Free( T *pMem );
  117. };
  118. template< class T >
  119. T* CThreadSafeClassMemoryPool<T>::Alloc()
  120. {
  121. T *pRet = (T*)CThreadSafeMemoryPool::Alloc();
  122. if ( pRet )
  123. {
  124. Construct( pRet );
  125. }
  126. return pRet;
  127. }
  128. template< class T >
  129. void CThreadSafeClassMemoryPool<T>::Free(T *pMem)
  130. {
  131. if ( pMem )
  132. {
  133. Destruct( pMem );
  134. }
  135. CThreadSafeMemoryPool::Free( pMem );
  136. }
  137. #endif // TSMEMPOOL_H