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
\r
223 mov [EMSpagesmapped],bx
\r
224 mov ah,EMS_ALLOCPAGES // allocate up to 64k of EMS
\r
234 strcpy (str,"MML_SetupEMS: EMS error 0x");
\r
235 itoa(error,str2,16);
\r
245 ======================
\r
249 =======================
\r
252 void MML_ShutdownEMS (void)
\r
258 mov ah,EMS_FREEPAGES
\r
265 Quit ("MML_ShutdownEMS: Error freeing EMS!");
\r
272 ====================
\r
276 = Maps the 64k of EMS used by memory manager into the page frame
\r
277 = for general use. This only needs to be called if you are keeping
\r
278 = other things in EMS.
\r
280 ====================
\r
283 void MM_MapEMS (void)
\r
285 char str[80],str2[10];
\r
289 for (i=0;i<EMSpagesmapped;i++)
\r
293 mov bx,[i] // logical page
\r
294 mov al,bl // physical page
\r
295 mov dx,[EMShandle] // handle
\r
306 strcpy (str,"MM_MapEMS: EMS error 0x");
\r
307 itoa(error,str2,16);
\r
312 //==========================================================================
\r
315 ======================
\r
319 = Check for XMM driver
\r
321 =======================
\r
324 boolean MML_CheckForXMS (void)
\r
330 int 0x2f // query status of installed diver
\r
341 ======================
\r
345 = Try to allocate all upper memory block
\r
347 =======================
\r
350 void MML_SetupXMS (void)
\r
352 unsigned base,size;
\r
357 mov [WORD PTR XMSaddr],bx
\r
358 mov [WORD PTR XMSaddr+2],es // function pointer to XMS driver
\r
363 mov ah,XMS_ALLOCUMB
\r
364 mov dx,0xffff // try for largest block possible
\r
365 call [DWORD PTR XMSaddr]
\r
369 cmp bl,0xb0 // error: smaller UMB is available
\r
372 mov ah,XMS_ALLOCUMB
\r
373 call [DWORD PTR XMSaddr] // DX holds largest available UMB
\r
375 jz done // another error...
\r
383 MML_UseSpace (base,size);
\r
384 mminfo.XMSmem += size*16;
\r
385 UMBbase[numUMBs] = base;
\r
387 if (numUMBs < MAXUMBS)
\r
395 ======================
\r
399 ======================
\r
402 void MML_ShutdownXMS (void)
\r
407 for (i=0;i<numUMBs;i++)
\r
413 call [DWORD PTR XMSaddr]
\r
417 //==========================================================================
\r
420 ======================
\r
424 = Marks a range of paragraphs as usable by the memory manager
\r
425 = This is used to mark space for the near heap, far heap, ems page frame,
\r
426 = and upper memory blocks
\r
428 ======================
\r
431 void MML_UseSpace (unsigned segstart, unsigned seglength)
\r
433 mmblocktype far *scan,far *last;
\r
437 scan = last = mmhead;
\r
438 mmrover = mmhead; // reset rover to start of memory
\r
441 // search for the block that contains the range of segments
\r
443 while (scan->start+scan->length < segstart)
\r
450 // take the given range out of the block
\r
452 oldend = scan->start + scan->length;
\r
453 extra = oldend - (segstart+seglength);
\r
455 Quit ("MML_UseSpace: Segment spans two blocks!");
\r
457 if (segstart == scan->start)
\r
459 last->next = scan->next; // unlink block
\r
464 scan->length = segstart-scan->start; // shorten block
\r
469 mmnew->next = scan->next;
\r
470 scan->next = mmnew;
\r
471 mmnew->start = segstart+seglength;
\r
472 mmnew->length = extra;
\r
473 mmnew->attributes = LOCKBIT;
\r
478 //==========================================================================
\r
481 ====================
\r
485 = We are out of blocks, so free a purgable block
\r
487 ====================
\r
490 void MML_ClearBlock (void)
\r
492 mmblocktype far *scan,far *last;
\r
494 scan = mmhead->next;
\r
498 if (!(scan->attributes&LOCKBIT) && (scan->attributes&PURGEBITS) )
\r
500 MM_FreePtr(scan->useptr);
\r
506 Quit ("MM_ClearBlock: No purgable blocks!");
\r
510 //==========================================================================
\r
513 ===================
\r
517 = Grabs all space from turbo with malloc/farmalloc
\r
518 = Allocates bufferseg misc buffer
\r
520 ===================
\r
523 static char *ParmStrings[] = {"noems","noxms",""};
\r
525 void MM_Startup (void)
\r
528 unsigned long length;
\r
530 unsigned segstart,seglength,endfree;
\r
537 bombonerror = true;
\r
539 // set up the linked list (everything in the free list;
\r
542 mmfree = &mmblocks[0];
\r
543 for (i=0;i<MAXBLOCKS-1;i++)
\r
544 mmblocks[i].next = &mmblocks[i+1];
\r
545 mmblocks[i].next = NULL;
\r
548 // locked block of all memory until we punch out free space
\r
551 mmhead = mmnew; // this will allways be the first node
\r
553 mmnew->length = 0xffff;
\r
554 mmnew->attributes = LOCKBIT;
\r
555 mmnew->next = NULL;
\r
560 // get all available near conventional memory segments
\r
563 start = (void far *)(nearheap = malloc(length));
\r
565 length -= 16-(FP_OFF(start)&15);
\r
566 length -= SAVENEARHEAP;
\r
567 seglength = length / 16; // now in paragraphs
\r
568 segstart = FP_SEG(start)+(FP_OFF(start)+15)/16;
\r
569 MML_UseSpace (segstart,seglength);
\r
570 mminfo.nearheap = length;
\r
573 // get all available far conventional memory segments
\r
575 length=farcoreleft();
\r
576 start = farheap = farmalloc(length);
\r
577 length -= 16-(FP_OFF(start)&15);
\r
578 length -= SAVEFARHEAP;
\r
579 seglength = length / 16; // now in paragraphs
\r
580 segstart = FP_SEG(start)+(FP_OFF(start)+15)/16;
\r
581 MML_UseSpace (segstart,seglength);
\r
582 mminfo.farheap = length;
\r
583 mminfo.mainmem = mminfo.nearheap + mminfo.farheap;
\r
587 // detect EMS and allocate up to 64K at page frame
\r
590 for (i = 1;i < _argc;i++)
\r
592 if ( US_CheckParm(_argv[i],ParmStrings) == 0)
\r
593 goto emsskip; // param NOEMS
\r
596 if (MML_CheckForEMS())
\r
598 MML_SetupEMS(); // allocate space
\r
599 MML_UseSpace (EMSpageframe,EMSpagesmapped*0x400);
\r
600 MM_MapEMS(); // map in used pages
\r
601 mminfo.EMSmem = EMSpagesmapped*0x4000l;
\r
605 // detect XMS and get upper memory blocks
\r
609 for (i = 1;i < _argc;i++)
\r
611 if ( US_CheckParm(_argv[i],ParmStrings) == 0)
\r
612 goto xmsskip; // param NOXMS
\r
615 if (MML_CheckForXMS())
\r
616 MML_SetupXMS(); // allocate as many UMBs as possible
\r
619 // allocate the misc buffer
\r
622 mmrover = mmhead; // start looking for space after low block
\r
624 MM_GetPtr (&bufferseg,BUFFERSIZE);
\r
627 //==========================================================================
\r
630 ====================
\r
634 = Frees all conventional, EMS, and XMS allocated
\r
636 ====================
\r
639 void MM_Shutdown (void)
\r
646 MML_ShutdownEMS ();
\r
647 MML_ShutdownXMS ();
\r
650 //==========================================================================
\r
653 ====================
\r
657 = Allocates an unlocked, unpurgable block
\r
659 ====================
\r
662 void MM_GetPtr (memptr *baseptr,unsigned long size)
\r
664 mmblocktype far *scan,far *lastscan,far *endscan
\r
665 ,far *purge,far *next;
\r
667 unsigned needed,startseg;
\r
669 needed = (size+15)/16; // convert size from bytes to paragraphs
\r
671 GETNEWBLOCK; // fill in start and next after a spot is found
\r
672 mmnew->length = needed;
\r
673 mmnew->useptr = baseptr;
\r
674 mmnew->attributes = BASEATTRIBUTES;
\r
676 for (search = 0; search<3; search++)
\r
679 // first search: try to allocate right after the rover, then on up
\r
680 // second search: search from the head pointer up to the rover
\r
681 // third search: compress memory, then scan from start
\r
682 if (search == 1 && mmrover == mmhead)
\r
688 lastscan = mmrover;
\r
689 scan = mmrover->next;
\r
694 scan = mmhead->next;
\r
700 scan = mmhead->next;
\r
705 startseg = lastscan->start + lastscan->length;
\r
707 while (scan != endscan)
\r
709 if (scan->start - startseg >= needed)
\r
712 // got enough space between the end of lastscan and
\r
713 // the start of scan, so throw out anything in the middle
\r
714 // and allocate the new block
\r
716 purge = lastscan->next;
\r
717 lastscan->next = mmnew;
\r
718 mmnew->start = *(unsigned *)baseptr = startseg;
\r
719 mmnew->next = scan;
\r
720 while ( purge != scan)
\r
721 { // free the purgable block
\r
722 next = purge->next;
\r
724 purge = next; // purge another if not at scan
\r
727 return; // good allocation!
\r
731 // if this block is purge level zero or locked, skip past it
\r
733 if ( (scan->attributes & LOCKBIT)
\r
734 || !(scan->attributes & PURGEBITS) )
\r
737 startseg = lastscan->start + lastscan->length;
\r
741 scan=scan->next; // look at next line
\r
746 Quit (OUT_OF_MEM_MSG,(size-mminfo.nearheap));
\r
751 //==========================================================================
\r
754 ====================
\r
758 = Allocates an unlocked, unpurgable block
\r
760 ====================
\r
763 void MM_FreePtr (memptr *baseptr)
\r
765 mmblocktype far *scan,far *last;
\r
770 if (baseptr == mmrover->useptr) // removed the last allocated block
\r
773 while (scan->useptr != baseptr && scan)
\r
780 Quit ("MM_FreePtr: Block not found!");
\r
782 last->next = scan->next;
\r
786 //==========================================================================
\r
789 =====================
\r
793 = Sets the purge level for a block (locked blocks cannot be made purgable)
\r
795 =====================
\r
798 void MM_SetPurge (memptr *baseptr, int purge)
\r
800 mmblocktype far *start;
\r
806 if (mmrover->useptr == baseptr)
\r
809 mmrover = mmrover->next;
\r
813 else if (mmrover == start)
\r
814 Quit ("MM_SetPurge: Block not found!");
\r
818 mmrover->attributes &= ~PURGEBITS;
\r
819 mmrover->attributes |= purge;
\r
822 //==========================================================================
\r
825 =====================
\r
829 = Locks / unlocks the block
\r
831 =====================
\r
834 void MM_SetLock (memptr *baseptr, boolean locked)
\r
836 mmblocktype far *start;
\r
842 if (mmrover->useptr == baseptr)
\r
845 mmrover = mmrover->next;
\r
849 else if (mmrover == start)
\r
850 Quit ("MM_SetLock: Block not found!");
\r
854 mmrover->attributes &= ~LOCKBIT;
\r
855 mmrover->attributes |= locked*LOCKBIT;
\r
858 //==========================================================================
\r
861 =====================
\r
865 = Throws out all purgable stuff and compresses movable blocks
\r
867 =====================
\r
870 void MM_SortMem (void)
\r
872 mmblocktype far *scan,far *last,far *next;
\r
873 unsigned start,length,source,dest,oldborder;
\r
877 // lock down a currently playing sound
\r
879 playing = SD_SoundPlaying ();
\r
885 playing += STARTPCSOUNDS;
\r
888 playing += STARTADLIBSOUNDS;
\r
891 MM_SetLock(&(memptr)audiosegs[playing],true);
\r
896 // oldborder = bordercolor;
\r
897 // VW_ColorBorder (15);
\r
904 last = NULL; // shut up compiler warning
\r
908 if (scan->attributes & LOCKBIT)
\r
911 // block is locked, so try to pile later blocks right after it
\r
913 start = scan->start + scan->length;
\r
917 if (scan->attributes & PURGEBITS)
\r
920 // throw out the purgable block
\r
931 // push the non purgable block on top of the last moved block
\r
933 if (scan->start != start)
\r
935 length = scan->length;
\r
936 source = scan->start;
\r
938 while (length > 0xf00)
\r
940 movedata(source,0,dest,0,0xf00*16);
\r
945 movedata(source,0,dest,0,length*16);
\r
947 scan->start = start;
\r
948 *(unsigned *)scan->useptr = start;
\r
950 start = scan->start + scan->length;
\r
955 scan = scan->next; // go to next block
\r
963 // VW_ColorBorder (oldborder);
\r
966 MM_SetLock(&(memptr)audiosegs[playing],false);
\r
970 //==========================================================================
\r
974 =====================
\r
978 =====================
\r
981 void MM_ShowMemory (void)
\r
983 mmblocktype far *scan;
\r
984 unsigned color,temp;
\r
986 char scratch[80],str[10];
\r
988 VW_SetDefaultColors();
\r
989 VW_SetLineWidth(40);
\r
992 VW_SetScreen (0,0);
\r
1002 if (scan->attributes & PURGEBITS)
\r
1003 color = 5; // dark purple = purgable
\r
1005 color = 9; // medium blue = non purgable
\r
1006 if (scan->attributes & LOCKBIT)
\r
1007 color = 12; // red = locked
\r
1008 if (scan->start<=end)
\r
1009 Quit ("MM_ShowMemory: Memory block order currupted!");
\r
1010 end = scan->start+scan->length-1;
\r
1011 VW_Hlin(scan->start,(unsigned)end,0,color);
\r
1012 VW_Plot(scan->start,0,15);
\r
1013 if (scan->next->start > end+1)
\r
1014 VW_Hlin(end+1,scan->next->start,0,0); // black = free
\r
1017 strcpy (scratch,"Size:");
\r
1018 ltoa ((long)scan->length*16,str,10);
\r
1019 strcat (scratch,str);
\r
1020 strcat (scratch,"\tOwner:0x");
\r
1021 owner = (unsigned)scan->useptr;
\r
1022 ultoa (owner,str,16);
\r
1023 strcat (scratch,str);
\r
1024 strcat (scratch,"\n");
\r
1025 write (debughandle,scratch,strlen(scratch));
\r
1028 scan = scan->next;
\r
1031 //CA_CloseDebug ();
\r
1034 VW_SetLineWidth(64);
\r
1039 //==========================================================================
\r
1043 ======================
\r
1047 = Returns the total free space without purging
\r
1049 ======================
\r
1052 long MM_UnusedMemory (void)
\r
1055 mmblocktype far *scan;
\r
1060 while (scan->next)
\r
1062 free += scan->next->start - (scan->start + scan->length);
\r
1063 scan = scan->next;
\r
1069 //==========================================================================
\r
1073 ======================
\r
1077 = Returns the total free space with purging
\r
1079 ======================
\r
1082 long MM_TotalFree (void)
\r
1085 mmblocktype far *scan;
\r
1090 while (scan->next)
\r
1092 if ((scan->attributes&PURGEBITS) && !(scan->attributes&LOCKBIT))
\r
1093 free += scan->length;
\r
1094 free += scan->next->start - (scan->start + scan->length);
\r
1095 scan = scan->next;
\r
1101 //==========================================================================
\r
1104 =====================
\r
1108 =====================
\r
1111 void MM_BombOnError (boolean bomb)
\r
1113 bombonerror = bomb;
\r