ALLOC.CPP 19 KB

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