1 /* Catacomb Armageddon Source Code
2 * Copyright (C) 1993-2014 Flat Rock Software
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 =============================================================================
24 ID software memory manager
25 --------------------------
27 Primary coder: John Carmack
31 Quit (char *error) function
36 MM_SizePtr to change the size of a given pointer
38 Multiple purge levels utilized
40 EMS / XMS unmanaged routines
42 =============================================================================
45 #include "src/lib/16_mm.h"
48 ======================
52 = Routine from p36 of Extending DOS
54 =======================
57 boolean MML_CheckForEMS (void)
60 char emmname[] = "EMMXXXX0";
61 // mov dx,OFFSET emmname
63 LEA DX, emmname //fix by andrius4669
65 int 0x21 // try to open EMMXXXX0 device
71 int 0x21 // get device info
79 int 0x21 // get status
85 int 0x21 // close handle
104 ======================
108 =======================
111 void MML_SetupEMS (void)
113 char str[80],str2[10];
115 boolean errorflag=false;
118 totalEMSpages = freeEMSpages = EMSpageframe = EMSpagesmapped = 0;
123 int EMS_INT // make sure EMS hardware is present
131 cmp al,0x32 // only work on ems 3.2 or greater
135 int EMS_INT // find the page frame address
138 mov [EMSpageframe],bx
141 int EMS_INT // find out how much EMS is there
144 mov [totalEMSpages],dx
145 mov [freeEMSpages],bx
147 jz noEMS // no EMS at all to allocate
150 jle getpages // there is only 1,2,3,or 4 pages
151 mov bx,4 // we can't use more than 4 pages
154 mov [EMSpagesmapped],bx
155 mov ah,EMS_ALLOCPAGES // allocate up to 64k of EMS
170 strcpy(str,"MML_SetupEMS: EMS error 0x");
179 ======================
183 =======================
186 void MML_ShutdownEMS (void)
188 boolean errorflag=false;
201 if(errorflag==true) printf("MML_ShutdownEMS: Error freeing EMS!"); //++++ add something
209 = Maps the 64k of EMS used by memory manager into the page frame
210 = for general use. This only needs to be called if you are keeping
211 = other things in EMS.
216 void MM_MapEMS (void)
218 char str[80],str2[10];
220 boolean errorflag=false;
224 for (i=0;i<EMSpagesmapped;i++)
229 mov bx,[i] // logical page
230 mov al,bl // physical page
231 mov dx,[EMShandle] // handle
243 strcpy(str,"MM_MapEMS: EMS error 0x");
252 //==========================================================================
255 ======================
259 = Check for XMM driver
261 =======================
264 boolean MML_CheckForXMS (void)
266 boolean errorflag=false;
272 int 0x2f // query status of installed diver
278 if(errorflag==true) return false;
284 ======================
288 = Try to allocate all upper memory block
290 =======================
293 void MML_SetupXMS (void)
301 mov [WORD PTR XMSaddr],bx
302 mov [WORD PTR XMSaddr+2],es // function pointer to XMS driver
308 mov dx,0xffff // try for largest block possible
309 call [DWORD PTR XMSaddr]
313 cmp bl,0xb0 // error: smaller UMB is available
317 call [DWORD PTR XMSaddr] // DX holds largest available UMB
319 jz done // another error...
326 MML_UseSpace (base,size);
327 mminfo.XMSmem += size*16;
328 UMBbase[numUMBs] = base;
330 if (numUMBs < MAXUMBS)
336 ======================
340 ======================
343 void MML_ShutdownXMS (void)
348 for (i=0;i<numUMBs;i++)
355 call [DWORD PTR XMSaddr]
360 //==========================================================================
363 ======================
367 = Marks a range of paragraphs as usable by the memory manager
368 = This is used to mark space for the near heap, far heap, ems page frame,
369 = and upper memory blocks
371 ======================
374 void MML_UseSpace (unsigned segstart, unsigned seglength)
376 mmblocktype far *scan,far *last;
380 scan = last = mmhead;
381 mmrover = mmhead; // reset rover to start of memory
384 // search for the block that contains the range of segments
386 while (scan->start+scan->length < segstart)
393 // take the given range out of the block
395 oldend = scan->start + scan->length;
396 extra = oldend - (segstart+seglength);
399 printf("MML_UseSpace: Segment spans two blocks!");
404 if (segstart == scan->start)
406 last->next = scan->next; // unlink block
411 scan->length = segstart-scan->start; // shorten block
416 mmnew->next = scan->next;
418 mmnew->start = segstart+seglength;
419 mmnew->length = extra;
420 mmnew->attributes = LOCKBIT;
425 //==========================================================================
432 = We are out of blocks, so free a purgable block
437 void MML_ClearBlock (void)
439 mmblocktype far *scan,far *last;
445 if (!(scan->attributes&LOCKBIT) && (scan->attributes&PURGEBITS) )
447 MM_FreePtr(scan->useptr);
453 printf("MM_ClearBlock: No purgable blocks!");
457 //==========================================================================
464 = Grabs all space from turbo with malloc/farmalloc
465 = Allocates bufferseg misc buffer
470 void MM_Startup (void)
475 unsigned segstart,seglength,endfree;
484 // set up the linked list (everything in the free list;
487 mmfree = &mmblocks[0];
488 for (i=0;i<MAXBLOCKS-1;i++)
489 mmblocks[i].next = &mmblocks[i+1];
490 mmblocks[i].next = NULL;
493 // locked block of all memory until we punch out free space
496 mmhead = mmnew; // this will allways be the first node
498 mmnew->length = 0xffff;
499 mmnew->attributes = LOCKBIT;
505 // get all available near conventional memory segments
507 //---- length=coreleft();
509 start = (void far *)(nearheap = malloc(length));
511 length -= 16-(FP_OFF(start)&15);
512 length -= SAVENEARHEAP;
513 seglength = length / 16; // now in paragraphs
514 segstart = FP_SEG(start)+(FP_OFF(start)+15)/16;
515 MML_UseSpace (segstart,seglength);
516 mminfo.nearheap = length;
519 // get all available far conventional memory segments
521 //---- length=farcoreleft();
523 start = farheap = _fmalloc(length);
524 length -= 16-(FP_OFF(start)&15);
525 length -= SAVEFARHEAP;
526 seglength = length / 16; // now in paragraphs
527 segstart = FP_SEG(start)+(FP_OFF(start)+15)/16;
528 MML_UseSpace (segstart,seglength);
529 mminfo.farheap = length;
530 mminfo.mainmem = mminfo.nearheap + mminfo.farheap;
534 // detect EMS and allocate up to 64K at page frame
537 for (i = 1;i < __argc;i++)
539 if ( US_CheckParm(__argv[i],ParmStringsexmm) == 0)
540 goto emsskip; // param NOEMS
543 if (MML_CheckForEMS())
546 MML_SetupEMS(); // allocate space
547 MML_UseSpace (EMSpageframe,EMSpagesmapped*0x400);
548 MM_MapEMS(); // map in used pages
549 mminfo.EMSmem = EMSpagesmapped*0x4000l;
553 // detect XMS and get upper memory blocks
557 for (i = 1;i < __argc;i++)
559 if ( US_CheckParm(__argv[i],ParmStringsexmm) == 0)
560 goto xmsskip; // param NOXMS
563 if (MML_CheckForXMS())
566 MML_SetupXMS(); // allocate as many UMBs as possible
570 // allocate the misc buffer
573 mmrover = mmhead; // start looking for space after low block
575 MM_GetPtr (&bufferseg,BUFFERSIZE);
578 //==========================================================================
585 = Frees all conventional, EMS, and XMS allocated
590 void MM_Shutdown (void)
602 //==========================================================================
609 = Allocates an unlocked, unpurgable block
614 void MM_GetPtr (memptr *baseptr,dword size)
616 mmblocktype far *scan,far *lastscan,far *endscan
617 ,far *purge,far *next;
619 unsigned needed,startseg;
621 needed = (size+15)/16; // convert size from bytes to paragraphs
623 GETNEWBLOCK; // fill in start and next after a spot is found
624 mmnew->length = needed;
625 mmnew->useptr = baseptr;
626 mmnew->attributes = BASEATTRIBUTES;
628 for (search = 0; search<3; search++)
631 // first search: try to allocate right after the rover, then on up
632 // second search: search from the head pointer up to the rover
633 // third search: compress memory, then scan from start
634 if (search == 1 && mmrover == mmhead)
641 scan = mmrover->next;
657 startseg = lastscan->start + lastscan->length;
659 while (scan != endscan)
661 if (scan->start - startseg >= needed)
664 // got enough space between the end of lastscan and
665 // the start of scan, so throw out anything in the middle
666 // and allocate the new block
668 purge = lastscan->next;
669 lastscan->next = mmnew;
670 mmnew->start = *(unsigned *)baseptr = startseg;
672 while ( purge != scan)
673 { // free the purgable block
676 purge = next; // purge another if not at scan
679 return; // good allocation!
683 // if this block is purge level zero or locked, skip past it
685 if ( (scan->attributes & LOCKBIT)
686 || !(scan->attributes & PURGEBITS) )
689 startseg = lastscan->start + lastscan->length;
693 scan=scan->next; // look at next line
698 printf(OUT_OF_MEM_MSG,(size-mminfo.nearheap));
703 //==========================================================================
710 = Allocates an unlocked, unpurgable block
715 void MM_FreePtr (memptr *baseptr)
717 mmblocktype far *scan,far *last;
722 if (baseptr == mmrover->useptr) // removed the last allocated block
725 while (scan->useptr != baseptr && scan)
733 printf("MM_FreePtr: Block not found!");
737 last->next = scan->next;
741 //==========================================================================
744 =====================
748 = Sets the purge level for a block (locked blocks cannot be made purgable)
750 =====================
753 void MM_SetPurge (memptr *baseptr, int purge)
755 mmblocktype far *start;
761 if (mmrover->useptr == baseptr)
764 mmrover = mmrover->next;
768 else if (mmrover == start)
770 printf("MM_SetPurge: Block not found!");
776 mmrover->attributes &= ~PURGEBITS;
777 mmrover->attributes |= purge;
780 //==========================================================================
783 =====================
787 = Locks / unlocks the block
789 =====================
792 void MM_SetLock (memptr *baseptr, boolean locked)
794 mmblocktype far *start;
800 if (mmrover->useptr == baseptr)
803 mmrover = mmrover->next;
807 else if (mmrover == start)
809 printf("MM_SetLock: Block not found!");
815 mmrover->attributes &= ~LOCKBIT;
816 mmrover->attributes |= locked*LOCKBIT;
819 //==========================================================================
822 =====================
826 = Throws out all purgable stuff and compresses movable blocks
828 =====================
831 void MM_SortMem (void)
833 mmblocktype far *scan,far *last,far *next;
834 unsigned start,length,source,dest,oldborder;
838 // lock down a currently playing sound
840 /*++++ playing = SD_SoundPlaying ();
846 playing += STARTPCSOUNDS;
849 playing += STARTADLIBSOUNDS;
852 MM_SetLock(&(memptr)audiosegs[playing],true);
857 // oldborder = bordercolor;
858 // VW_ColorBorder (15);
865 last = NULL; // shut up compiler warning
869 if (scan->attributes & LOCKBIT)
872 // block is locked, so try to pile later blocks right after it
874 start = scan->start + scan->length;
878 if (scan->attributes & PURGEBITS)
881 // throw out the purgable block
892 // push the non purgable block on top of the last moved block
894 if (scan->start != start)
896 length = scan->length;
897 source = scan->start;
899 while (length > 0xf00)
901 movedata(source,0,dest,0,0xf00*16);
906 movedata(source,0,dest,0,length*16);
909 *(unsigned *)scan->useptr = start;
911 start = scan->start + scan->length;
916 scan = scan->next; // go to next block
924 // VW_ColorBorder (oldborder);
927 MM_SetLock(&(memptr)audiosegs[playing],false);*/
931 //==========================================================================
935 =====================
939 =====================
942 void MM_ShowMemory (void)
944 mmblocktype far *scan;
945 unsigned color,temp;//, i;
947 char scratch[80],str[10];
949 //**** VW_SetDefaultColors();
950 //**** VW_SetLineWidth(40);
951 //++++mh temp = bufferofs;
952 //++++mh bufferofs = 0;
953 //**** VW_SetScreen (0,0);
963 if (scan->attributes & PURGEBITS)
964 color = 5; // dark purple = purgable
966 color = 9; // medium blue = non purgable
967 if (scan->attributes & LOCKBIT)
968 color = 12; // red = locked
969 if (scan->start<=end)
971 printf("MM_ShowMemory: Memory block order currupted!");
974 end = scan->start+scan->length-1;
975 //++++ VW_Hlin(scan->start,(unsigned)end,0,color);
976 //++++ VW_Plot(scan->start,0,15);
977 if (scan->next->start > end+1)
978 //++++ VW_Hlin(end+1,scan->next->start,0,0); // black = free
982 printf("%Fp\t", scan->start);
983 strcpy (scratch,"Size:");
984 ltoa ((long)scan->length*16,str,10);
985 strcat (scratch,str);
986 strcat (scratch,"\tOwner:0x");
987 owner = (unsigned)scan->useptr;
988 ultoa (owner,str,16);
989 strcat (scratch,str);
990 strcat (scratch,"\n");
991 //++++write (debughandle,scratch,strlen(scratch));
992 fprintf(stdout, "%s", scratch);
1001 //**** VW_SetLineWidth(64);
1002 //++++mh bufferofs = temp;
1006 //==========================================================================
1010 ======================
1014 = Returns the total free space without purging
1016 ======================
1019 long MM_UnusedMemory (void)
1022 mmblocktype far *scan;
1029 free += scan->next->start - (scan->start + scan->length);
1036 //==========================================================================
1040 ======================
1044 = Returns the total free space with purging
1046 ======================
1049 long MM_TotalFree (void)
1052 mmblocktype far *scan;
1059 if ((scan->attributes&PURGEBITS) && !(scan->attributes&LOCKBIT))
1060 free += scan->length;
1061 free += scan->next->start - (scan->start + scan->length);
1068 //==========================================================================
1071 =====================
1075 =====================
1078 void MM_BombOnError (boolean bomb)
1083 ///////////////////////////////////////////////////////////////////////////
1085 // US_CheckParm() - checks to see if a string matches one of a set of
1086 // strings. The check is case insensitive. The routine returns the
1087 // index of the string that matched, or -1 if no matches were found
1089 ///////////////////////////////////////////////////////////////////////////
1091 US_CheckParm(char *parm,char **strings)
1097 while (!isalpha(*parm)) // Skip non-alphas
1100 for (i = 0;*strings && **strings;i++)
1102 for (s = *strings++,p = parm,cs = cp = 0;cs == cp;)