ALLOC.CPP 20 KB

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