ALLOC.CPP 20 KB

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