MEM.CPP 42 KB

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