Guest User

Untitled

a guest
Jul 18th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. #ifndef TMemoryPool_h__
  2. #define TMemoryPool_h__
  3.  
  4. #include "Globals.h"
  5. #include <vector>
  6.  
  7. template <class T>
  8. class TMemoryPool
  9. {
  10. public:
  11.  
  12. TMemoryPool() { /* ... */ };
  13. ~TMemoryPool() { /* ... */ };
  14.  
  15. // Initializes the memory pool.
  16. tgBool InitializeMemoryPool(tgUInt32 BlockSize = 32);
  17. // Destroys the memory pool.
  18. tgBool DestroyMemoryPool();
  19.  
  20. // Acquires a free resource from the memory pool.
  21. T* AcquireResource();
  22. // Drops a used resource from the memory pool.
  23. tgBool DropResource(T* Resource);
  24.  
  25. protected:
  26.  
  27. // Allocates a new block size worth of memory.
  28. void AllocateResourceBlock();
  29.  
  30. // Block size for our memory pool.
  31. tgUInt32 m_BlockSize;
  32.  
  33. // Memory pool handle used internally.
  34. template <class T>
  35. class TMemoryPoolHandle
  36. {
  37. public:
  38.  
  39. TMemoryPoolHandle() : Used(FALSE)
  40. {
  41. Resource = tgNew T;
  42. #ifdef _DEBUG
  43. tgLogPrint("MemoryPool ::: Resource, reporting for duty!");
  44. #endif
  45. }
  46. ~TMemoryPoolHandle()
  47. {
  48. tgDelete Resource;
  49. }
  50.  
  51. T* Resource;
  52. tgBool Used;
  53. };
  54.  
  55. // Memory pool typedef.
  56. typedef std::vector < TMemoryPoolHandle < T > * > MemoryPool;
  57.  
  58. // Our memory pool.
  59. MemoryPool m_Pool;
  60.  
  61. };
  62.  
  63.  
  64. template <class T>
  65. tgBool TMemoryPool<T>::InitializeMemoryPool( tgUInt32 BlockSize /*= 32*/ )
  66. {
  67. m_BlockSize = BlockSize;
  68.  
  69. // Allocate an initial block.
  70. AllocateResourceBlock();
  71.  
  72. return TRUE;
  73. }
  74.  
  75.  
  76. template <class T>
  77. tgBool TMemoryPool<T>::DestroyMemoryPool()
  78. {
  79. MemoryPool::iterator it = m_Pool.begin();
  80. while (it != m_Pool.end())
  81. {
  82. tgDelete (*it);
  83. it = m_Pool.erase(it);
  84. }
  85.  
  86. // Destroy all allocated resources.
  87. //m_Pool.clear();
  88.  
  89. return TRUE;
  90. }
  91.  
  92.  
  93. template <class T>
  94. T* TMemoryPool<T>::AcquireResource()
  95. {
  96. MemoryPool::iterator it = m_Pool.begin();
  97. while (it != m_Pool.end())
  98. {
  99. T& Handle = (*it);
  100. if (Handle.Used == FALSE)
  101. {
  102. Handle.Used = TRUE;
  103. return Handle.Resource;
  104. }
  105. }
  106.  
  107. // If we got this far, allocate more memory.
  108.  
  109. // Store the current size of our pool; this is where our newly allocated
  110. // memory block will start.
  111. const tgUInt32 NewBlockStart = m_Pool.size();
  112.  
  113. // Allocate the new block.
  114. AllocateResourceBlock();
  115.  
  116. // Retrieve the most recent resource handle.
  117. T& Handle = m_Pool[NewBlockStart];
  118.  
  119. // Mark as used, return resouce.
  120. Handle.Used = TRUE;
  121. return Handle.Resource;
  122. }
  123.  
  124.  
  125. template <class T>
  126. tgBool TMemoryPool<T>::DropResource( T* Resource )
  127. {
  128. MemoryPool::iterator it = m_Pool.begin();
  129. while (it != m_Pool.end())
  130. {
  131. T& Handle = (*it);
  132. if (Handle.Resource == Resource)
  133. {
  134. Handle.Used = FALSE;
  135. return TRUE;
  136. }
  137. }
  138.  
  139. return FALSE;
  140. }
  141.  
  142.  
  143. template <class T>
  144. void TMemoryPool<T>::AllocateResourceBlock()
  145. {
  146. // Reserve space in our vector.
  147. m_Pool.reserve(m_Pool.size() + m_BlockSize);
  148.  
  149. // Allocate new resources.
  150. for (tgUInt32 i = 0; i < m_BlockSize; ++i)
  151. {
  152. m_Pool.push_back(tgNew TMemoryPoolHandle<T>);
  153. }
  154.  
  155. #ifdef _DEBUG
  156. tgLogPrint("MemoryPool ::: New block allocated - total size is %u", m_Pool.size());
  157. #endif
  158. }
  159.  
  160.  
  161. #endif // TMemoryPool_h__
Add Comment
Please, Sign In to add comment