MEM.CPP 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  1. /*
  2. ** Command & Conquer Red Alert(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 : Westwood Library *
  23. * *
  24. * File Name : MEM.C *
  25. * *
  26. * Programmer : Joe L. Bostic *
  27. * Scott K. Bowen *
  28. * *
  29. * Start Date : March 31, 1993 *
  30. * *
  31. * Last Update : September 8, 1994 [IML] *
  32. * *
  33. *-------------------------------------------------------------------------*
  34. * Functions: *
  35. * Mem_Free -- Free a block of memory from system. *
  36. * Mem_Alloc -- Allocate a block of memory from the special memory pool. *
  37. * Mem_Init -- Initialize the private memory allocation pool. *
  38. * Mem_Reference -- Updates the reference time for the specified memory blo*
  39. * Mem_Find -- Returns with pointer to specified memory block. *
  40. * Mem_Find_Oldest -- Returns with the memory block with the oldest time st*
  41. * Mem_Free_Oldest -- Find and free the oldest memory block. *
  42. * Mem_Avail -- Returns the amount of free memory available in the cache.*
  43. * Mem_Cleanup -- Performes a garbage collection on the memory cache. *
  44. * MemNode_Unlink -- Unlinks a node from the cache. *
  45. * MemNode_Insert -- Inserts a node into a cache chain. *
  46. * Mem_Largest_Avail -- Largest free block available. *
  47. * Mem_Lock_Block -- Locks a block so that it cannot be moved in cleanup.*
  48. * Mem_In_Use -- Makes it so a block will never be returned as oldest*
  49. * Mem_Pool_Size -- Returns total amount of memory in pool. *
  50. * Mem_Get_ID -- Returns ID of node. *
  51. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  52. #include <wwstd.h>
  53. #include "wwmem.h"
  54. #include <timer.h>
  55. #include <stddef.h>
  56. #include <mem.h>
  57. #define DEBUG_FILL FALSE
  58. ////////////////////////////////////////////////////////////////////////////
  59. /*******************************************************************************
  60. ** A allocated block may have one of three meanings in the Time field. The first
  61. ** is the time stamp of the last time it was referenced. The other two values
  62. ** are defined below. MEM_BLOCK_IN_USE means that it will never be returned as the
  63. ** oldest since there is no valid time stamp. LOCKED_BLOCK has the same meaning as
  64. ** MEM_BLOCK_IN_USE with the added feature that the block will not be moved in a
  65. ** Mem_Cleanup(). Therefore, there may be some fragmentation after the cleanup
  66. ** if any blocks are LOCKED. It would be good practice to seldomly lock blocks,
  67. ** for instance, only when a sample is being played.
  68. ** WARNING: If these values change to anything else, logic will need to be changed
  69. ** in Mem_Find_Oldest since it relies on these being small values.
  70. */
  71. #define MEM_BLOCK_IN_USE 0x00
  72. #define MEM_BLOCK_LOCKED 0x01
  73. /*
  74. ** Each block of memory in the pool is headed by this structure.
  75. */
  76. typedef struct MemChain {
  77. struct MemChain *Next; // Pointer to next memory chain node.
  78. struct MemChain *Prev; // Pointer to previous memory chain node.
  79. unsigned long ID; // ID number of block.
  80. unsigned short Time; // TickCount of latest reference.
  81. unsigned long Size; // Size of memory block (in paragraphs).
  82. } MemChain_Type;
  83. /*
  84. ** Holding tank memory management data.
  85. */
  86. typedef struct MemPool {
  87. MemChain_Type *FreeChain; // Pointer to first node in free chain.
  88. MemChain_Type *UsedChain; // Pointer to first node in used chain.
  89. unsigned long FreeMem; // Current amount of free ram (in paragraphs).
  90. unsigned long TotalMem; // Total quantity of memory.
  91. long pad2;
  92. } MemPool_Type;
  93. /*=========================================================================*/
  94. /* The following PRIVATE functions are in this file: */
  95. /*=========================================================================*/
  96. PRIVATE void MemNode_Unlink(MemPool_Type *pool, int freechain, MemChain_Type *node);
  97. PRIVATE void MemNode_Insert(MemPool_Type *pool, int freechain, MemChain_Type *node, unsigned int size, unsigned long id, int merge);
  98. /*= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =*/
  99. /***************************************************************************
  100. * Mem_Init -- Initialize the private memory allocation pool. *
  101. * *
  102. * This routine is used to initialize the private memory allocation *
  103. * pool. *
  104. * *
  105. * INPUT: buffer -- Pointer to the buffer that is the allocation pool. *
  106. * *
  107. * size -- Size of the buffer in bytes. *
  108. * *
  109. * OUTPUT: TRUE/FALSE; Was it initialized successfully? *
  110. * *
  111. * WARNINGS: none *
  112. * *
  113. * HISTORY: *
  114. * 03/31/1993 JLB : Created. *
  115. * 04/13/1994 SKB : Update for 32 bit library, removed XMS calls, *
  116. * optimized for low memory only. *
  117. *=========================================================================*/
  118. int Mem_Init(void *buffer, long size)
  119. {
  120. MemChain_Type *mem; // Working memory chain node.
  121. MemPool_Type *pool; // Memory pool control structure.
  122. /*
  123. ** The buffer is rounded down to the nearest paragraph.
  124. */
  125. size = size & 0xFFFFFFF0L;
  126. if (!buffer || !size) return(FALSE);
  127. /*
  128. ** Initialize the pool control structure.
  129. */
  130. pool = (MemPool_Type *)buffer;
  131. pool->FreeMem = (size - sizeof(MemPool_Type)) >> 4;
  132. pool->UsedChain = NULL;
  133. pool->TotalMem = pool->FreeMem;
  134. mem = pool->FreeChain = (MemChain_Type *) (pool + 1);
  135. /*
  136. ** Initialize the free memory chain.
  137. */
  138. mem->Next = NULL;
  139. mem->Prev = NULL;
  140. mem->Size = pool->FreeMem;
  141. mem->ID = -1;
  142. mem->Time = 0;
  143. return(TRUE);
  144. }
  145. /***************************************************************************
  146. * Mem_Alloc -- Allocate a block of memory from the special memory pool. *
  147. * *
  148. * This routine will allocate a block of memory from the special *
  149. * memory allocation pool. *
  150. * *
  151. * INPUT: poolptr -- Pointer to the memory pool base address. *
  152. * *
  153. * size -- The size of the memory block to allocate. *
  154. * *
  155. * id -- ID number to give this memory block. *
  156. * *
  157. * OUTPUT: Returns with a pointer to the allocated block. If there was *
  158. * insufficient room, then NULL is returned. *
  159. * *
  160. * WARNINGS: Be sure to check for the NULL return case. *
  161. * *
  162. * HISTORY: *
  163. * 03/31/1993 JLB : Created. *
  164. * 08/06/1993 JLB : Optimized for low memory caches. *
  165. * 04/13/1994 SKB : Update for 32 bit library, removed XMS calls, *
  166. * optimized for low memory only. *
  167. *=========================================================================*/
  168. void *Mem_Alloc(void *poolptr, long lsize, unsigned long id)
  169. {
  170. MemPool_Type *pool;
  171. MemChain_Type *node; // Pointer to current memory node.
  172. unsigned int remainder=0; // Remaining bytes that are still free.
  173. int found;
  174. int size; // Paragraph size of allocation.
  175. /*
  176. ** If there is no free memory then the allocation will
  177. ** always fail.
  178. */
  179. if (!poolptr || !lsize) return(NULL);
  180. pool = (MemPool_Type *) poolptr;
  181. /*
  182. ** Allocations are forced to be paragraph sized.
  183. */
  184. lsize += sizeof(MemChain_Type); // Account for header.
  185. lsize = (lsize + 0x0FL) & 0xFFFFFFF0L;
  186. size = (int)(lsize >> 4);
  187. /*
  188. ** If the total free is less than the size of the desired allocation,
  189. ** then we KNOW that an allocation will fail -- just return.
  190. */
  191. if (pool->TotalMem < size) {
  192. return(NULL);
  193. }
  194. /*
  195. ** Walk down free chain looking for the first block that will
  196. ** accomodate the allocation.
  197. */
  198. node = pool->FreeChain;
  199. found = FALSE;
  200. while (!found && node) {
  201. /*
  202. ** Fetch free memory chunk block and see if it is big enough.
  203. */
  204. if (node->Size >= size) {
  205. found = TRUE;
  206. break;
  207. }
  208. node = node->Next;
  209. }
  210. if (!found) {
  211. return(NULL);
  212. }
  213. /*
  214. ** Determine if this allocation would split the block.
  215. */
  216. remainder = node->Size - size;
  217. /*
  218. ** If only a very small free chunk would remain, just tack it on
  219. ** to the current allocation.
  220. */
  221. if (remainder <= 2) {
  222. remainder = 0;
  223. size = node->Size;
  224. }
  225. /*
  226. ** Remove the primary block from the free memory list.
  227. */
  228. MemNode_Unlink(pool, TRUE, node);
  229. /*
  230. ** If a smaller block remains, then link it back into
  231. ** the free memory list.
  232. */
  233. if (remainder) {
  234. MemNode_Insert(pool, TRUE, (MemChain_Type *)Add_Long_To_Pointer(node, (long)size << 4), remainder, -1, FALSE);
  235. }
  236. /*
  237. ** Link in the allocated node into the used memory list.
  238. */
  239. MemNode_Insert(pool, FALSE, node, size, id, FALSE);
  240. /*
  241. ** Reflect the change to the total free count.
  242. */
  243. pool->FreeMem -= size;
  244. /*
  245. ** Return a pointer to the block of allocated memory just past
  246. ** the header.
  247. */
  248. #if DEBUG_FILL
  249. memset(node + 1, id, (size-1) << 4);
  250. #endif
  251. return((void *) (node + 1));
  252. }
  253. /***************************************************************************
  254. * Mem_Free -- Free a block of memory from system. *
  255. * *
  256. * This routine will free a block of memory from the special memory *
  257. * buffer. *
  258. * *
  259. * INPUT: poolptr -- Pointer to the memory pool base address. *
  260. * *
  261. * buffer -- Pointer to memory block to free. *
  262. * *
  263. * OUTPUT: TRUE/FALSE; Was the deallocation successful? *
  264. * *
  265. * WARNINGS: Be sure to only pass in to this routine a buffer that was *
  266. * returned from Mem_Alloc(). *
  267. * *
  268. * HISTORY: *
  269. * 03/31/1993 JLB : Created. *
  270. * 08/06/1993 JLB : Optimized for low memory caches. *
  271. * 04/13/1994 SKB : Update for 32 bit library, removed XMS calls, *
  272. * optimized for low memory only. *
  273. *=========================================================================*/
  274. int Mem_Free(void *poolptr, void *buffer)
  275. {
  276. MemPool_Type *pool; // pointer to structure.
  277. MemChain_Type *node; // Copy of current memory node.
  278. unsigned int size; // Size of the block being freed.
  279. /*
  280. ** One can't free what isn't there.
  281. */
  282. if (!buffer || !poolptr) {
  283. return(FALSE);
  284. }
  285. pool = (MemPool_Type *) poolptr;
  286. /*
  287. ** The node pointer is actually back a bit from the "normal" pointer.
  288. */
  289. node = (MemChain_Type *) buffer;
  290. node--;
  291. /*
  292. ** Get pointer to actual allocated node and unlink it from the used
  293. ** memory chain.
  294. */
  295. size = node->Size;
  296. MemNode_Unlink(pool, FALSE, node);
  297. MemNode_Insert(pool, TRUE, node, size, -1, TRUE);
  298. /*
  299. ** Reflect the new free memory into the total memory count.
  300. */
  301. pool->FreeMem += size;
  302. return(TRUE);
  303. }
  304. /***************************************************************************
  305. * Mem_Reference -- Updates the reference time for the specified memory blo*
  306. * *
  307. * This routine is used to update the memory reference time for the *
  308. * specified memory node. Typically, this is called every time a *
  309. * memory block is used in order to make sure the memory block time *
  310. * tracking (Last Recently Used) system works properly. *
  311. * *
  312. * INPUT: node -- Pointer to memory block returned from Mem_Find. *
  313. * *
  314. * OUTPUT: none *
  315. * *
  316. * WARNINGS: The node pointer must be valid. For maximum safety this *
  317. * routine should be called right after Mem_Find(). *
  318. * *
  319. * HISTORY: *
  320. * 08/06/1993 JLB : Created. *
  321. * 04/13/1994 SKB : Update for 32 bit library, removed XMS calls, *
  322. * optimized for low memory only. *
  323. *=========================================================================*/
  324. void Mem_Reference(void *node)
  325. {
  326. MemChain_Type *nodeptr; // Pointer of current memory node.
  327. if (!node) return;
  328. // Get to the node header.
  329. nodeptr = (MemChain_Type *) node;
  330. nodeptr--;
  331. nodeptr->Time = (unsigned short)(TickCount.Time() >> 4);
  332. }
  333. /***************************************************************************
  334. * MEM_LOCK_BLOCK -- Locks a block so that it cannot be moved in cleanup. *
  335. * By marking a memory block in use, the memory system will never return*
  336. * it as the oldest memory block. It also makes it so that the block *
  337. * will never be moved during a Cleanup process. *
  338. * *
  339. * INPUT: node -- Pointer to memory block returned from Mem_Find. *
  340. * *
  341. * OUTPUT: none *
  342. * *
  343. * WARNINGS: If one or more blocks are locked in a heap, Mem_Avail might *
  344. * not equal Mem_Largest_Avail after a call to Mem_Cleanup. *
  345. * *
  346. * HISTORY: *
  347. * 04/15/1994 SKB : Created. *
  348. *=========================================================================*/
  349. void Mem_Lock_Block(void *node)
  350. {
  351. MemChain_Type *nodeptr; // Pointer of current memory node.
  352. if (!node) return;
  353. // Get to the node header.
  354. nodeptr = (MemChain_Type *) node;
  355. nodeptr--;
  356. nodeptr->Time = MEM_BLOCK_LOCKED;
  357. }
  358. /***************************************************************************
  359. * MEM_IN_USE -- Makes it so a block will never be returned as oldest *
  360. * By marking a memory block in use, the memory system will never return*
  361. * it as the oldest memory block. It still can be moved in the Cleanup *
  362. * code. *
  363. * *
  364. * INPUT: node -- Pointer to memory block returned from Mem_Find. *
  365. * *
  366. * OUTPUT: none *
  367. * *
  368. * WARNINGS: Mem_Find_Oldest() will return NULL if only IN_USE blocks are *
  369. * in memory. *
  370. * HISTORY: *
  371. * 04/15/1994 SKB : Created. *
  372. *=========================================================================*/
  373. void Mem_In_Use(void *node)
  374. {
  375. MemChain_Type *nodeptr; // Pointer of current memory node.
  376. if (!node) return;
  377. // Get to the node header.
  378. nodeptr = (MemChain_Type *) node - 1;
  379. nodeptr->Time = MEM_BLOCK_IN_USE;
  380. }
  381. /***************************************************************************
  382. * Mem_Find -- Returns with pointer to specified memory block. *
  383. * *
  384. * Use this routine to convert a memory ID value into an actual memory *
  385. * pointer. It sweeps through all of the 'cached' memory blocks and *
  386. * returns with the matching block pointer. *
  387. * *
  388. * INPUT: poolptr -- Pointer to the memory cache block. *
  389. * *
  390. * id -- The ID of the block desired. *
  391. * *
  392. * OUTPUT: Returns with the pointer to the memory block. If NULL is *
  393. * returned then the desired block is not in the memory cache. *
  394. * *
  395. * WARNINGS: This routine may return NULL if the memory block is not *
  396. * present in the cache. *
  397. * *
  398. * HISTORY: *
  399. * 08/06/1993 JLB : Created. *
  400. * 08/06/1993 JLB : Optimized for low memory caches. *
  401. * 04/13/1994 SKB : Update for 32 bit library, removed XMS calls, *
  402. * optimized for low memory only. *
  403. *=========================================================================*/
  404. void *Mem_Find(void *poolptr, unsigned long id)
  405. {
  406. MemPool_Type *pool; // pointer to structure.
  407. MemChain_Type *node; // Working node structure.
  408. if (!poolptr) return(NULL);
  409. pool = (MemPool_Type *) poolptr;
  410. /*
  411. ** Cannot free a node that is not on the UsedChain list.
  412. */
  413. if (!pool->UsedChain) {
  414. return(NULL);
  415. }
  416. /*
  417. ** Sweep through entire allocation chain to find
  418. ** the one with the matching ID.
  419. */
  420. node = pool->UsedChain;
  421. while (node) {
  422. if (node->ID == id) {
  423. return(node + 1);
  424. }
  425. node = node->Next;
  426. }
  427. return(NULL);
  428. }
  429. /***************************************************************************
  430. * MEM_GET_ID -- Returns ID of node. *
  431. * *
  432. * INPUT: void *node - pointer to node. *
  433. * *
  434. * OUTPUT: The ID of the node that was supplied by user during Mem_Alloc().*
  435. * *
  436. * WARNINGS: pointer to node must be one that Mem_Alloc or *
  437. * Mem_Find returned. **
  438. * *
  439. * HISTORY: *
  440. * 04/18/1994 SKB : Created. *
  441. *=========================================================================*/
  442. unsigned long Mem_Get_ID(void *node)
  443. {
  444. MemChain_Type *nodeptr; // Pointer of current memory node.
  445. if (!node) return (0L);
  446. // Get to the node header.
  447. nodeptr = (MemChain_Type *) node - 1;
  448. return (nodeptr->ID);
  449. }
  450. /***************************************************************************
  451. * Mem_Find_Oldest -- Returns with the memory block with the oldest time st*
  452. * *
  453. * Use this routine to find the memory block with the oldest time stamp *
  454. * value. Typically, this is used when freeing memory blocks in the *
  455. * cache in order to make room for a new memory block. *
  456. * *
  457. * INPUT: poolptr -- Pointer to the memory cache. *
  458. * *
  459. * OUTPUT: Returns with the pointer to the oldest memory block. If NULL *
  460. * is returned, then the memory cache is empty. *
  461. * *
  462. * WARNINGS: This routine could return NULL. *
  463. * *
  464. * HISTORY: *
  465. * 08/06/1993 JLB : Created. *
  466. * 08/06/1993 JLB : Optimized for low memory caches. *
  467. * 04/13/1994 SKB : Update for 32 bit library, removed XMS calls, *
  468. * optimized for low memory only. *
  469. * 04/15/1994 SKB : Handle time wrap, locked blocks, and no_refenece blocks*
  470. *=========================================================================*/
  471. void *Mem_Find_Oldest(void *poolptr)
  472. {
  473. MemChain_Type *node; // Working node pointer.
  474. MemChain_Type *oldnode; // Pointer to oldest block.
  475. unsigned int oldtime; // Time of oldest block.
  476. unsigned int basetime; // Time to mark our base time with.
  477. unsigned int time; // basetime + time of node.
  478. if (!poolptr) return(NULL);
  479. /*
  480. ** Sweep through entire allocation chain to find
  481. ** the oldest referenced memory block.
  482. */
  483. oldnode = NULL;
  484. oldtime = 0;
  485. node = ((MemPool_Type*) poolptr)->UsedChain;
  486. basetime = (unsigned int)(TickCount.Time() >> 4);
  487. while (node) {
  488. /*
  489. ** Don't allow MEM_BLOCK_IN_USE or MEM_BLOCK_LOCKED to be returned.
  490. */
  491. if (node->Time > MEM_BLOCK_LOCKED) {
  492. /*
  493. ** Adjust time for wrap around (after about 5 hrs).
  494. ** times less then the base time will wrap up high while
  495. ** and times greater then base time will then be lower since
  496. ** any time greater has been on the thing a long time.
  497. */
  498. time = node->Time - basetime ;
  499. if (time < oldtime || !oldnode) {
  500. oldtime = time;
  501. oldnode = node;
  502. }
  503. }
  504. node = node->Next;
  505. }
  506. /*
  507. ** Return with the value that matches the pointer that
  508. ** was allocated by the system previously.
  509. */
  510. if (oldnode) {
  511. oldnode++;
  512. }
  513. return(oldnode);
  514. }
  515. /***************************************************************************
  516. * Mem_Free_Oldest -- Find and free the oldest memory block. *
  517. * *
  518. * This routine is used to free the oldest memory block in the memory *
  519. * cache. This routine is typcially used in order to create more room *
  520. * in the cache for a new allocation. *
  521. * *
  522. * INPUT: poolptr -- Pointer to the memory cache. *
  523. * *
  524. * OUTPUT: Returns with the node that it freed. Although this node is *
  525. * is no longer valid, it may be used to mark that pointer as *
  526. * invalid in the main code. *
  527. * *
  528. * WARNINGS: If this routine returns NULL, then no memory was freed. *
  529. * *
  530. * HISTORY: *
  531. * 08/06/1993 JLB : Created. *
  532. * 04/13/1994 SKB : Update for 32 bit library, removed XMS calls, *
  533. * optimized for low memory only. *
  534. *=========================================================================*/
  535. void *Mem_Free_Oldest(void *poolptr)
  536. {
  537. MemChain_Type *node; // Copy of pointer to oldest node.
  538. if (!poolptr) return(NULL);
  539. node = (MemChain *) Mem_Find_Oldest(poolptr);
  540. if (Mem_Free(poolptr, node)) {
  541. return(node);
  542. }
  543. return(NULL);
  544. }
  545. /***************************************************************************
  546. * MEM_POOL_SIZE -- Returns total amount of memory in pool. *
  547. * *
  548. * INPUT: poolptr -- Pointer to the memory cache. *
  549. * *
  550. * OUTPUT: long total size of pool. i.e. largest possible allocation if *
  551. * no memory was allocated. *
  552. * *
  553. * WARNINGS: *
  554. * *
  555. * HISTORY: *
  556. * 04/18/1994 SKB : Created. *
  557. *=========================================================================*/
  558. long Mem_Pool_Size(void *poolptr)
  559. {
  560. MemPool_Type *pool; // Memory pool control structure.
  561. long memtotal; // Total amount of memory free.
  562. if (!poolptr) return(NULL);
  563. pool = (MemPool_Type *) poolptr;
  564. memtotal = ((long)pool->TotalMem) << 4;
  565. memtotal -= sizeof(MemChain_Type);
  566. memtotal = MAX(memtotal, (long)0);
  567. return(memtotal);
  568. }
  569. /***************************************************************************
  570. * Mem_Avail -- Returns the amount of free memory available in the cache. *
  571. * *
  572. * This routine examines the memory cache and returns the amount of *
  573. * free memory available. This memory total MAY be fragmented but *
  574. * after Mem_Cleanup() is called, an allocation of the amount returned *
  575. * by this function is guaranteed. *
  576. * *
  577. * INPUT: poolptr -- Pointer to the memory cache. *
  578. * *
  579. * OUTPUT: Returns the largest allocation possible from the memory cache. *
  580. * *
  581. * WARNINGS: The value returned may represent the FRAGMENTED total *
  582. * amount of memory free in the cache. *
  583. * *
  584. * HISTORY: *
  585. * 08/06/1993 JLB : Created. *
  586. * 04/13/1994 SKB : Update for 32 bit library, removed XMS calls, *
  587. * optimized for low memory only. *
  588. *=========================================================================*/
  589. long Mem_Avail(void *poolptr)
  590. {
  591. MemPool_Type *pool; // Memory pool control structure.
  592. long memtotal; // Total amount of memory free.
  593. if (!poolptr) return(NULL);
  594. pool = (MemPool_Type *) poolptr;
  595. memtotal = ((long)pool->FreeMem) << 4;
  596. memtotal -= sizeof(MemChain_Type);
  597. //memtotal -= sizeof(MemChain_Type) + 15;
  598. memtotal = MAX(memtotal, (long)0);
  599. return(memtotal);
  600. }
  601. /***************************************************************************
  602. * MEM_LARGEST_AVAIL -- Largest free block available. *
  603. * This routine examines the free node list to find the largest block *
  604. * available. User can Mem_Alloc() this return size successfully. *
  605. * *
  606. * INPUT: poolptr -- Pointer to the memory cache. *
  607. * *
  608. * OUTPUT: Returns largest allocation currently possible from the cache. *
  609. * *
  610. * WARNINGS: *
  611. * *
  612. * HISTORY: *
  613. * 04/15/1994 SKB : Created. *
  614. *=========================================================================*/
  615. long Mem_Largest_Avail(void *poolptr)
  616. {
  617. MemChain_Type *node; // Pointer to current memory node.
  618. unsigned int size;
  619. long truesize;
  620. /*
  621. ** Make sure that it is a buffer.
  622. */
  623. if (!poolptr) return(NULL);
  624. /*
  625. ** Go through the entire free chain looking for the largest block.
  626. */
  627. node = ((MemPool_Type *)poolptr)->FreeChain;
  628. size = 0;
  629. while (node) {
  630. /*
  631. ** Fetch free memory chunk block and see if it is big enough.
  632. */
  633. if (node->Size >= size) {
  634. size = node->Size;
  635. }
  636. node = node->Next;
  637. }
  638. truesize = (long)size << 4;
  639. truesize -= sizeof(MemChain_Type);
  640. truesize = MAX(truesize, 0L);
  641. return (truesize);
  642. }
  643. /***************************************************************************
  644. * Mem_Cleanup -- Performs a garbage collection on the memory cache. *
  645. * *
  646. * This routine is used to coalesce all adjacent free blocks of *
  647. * memory in the specified cache. As a result, all previous pointers *
  648. * provided by Mem_Find() are invalidated. This routine consumes a *
  649. * fair amount of time and should be called as infrequently as *
  650. * possible. *
  651. * *
  652. * INPUT: poolptr -- Pointer to the memory cache. *
  653. * *
  654. * OUTPUT: none *
  655. * *
  656. * WARNINGS: This routine takes a significant amount of time! *
  657. * If there are locked block in memory, the pool may still *
  658. * be fragmented. *
  659. * *
  660. * HISTORY: *
  661. * 08/06/1993 JLB : Created. *
  662. * 08/06/1993 JLB : Updated for low memory caches. *
  663. * 04/13/1994 SKB : Update for 32 bit library, removed XMS calls, *
  664. * optimized for low memory only. *
  665. *=========================================================================*/
  666. void Mem_Cleanup(void *poolptr)
  667. {
  668. MemPool_Type *pool; // Memory pool control structure.
  669. MemChain_Type *free, // Pointer to first free area.
  670. *cur; // Pointer to first used block that is after free.
  671. unsigned long size;
  672. unsigned long freesize;// Size of free heap at the end of the block.
  673. if (!poolptr) return;
  674. /*
  675. ** Fetch working copy of pool control structure.
  676. */
  677. pool = (MemPool_Type *) poolptr;
  678. /*
  679. ** Basic parameter and condition legality checks. If the memory pool
  680. ** has no free space, no free blocks, or no allocated blocks, then
  681. ** memory cleanup is unnecessary -- just exit.
  682. */
  683. if (!pool->FreeMem || !pool->FreeChain || !pool->UsedChain) return;
  684. freesize = pool->FreeMem;
  685. free = pool->FreeChain;
  686. pool->FreeChain = NULL;
  687. cur = pool->UsedChain;
  688. while (TRUE) {
  689. /*
  690. ** Setup pointers so that free points to the first free block and cur
  691. ** points to the next used block after the free block.
  692. */
  693. while (cur < free && cur) {
  694. cur = cur->Next;
  695. }
  696. // All used blocks are at the front of the free. We are done.
  697. if (!cur) {
  698. break;
  699. }
  700. /*
  701. ** Do not allow a locked block to be moved.
  702. */
  703. if (cur->Time == MEM_BLOCK_LOCKED) {
  704. /*
  705. ** Figure the size of the new free block that we are creating.
  706. ** Subtract off the total block size.
  707. ** Add the node to the free list.
  708. */
  709. size = (char *) cur - (char *) free;
  710. size >>= 4;
  711. freesize -= size;
  712. MemNode_Insert(pool, TRUE, free, (unsigned int) size, -1, FALSE);
  713. /*
  714. ** Time to find a new free position to start working from.
  715. ** Cur will be in the position just following.
  716. */
  717. free = (MemChain_Type *) Add_Long_To_Pointer(cur, (unsigned long)cur->Size << 4);
  718. cur = cur->Next;
  719. while (free == cur) {
  720. free = (MemChain_Type *) Add_Long_To_Pointer(cur, (unsigned long)cur->Size << 4);
  721. cur = cur->Next;
  722. }
  723. // All used blocks are at the front of the free. We are done.
  724. if (!cur) {
  725. break;
  726. }
  727. } else {
  728. // Copy the block up.
  729. size = (unsigned long)cur->Size << 4;
  730. Mem_Copy(cur, free, size);
  731. cur = free;
  732. // Change pointers of surrounding blocks.
  733. if (cur->Next) {
  734. cur->Next->Prev = cur;
  735. }
  736. if (cur->Prev) {
  737. cur->Prev->Next = cur;
  738. } else {
  739. pool->UsedChain = cur;
  740. }
  741. // Change to next new free area.
  742. free = (MemChain_Type *) Add_Long_To_Pointer(cur, size);
  743. }
  744. }
  745. /*
  746. ** Now build the single free chunk.
  747. */
  748. MemNode_Insert(pool, TRUE, free, freesize, -1, FALSE);
  749. }
  750. /***************************************************************************
  751. * MemNode_Unlink -- Unlinks a node from the cache. *
  752. * *
  753. * A private routine the actually unlinks a memory block from the *
  754. * memory cache. It doesn't perform a complete update of the memory *
  755. * cache. *
  756. * *
  757. * INPUT: pool -- Pointer to the memory cache header (copy in real *
  758. * memory). *
  759. * *
  760. * freechain-- Is the block part of the free memory chain? *
  761. * *
  762. * node -- Pointer to the node that will be unlinked. *
  763. * *
  764. * OUTPUT: none *
  765. * *
  766. * WARNINGS: This routine doesn't update memory totals. It is a support *
  767. * function. *
  768. * *
  769. * HISTORY: *
  770. * 08/06/1993 JLB : Created. *
  771. * 04/13/1994 SKB : Update for 32 bit library, removed XMS calls, *
  772. * optimized for low memory only. *
  773. *=========================================================================*/
  774. PRIVATE void MemNode_Unlink(MemPool_Type *pool, int freechain, MemChain_Type *node)
  775. {
  776. MemChain_Type *other; // Copy of node data to unlink.
  777. MemChain_Type **chain; // A pointer to one of the chains pointer.
  778. /*
  779. ** Check for parameter validity.
  780. */
  781. if (!pool || !node) return;
  782. /*
  783. ** Setup working pointer for the particular chain desired.
  784. */
  785. if (freechain) {
  786. chain = &pool->FreeChain;
  787. } else {
  788. chain = &pool->UsedChain;
  789. }
  790. /*
  791. ** Make adjustments to the previous node. If the pointer
  792. ** to the previous node is NULL then this indicates the
  793. ** first node in the list and thus the chain pointer needs
  794. ** to be updated instead.
  795. */
  796. if (node->Prev) {
  797. other = node->Prev;
  798. other->Next = node->Next;
  799. } else {
  800. *chain = node->Next;
  801. }
  802. if (node->Next) {
  803. other = node->Next;
  804. other->Prev = node->Prev;
  805. }
  806. }
  807. /***************************************************************************
  808. * MemNode_Insert -- Inserts a node into a cache chain. *
  809. * *
  810. * This routine is used to add a node to a cache chain. Since nodes *
  811. * do not contain double links, they must be placed in sequence. *
  812. * *
  813. * INPUT: pool -- Pointer to memory pool (must be in real memory). *
  814. * *
  815. * freechain-- Is the node to be inserted into the free chain? *
  816. * *
  817. * node -- Pointer to the node to insert. *
  818. * *
  819. * size -- Size of the memory block (in paragraphs). *
  820. * *
  821. * id -- The ID number to associate with this block. *
  822. * *
  823. * merge -- Merge inserted block with adjacent blocks. *
  824. * *
  825. * OUTPUT: return *
  826. * *
  827. * WARNINGS: This is a support routine. *
  828. * *
  829. * HISTORY: *
  830. * 08/06/1993 JLB : Created. *
  831. *=========================================================================*/
  832. PRIVATE void MemNode_Insert(MemPool_Type *pool, int freechain, MemChain_Type *node, unsigned int size, unsigned long id, int merge)
  833. {
  834. MemChain_Type **chain; // Pointer to chain that will be linked.
  835. MemChain_Type *prev, // Successor node pointer.
  836. *next; // Predecessor node pointer.
  837. int doit=TRUE; // Link the node into the list.
  838. /*
  839. ** Determine if the parameters are valid.
  840. */
  841. if (!pool || !node || !size) return;
  842. /*
  843. ** Setup working pointer for the particular chain desired.
  844. */
  845. if (freechain) {
  846. chain = &pool->FreeChain;
  847. } else {
  848. chain = &pool->UsedChain;
  849. }
  850. /*
  851. ** Handle the "no node in list" condition (easiest).
  852. */
  853. if (!*chain) {
  854. node->Next = NULL;
  855. node->Prev = NULL;
  856. node->Size = size;
  857. node->Time = (unsigned short)(TickCount.Time() >> 4);
  858. node->ID = id;
  859. *chain = node;
  860. return;
  861. }
  862. /*
  863. ** Sweep through the memory chain looking for a likely spot
  864. ** to insert the new node. It will stop with "next" pointing
  865. ** to the node to come after the block to be inserted and "prev"
  866. ** will point to the node right before.
  867. */
  868. prev = NULL;
  869. next = *chain;
  870. while (next && (next < node)) {
  871. /*
  872. ** Move up the memory chain.
  873. */
  874. prev = next;
  875. next = next->Next;
  876. }
  877. /*
  878. ** Coallescing of adjacent blocks (if requested).
  879. */
  880. if (merge) {
  881. /*
  882. ** If the previous block is touching the block to insert
  883. ** then merely adjust the size of the previous block and
  884. ** that is all that is necessary.
  885. */
  886. if (prev) {
  887. if (((char *)prev + ((long)prev->Size << 4)) == ((char *) node)) {
  888. prev->Size += size;
  889. size = prev->Size;
  890. node = prev;
  891. prev = prev->Prev;
  892. doit = FALSE;
  893. }
  894. }
  895. /*
  896. ** If the following block is touching the block to insert
  897. ** then remove the following block and increase the size of
  898. ** the original insertion block by the size of the other
  899. ** block.
  900. */
  901. if (next) {
  902. if (((char *)node + ((long)size << 4)) == (char *)next) {
  903. if (!doit) {
  904. /*
  905. ** If the node was already merged with the previous block
  906. ** then merely increase the previous block's size
  907. ** and adjust it's next pointer appropriately.
  908. */
  909. node->Size += next->Size;
  910. node->Next = next->Next;
  911. next = next->Next;
  912. } else {
  913. /*
  914. ** Increase the size of the current block and adjust
  915. ** the "next" pointer so that it gets fixed up
  916. ** accordingly.
  917. */
  918. size += next->Size;
  919. next = next->Next;
  920. }
  921. }
  922. }
  923. }
  924. #if DEBUG_FILL
  925. if (doit) {
  926. memset(node + 1, 0xFF, (size - 1) << 4);
  927. } else {
  928. memset(node + 1, 0xFF, (node->Size - 1) << 4);
  929. }
  930. #endif
  931. /*
  932. ** Fixup the node pointers.
  933. */
  934. if (prev) {
  935. prev->Next = node;
  936. }else{
  937. *chain = node;
  938. }
  939. if (next) {
  940. next->Prev = node;
  941. }
  942. if (doit) {
  943. node->Prev = prev;
  944. node->Next = next;
  945. node->Size = size;
  946. node->Time = (unsigned short)(TickCount.Time() >> 4);
  947. node->ID = id;
  948. }
  949. }