1 /* Catacomb Armageddon Source Code
\r
2 * Copyright (C) 1993-2014 Flat Rock Software
\r
4 * This program is free software; you can redistribute it and/or modify
\r
5 * it under the terms of the GNU General Public License as published by
\r
6 * the Free Software Foundation; either version 2 of the License, or
\r
7 * (at your option) any later version.
\r
9 * This program is distributed in the hope that it will be useful,
\r
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
12 * GNU General Public License for more details.
\r
14 * You should have received a copy of the GNU General Public License along
\r
15 * with this program; if not, write to the Free Software Foundation, Inc.,
\r
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
\r
22 =============================================================================
\r
24 ID software memory manager
\r
25 --------------------------
\r
27 Primary coder: John Carmack
\r
31 Quit (char *error) function
\r
36 MM_SizePtr to change the size of a given pointer
\r
38 Multiple purge levels utilized
\r
40 EMS / XMS unmanaged routines
\r
42 =============================================================================
\r
45 //#include "LIB_HEAD.H"
49 =============================================================================
\r
53 =============================================================================
\r
56 #define LOCKBIT 0x80 // if set in attributes, block cannot be moved
\r
57 #define PURGEBITS 3 // 0-3 level, 0= unpurgable, 3= purge first
\r
58 #define PURGEMASK 0xfffc
\r
59 #define BASEATTRIBUTES 0 // unlocked, non purgable
\r
63 typedef struct mmblockstruct
\r
65 unsigned start,length;
\r
66 unsigned attributes;
\r
67 memptr *useptr; // pointer to the segment start
\r
68 struct mmblockstruct far *next;
\r
72 //#define GETNEWBLOCK {if(!(mmnew=mmfree))Quit("MM_GETNEWBLOCK: No free blocks!");mmfree=mmfree->next;}
\r
73 #define GETNEWBLOCK {if(!mmfree)MML_ClearBlock();mmnew=mmfree;mmfree=mmfree->next;}
\r
74 #define FREEBLOCK(x) {*x->useptr=NULL;x->next=mmfree;mmfree=x;}
\r
77 =============================================================================
\r
81 =============================================================================
\r
88 void (* beforesort) (void);
\r
89 void (* aftersort) (void);
\r
92 =============================================================================
\r
96 =============================================================================
\r
101 void huge *hugeheap;
\r
105 mmblocktype far mmblocks[MAXBLOCKS]
\r
106 ,far *mmhead,far *mmfree,far *mmrover,far *mmnew;
\r
108 boolean bombonerror;
\r
110 unsigned totalEMSpages,freeEMSpages,EMSpageframe,EMSpagesmapped,EMShandle;
\r
112 void (* XMSaddr) (void); // far pointer to XMS driver
\r
114 unsigned numUMBs,UMBbase[MAXUMBS];
\r
118 ======================
\r
122 = Routine from p36 of Extending DOS
\r
124 =======================
\r
127 boolean MML_CheckForEMS (void)
\r
130 char emmname[] = "EMMXXXX0";
135 int 0x21 // try to open EMMXXXX0 device
\r
141 int 0x21 // get device info
\r
149 int 0x21 // get status
\r
155 int 0x21 // close handle
\r
174 ======================
\r
178 =======================
\r
181 void MML_SetupEMS (void)
\r
183 char str[80],str2[10];
\r
186 totalEMSpages = freeEMSpages = EMSpageframe = EMSpagesmapped = 0;
\r
190 int EMS_INT // make sure EMS hardware is present
\r
198 cmp al,0x32 // only work on ems 3.2 or greater
\r
201 mov ah,EMS_GETFRAME
\r
202 int EMS_INT // find the page frame address
\r
205 mov [EMSpageframe],bx
\r
207 mov ah,EMS_GETPAGES
\r
208 int EMS_INT // find out how much EMS is there
\r
211 mov [totalEMSpages],dx
\r
212 mov [freeEMSpages],bx
\r
214 jz noEMS // no EMS at all to allocate
\r
217 jle getpages // there is only 1,2,3,or 4 pages
\r
218 mov bx,4 // we can't use more than 4 pages
221 mov [EMSpagesmapped],bx
\r
222 mov ah,EMS_ALLOCPAGES // allocate up to 64k of EMS
\r
230 strcpy (str,"MML_SetupEMS: EMS error 0x");
\r
231 itoa(error,str2,16);
\r
242 ======================
\r
246 =======================
\r
249 void MML_ShutdownEMS (void)
\r
256 mov ah,EMS_FREEPAGES
\r
261 printf("MML_ShutdownEMS: Error freeing EMS!");
267 ====================
\r
271 = Maps the 64k of EMS used by memory manager into the page frame
\r
272 = for general use. This only needs to be called if you are keeping
\r
273 = other things in EMS.
\r
275 ====================
\r
278 void MM_MapEMS (void)
\r
280 char str[80],str2[10];
\r
284 for (i=0;i<EMSpagesmapped;i++)
\r
289 mov bx,[i] // logical page
\r
290 mov al,bl // physical page
\r
291 mov dx,[EMShandle] // handle
\r
298 strcpy (str,"MM_MapEMS: EMS error 0x");
\r
299 itoa(error,str2,16);
\r
308 //==========================================================================
\r
311 ======================
\r
315 = Check for XMM driver
\r
317 =======================
\r
320 boolean MML_CheckForXMS (void)
\r
327 int 0x2f // query status of installed diver
\r
339 ======================
\r
343 = Try to allocate all upper memory block
\r
345 =======================
\r
348 void MML_SetupXMS (void)
\r
350 unsigned base,size;
\r
356 mov [WORD PTR XMSaddr],bx
\r
357 mov [WORD PTR XMSaddr+2],es // function pointer to XMS driver
\r
360 mov ah,XMS_ALLOCUMB
\r
361 mov dx,0xffff // try for largest block possible
\r
362 call [DWORD PTR XMSaddr]
\r
366 cmp bl,0xb0 // error: smaller UMB is available
\r
369 mov ah,XMS_ALLOCUMB
\r
370 call [DWORD PTR XMSaddr] // DX holds largest available UMB
\r
372 jz done // another error...
\r
378 MML_UseSpace (base,size);
\r
379 mminfo.XMSmem += size*16;
\r
380 UMBbase[numUMBs] = base;
\r
382 if (numUMBs < MAXUMBS)
\r
390 ======================
\r
394 ======================
\r
397 void MML_ShutdownXMS (void)
\r
402 for (i=0;i<numUMBs;i++)
\r
409 call [DWORD PTR XMSaddr]
414 //==========================================================================
\r
417 ======================
\r
421 = Marks a range of paragraphs as usable by the memory manager
\r
422 = This is used to mark space for the near heap, far heap, ems page frame,
\r
423 = and upper memory blocks
\r
425 ======================
\r
428 void MML_UseSpace (unsigned segstart, unsigned seglength)
\r
430 mmblocktype far *scan,far *last;
\r
434 scan = last = mmhead;
\r
435 mmrover = mmhead; // reset rover to start of memory
\r
438 // search for the block that contains the range of segments
\r
440 while (scan->start+scan->length < segstart)
\r
447 // take the given range out of the block
\r
449 oldend = scan->start + scan->length;
\r
450 extra = oldend - (segstart+seglength);
\r
452 Quit ("MML_UseSpace: Segment spans two blocks!");
\r
454 if (segstart == scan->start)
\r
456 last->next = scan->next; // unlink block
\r
461 scan->length = segstart-scan->start; // shorten block
\r
466 mmnew->next = scan->next;
\r
467 scan->next = mmnew;
\r
468 mmnew->start = segstart+seglength;
\r
469 mmnew->length = extra;
\r
470 mmnew->attributes = LOCKBIT;
\r
475 //==========================================================================
\r
478 ====================
\r
482 = We are out of blocks, so free a purgable block
\r
484 ====================
\r
487 void MML_ClearBlock (void)
\r
489 mmblocktype far *scan,far *last;
\r
491 scan = mmhead->next;
\r
495 if (!(scan->attributes&LOCKBIT) && (scan->attributes&PURGEBITS) )
\r
497 MM_FreePtr(scan->useptr);
\r
503 Quit ("MM_ClearBlock: No purgable blocks!");
\r
507 //==========================================================================
\r
510 ===================
\r
514 = Grabs all space from turbo with malloc/farmalloc
\r
515 = Allocates bufferseg misc buffer
\r
517 ===================
\r
520 static char *ParmStrings[] = {"noems","noxms",""};
\r
522 void MM_Startup (void)
\r
525 unsigned long length;
\r
527 unsigned segstart,seglength,endfree;
\r
534 bombonerror = true;
\r
536 // set up the linked list (everything in the free list;
\r
539 mmfree = &mmblocks[0];
\r
540 for (i=0;i<MAXBLOCKS-1;i++)
\r
541 mmblocks[i].next = &mmblocks[i+1];
\r
542 mmblocks[i].next = NULL;
\r
545 // locked block of all memory until we punch out free space
\r
548 mmhead = mmnew; // this will allways be the first node
\r
550 mmnew->length = 0xffff;
\r
551 mmnew->attributes = LOCKBIT;
\r
552 mmnew->next = NULL;
\r
557 // get all available near conventional memory segments
\r
560 start = (void far *)(nearheap = malloc(length));
\r
562 length -= 16-(FP_OFF(start)&15);
\r
563 length -= SAVENEARHEAP;
\r
564 seglength = length / 16; // now in paragraphs
\r
565 segstart = FP_SEG(start)+(FP_OFF(start)+15)/16;
\r
566 MML_UseSpace (segstart,seglength);
\r
567 mminfo.nearheap = length;
\r
570 // get all available far conventional memory segments
\r
572 length=farcoreleft();
\r
573 start = farheap = farmalloc(length);
\r
574 length -= 16-(FP_OFF(start)&15);
\r
575 length -= SAVEFARHEAP;
\r
576 seglength = length / 16; // now in paragraphs
\r
577 segstart = FP_SEG(start)+(FP_OFF(start)+15)/16;
\r
578 MML_UseSpace (segstart,seglength);
\r
579 mminfo.farheap = length;
\r
580 mminfo.mainmem = mminfo.nearheap + mminfo.farheap;
\r
584 // detect EMS and allocate up to 64K at page frame
\r
587 for (i = 1;i < _argc;i++)
\r
589 if ( US_CheckParm(_argv[i],ParmStrings) == 0)
\r
590 goto emsskip; // param NOEMS
\r
593 if (MML_CheckForEMS())
\r
595 MML_SetupEMS(); // allocate space
\r
596 MML_UseSpace (EMSpageframe,EMSpagesmapped*0x400);
\r
597 MM_MapEMS(); // map in used pages
\r
598 mminfo.EMSmem = EMSpagesmapped*0x4000l;
\r
602 // detect XMS and get upper memory blocks
\r
606 for (i = 1;i < _argc;i++)
\r
608 if ( US_CheckParm(_argv[i],ParmStrings) == 0)
\r
609 goto xmsskip; // param NOXMS
\r
612 if (MML_CheckForXMS())
\r
613 MML_SetupXMS(); // allocate as many UMBs as possible
\r
616 // allocate the misc buffer
\r
619 mmrover = mmhead; // start looking for space after low block
\r
621 MM_GetPtr (&bufferseg,BUFFERSIZE);
\r
624 //==========================================================================
\r
627 ====================
\r
631 = Frees all conventional, EMS, and XMS allocated
\r
633 ====================
\r
636 void MM_Shutdown (void)
\r
643 MML_ShutdownEMS ();
\r
644 MML_ShutdownXMS ();
\r
647 //==========================================================================
\r
650 ====================
\r
654 = Allocates an unlocked, unpurgable block
\r
656 ====================
\r
659 void MM_GetPtr (memptr *baseptr,unsigned long size)
\r
661 mmblocktype far *scan,far *lastscan,far *endscan
\r
662 ,far *purge,far *next;
\r
664 unsigned needed,startseg;
\r
666 needed = (size+15)/16; // convert size from bytes to paragraphs
\r
668 GETNEWBLOCK; // fill in start and next after a spot is found
\r
669 mmnew->length = needed;
\r
670 mmnew->useptr = baseptr;
\r
671 mmnew->attributes = BASEATTRIBUTES;
\r
673 for (search = 0; search<3; search++)
\r
676 // first search: try to allocate right after the rover, then on up
\r
677 // second search: search from the head pointer up to the rover
\r
678 // third search: compress memory, then scan from start
\r
679 if (search == 1 && mmrover == mmhead)
\r
685 lastscan = mmrover;
\r
686 scan = mmrover->next;
\r
691 scan = mmhead->next;
\r
697 scan = mmhead->next;
\r
702 startseg = lastscan->start + lastscan->length;
\r
704 while (scan != endscan)
\r
706 if (scan->start - startseg >= needed)
\r
709 // got enough space between the end of lastscan and
\r
710 // the start of scan, so throw out anything in the middle
\r
711 // and allocate the new block
\r
713 purge = lastscan->next;
\r
714 lastscan->next = mmnew;
\r
715 mmnew->start = *(unsigned *)baseptr = startseg;
\r
716 mmnew->next = scan;
\r
717 while ( purge != scan)
\r
718 { // free the purgable block
\r
719 next = purge->next;
\r
721 purge = next; // purge another if not at scan
\r
724 return; // good allocation!
\r
728 // if this block is purge level zero or locked, skip past it
\r
730 if ( (scan->attributes & LOCKBIT)
\r
731 || !(scan->attributes & PURGEBITS) )
\r
734 startseg = lastscan->start + lastscan->length;
\r
738 scan=scan->next; // look at next line
\r
743 Quit (OUT_OF_MEM_MSG,(size-mminfo.nearheap));
\r
748 //==========================================================================
\r
751 ====================
\r
755 = Allocates an unlocked, unpurgable block
\r
757 ====================
\r
760 void MM_FreePtr (memptr *baseptr)
\r
762 mmblocktype far *scan,far *last;
\r
767 if (baseptr == mmrover->useptr) // removed the last allocated block
\r
770 while (scan->useptr != baseptr && scan)
\r
777 Quit ("MM_FreePtr: Block not found!");
\r
779 last->next = scan->next;
\r
783 //==========================================================================
\r
786 =====================
\r
790 = Sets the purge level for a block (locked blocks cannot be made purgable)
\r
792 =====================
\r
795 void MM_SetPurge (memptr *baseptr, int purge)
\r
797 mmblocktype far *start;
\r
803 if (mmrover->useptr == baseptr)
\r
806 mmrover = mmrover->next;
\r
810 else if (mmrover == start)
\r
811 Quit ("MM_SetPurge: Block not found!");
\r
815 mmrover->attributes &= ~PURGEBITS;
\r
816 mmrover->attributes |= purge;
\r
819 //==========================================================================
\r
822 =====================
\r
826 = Locks / unlocks the block
\r
828 =====================
\r
831 void MM_SetLock (memptr *baseptr, boolean locked)
\r
833 mmblocktype far *start;
\r
839 if (mmrover->useptr == baseptr)
\r
842 mmrover = mmrover->next;
\r
846 else if (mmrover == start)
\r
847 Quit ("MM_SetLock: Block not found!");
\r
851 mmrover->attributes &= ~LOCKBIT;
\r
852 mmrover->attributes |= locked*LOCKBIT;
\r
855 //==========================================================================
\r
858 =====================
\r
862 = Throws out all purgable stuff and compresses movable blocks
\r
864 =====================
\r
867 void MM_SortMem (void)
\r
869 mmblocktype far *scan,far *last,far *next;
\r
870 unsigned start,length,source,dest,oldborder;
\r
874 // lock down a currently playing sound
\r
876 playing = SD_SoundPlaying ();
\r
882 playing += STARTPCSOUNDS;
\r
885 playing += STARTADLIBSOUNDS;
\r
888 MM_SetLock(&(memptr)audiosegs[playing],true);
\r
893 // oldborder = bordercolor;
\r
894 // VW_ColorBorder (15);
\r
901 last = NULL; // shut up compiler warning
\r
905 if (scan->attributes & LOCKBIT)
\r
908 // block is locked, so try to pile later blocks right after it
\r
910 start = scan->start + scan->length;
\r
914 if (scan->attributes & PURGEBITS)
\r
917 // throw out the purgable block
\r
928 // push the non purgable block on top of the last moved block
\r
930 if (scan->start != start)
\r
932 length = scan->length;
\r
933 source = scan->start;
\r
935 while (length > 0xf00)
\r
937 movedata(source,0,dest,0,0xf00*16);
\r
942 movedata(source,0,dest,0,length*16);
\r
944 scan->start = start;
\r
945 *(unsigned *)scan->useptr = start;
\r
947 start = scan->start + scan->length;
\r
952 scan = scan->next; // go to next block
\r
960 // VW_ColorBorder (oldborder);
\r
963 MM_SetLock(&(memptr)audiosegs[playing],false);
\r
967 //==========================================================================
\r
971 =====================
\r
975 =====================
\r
978 void MM_ShowMemory (void)
\r
980 mmblocktype far *scan;
\r
981 unsigned color,temp;
\r
983 char scratch[80],str[10];
\r
985 VW_SetDefaultColors();
\r
986 VW_SetLineWidth(40);
\r
989 VW_SetScreen (0,0);
\r
999 if (scan->attributes & PURGEBITS)
\r
1000 color = 5; // dark purple = purgable
\r
1002 color = 9; // medium blue = non purgable
\r
1003 if (scan->attributes & LOCKBIT)
\r
1004 color = 12; // red = locked
\r
1005 if (scan->start<=end)
\r
1006 Quit ("MM_ShowMemory: Memory block order currupted!");
\r
1007 end = scan->start+scan->length-1;
\r
1008 VW_Hlin(scan->start,(unsigned)end,0,color);
\r
1009 VW_Plot(scan->start,0,15);
\r
1010 if (scan->next->start > end+1)
\r
1011 VW_Hlin(end+1,scan->next->start,0,0); // black = free
\r
1014 strcpy (scratch,"Size:");
\r
1015 ltoa ((long)scan->length*16,str,10);
\r
1016 strcat (scratch,str);
\r
1017 strcat (scratch,"\tOwner:0x");
\r
1018 owner = (unsigned)scan->useptr;
\r
1019 ultoa (owner,str,16);
\r
1020 strcat (scratch,str);
\r
1021 strcat (scratch,"\n");
\r
1022 write (debughandle,scratch,strlen(scratch));
\r
1025 scan = scan->next;
\r
1028 //CA_CloseDebug ();
\r
1031 VW_SetLineWidth(64);
\r
1036 //==========================================================================
\r
1040 ======================
\r
1044 = Returns the total free space without purging
\r
1046 ======================
\r
1049 long MM_UnusedMemory (void)
\r
1052 mmblocktype far *scan;
\r
1057 while (scan->next)
\r
1059 free += scan->next->start - (scan->start + scan->length);
\r
1060 scan = scan->next;
\r
1066 //==========================================================================
\r
1070 ======================
\r
1074 = Returns the total free space with purging
\r
1076 ======================
\r
1079 long MM_TotalFree (void)
\r
1082 mmblocktype far *scan;
\r
1087 while (scan->next)
\r
1089 if ((scan->attributes&PURGEBITS) && !(scan->attributes&LOCKBIT))
\r
1090 free += scan->length;
\r
1091 free += scan->next->start - (scan->start + scan->length);
\r
1092 scan = scan->next;
\r
1098 //==========================================================================
\r
1101 =====================
\r
1105 =====================
\r
1108 void MM_BombOnError (boolean bomb)
\r
1110 bombonerror = bomb;
\r