mempool.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. ** Command & Conquer Generals(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:: 6/06/01 11:04a $*
  29. * *
  30. * $Revision:: 7 $*
  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 <stdlib.h>
  51. #include <stddef.h>
  52. /**********************************************************************************************
  53. ** ObjectPoolClass
  54. **
  55. ** This class is designed to allocate blocks of objects of type T and then dole them out
  56. ** to you individually. The motivation for it is the situation where you are allocating
  57. ** and freeing lots of little objects directly off the heap. Through the use of this
  58. ** class, far fewer allocations will be actually made.
  59. **
  60. ** Example Usage:
  61. **
  62. ** ObjectPoolClass<ListNodeClass,256> NodePool;
  63. ** ListNodeClass * node = NodePool.Allocate_Object();
  64. ** NodePool.Free_Object(node);
  65. **
  66. **********************************************************************************************/
  67. template<class T,int BLOCK_SIZE = 64>
  68. class ObjectPoolClass
  69. {
  70. public:
  71. ObjectPoolClass(void);
  72. ~ObjectPoolClass(void);
  73. T * Allocate_Object(void);
  74. void Free_Object(T * obj);
  75. T * Allocate_Object_Memory(void);
  76. void Free_Object_Memory(T * obj);
  77. protected:
  78. T * FreeListHead;
  79. uint32 * BlockListHead;
  80. int FreeObjectCount;
  81. int TotalObjectCount;
  82. };
  83. /**********************************************************************************************
  84. ** AutoPoolClass
  85. **
  86. ** This class is designed to be derived from in order to give your class built-in
  87. ** object pool behavior. The new and delete operators for your class will call
  88. ** to the internal ObjectPoolClass for fast allocation and de-allocation. This
  89. ** is very well suited to being the base class for a list node class for example.
  90. **
  91. ** Notes:
  92. ** - The array forms of new and delete are not supported
  93. ** - You must define the instance of the static object pool (Allocator)
  94. ** - You can't derive a class from a class that is derived from AutoPoolClass
  95. ** because its size won't match but it will try to use the same pool...
  96. **
  97. ** Example Usage:
  98. ** --------------
  99. **
  100. ** ListNode.h:
  101. ** class ListNodeClass : public AutoPoolClass<ListNodeClass,256>
  102. ** {
  103. ** ListNodeClass * Next;
  104. ** void * Data;
  105. ** };
  106. **
  107. ** ListNode.cpp:
  108. ** DEFINE_AUTO_POOL(ListNodeClass);
  109. **
  110. ** function do_stuff(void) {
  111. ** ListNodeClass * node = new ListNodeClass;
  112. ** delete node;
  113. ** }
  114. **
  115. **********************************************************************************************/
  116. template<class T, int BLOCK_SIZE = 64>
  117. class AutoPoolClass
  118. {
  119. public:
  120. static void * operator new(size_t size);
  121. static void operator delete(void * memory);
  122. private:
  123. // not implemented
  124. static void * operator new [] (size_t size);
  125. static void operator delete[] (void * memory);
  126. // This must be staticly declared by user
  127. static ObjectPoolClass<T,BLOCK_SIZE> Allocator;
  128. };
  129. /*
  130. ** DEFINE_AUTO_POOL(T,BLOCKSIZE)
  131. ** Macro to declare the allocator for your class. Put this in the cpp file for
  132. ** the class.
  133. */
  134. #define DEFINE_AUTO_POOL(T,BLOCKSIZE) \
  135. ObjectPoolClass<T,BLOCKSIZE> AutoPoolClass<T,BLOCKSIZE>::Allocator
  136. /***********************************************************************************************
  137. * ObjectPoolClass::ObjectPoolClass -- constructor for ObjectPoolClass *
  138. * *
  139. * Initializes the object pool to the empty state *
  140. * *
  141. * INPUT: *
  142. * *
  143. * OUTPUT: *
  144. * *
  145. * WARNINGS: *
  146. * *
  147. * HISTORY: *
  148. *=============================================================================================*/
  149. template<class T,int BLOCK_SIZE>
  150. ObjectPoolClass<T,BLOCK_SIZE>::ObjectPoolClass(void) :
  151. FreeListHead(NULL),
  152. BlockListHead(NULL),
  153. FreeObjectCount(0),
  154. TotalObjectCount(0)
  155. {
  156. }
  157. /***********************************************************************************************
  158. * ObjectPoolClass::~ObjectPoolClass -- destructor for ObjectPoolClass *
  159. * *
  160. * deletes the blocks of memory in use by the object pool. *
  161. * *
  162. * INPUT: *
  163. * *
  164. * OUTPUT: *
  165. * *
  166. * WARNINGS: *
  167. * *
  168. * HISTORY: *
  169. *=============================================================================================*/
  170. template<class T,int BLOCK_SIZE>
  171. ObjectPoolClass<T,BLOCK_SIZE>::~ObjectPoolClass(void)
  172. {
  173. // assert that the user gave back all of the memory he was using
  174. WWASSERT(FreeObjectCount == TotalObjectCount);
  175. // delete all of the blocks we allocated
  176. int block_count = 0;
  177. while (BlockListHead != NULL) {
  178. uint32 * next_block = *(uint32 **)BlockListHead;
  179. ::operator delete(BlockListHead);
  180. BlockListHead = next_block;
  181. block_count++;
  182. }
  183. WWASSERT(block_count == TotalObjectCount / BLOCK_SIZE);
  184. }
  185. /***********************************************************************************************
  186. * ObjectPoolClass::Allocate_Object -- allocates an object for the user *
  187. * *
  188. * If there are no free objects, another block of objects will be allocated. *
  189. * *
  190. * INPUT: *
  191. * *
  192. * OUTPUT: *
  193. * *
  194. * WARNINGS: *
  195. * *
  196. * HISTORY: *
  197. * 7/29/99 GTH : Created. *
  198. *=============================================================================================*/
  199. template<class T,int BLOCK_SIZE>
  200. T * ObjectPoolClass<T,BLOCK_SIZE>::Allocate_Object(void)
  201. {
  202. // allocate memory for the object
  203. T * obj = Allocate_Object_Memory();
  204. // construct the object in-place
  205. return new (obj) T;
  206. }
  207. /***********************************************************************************************
  208. * ObjectPoolClass::Free_Object -- releases obj back into the pool *
  209. * *
  210. * INPUT: *
  211. * *
  212. * OUTPUT: *
  213. * *
  214. * WARNINGS: *
  215. * *
  216. * HISTORY: *
  217. * 7/29/99 GTH : Created. *
  218. *=============================================================================================*/
  219. template<class T,int BLOCK_SIZE>
  220. void ObjectPoolClass<T,BLOCK_SIZE>::Free_Object(T * obj)
  221. {
  222. // destruct the object
  223. obj->T::~T();
  224. // release the memory
  225. Free_Object_Memory(obj);
  226. }
  227. /***********************************************************************************************
  228. * ObjectPoolClass::Allocate_Object_Memory -- internal function which returns memory for an in *
  229. * *
  230. * INPUT: *
  231. * *
  232. * OUTPUT: *
  233. * *
  234. * WARNINGS: *
  235. * *
  236. * HISTORY: *
  237. * 7/29/99 GTH : Created. *
  238. *=============================================================================================*/
  239. template<class T,int BLOCK_SIZE>
  240. T * ObjectPoolClass<T,BLOCK_SIZE>::Allocate_Object_Memory(void)
  241. {
  242. if ( FreeListHead == 0 ) {
  243. // No free objects, allocate another block
  244. uint32 * tmp_block_head = BlockListHead;
  245. BlockListHead = (uint32*)::operator new( sizeof(T) * BLOCK_SIZE + sizeof(uint32 *));
  246. // Link this block into the block list
  247. *(void **)BlockListHead = tmp_block_head;
  248. // Link the objects in the block into the free object list
  249. FreeListHead = (T*)(BlockListHead + 1);
  250. for ( int i = 0; i < BLOCK_SIZE; i++ ) {
  251. *(T**)(&(FreeListHead[i])) = &(FreeListHead[i+1]); // link up the elements
  252. }
  253. *(T**)(&(FreeListHead[BLOCK_SIZE-1])) = 0; // Mark the end
  254. FreeObjectCount += BLOCK_SIZE;
  255. TotalObjectCount += BLOCK_SIZE;
  256. }
  257. T * obj = FreeListHead; // Get the next free object
  258. FreeListHead = *(T**)(FreeListHead); // Bump the Head
  259. FreeObjectCount--;
  260. return obj;
  261. }
  262. /***********************************************************************************************
  263. * ObjectPoolClass::Free_Object_Memory -- internal function, returns object's memory to the po *
  264. * *
  265. * INPUT: *
  266. * *
  267. * OUTPUT: *
  268. * *
  269. * WARNINGS: *
  270. * *
  271. * HISTORY: *
  272. * 7/29/99 GTH : Created. *
  273. *=============================================================================================*/
  274. template<class T,int BLOCK_SIZE>
  275. void ObjectPoolClass<T,BLOCK_SIZE>::Free_Object_Memory(T * obj)
  276. {
  277. WWASSERT(obj != NULL);
  278. *(T**)(obj) = FreeListHead; // Link to the Head
  279. FreeListHead = obj; // Set the Head
  280. FreeObjectCount++;
  281. }
  282. /***********************************************************************************************
  283. * AutoPoolClass::operator new -- overriden new which calls the internal ObjectPool *
  284. * *
  285. * INPUT: *
  286. * *
  287. * OUTPUT: *
  288. * *
  289. * WARNINGS: *
  290. * *
  291. * HISTORY: *
  292. * 7/29/99 GTH : Created. *
  293. *=============================================================================================*/
  294. template<class T, int BLOCK_SIZE>
  295. void * AutoPoolClass<T,BLOCK_SIZE>::operator new( size_t size )
  296. {
  297. WWASSERT(size == sizeof(T));
  298. return (void *)(Allocator.Allocate_Object_Memory());
  299. }
  300. /***********************************************************************************************
  301. * AutoPoolClass::operator delete -- overriden delete which calls the internal ObjectPool *
  302. * *
  303. * INPUT: *
  304. * *
  305. * OUTPUT: *
  306. * *
  307. * WARNINGS: *
  308. * *
  309. * HISTORY: *
  310. * 7/29/99 GTH : Created. *
  311. *=============================================================================================*/
  312. template<class T, int BLOCK_SIZE>
  313. void AutoPoolClass<T,BLOCK_SIZE>::operator delete( void * memory )
  314. {
  315. if ( memory == 0 ) return;
  316. Allocator.Free_Object_Memory((T*)memory);
  317. }
  318. #endif // MEMPOOL_H