HEAP.CPP 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. /*
  2. ** Command & Conquer(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. /* $Header: F:\projects\c&c\vcs\code\heap.cpv 2.18 16 Oct 1995 16:49:56 JOE_BOSTIC $ */
  19. /***********************************************************************************************
  20. *** 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 ***
  21. ***********************************************************************************************
  22. * *
  23. * Project Name : Command & Conquer *
  24. * *
  25. * File Name : HEAP.CPP *
  26. * *
  27. * Programmer : Joe L. Bostic *
  28. * *
  29. * Start Date : 02/18/95 *
  30. * *
  31. * Last Update : May 22, 1995 [JLB] *
  32. * *
  33. *---------------------------------------------------------------------------------------------*
  34. * Functions: *
  35. * FixedIHeapClass::Free -- Frees an object in the heap. *
  36. * FixedHeapClass::FixedHeapClass -- Normal constructor for heap management class. *
  37. * FixedHeapClass::~FixedHeapClass -- Destructor for the heap manager class. *
  38. * FixedHeapClass::Set_Heap -- Assigns a memory block for this heap manager. *
  39. * FixedHeapClass::Allocate -- Allocate a sub-block from the heap. *
  40. * FixedHeapClass::Free -- Frees a sub-block in the heap. *
  41. * FixedHeapClass::ID -- Converts a pointer to a sub-block index number. *
  42. * FixedHeapClass::Clear -- Clears (and frees) the heap manager memory. *
  43. * TFixedIHeapClass::Save -- Saves all active objects *
  44. * TFixedIHeapClass::Load -- Loads all active objects *
  45. * TFixedIHeapClass::Code_Pointers -- codes pointers for every object, to prepare for save *
  46. * TFixedIHeapClass::Decode_Pointers -- Decodes all object pointers, for after loading *
  47. * FixedHeapClass::Free_All -- Frees all objects in the fixed heap. *
  48. * FixedIHeapClass::Free_All -- Frees all objects out of the indexed heap. *
  49. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  50. #include "function.h"
  51. #include "heap.h"
  52. #include <mem.h>
  53. #include <stdio.h>
  54. #include <stddef.h>
  55. #include <conio.h>
  56. #include <string.h>
  57. /***********************************************************************************************
  58. * FixedHeapClass::FixedHeapClass -- Normal constructor for heap management class. *
  59. * *
  60. * This is the normal constructor used for the heap manager class. This initializes *
  61. * the class but doesn't yet assign actual heap memory to this manager. That is handled *
  62. * by the Set_Heap() function. *
  63. * *
  64. * INPUT: size -- The size of the individual sub-blocks in this heap. This value is *
  65. * typically the size of some class or structure. *
  66. * *
  67. * OUTPUT: none *
  68. * *
  69. * WARNINGS: The heap must first be assigned a block of memory to manage before it can *
  70. * be used. *
  71. * *
  72. * HISTORY: *
  73. * 02/21/1995 JLB : Created. *
  74. *=============================================================================================*/
  75. FixedHeapClass::FixedHeapClass(int size)
  76. {
  77. Size = size;
  78. Buffer = 0;
  79. IsAllocated = false;
  80. TotalCount = 0;
  81. ActiveCount = 0;
  82. }
  83. /***********************************************************************************************
  84. * FixedHeapClass::~FixedHeapClass -- Destructor for the heap manager class. *
  85. * *
  86. * This is the default constructor for the heap manager class. It handles freeing the *
  87. * memory assigned to this heap. *
  88. * *
  89. * INPUT: none *
  90. * *
  91. * OUTPUT: none *
  92. * *
  93. * WARNINGS: none *
  94. * *
  95. * HISTORY: *
  96. * 02/21/1995 JLB : Created. *
  97. *=============================================================================================*/
  98. FixedHeapClass::~FixedHeapClass(void)
  99. {
  100. FixedHeapClass::Clear();
  101. }
  102. /***********************************************************************************************
  103. * FixedHeapClass::Set_Heap -- Assigns a memory block for this heap manager. *
  104. * *
  105. * This routine is used to assign a memory heap to this object. A memory heap so assigned *
  106. * will start with all sub-blocks unallocated. After this routine is called, normal *
  107. * allocation and freeing may occur. This routine will allocate necessary memory if the *
  108. * buffer parameter is NULL. *
  109. * *
  110. * INPUT: count -- The number of objects that this heap should manage. *
  111. * *
  112. * buffer -- Pointer to pre-allocated buffer that this manager will use. If this *
  113. * parameter is NULL, then memory will be automatically allocated. *
  114. * *
  115. * OUTPUT: bool; Was the heap successfully initialized? *
  116. * *
  117. * WARNINGS: none *
  118. * *
  119. * HISTORY: *
  120. * 02/21/1995 JLB : Created. *
  121. *=============================================================================================*/
  122. int FixedHeapClass::Set_Heap(int count, void * buffer)
  123. {
  124. /*
  125. ** Clear out the old heap data.
  126. */
  127. Clear();
  128. /*
  129. ** If there is no size to the objects in the heap, then this block memory
  130. ** handler can NEVER function. Return with a failure condition.
  131. */
  132. if (!Size) return(false);
  133. /*
  134. ** If there is no count specified, then this indicates that the heap should
  135. ** be disabled.
  136. */
  137. if (!count) return(true);
  138. /*
  139. ** Initialize the free boolean vector and the buffer for the actual
  140. ** allocation objects.
  141. */
  142. if (FreeFlag.Resize(count)) {
  143. if (!buffer) {
  144. buffer = new char[count * Size];
  145. if (!buffer) {
  146. FreeFlag.Clear();
  147. return(false);
  148. }
  149. IsAllocated = true;
  150. }
  151. Buffer = buffer;
  152. TotalCount = count;
  153. return(true);
  154. }
  155. return(false);
  156. }
  157. /***********************************************************************************************
  158. * FixedHeapClass::Allocate -- Allocate a sub-block from the heap. *
  159. * *
  160. * Finds the first available sub-block in the heap and returns a pointer to it. The sub- *
  161. * block is marked as allocated by this routine. If there are no more sub-blocks *
  162. * available, then this routine will return NULL. *
  163. * *
  164. * INPUT: none *
  165. * *
  166. * OUTPUT: Returns with a pointer to the newly allocated sub-block. *
  167. * *
  168. * WARNINGS: none *
  169. * *
  170. * HISTORY: *
  171. * 02/21/1995 JLB : Created. *
  172. *=============================================================================================*/
  173. void * FixedHeapClass::Allocate(void)
  174. {
  175. if (ActiveCount < TotalCount) {
  176. int index = FreeFlag.First_False();
  177. if (index != -1) {
  178. ActiveCount++;
  179. FreeFlag[index] = true;
  180. return((*this)[index]);
  181. }
  182. }
  183. return(0);
  184. }
  185. /***********************************************************************************************
  186. * FixedHeapClass::Free -- Frees a sub-block in the heap. *
  187. * *
  188. * Use this routine to free a previously allocated sub-block in the heap. *
  189. * *
  190. * INPUT: pointer -- A pointer to the sub-block to free. This is the same pointer that *
  191. * was returned from the Allocate() function. *
  192. * *
  193. * OUTPUT: bool; Was the deallocation successful? Failure could indicate a pointer that *
  194. * doesn't refer to this heap or a null pointer. *
  195. * *
  196. * WARNINGS: none *
  197. * *
  198. * HISTORY: *
  199. * 02/21/1995 JLB : Created. *
  200. *=============================================================================================*/
  201. int FixedHeapClass::Free(void * pointer)
  202. {
  203. if (pointer && ActiveCount) {
  204. int index = ID(pointer);
  205. if ((unsigned)index < TotalCount) {
  206. if (FreeFlag[index]) {
  207. ActiveCount--;
  208. FreeFlag[index] = false;
  209. return(true);
  210. }
  211. }
  212. }
  213. return(false);
  214. }
  215. /***********************************************************************************************
  216. * FixedHeapClass::ID -- Converts a pointer to a sub-block index number. *
  217. * *
  218. * Use this routine to convert a pointer (returned by Allocate) into the sub-block *
  219. * index number. This index number can be used as a form of identifier for the block. *
  220. * *
  221. * INPUT: pointer -- A pointer to the sub-block to conver into an ID number. *
  222. * *
  223. * OUTPUT: Returns with the index (ID) number for the sub-block specified. This number will *
  224. * range between 0 and the sub-block max -1. If -1 is returned, then the pointer *
  225. * was invalid. *
  226. * *
  227. * WARNINGS: none *
  228. * *
  229. * HISTORY: *
  230. * 02/21/1995 JLB : Created. *
  231. *=============================================================================================*/
  232. int FixedHeapClass::ID(void const * pointer)
  233. {
  234. if (pointer && Size) {
  235. return((int)(((char *)pointer - (char *)Buffer) / Size));
  236. }
  237. return(-1);
  238. }
  239. /***********************************************************************************************
  240. * FixedHeapClass::Clear -- Clears (and frees) the heap manager memory. *
  241. * *
  242. * This routine is used to bring the heap manager back into a non-functioning state. All *
  243. * memory allocated by this manager is freeed. Any previous pointers to allocated blocks *
  244. * from this heap are now invalid. *
  245. * *
  246. * INPUT: none *
  247. * *
  248. * OUTPUT: none *
  249. * *
  250. * WARNINGS: none *
  251. * *
  252. * HISTORY: *
  253. * 02/21/1995 JLB : Created. *
  254. *=============================================================================================*/
  255. void FixedHeapClass::Clear(void)
  256. {
  257. /*
  258. ** Free the old buffer (if present).
  259. */
  260. if (Buffer && IsAllocated) {
  261. delete[] Buffer;
  262. }
  263. Buffer = 0;
  264. IsAllocated = false;
  265. ActiveCount = 0;
  266. TotalCount = 0;
  267. FreeFlag.Clear();
  268. }
  269. /***********************************************************************************************
  270. * FixedHeapClass::Free_All -- Frees all objects in the fixed heap. *
  271. * *
  272. * This routine will free all previously allocated objects out of the heap. Use this *
  273. * routine to ensure that the heap is empty. *
  274. * *
  275. * INPUT: none *
  276. * *
  277. * OUTPUT: Was the heap successfully cleared of all objects? *
  278. * *
  279. * WARNINGS: none *
  280. * *
  281. * HISTORY: *
  282. * 05/22/1995 JLB : Created. *
  283. *=============================================================================================*/
  284. int FixedHeapClass::Free_All(void)
  285. {
  286. ActiveCount = 0;
  287. FreeFlag.Reset();
  288. return(true);
  289. }
  290. /////////////////////////////////////////////////////////////////////
  291. /***********************************************************************************************
  292. * FixedIHeapClass::Free_All -- Frees all objects out of the indexed heap. *
  293. * *
  294. * Use this routine to free all previously allocated objects in the heap. This routine will *
  295. * also clear out the allocated object vector as well. *
  296. * *
  297. * INPUT: none *
  298. * *
  299. * OUTPUT: Was the heap successfully cleared of objects? *
  300. * *
  301. * WARNINGS: none *
  302. * *
  303. * HISTORY: *
  304. * 05/22/1995 JLB : Created. *
  305. *=============================================================================================*/
  306. int FixedIHeapClass::Free_All(void)
  307. {
  308. ActivePointers.Delete_All();
  309. return(FixedHeapClass::Free_All());
  310. }
  311. void FixedIHeapClass::Clear(void)
  312. {
  313. FixedHeapClass::Clear();
  314. ActivePointers.Clear();
  315. }
  316. int FixedIHeapClass::Set_Heap(int count, void * buffer)
  317. {
  318. Clear();
  319. if (FixedHeapClass::Set_Heap(count, buffer)) {
  320. ActivePointers.Resize(count);
  321. return(true);
  322. }
  323. return(false);
  324. }
  325. void * FixedIHeapClass::Allocate(void)
  326. {
  327. void * ptr = FixedHeapClass::Allocate();
  328. if (ptr) {
  329. ActivePointers.Add(ptr);
  330. memset (ptr, 0, Size);
  331. }
  332. return(ptr);
  333. }
  334. /***********************************************************************************************
  335. * FixedIHeapClass::Free -- Frees an object in the heap. *
  336. * *
  337. * This routine is used to free an object in the heap. Freeing is accomplished by marking *
  338. * the object's memory as free to be reallocated. The object is also removed from the *
  339. * allocated object pointer vector. *
  340. * *
  341. * INPUT: pointer -- Pointer to the object that is to be removed from the heap. *
  342. * *
  343. * OUTPUT: none *
  344. * *
  345. * WARNINGS: none *
  346. * *
  347. * HISTORY: *
  348. * 02/21/1995 JLB : Created. *
  349. *=============================================================================================*/
  350. int FixedIHeapClass::Free(void * pointer)
  351. {
  352. if (FixedHeapClass::Free(pointer)) {
  353. ActivePointers.Delete(pointer);
  354. }
  355. return(false);
  356. }
  357. /***********************************************************************************************
  358. * TFixedIHeapClass::Save -- Saves all active objects *
  359. * *
  360. * INPUT: file file to write to *
  361. * *
  362. * OUTPUT: true = OK, false = error *
  363. * *
  364. * WARNINGS: none *
  365. * *
  366. * HISTORY: *
  367. * 03/15/1995 BRR : Created. *
  368. *=============================================================================================*/
  369. template<class T>
  370. int TFixedIHeapClass<T>::Save(FileClass &file)
  371. {
  372. int i; // loop counter
  373. int idx; // object index
  374. /*
  375. ** Save the number of instances of this class
  376. */
  377. if (file.Write(&ActiveCount, sizeof(ActiveCount)) != sizeof(ActiveCount)) {
  378. return(false);
  379. }
  380. /*
  381. ** Save each instance of this class
  382. */
  383. for (i = 0; i < ActiveCount; i++) {
  384. /*
  385. ** Save the array index of the object, so it can be loaded back into the
  386. ** same array location (so TARGET translations will work)
  387. */
  388. idx = ID(Ptr(i));
  389. if (file.Write(&idx, sizeof(idx)) != sizeof(idx)) {
  390. return(false);
  391. }
  392. /*
  393. ** Save the object itself
  394. */
  395. if (!Ptr(i)->Save(file)) {
  396. return(false);
  397. }
  398. }
  399. return(true);
  400. }
  401. /***********************************************************************************************
  402. * TFixedIHeapClass::Load -- Loads all active objects *
  403. * *
  404. * INPUT: file file to read from *
  405. * *
  406. * OUTPUT: true = OK, false = error *
  407. * *
  408. * WARNINGS: none *
  409. * *
  410. * HISTORY: *
  411. * 03/15/1995 BRR : Created. *
  412. *=============================================================================================*/
  413. template<class T>
  414. int TFixedIHeapClass<T>::Load(FileClass &file)
  415. {
  416. int i; // loop counter
  417. int idx; // object index
  418. T *ptr; // object pointer
  419. int a_count;
  420. /*
  421. ** Read the number of instances of this class
  422. */
  423. if (file.Read(&a_count, sizeof(a_count)) != sizeof(a_count)) {
  424. return(false);
  425. }
  426. /*
  427. ** Error if more objects than we can hold
  428. */
  429. if (a_count > TotalCount) {
  430. return(false);
  431. }
  432. /*
  433. ** Read each class instance
  434. */
  435. for (i = 0; i < a_count; i++) {
  436. /*
  437. ** Read the object's array index
  438. */
  439. if (file.Read (&idx, sizeof(idx)) != sizeof(idx)) {
  440. return(false);
  441. }
  442. /*
  443. ** Get a pointer to the object, activate that object
  444. */
  445. ptr = (T *)(*this)[idx];
  446. FreeFlag[idx] = true;
  447. ActiveCount++;
  448. ActivePointers.Add(ptr);
  449. /*
  450. ** Load the object
  451. */
  452. if (!ptr->Load(file)) {
  453. return(false);
  454. }
  455. }
  456. return(true);
  457. }
  458. /***********************************************************************************************
  459. * TFixedIHeapClass::Code_Pointers -- codes pointers for every object, to prepare for save *
  460. * *
  461. * INPUT: file file to read from *
  462. * *
  463. * OUTPUT: true = OK, false = error *
  464. * *
  465. * WARNINGS: none *
  466. * *
  467. * HISTORY: *
  468. * 03/15/1995 BRR : Created. *
  469. *=============================================================================================*/
  470. template<class T>
  471. void TFixedIHeapClass<T>::Code_Pointers(void)
  472. {
  473. int i;
  474. for (i = 0; i < ActiveCount; i++) {
  475. Ptr(i)->Code_Pointers();
  476. }
  477. }
  478. /***********************************************************************************************
  479. * TFixedIHeapClass::Decode_Pointers -- Decodes all object pointers, for after loading *
  480. * *
  481. * INPUT: file file to read from *
  482. * *
  483. * OUTPUT: true = OK, false = error *
  484. * *
  485. * WARNINGS: none *
  486. * *
  487. * HISTORY: *
  488. * 03/15/1995 BRR : Created. *
  489. *=============================================================================================*/
  490. template<class T>
  491. void TFixedIHeapClass<T>::Decode_Pointers(void)
  492. {
  493. int i;
  494. for (i = 0; i < ActiveCount; i++) {
  495. Ptr(i)->Decode_Pointers();
  496. }
  497. }