ALLOC.CPP 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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. #include <stdio.h>
  47. #ifndef WWMEM_H
  48. #include "wwmem.h"
  49. #endif
  50. extern "C" unsigned long Largest_Mem_Block ( void ) ;
  51. //
  52. // use double-word alignment for allocs
  53. //
  54. #define LONG_ALIGNMENT 1
  55. /*
  56. ** Define the equates necessary to call a DPMI interrupt.
  57. */
  58. #define DPMI_INT 0x0031
  59. #define DPMI_LOCK_MEM 0x0600
  60. #define DPMI_UNLOCK_MEM 0x0601
  61. #define LOGGING FALSE
  62. /*=========================================================================*/
  63. /* The following PRIVATE functions are in this file: */
  64. /*=========================================================================*/
  65. /*= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =*/
  66. unsigned long MinRam=0L; // Record of least memory at worst case.
  67. unsigned long MaxRam=0L; // Record of total allocated at worst case.
  68. static unsigned long TotalRam = 0L;
  69. static unsigned long Memory_Calls = 0L;
  70. static unsigned long RequestedSystemRam = 8*1024*1024;
  71. static unsigned long LargestRamBlock = 0L;
  72. void (*Memory_Error)(void) = NULL;
  73. void (*Memory_Error_Exit)(char *string) = NULL;
  74. /***************************************************************************
  75. * DPMI_LOCK -- handles locking a block of DPMI memory *
  76. * *
  77. * INPUT: *
  78. * *
  79. * OUTPUT: *
  80. * *
  81. * WARNINGS: *
  82. * *
  83. * HISTORY: *
  84. * 06/23/1995 PWG : Created. *
  85. *=========================================================================*/
  86. #include"mono.h"
  87. void DPMI_Lock(VOID const *ptr, long const size)
  88. {
  89. union REGS regs;
  90. struct SREGS sregs;
  91. /*
  92. ** Lock memory
  93. ** AX = 0x600
  94. ** BX:CX = starting linear address of memory to lock
  95. ** SI:DI = size of region to lock (in bytes)
  96. ** - If Failure, carry flag is set.
  97. */
  98. memset (&regs, 0 ,sizeof(regs));
  99. segread (&sregs);
  100. regs.x.eax = DPMI_LOCK_MEM;
  101. regs.x.ebx = ((long)ptr & 0xffff0000) >> 16;
  102. regs.x.ecx = ((long)ptr & 0x0000ffff);
  103. regs.x.esi = ((long)size & 0xffff0000) >> 16;
  104. regs.x.edi = ((long)size & 0x0000ffff);
  105. int386x (DPMI_INT, &regs, &regs, &sregs); // call DPMI
  106. // if (regs.x.cflag) {
  107. // }
  108. #if(0)
  109. char *temp = (char *)ptr;
  110. char hold;
  111. for (int lp = 0; lp < size; lp += 2048) {
  112. hold = *temp;
  113. temp += 2048;
  114. }
  115. #endif
  116. }
  117. /***************************************************************************
  118. * DPMI_UNLOCK -- Handles unlocking a locked block of DPMI *
  119. * *
  120. * INPUT: *
  121. * *
  122. * OUTPUT: *
  123. * *
  124. * WARNINGS: *
  125. * *
  126. * HISTORY: *
  127. * 06/23/1995 PWG : Created. *
  128. *=========================================================================*/
  129. void DPMI_Unlock(void const *ptr, long const size)
  130. {
  131. union REGS regs;
  132. struct SREGS sregs;
  133. /*
  134. ** Unlock the memory
  135. */
  136. memset (&regs, 0 ,sizeof(regs));
  137. segread (&sregs);
  138. regs.x.eax = DPMI_UNLOCK_MEM; // DPMI function to call
  139. regs.x.ebx = ((long)ptr & 0xffff0000) >> 16;
  140. regs.x.ecx = ((long)ptr & 0x0000ffff);
  141. regs.x.esi = ((long)size & 0xffff0000) >> 16;
  142. regs.x.edi = ((long)size & 0x0000ffff);
  143. int386x (DPMI_INT, &regs, &regs, &sregs); // call DPMI
  144. // if (regs.x.cflag) {
  145. // }
  146. }
  147. /***************************************************************************
  148. * Alloc -- Allocates system RAM. *
  149. * *
  150. * This is the basic RAM allocation function. It is used for all *
  151. * memory allocations needed by the system or the main program. *
  152. * *
  153. * INPUT: bytes_to_alloc -- LONG value of the number of bytes to alloc. *
  154. * *
  155. * flags -- Memory allocation control flags. *
  156. * MEM_NORMAL: No special flags. *
  157. * MEM_CLEAR: Zero out memory block. *
  158. * MEM_NEW: Called by a new. *
  159. * *
  160. * OUTPUT: Returns with pointer to allocated block. If NULL was returned *
  161. * it indicates a failure to allocate. Note: NULL will never be *
  162. * returned if the standard library allocation error routine is *
  163. * used. *
  164. * *
  165. * WARNINGS: If you replace the standard memory allocation error routine *
  166. * and make it so that Alloc CAN return with a NULL, be sure *
  167. * and check for this in your code. *
  168. * *
  169. * HISTORY: *
  170. * 09/03/1991 JLB : Documented. *
  171. * 08/09/1993 JLB : Updated with EMS memory support. *
  172. * 04/28/1994 JAW : Updated to 32bit Protected mode. *
  173. * 03/09/1995 JLB : Fixed *
  174. *=========================================================================*/
  175. void *Alloc(unsigned long bytes_to_alloc, MemoryFlagType flags)
  176. {
  177. union REGS regs ;
  178. struct SREGS sregs ;
  179. unsigned char *retval=NULL; // Pointer to allocated block.
  180. unsigned long original_size; // Original allocation size.
  181. unsigned long bytesfree; // Number of free bytes.
  182. long *longptr=NULL; // Pointer used to store selector
  183. static unsigned char _allocinit=0;
  184. //
  185. // Init memory system by finding largest block to alloc
  186. // then allocate it to get one large heap and free it.
  187. // There may be more memory available from DPMI but we only are
  188. // for now allocating and freeing the first largest block.
  189. //
  190. if ( !_allocinit ) {
  191. unsigned long largestblock = Largest_Mem_Block();
  192. largestblock -= 1024; // subtract for heap header and misc
  193. largestblock &= 0xffff0000; // forcing to 64K boundary
  194. if ( largestblock ) {
  195. LargestRamBlock = MIN( largestblock, RequestedSystemRam );
  196. unsigned char *lptr = (unsigned char *)malloc( LargestRamBlock );
  197. if ( lptr ) {
  198. free( (void *)lptr );
  199. }
  200. }
  201. /*
  202. ** Initialize the total ram available value.
  203. */
  204. TotalRam = Total_Ram_Free(MEM_NORMAL);
  205. _allocinit = 1;
  206. }
  207. /*
  208. ** Save the original allocated space size so that we can clear the
  209. ** exact amount of RAM if they specified MEM_CLEAR.
  210. */
  211. original_size = bytes_to_alloc;
  212. /*
  213. ** Reserve one byte for the header of the memory we allocated.
  214. ** We will store the flags variable there for later use.
  215. */
  216. #if (LONG_ALIGNMENT)
  217. bytes_to_alloc += (flags & MEM_LOCK) ? 8 : 4;
  218. #else
  219. bytes_to_alloc += (flags & MEM_LOCK) ? 5 : 1;
  220. #endif
  221. // Try to allocate the memory out of the protected mode memory
  222. // chain if we did not require a real mode allocation. If this
  223. // fails we will have to try to allocate it out of real mode memory.
  224. // Real mode memory is a last resort because some types of applications
  225. // require real mode memory.
  226. if (!(flags & MEM_REAL)) {
  227. retval = (unsigned char*)malloc(bytes_to_alloc);
  228. }
  229. // Try to allocate the memory out of the real mode memory using DPMI
  230. // service 0x100. Note that retval will be null if we are requesting
  231. // real mode memory so that we do not have to explicitly check for the
  232. // real mode flag. Remember we need to reserve room for the dos
  233. // selector value at the beginning of our allocated block so rather than
  234. // adding fifteen and rounding, we need to add 19 and round.
  235. if (!retval) {
  236. flags = (MemoryFlagType)(flags | MEM_REAL);
  237. regs.x.eax = 0x100;
  238. regs.x.ebx = (bytes_to_alloc + 19) >> 4;
  239. if (regs.x.ebx & 0xFFFF0000) {
  240. retval = NULL;
  241. } else {
  242. segread ( & sregs ) ;
  243. int386x ( 0x31 , & regs, & regs , & sregs ) ;
  244. if (regs.x.cflag)
  245. retval = NULL;
  246. else {
  247. #if (LONG_ALIGNMENT)
  248. longptr = (long *)(((regs.x.eax & 0xFFFF) << 4)+ 4);
  249. #else
  250. longptr = (long *)(((regs.x.eax & 0xFFFF) << 4)+ 1);
  251. #endif
  252. *longptr++ = regs.x.edx & 0xFFFF;
  253. retval = (unsigned char *)longptr;
  254. }
  255. }
  256. }
  257. // If the alloc failed then we need to signify a memory error.
  258. if (retval == NULL) {
  259. if (Memory_Error != NULL)
  260. Memory_Error();
  261. return NULL;
  262. }
  263. // If the memory needs to be DPMI locked then we should store the
  264. // original size in the header before we store the flags.
  265. if (flags & MEM_LOCK) {
  266. longptr = (long *)retval;
  267. *longptr++ = original_size;
  268. retval = (unsigned char *)longptr;
  269. }
  270. // Now that we know the alloc was sucessful (and for an extra byte
  271. // more than the user wanted) we need to stick in the memory flags.
  272. #if (LONG_ALIGNMENT)
  273. if ( !(flags & (MEM_LOCK|MEM_REAL)) ) {
  274. //
  275. // WARNING!!!!!!!!!!
  276. // USE this only with the WATCOM malloc ALLOCATION!!!!!!!!!
  277. // it reads the actual block size before the ptr returned.
  278. // then eors and uses the upper word for a validation later on free.
  279. //
  280. longptr = (long *)retval;
  281. *longptr = ((*(longptr - 1)) ^ 0xffffffff) & 0xffff0000;
  282. *retval++ = flags;
  283. *retval++ = (unsigned char)(flags ^ 0xff);
  284. retval += 2;
  285. }
  286. else {
  287. *retval++ = flags;
  288. *retval++ = (unsigned char)(flags ^ 0xff);
  289. *retval++ = 0;
  290. *retval++ = 0;
  291. }
  292. #else
  293. *retval++ = (unsigned char)(flags | (((flags ^ 0x07) & 0x07) << 5));
  294. #endif
  295. // If the memory needed to be DPMI locked then set it up so it
  296. // is locked.
  297. if (flags & MEM_LOCK) {
  298. DPMI_Lock(retval, original_size);
  299. }
  300. /* Clear the space if they wanted it clear */
  301. if (flags & MEM_CLEAR) {
  302. unsigned char *ptr; // Working memory block pointer.
  303. ptr = retval;
  304. memset(ptr, '\0', original_size);
  305. }
  306. bytesfree = Total_Ram_Free(MEM_NORMAL);
  307. if (bytesfree < MinRam) {
  308. MinRam = bytesfree;
  309. }
  310. if (TotalRam-bytesfree > MaxRam) {
  311. MaxRam = TotalRam-bytesfree;
  312. }
  313. Memory_Calls++;
  314. #if(LOGGING)
  315. int val = _heapchk();
  316. FILE *file = fopen("mem.txt","at");
  317. fprintf(file, "%P Alloc size = %d, Actual Size = %d, flags = %d, heap = %d\n",
  318. retval,
  319. original_size,
  320. bytes_to_alloc,
  321. flags,
  322. val);
  323. fclose(file);
  324. #endif
  325. return(retval);
  326. }
  327. /***************************************************************************
  328. * Free -- Free an Alloc'ed block of RAM. *
  329. * *
  330. * FUNCTION: *
  331. * *
  332. * INPUT: A pointer to a block of RAM from Alloc. *
  333. * *
  334. * OUTPUT: None. *
  335. * *
  336. * WARNINGS: Don't use this for an Alloc_Block'ed RAM block. *
  337. * *
  338. * HISTORY: *
  339. * 05/25/1990 : Created. *
  340. ***************************************************************************/
  341. void Free(void const *pointer)
  342. {
  343. union REGS regs ;
  344. struct SREGS sregs ;
  345. // void const *original = pointer;
  346. char string[80];
  347. if (pointer) {
  348. /*
  349. ** Get a pointer to the flags that we stored off.
  350. */
  351. #if (LONG_ALIGNMENT)
  352. unsigned char *byteptr = ((unsigned char *)pointer) - 4;
  353. //
  354. // validate the flags with and eor of the flags
  355. //
  356. if ( *byteptr != ((*(byteptr + 1)) ^ 0xff) ) {
  357. if (Memory_Error_Exit != NULL) {
  358. sprintf( string, "Error freeing pointer %p. Header invalid!!!\n", pointer );
  359. Memory_Error_Exit( string );
  360. }
  361. }
  362. else {
  363. if ( !(*byteptr & (MEM_LOCK|MEM_REAL)) ) {
  364. unsigned short *wordptr = (unsigned short *)(byteptr - 2);
  365. //
  366. // WARNING!!!!!!!!!!
  367. // USE this only with the WATCOM malloc ALLOCATION!!!!!!!!!
  368. // it reads the actual block size before the ptr to be freed.
  369. // then compares with the EOR to the value stored during allocation.
  370. //
  371. if ( *wordptr != ((*(wordptr + 2)) ^ 0xffff) ) {
  372. if (Memory_Error_Exit != NULL) {
  373. sprintf( string, "Error freeing pointer %p. Header invalid!!!\n", pointer );
  374. Memory_Error_Exit( string );
  375. }
  376. }
  377. }
  378. else if ( *(byteptr + 2) || *(byteptr + 3) ) {
  379. if (Memory_Error_Exit != NULL) {
  380. sprintf( string, "Error freeing pointer %p. Header invalid!!!\n", pointer );
  381. Memory_Error_Exit( string );
  382. }
  383. }
  384. }
  385. // if ( *byteptr != (*(byteptr + 1) ^ 0xff) ||
  386. // *(byteptr + 2) || *(byteptr + 3) ) {
  387. // if (Memory_Error_Exit != NULL) {
  388. // sprintf( string, "Error freeing pointer %p. Header invalid!!!\n", pointer );
  389. // Memory_Error_Exit( string );
  390. // }
  391. // }
  392. #else
  393. unsigned char *byteptr = ((unsigned char *)pointer) - 1;
  394. if ( (*byteptr & 0xe0) != (((*byteptr ^ 0x07) & 0x07) << 5) ) {
  395. if (Memory_Error_Exit != NULL) {
  396. sprintf( string, "Error freeing pointer %p. Header invalid!!!\n", pointer );
  397. Memory_Error_Exit( string );
  398. }
  399. }
  400. #endif
  401. /*
  402. ** Check to see if this was locked me and if it was unlock it.
  403. */
  404. if (*byteptr & MEM_LOCK) {
  405. long *longptr = ((long *)byteptr) - 1;
  406. DPMI_Unlock(pointer, *longptr);
  407. pointer = (void *)longptr;
  408. } else
  409. pointer = (void *)byteptr;
  410. #if(LOGGING)
  411. int val = _heapchk();
  412. FILE *file = fopen("mem.txt","at");
  413. fprintf(file, "%P Free flags = %d, Heap = %d\n",
  414. original,
  415. *byteptr,
  416. val);
  417. fclose(file);
  418. #endif
  419. // If the pointer is a real mode pointer than it will point to the
  420. // first megabyte of system memory. If it does than we need to
  421. // use DPMI to free it.
  422. if (*byteptr & MEM_REAL) {
  423. regs.x.eax = 0x101;
  424. regs.x.edx = *(((long *)pointer) - 1);
  425. segread ( & sregs ) ;
  426. int386x(0x31, &regs, &regs, &sregs);
  427. } else {
  428. free((void *)pointer);
  429. }
  430. Memory_Calls--;
  431. }
  432. }
  433. /***************************************************************************
  434. * Resize_Alloc -- Change the size of an allocated block. *
  435. * *
  436. * This routine will take a previously allocated block and change its *
  437. * size without unnecessarily altering its contents. *
  438. * *
  439. * INPUT: pointer -- Pointer to the original memory allocation. *
  440. * *
  441. * new_size -- Size in bytes that it will be converted to. *
  442. * *
  443. * OUTPUT: Returns with a pointer to the new allocation. *
  444. * *
  445. * WARNINGS: ??? *
  446. * *
  447. * HISTORY: *
  448. * 02/01/1992 JLB : Commented. *
  449. *=========================================================================*/
  450. void *Resize_Alloc(void *original_ptr, unsigned long new_size_in_bytes)
  451. {
  452. unsigned long *temp;
  453. // unsigned long diff, flags;
  454. temp = (unsigned long*)original_ptr;
  455. /* ReAlloc the space */
  456. temp = (unsigned long *)realloc(temp, new_size_in_bytes);
  457. if (temp == NULL) {
  458. if (Memory_Error != NULL)
  459. Memory_Error();
  460. return NULL;
  461. }
  462. return(temp);
  463. }
  464. /***************************************************************************
  465. * Ram_Free -- Determines the largest free chunk of RAM. *
  466. * *
  467. * Use this routine to determine the largest free chunk of available *
  468. * RAM for allocation. It also performs a check of the memory chain. *
  469. * *
  470. * INPUT: none *
  471. * *
  472. * OUTPUT: Returns with the size of the largest free chunk of RAM. *
  473. * *
  474. * WARNINGS: This does not return the TOTAL memory free, only the *
  475. * largest free chunk. *
  476. * *
  477. * HISTORY: *
  478. * 09/03/1991 JLB : Commented. *
  479. *=========================================================================*/
  480. long Ram_Free(MemoryFlagType)
  481. {
  482. return(_memmax());
  483. // return Largest_Mem_Block();
  484. }
  485. /***************************************************************************
  486. * Heap_Size -- Size of the heap we have. *
  487. * *
  488. * *
  489. * *
  490. * INPUT: *
  491. * *
  492. * OUTPUT: *
  493. * *
  494. * WARNINGS: *
  495. * *
  496. * HISTORY: *
  497. * 06/21/1994 SKB : Created. *
  498. *=========================================================================*/
  499. long Heap_Size(MemoryFlagType )
  500. {
  501. if (!TotalRam) {
  502. TotalRam = Total_Ram_Free(MEM_NORMAL);
  503. }
  504. return(TotalRam);
  505. }
  506. /***************************************************************************
  507. * Total_Ram_Free -- Total amount of free RAM. *
  508. * *
  509. * *
  510. * *
  511. * INPUT: *
  512. * *
  513. * OUTPUT: *
  514. * *
  515. * WARNINGS: *
  516. * *
  517. * HISTORY: *
  518. * 06/21/1994 SKB : Created. *
  519. * 03/09/1995 JLB : Uses prerecorded heap size maximum. *
  520. *=========================================================================*/
  521. long Total_Ram_Free(MemoryFlagType )
  522. {
  523. return(_memavl());
  524. // return Largest_Mem_Block () ;
  525. }