ALLOC.CPP 23 KB

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