mempool.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : WWLib *
  23. * *
  24. * $Archive:: /Commando/Code/wwlib/mempool.h $*
  25. * *
  26. * Author:: Greg Hjelstrom *
  27. * *
  28. * $Modtime:: 9/26/01 3:11p $*
  29. * *
  30. * $Revision:: 9 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * ObjectPoolClass::ObjectPoolClass -- constructor for ObjectPoolClass *
  35. * ObjectPoolClass::~ObjectPoolClass -- destructor for ObjectPoolClass *
  36. * ObjectPoolClass::Allocate_Object -- allocates an object for the user *
  37. * ObjectPoolClass::Free_Object -- releases obj back into the pool *
  38. * ObjectPoolClass::Allocate_Object_Memory -- internal function which returns memory for an *
  39. * ObjectPoolClass::Free_Object_Memory -- internal function, returns object's memory to the *
  40. * AutoPoolClass::operator new -- overriden new which calls the internal ObjectPool *
  41. * AutoPoolClass::operator delete -- overriden delete which calls the internal ObjectPool *
  42. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  43. #if defined(_MSC_VER)
  44. #pragma once
  45. #endif
  46. #ifndef MEMPOOL_H
  47. #define MEMPOOL_H
  48. #include "bittype.h"
  49. #include "wwdebug.h"
  50. #include "mutex.h"
  51. #include <new.h>
  52. #include <stdlib.h>
  53. #include <stddef.h>
  54. /**********************************************************************************************
  55. ** ObjectPoolClass
  56. **
  57. ** This class is designed to allocate blocks of objects of type T and then dole them out
  58. ** to you individually. The motivation for it is the situation where you are allocating
  59. ** and freeing lots of little objects directly off the heap. Through the use of this
  60. ** class, far fewer allocations will be actually made.
  61. **
  62. ** Example Usage:
  63. **
  64. ** ObjectPoolClass<ListNodeClass,256> NodePool;
  65. ** ListNodeClass * node = NodePool.Allocate_Object();
  66. ** NodePool.Free_Object(node);
  67. **
  68. **********************************************************************************************/
  69. template<class T,int BLOCK_SIZE = 64>
  70. class ObjectPoolClass
  71. {
  72. public:
  73. ObjectPoolClass(void);
  74. ~ObjectPoolClass(void);
  75. T * Allocate_Object(void);
  76. void Free_Object(T * obj);
  77. T * Allocate_Object_Memory(void);
  78. void Free_Object_Memory(T * obj);
  79. protected:
  80. T * FreeListHead;
  81. uint32 * BlockListHead;
  82. int FreeObjectCount;
  83. int TotalObjectCount;
  84. FastCriticalSectionClass ObjectPoolCS;
  85. };
  86. /**********************************************************************************************
  87. ** AutoPoolClass
  88. **
  89. ** This class is designed to be derived from in order to give your class built-in
  90. ** object pool behavior. The new and delete operators for your class will call
  91. ** to the internal ObjectPoolClass for fast allocation and de-allocation. This
  92. ** is very well suited to being the base class for a list node class for example.
  93. **
  94. ** Notes:
  95. ** - The array forms of new and delete are not supported
  96. ** - You must define the instance of the static object pool (Allocator)
  97. ** - You can't derive a class from a class that is derived from AutoPoolClass
  98. ** because its size won't match but it will try to use the same pool...
  99. **
  100. ** Example Usage:
  101. ** --------------
  102. **
  103. ** ListNode.h:
  104. ** class ListNodeClass : public AutoPoolClass<ListNodeClass,256>
  105. ** {
  106. ** ListNodeClass * Next;
  107. ** void * Data;
  108. ** };
  109. **
  110. ** ListNode.cpp:
  111. ** DEFINE_AUTO_POOL(ListNodeClass);
  112. **
  113. ** function do_stuff(void) {
  114. ** ListNodeClass * node = new ListNodeClass;
  115. ** delete node;
  116. ** }
  117. **
  118. **********************************************************************************************/
  119. template<class T, int BLOCK_SIZE = 64>
  120. class AutoPoolClass
  121. {
  122. public:
  123. static void * operator new(size_t size);
  124. static void operator delete(void * memory);
  125. private:
  126. // not implemented
  127. static void * operator new [] (size_t size);
  128. static void operator delete[] (void * memory);
  129. // This must be staticly declared by user
  130. static ObjectPoolClass<T,BLOCK_SIZE> Allocator;
  131. };
  132. /*
  133. ** DEFINE_AUTO_POOL(T,BLOCKSIZE)
  134. ** Macro to declare the allocator for your class. Put this in the cpp file for
  135. ** the class.
  136. */
  137. #define DEFINE_AUTO_POOL(T,BLOCKSIZE) \
  138. ObjectPoolClass<T,BLOCKSIZE> AutoPoolClass<T,BLOCKSIZE>::Allocator;
  139. /***********************************************************************************************
  140. * ObjectPoolClass::ObjectPoolClass -- constructor for ObjectPoolClass *
  141. * *
  142. * Initializes the object pool to the empty state *
  143. * *
  144. * INPUT: *
  145. * *
  146. * OUTPUT: *
  147. * *
  148. * WARNINGS: *
  149. * *
  150. * HISTORY: *
  151. *=============================================================================================*/
  152. template<class T,int BLOCK_SIZE>
  153. ObjectPoolClass<T,BLOCK_SIZE>::ObjectPoolClass(void) :
  154. FreeListHead(NULL),
  155. BlockListHead(NULL),
  156. FreeObjectCount(0),
  157. TotalObjectCount(0)
  158. {
  159. }
  160. /***********************************************************************************************
  161. * ObjectPoolClass::~ObjectPoolClass -- destructor for ObjectPoolClass *
  162. * *
  163. * deletes the blocks of memory in use by the object pool. *
  164. * *
  165. * INPUT: *
  166. * *
  167. * OUTPUT: *
  168. * *
  169. * WARNINGS: *
  170. * *
  171. * HISTORY: *
  172. *=============================================================================================*/
  173. template<class T,int BLOCK_SIZE>
  174. ObjectPoolClass<T,BLOCK_SIZE>::~ObjectPoolClass(void)
  175. {
  176. // assert that the user gave back all of the memory he was using
  177. WWASSERT(FreeObjectCount == TotalObjectCount);
  178. // delete all of the blocks we allocated
  179. int block_count = 0;
  180. while (BlockListHead != NULL) {
  181. uint32 * next_block = *(uint32 **)BlockListHead;
  182. ::operator delete(BlockListHead);
  183. BlockListHead = next_block;
  184. block_count++;
  185. }
  186. WWASSERT(block_count == TotalObjectCount / BLOCK_SIZE);
  187. }
  188. /***********************************************************************************************
  189. * ObjectPoolClass::Allocate_Object -- allocates an object for the user *
  190. * *
  191. * If there are no free objects, another block of objects will be allocated. *
  192. * *
  193. * INPUT: *
  194. * *
  195. * OUTPUT: *
  196. * *
  197. * WARNINGS: *
  198. * *
  199. * HISTORY: *
  200. * 7/29/99 GTH : Created. *
  201. *=============================================================================================*/
  202. template<class T,int BLOCK_SIZE>
  203. T * ObjectPoolClass<T,BLOCK_SIZE>::Allocate_Object(void)
  204. {
  205. // allocate memory for the object
  206. T * obj = Allocate_Object_Memory();
  207. // construct the object in-place
  208. return new (obj) T;
  209. }
  210. /***********************************************************************************************
  211. * ObjectPoolClass::Free_Object -- releases obj back into the pool *
  212. * *
  213. * INPUT: *
  214. * *
  215. * OUTPUT: *
  216. * *
  217. * WARNINGS: *
  218. * *
  219. * HISTORY: *
  220. * 7/29/99 GTH : Created. *
  221. *=============================================================================================*/
  222. template<class T,int BLOCK_SIZE>
  223. void ObjectPoolClass<T,BLOCK_SIZE>::Free_Object(T * obj)
  224. {
  225. // destruct the object
  226. obj->T::~T();
  227. // release the memory
  228. Free_Object_Memory(obj);
  229. }
  230. /***********************************************************************************************
  231. * ObjectPoolClass::Allocate_Object_Memory -- internal function which returns memory for an in *
  232. * *
  233. * INPUT: *
  234. * *
  235. * OUTPUT: *
  236. * *
  237. * WARNINGS: *
  238. * *
  239. * HISTORY: *
  240. * 7/29/99 GTH : Created. *
  241. *=============================================================================================*/
  242. template<class T,int BLOCK_SIZE>
  243. T * ObjectPoolClass<T,BLOCK_SIZE>::Allocate_Object_Memory(void)
  244. {
  245. FastCriticalSectionClass::LockClass lock(ObjectPoolCS);
  246. if ( FreeListHead == 0 ) {
  247. // No free objects, allocate another block
  248. uint32 * tmp_block_head = BlockListHead;
  249. BlockListHead = (uint32*)::operator new( sizeof(T) * BLOCK_SIZE + sizeof(uint32 *));
  250. // Link this block into the block list
  251. *(void **)BlockListHead = tmp_block_head;
  252. // Link the objects in the block into the free object list
  253. FreeListHead = (T*)(BlockListHead + 1);
  254. for ( int i = 0; i < BLOCK_SIZE; i++ ) {
  255. *(T**)(&(FreeListHead[i])) = &(FreeListHead[i+1]); // link up the elements
  256. }
  257. *(T**)(&(FreeListHead[BLOCK_SIZE-1])) = 0; // Mark the end
  258. FreeObjectCount += BLOCK_SIZE;
  259. TotalObjectCount += BLOCK_SIZE;
  260. }
  261. T * obj = FreeListHead; // Get the next free object
  262. FreeListHead = *(T**)(FreeListHead); // Bump the Head
  263. FreeObjectCount--;
  264. return obj;
  265. }
  266. /***********************************************************************************************
  267. * ObjectPoolClass::Free_Object_Memory -- internal function, returns object's memory to the po *
  268. * *
  269. * INPUT: *
  270. * *
  271. * OUTPUT: *
  272. * *
  273. * WARNINGS: *
  274. * *
  275. * HISTORY: *
  276. * 7/29/99 GTH : Created. *
  277. *=============================================================================================*/
  278. template<class T,int BLOCK_SIZE>
  279. void ObjectPoolClass<T,BLOCK_SIZE>::Free_Object_Memory(T * obj)
  280. {
  281. FastCriticalSectionClass::LockClass lock(ObjectPoolCS);
  282. WWASSERT(obj != NULL);
  283. *(T**)(obj) = FreeListHead; // Link to the Head
  284. FreeListHead = obj; // Set the Head
  285. FreeObjectCount++;
  286. }
  287. /***********************************************************************************************
  288. * AutoPoolClass::operator new -- overriden new which calls the internal ObjectPool *
  289. * *
  290. * INPUT: *
  291. * *
  292. * OUTPUT: *
  293. * *
  294. * WARNINGS: *
  295. * *
  296. * HISTORY: *
  297. * 7/29/99 GTH : Created. *
  298. *=============================================================================================*/
  299. template<class T, int BLOCK_SIZE>
  300. void * AutoPoolClass<T,BLOCK_SIZE>::operator new( size_t size )
  301. {
  302. WWASSERT(size == sizeof(T));
  303. return (void *)(Allocator.Allocate_Object_Memory());
  304. }
  305. /***********************************************************************************************
  306. * AutoPoolClass::operator delete -- overriden delete which calls the internal ObjectPool *
  307. * *
  308. * INPUT: *
  309. * *
  310. * OUTPUT: *
  311. * *
  312. * WARNINGS: *
  313. * *
  314. * HISTORY: *
  315. * 7/29/99 GTH : Created. *
  316. *=============================================================================================*/
  317. template<class T, int BLOCK_SIZE>
  318. void AutoPoolClass<T,BLOCK_SIZE>::operator delete( void * memory )
  319. {
  320. if ( memory == 0 ) return;
  321. Allocator.Free_Object_Memory((T*)memory);
  322. }
  323. #endif // MEMPOOL_H