2
0

ALLOC.CPP 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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 A S S O C I A T E S **
  20. ***************************************************************************
  21. * *
  22. * Project Name : Westwood Library *
  23. * *
  24. * File Name : ALLOC.CPP *
  25. * *
  26. * Programmer : Joe L. Bostic *
  27. * *
  28. * Start Date : February 1, 1992 *
  29. * *
  30. * Last Update : March 9, 1995 [JLB] *
  31. * *
  32. *-------------------------------------------------------------------------*
  33. * Functions: *
  34. * Alloc -- Allocates system RAM. *
  35. * Ram_Free -- Determines the largest free chunk of RAM. *
  36. * Free -- Free an Alloc'ed block of RAM. *
  37. * Resize_Alloc -- Change the size of an allocated block. *
  38. * Heap_Size -- Size of the heap we have. *
  39. * Total_Ram_Free -- Total amount of free RAM. *
  40. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  41. #include <malloc.h>
  42. #include <string.h>
  43. #include <stdlib.h>
  44. #include <dos.h>
  45. #include <io.h>
  46. #ifndef WWMEM_H
  47. #include "wwmem.h"
  48. #endif
  49. extern "C" unsigned long Largest_Mem_Block ( void ) ;
  50. /*
  51. ** Define the equates necessary to call a DPMI interrupt.
  52. */
  53. #define DPMI_INT 0x0031
  54. #define DPMI_LOCK_MEM 0x0600
  55. #define DPMI_UNLOCK_MEM 0x0601
  56. /*=========================================================================*/
  57. /* The following PRIVATE functions are in this file: */
  58. /*=========================================================================*/
  59. /*= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =*/
  60. unsigned long MinRam=0L; // Record of least memory at worst case.
  61. unsigned long MaxRam=0L; // Record of total allocated at worst case.
  62. static unsigned long TotalRam = 0L;
  63. static unsigned long Memory_Calls = 0L;
  64. void (*Memory_Error)(void) = NULL;
  65. /***************************************************************************
  66. * DPMI_LOCK -- handles locking a block of DPMI memory *
  67. * *
  68. * INPUT: *
  69. * *
  70. * OUTPUT: *
  71. * *
  72. * WARNINGS: *
  73. * *
  74. * HISTORY: *
  75. * 06/23/1995 PWG : Created. *
  76. *=========================================================================*/
  77. #include"mono.h"
  78. void DPMI_Lock(VOID const *ptr, long const size)
  79. {
  80. #ifdef cuts
  81. union REGS regs;
  82. struct SREGS sregs;
  83. /*
  84. ** Lock memory
  85. ** AX = 0x600
  86. ** BX:CX = starting linear address of memory to lock
  87. ** SI:DI = size of region to lock (in bytes)
  88. ** - If Failure, carry flag is set.
  89. */
  90. memset (&regs, 0 ,sizeof(regs));
  91. segread (&sregs);
  92. regs.x.eax = DPMI_LOCK_MEM;
  93. regs.x.ebx = ((long)ptr & 0xffff0000) >> 16;
  94. regs.x.ecx = ((long)ptr & 0x0000ffff);
  95. regs.x.esi = ((long)size & 0xffff0000) >> 16;
  96. regs.x.edi = ((long)size & 0x0000ffff);
  97. int386x (DPMI_INT, &regs, &regs, &sregs); // call DPMI
  98. // if (regs.x.cflag) {
  99. // }
  100. #if(0)
  101. char *temp = (char *)ptr;
  102. char hold;
  103. for (int lp = 0; lp < size; lp += 2048) {
  104. hold = *temp;
  105. temp += 2048;
  106. }
  107. #endif
  108. #endif
  109. }
  110. /***************************************************************************
  111. * DPMI_UNLOCK -- Handles unlocking a locked block of DPMI *
  112. * *
  113. * INPUT: *
  114. * *
  115. * OUTPUT: *
  116. * *
  117. * WARNINGS: *
  118. * *
  119. * HISTORY: *
  120. * 06/23/1995 PWG : Created. *
  121. *=========================================================================*/
  122. void DPMI_Unlock(void const *ptr, long const size)
  123. {
  124. #ifdef cuts
  125. union REGS regs;
  126. struct SREGS sregs;
  127. /*
  128. ** Unlock the memory
  129. */
  130. memset (&regs, 0 ,sizeof(regs));
  131. segread (&sregs);
  132. regs.x.eax = DPMI_UNLOCK_MEM; // DPMI function to call
  133. regs.x.ebx = ((long)ptr & 0xffff0000) >> 16;
  134. regs.x.ecx = ((long)ptr & 0x0000ffff);
  135. regs.x.esi = ((long)size & 0xffff0000) >> 16;
  136. regs.x.edi = ((long)size & 0x0000ffff);
  137. int386x (DPMI_INT, &regs, &regs, &sregs); // call DPMI
  138. // if (regs.x.cflag) {
  139. // }
  140. #endif
  141. }
  142. /***************************************************************************
  143. * Alloc -- Allocates system RAM. *
  144. * *
  145. * This is the basic RAM allocation function. It is used for all *
  146. * memory allocations needed by the system or the main program. *
  147. * *
  148. * INPUT: bytes_to_alloc -- LONG value of the number of bytes to alloc. *
  149. * *
  150. * flags -- Memory allocation control flags. *
  151. * MEM_NORMAL: No special flags. *
  152. * MEM_CLEAR: Zero out memory block. *
  153. * MEM_NEW: Called by a new. *
  154. * *
  155. * OUTPUT: Returns with pointer to allocated block. If NULL was returned *
  156. * it indicates a failure to allocate. Note: NULL will never be *
  157. * returned if the standard library allocation error routine is *
  158. * used. *
  159. * *
  160. * WARNINGS: If you replace the standard memory allocation error routine *
  161. * and make it so that Alloc CAN return with a NULL, be sure *
  162. * and check for this in your code. *
  163. * *
  164. * HISTORY: *
  165. * 09/03/1991 JLB : Documented. *
  166. * 08/09/1993 JLB : Updated with EMS memory support. *
  167. * 04/28/1994 JAW : Updated to 32bit Protected mode. *
  168. * 03/09/1995 JLB : Fixed *
  169. *=========================================================================*/
  170. void *Alloc(unsigned long bytes_to_alloc, MemoryFlagType flags)
  171. {
  172. // union REGS regs ;
  173. // struct SREGS sregs ;
  174. unsigned char *retval=NULL; // Pointer to allocated block.
  175. unsigned long original_size; // Original allocation size.
  176. unsigned long bytesfree; // Number of free bytes.
  177. long *longptr=NULL; // Pointer used to store selector
  178. /*
  179. ** Save the original allocated space size so that we can clear the
  180. ** exact amount of RAM if they specified MEM_CLEAR.
  181. */
  182. original_size = bytes_to_alloc;
  183. /*
  184. ** Reserve one byte for the header of the memory we allocated.
  185. ** We will store the flags variable there for later use.
  186. */
  187. bytes_to_alloc += (flags & MEM_LOCK) ? 5 : 1;
  188. /*
  189. ** Initialize the total ram available value.
  190. */
  191. if (!TotalRam) {
  192. TotalRam = Total_Ram_Free(MEM_NORMAL);
  193. }
  194. // Try to allocate the memory out of the protected mode memory
  195. // chain if we did not require a real mode allocation. If this
  196. // fails we will have to try to allocate it out of real mode memory.
  197. // Real mode memory is a last resort because some types of applications
  198. // require real mode memory.
  199. if (!(flags & MEM_REAL)) {
  200. retval = (unsigned char*)malloc(bytes_to_alloc);
  201. }
  202. #ifdef cuts
  203. // Try to allocate the memory out of the real mode memory using DPMI
  204. // service 0x100. Note that retval will be null if we are requesting
  205. // real mode memory so that we do not have to explicitly check for the
  206. // real mode flag. Remember we need to reserve room for the dos
  207. // selector value at the beginning of our allocated block so rather than
  208. // adding fifteen and rounding, we need to add 19 and round.
  209. if (!retval) {
  210. flags = (MemoryFlagType)(flags | MEM_REAL);
  211. regs.x.eax = 0x100;
  212. regs.x.ebx = (bytes_to_alloc + 19) >> 4;
  213. if (regs.x.ebx & 0xFFFF0000) {
  214. retval = NULL;
  215. } else {
  216. segread ( & sregs ) ;
  217. int386x ( 0x31 , & regs, & regs , & sregs ) ;
  218. if (regs.x.cflag)
  219. retval = NULL;
  220. else {
  221. longptr = (long *)(((regs.x.eax & 0xFFFF) << 4)+ 1);
  222. *longptr++ = regs.x.edx & 0xFFFF;
  223. retval = (unsigned char *)longptr;
  224. }
  225. }
  226. }
  227. #endif
  228. // If the alloc failed then we need to signify a memory error.
  229. if (retval == NULL) {
  230. if(Memory_Error != NULL)
  231. Memory_Error();
  232. return NULL;
  233. }
  234. // If the memory needs to be DPMI locked then we should store the
  235. // original size in the header before we store the flags.
  236. if (flags & MEM_LOCK) {
  237. longptr = (long *)retval;
  238. *longptr++ = original_size;
  239. retval = (unsigned char *)longptr;
  240. }
  241. // Now that we know the alloc was sucessful (and for an extra byte
  242. // more than the user wanted) we need to stick in the memory flags.
  243. *retval++ = flags;
  244. // If the memory needed to be DPMI locked then set it up so it
  245. // is locked.
  246. if (flags & MEM_LOCK) {
  247. DPMI_Lock(retval, original_size);
  248. }
  249. /* Clear the space if they wanted it clear */
  250. if (flags & MEM_CLEAR) {
  251. unsigned char *ptr; // Working memory block pointer.
  252. ptr = retval;
  253. memset(ptr, '\0', original_size);
  254. }
  255. bytesfree = Total_Ram_Free(MEM_NORMAL);
  256. if (bytesfree < MinRam) {
  257. MinRam = bytesfree;
  258. }
  259. if (TotalRam-bytesfree > MaxRam) {
  260. MaxRam = TotalRam-bytesfree;
  261. }
  262. Memory_Calls++;
  263. return(retval);
  264. }
  265. /***************************************************************************
  266. * Free -- Free an Alloc'ed block of RAM. *
  267. * *
  268. * FUNCTION: *
  269. * *
  270. * INPUT: A pointer to a block of RAM from Alloc. *
  271. * *
  272. * OUTPUT: None. *
  273. * *
  274. * WARNINGS: Don't use this for an Alloc_Block'ed RAM block. *
  275. * *
  276. * HISTORY: *
  277. * 05/25/1990 : Created. *
  278. ***************************************************************************/
  279. void Free(void const *pointer)
  280. {
  281. // union REGS regs ;
  282. // struct SREGS sregs ;
  283. if (pointer) {
  284. /*
  285. ** Get a pointer to the flags that we stored off.
  286. */
  287. char *byteptr = ((char *)pointer) - 1;
  288. /*
  289. ** Check to see if this was locked me and if it was unlock it.
  290. */
  291. if (*byteptr & MEM_LOCK) {
  292. long *longptr = ((long *)byteptr) - 1;
  293. DPMI_Unlock(pointer, *longptr);
  294. pointer = (void *)longptr;
  295. } else
  296. pointer = (void *)byteptr;
  297. // If the pointer is a real mode pointer than it will point to the
  298. // first megabyte of system memory. If it does than we need to
  299. // use DPMI to free it.
  300. // if (*byteptr & MEM_REAL) {
  301. // regs.x.eax = 0x101;
  302. // regs.x.edx = *(((long *)pointer) - 1);
  303. // segread ( & sregs ) ;
  304. // int386x(0x31, &regs, &regs, &sregs);
  305. // } else {
  306. free((void *)pointer);
  307. // }
  308. Memory_Calls--;
  309. }
  310. }
  311. /***************************************************************************
  312. * Resize_Alloc -- Change the size of an allocated block. *
  313. * *
  314. * This routine will take a previously allocated block and change its *
  315. * size without unnecessarily altering its contents. *
  316. * *
  317. * INPUT: pointer -- Pointer to the original memory allocation. *
  318. * *
  319. * new_size -- Size in bytes that it will be converted to. *
  320. * *
  321. * OUTPUT: Returns with a pointer to the new allocation. *
  322. * *
  323. * WARNINGS: ??? *
  324. * *
  325. * HISTORY: *
  326. * 02/01/1992 JLB : Commented. *
  327. *=========================================================================*/
  328. void *Resize_Alloc(void *original_ptr, unsigned long new_size_in_bytes)
  329. {
  330. unsigned long *temp;
  331. unsigned long diff, flags;
  332. temp = (unsigned long*)original_ptr;
  333. /* ReAlloc the space */
  334. temp = (unsigned long *)realloc(temp, new_size_in_bytes);
  335. if (temp == NULL) {
  336. if(Memory_Error != NULL)
  337. Memory_Error();
  338. return NULL;
  339. }
  340. return(temp);
  341. }
  342. /***************************************************************************
  343. * Ram_Free -- Determines the largest free chunk of RAM. *
  344. * *
  345. * Use this routine to determine the largest free chunk of available *
  346. * RAM for allocation. It also performs a check of the memory chain. *
  347. * *
  348. * INPUT: none *
  349. * *
  350. * OUTPUT: Returns with the size of the largest free chunk of RAM. *
  351. * *
  352. * WARNINGS: This does not return the TOTAL memory free, only the *
  353. * largest free chunk. *
  354. * *
  355. * HISTORY: *
  356. * 09/03/1991 JLB : Commented. *
  357. *=========================================================================*/
  358. long Ram_Free(MemoryFlagType)
  359. {
  360. // return(_memmax());
  361. return Largest_Mem_Block();
  362. }
  363. /***************************************************************************
  364. * Heap_Size -- Size of the heap we have. *
  365. * *
  366. * *
  367. * *
  368. * INPUT: *
  369. * *
  370. * OUTPUT: *
  371. * *
  372. * WARNINGS: *
  373. * *
  374. * HISTORY: *
  375. * 06/21/1994 SKB : Created. *
  376. *=========================================================================*/
  377. long Heap_Size(MemoryFlagType )
  378. {
  379. if (!TotalRam) {
  380. TotalRam = Total_Ram_Free(MEM_NORMAL);
  381. }
  382. return(TotalRam);
  383. }
  384. /***************************************************************************
  385. * Total_Ram_Free -- Total amount of free RAM. *
  386. * *
  387. * *
  388. * *
  389. * INPUT: *
  390. * *
  391. * OUTPUT: *
  392. * *
  393. * WARNINGS: *
  394. * *
  395. * HISTORY: *
  396. * 06/21/1994 SKB : Created. *
  397. * 03/09/1995 JLB : Uses prerecorded heap size maximum. *
  398. *=========================================================================*/
  399. long Total_Ram_Free(MemoryFlagType )
  400. {
  401. // return(_memavl());
  402. return Largest_Mem_Block () ;
  403. }