1 /* Catacomb Apocalypse 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 =============================================================================
46 Open Watcom port by sparky4
49 #include "src/lib/16_mm.h"
52 =============================================================================
56 =============================================================================
59 void (* beforesort) (void);
60 void (* aftersort) (void);
61 void (* XMSaddr) (void); // far pointer to XMS driver
64 =============================================================================
68 =============================================================================
71 static char *ParmStringsexmm[] = {"noems","noxms",""};
74 ======================
78 = Routine from p36 of Extending DOS
80 =======================
83 boolean MML_CheckForEMS(void)
86 char emmname[] = "EMMXXXX0";
87 // mov dx,OFFSET emmname
89 LEA DX, emmname //fix by andrius4669
91 int 0x21 // try to open EMMXXXX0 device
97 int 0x21 // get device info
105 int 0x21 // get status
111 int 0x21 // close handle
130 ======================
134 =======================
137 unsigned MML_SetupEMS(mminfo_t *mm)
139 char str[80],str2[10];
141 boolean errorflag=false;
144 unsigned int EMSVer = 0;
145 unsigned totalEMSpages,freeEMSpages,EMSpageframe,EMSpagesmapped,EMShandle;
146 totalEMSpages = freeEMSpages = EMSpageframe = EMSpagesmapped = 0;
151 int EMS_INT // make sure EMS hardware is present
159 mov [EMSVer],ax // set EMSVer
160 cmp al,0x32 // only work on ems 3.2 or greater
164 int EMS_INT // find the page frame address
167 mov [EMSpageframe],bx
170 int EMS_INT // find out how much EMS is there
173 mov [totalEMSpages],dx
174 mov [freeEMSpages],bx
176 jz noEMS // no EMS at all to allocate
179 jle getpages // there is only 1,2,3,or 4 pages
180 mov bx,4 // we can't use more than 4 pages
183 mov [EMSpagesmapped],bx
184 mov ah,EMS_ALLOCPAGES // allocate up to 64k of EMS
199 strcpy(str,"MML_SetupEMS: EMS error 0x");
205 mm->totalEMSpages=totalEMSpages;
206 mm->freeEMSpages=freeEMSpages;
207 mm->EMSpageframe=EMSpageframe;
208 mm->EMSpagesmapped=EMSpagesmapped;
209 mm->EMShandle=EMShandle;
216 ======================
220 =======================
223 void MML_ShutdownEMS(mminfo_t *mm)
225 boolean errorflag=false;
226 unsigned EMShandle=mm->EMShandle;
240 if(errorflag==true) printf("MML_ShutdownEMS: Error freeing EMS!"); //++++ add something
248 = Maps the 64k of EMS used by memory manager into the page frame
249 = for general use. This only needs to be called if you are keeping
250 = other things in EMS.
255 unsigned MM_MapEMS(mminfo_t *mm)
257 char str[80],str2[10];
258 unsigned err, EMShandle;
259 boolean errorflag=false;
262 EMShandle=mm->EMShandle;
264 for (i=0;i<mm->EMSpagesmapped;i++)
269 mov bx,[i] // logical page
270 mov al,bl // physical page
271 mov dx,[EMShandle] // handle
283 strcpy(str,"MM_MapEMS: EMS error 0x");
293 //==========================================================================
296 ======================
300 = Check for XMM driver
302 =======================
305 boolean MML_CheckForXMS(mminfo_t *mm)
307 boolean errorflag=false;
313 int 0x2f // query status of installed diver
319 if(errorflag==true) return false;
325 ======================
329 = Try to allocate all upper memory block
331 =======================
334 void MML_SetupXMS(mminfo_t *mm, mminfotype *mmi)
343 mov [WORD PTR XMSaddr],bx
344 mov [WORD PTR XMSaddr+2],es // function pointer to XMS driver
347 mov dx,0xffff // try for largest block possible
348 call [DWORD PTR XMSaddr]
352 cmp bl,0xb0 // error: smaller UMB is available
356 call [DWORD PTR XMSaddr] // DX holds largest available UMB
358 jz done // another error...
365 MML_UseSpace(base,size, mm);
366 mmi->XMSmem += size*16;
367 mm->UMBbase[mm->numUMBs] = base;
369 if(mm->numUMBs < MAXUMBS)
375 ======================
379 ======================
382 void MML_ShutdownXMS(mminfo_t *mm)
387 for (i=0;i<mm->numUMBs;i++)
389 base = mm->UMBbase[i];
394 call [DWORD PTR XMSaddr]
399 //==========================================================================
402 ======================
406 = Marks a range of paragraphs as usable by the memory manager
407 = This is used to mark space for the near heap, far heap, ems page frame,
408 = and upper memory blocks
410 ======================
413 void MML_UseSpace(unsigned segstart, unsigned seglength, mminfo_t *mm)
415 mmblocktype huge *scan,huge *last;
420 scan = last = mm->mmhead;
421 mm->mmrover = mm->mmhead; // reset rover to start of memory
424 // search for the block that contains the range of segments
426 while(scan->start+scan->length < segstart)
433 // take the given range out of the block
435 oldend = scan->start + scan->length;
436 extra = oldend - (segstart+seglength);
440 printf("MML_UseSpace: Segment spans two blocks!");
445 if(segstart == scan->start)
447 last->next = scan->next; // unlink block
448 MM_FreeBlock(scan, mm);
452 scan->length = segstart-scan->start; // shorten block
457 mm->mmnew->next = scan->next;
458 scan->next = mm->mmnew;
459 mm->mmnew->start = segstart+seglength;
460 mm->mmnew->length = extra;
461 mm->mmnew->attributes = LOCKBIT;
466 //==========================================================================
473 = We are out of blocks, so free a purgable block
478 void MML_ClearBlock(mminfo_t *mm)
480 mmblocktype huge *scan,huge *last;
482 scan = mm->mmhead->next;
486 if(!(scan->attributes&LOCKBIT) && (scan->attributes&PURGEBITS))
488 MM_FreePtr(scan->useptr, mm);
494 printf("MM_ClearBlock: No purgable blocks!");
498 //==========================================================================
505 = Grabs all space from turbo with malloc/farmalloc
506 = Allocates bufferseg misc buffer
511 void MM_Startup(mminfo_t *mm, mminfotype *mmi)
516 unsigned segstart,seglength,endfree;
521 mm->mmstarted = true;
522 mm->bombonerror = true;
524 // set up the linked list (everything in the free list;
527 mm->mmfree = &(mm->mmblocks[0]);
528 for(i=0;i<MAXBLOCKS-1;i++)
530 mm->mmblocks[i].next = &(mm->mmblocks[i+1]);
532 mm->mmblocks[i].next = NULL;
535 // locked block of all memory until we punch out free space
538 mm->mmhead = mm->mmnew; // this will allways be the first node
539 mm->mmnew->start = 0;
540 mm->mmnew->length = 0xffff;
541 mm->mmnew->attributes = LOCKBIT;
542 mm->mmnew->next = NULL;
543 mm->mmrover = mm->mmhead;
547 // get all available near conventional memory segments
549 //---- length=coreleft();
552 start = (void huge *)(mm->nearheap = malloc(length));
554 length -= 16-(FP_OFF(start)&15);
555 length -= SAVENEARHEAP;
556 seglength = length / 16; // now in paragraphs
557 segstart = FP_SEG(start)+(FP_OFF(start)+15)/16;
558 MML_UseSpace(segstart,seglength, mm);
559 mmi->nearheap = length;
562 // get all available far conventional memory segments
564 //---- length=farcoreleft();
567 start = mm->farheap = halloc(length, 1);
568 //start = mm->farheap = _fmalloc(length);
569 length -= 16-(FP_OFF(start)&15);
570 length -= SAVEFARHEAP;
571 seglength = length / 16; // now in paragraphs
572 segstart = FP_SEG(start)+(FP_OFF(start)+15)/16;
573 MML_UseSpace(segstart,seglength, mm);
574 mmi->farheap = length;
575 mmi->mainmem = mmi->nearheap + mmi->farheap;
579 // detect EMS and allocate up to 64K at page frame
582 for(i = 1;i < __argc;i++)
584 if(US_CheckParm(__argv[i],ParmStringsexmm) == 0)
585 goto emsskip; // param NOEMS
587 printf("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"); //bug!
588 if(MML_CheckForEMS())
591 MML_SetupEMS(mm); // allocate space
592 printf("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"); //bug!
593 MML_UseSpace(mm->EMSpageframe,mm->EMSpagesmapped*0x400, mm);
595 MM_MapEMS(mm); // map in used pages
597 mmi->EMSmem = mm->EMSpagesmapped*0x4000l;
601 // detect XMS and get upper memory blocks
605 for(i = 1;i < __argc;i++)
607 if(US_CheckParm(__argv[i],ParmStringsexmm) == 0)
608 goto xmsskip; // param NOXMS
610 // printf("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"); //bug!
611 if(MML_CheckForXMS(mm))
614 MML_SetupXMS(mm, mmi); // allocate as many UMBs as possible
618 // allocate the misc buffer
621 mm->mmrover = mm->mmhead; // start looking for space after low block
623 MM_GetPtr(&(mm->bufferseg),BUFFERSIZE, mm, mmi);
626 //==========================================================================
633 = Frees all conventional, EMS, and XMS allocated
638 void MM_Shutdown(mminfo_t *mm)
644 printf("far freed\n");
646 printf("near freed\n");
647 //hfree(mm->hugeheap);
648 //printf("huge freed\n");
649 if(MML_CheckForEMS()){ MML_ShutdownEMS(mm); printf("EMS freed\n"); }
650 if(MML_CheckForXMS(mm)){ MML_ShutdownXMS(mm); printf("XMS freed\n"); }
653 //==========================================================================
660 = Allocates an unlocked, unpurgable block
665 void MM_GetPtr(memptr *baseptr,dword size, mminfo_t *mm, mminfotype *mmi)
667 mmblocktype huge *scan,huge *lastscan,huge *endscan,huge *purge,huge *next;
669 unsigned needed,startseg;
671 needed = (size+15)/16; // convert size from bytes to paragraphs
673 MM_GetNewBlock(mm); // fill in start and next after a spot is found
674 mm->mmnew->length = needed;
675 mm->mmnew->useptr = baseptr;
676 mm->mmnew->attributes = BASEATTRIBUTES;
678 for(search = 0; search<3; search++)
681 // first search: try to allocate right after the rover, then on up
682 // second search: search from the head pointer up to the rover
683 // third search: compress memory, then scan from start
684 if(search == 1 && mm->mmrover == mm->mmhead)
690 lastscan = mm->mmrover;
691 scan = mm->mmrover->next;
695 lastscan = mm->mmhead;
696 scan = mm->mmhead->next;
697 endscan = mm->mmrover;
701 lastscan = mm->mmhead;
702 scan = mm->mmhead->next;
707 startseg = lastscan->start + lastscan->length;
709 while(scan != endscan)
711 if(scan->start - startseg >= needed)
714 // got enough space between the end of lastscan and
715 // the start of scan, so throw out anything in the middle
716 // and allocate the new block
718 purge = lastscan->next;
719 lastscan->next = mm->mmnew;
720 mm->mmnew->start = *(unsigned *)baseptr = startseg;
721 mm->mmnew->next = scan;
723 { // free the purgable block
725 MM_FreeBlock(purge, mm);
726 purge = next; // purge another if not at scan
728 mm->mmrover = mm->mmnew;
729 return; // good allocation!
733 // if this block is purge level zero or locked, skip past it
735 if((scan->attributes & LOCKBIT)
736 || !(scan->attributes & PURGEBITS) )
739 startseg = lastscan->start + lastscan->length;
743 scan=scan->next; // look at next line
748 printf(OUT_OF_MEM_MSG,(size-mmi->nearheap));
753 //==========================================================================
760 = Allocates an unlocked, unpurgable block
765 void MM_FreePtr(memptr *baseptr, mminfo_t *mm)
767 mmblocktype huge *scan,huge *last;
772 if(baseptr == mm->mmrover->useptr) // removed the last allocated block
773 mm->mmrover = mm->mmhead;
775 while(scan->useptr != baseptr && scan)
783 printf("MM_FreePtr: Block not found!");
787 last->next = scan->next;
789 MM_FreeBlock(scan, mm);
791 //==========================================================================
794 =====================
798 = Sets the purge level for a block (locked blocks cannot be made purgable)
800 =====================
803 void MM_SetPurge(memptr *baseptr, int purge, mminfo_t *mm)
805 mmblocktype huge *start;
811 if(mm->mmrover->useptr == baseptr)
814 mm->mmrover = mm->mmrover->next;
817 mm->mmrover = mm->mmhead;
818 else if(mm->mmrover == start)
820 printf("MM_SetPurge: Block not found!");
826 mm->mmrover->attributes &= ~PURGEBITS;
827 mm->mmrover->attributes |= purge;
830 //==========================================================================
833 =====================
837 = Locks / unlocks the block
839 =====================
842 void MM_SetLock(memptr *baseptr, boolean locked, mminfo_t *mm)
844 mmblocktype huge *start;
850 if(mm->mmrover->useptr == baseptr)
853 mm->mmrover = mm->mmrover->next;
856 mm->mmrover = mm->mmhead;
857 else if(mm->mmrover == start)
859 printf("MM_SetLock: Block not found!");
865 mm->mmrover->attributes &= ~LOCKBIT;
866 mm->mmrover->attributes |= locked*LOCKBIT;
869 //==========================================================================
872 =====================
876 = Throws out all purgable stuff and compresses movable blocks
878 =====================
881 void MM_SortMem(mminfo_t *mm)
883 mmblocktype huge *scan,huge *last,huge *next;
884 unsigned start,length,source,dest,oldborder;
888 // lock down a currently playing sound
890 /*++++ playing = SD_SoundPlaying ();
896 playing += STARTPCSOUNDS;
899 playing += STARTADLIBSOUNDS;
902 MM_SetLock(&(memptr)audiosegs[playing],true);
907 // oldborder = bordercolor;
908 // VW_ColorBorder (15);
915 last = NULL; // shut up compiler warning
919 if(scan->attributes & LOCKBIT)
922 // block is locked, so try to pile later blocks right after it
924 start = scan->start + scan->length;
928 if(scan->attributes & PURGEBITS)
931 // throw out the purgable block
934 MM_FreeBlock(scan, mm);
942 // push the non purgable block on top of the last moved block
944 if(scan->start != start)
946 length = scan->length;
947 source = scan->start;
949 while(length > 0xf00)
951 movedata(source,0,dest,0,0xf00*16);
956 movedata(source,0,dest,0,length*16);
959 *(unsigned *)scan->useptr = start;
961 start = scan->start + scan->length;
966 scan = scan->next; // go to next block
969 mm->mmrover = mm->mmhead;
974 // VW_ColorBorder (oldborder);
977 MM_SetLock(&(memptr)audiosegs[playing],false);*/
981 //==========================================================================
985 =====================
989 =====================
992 void MM_ShowMemory(mminfo_t *mm)
994 mmblocktype huge *scan;
995 unsigned color,temp;//, i;
997 char scratch[80],str[10];
999 //**** VW_SetDefaultColors();
1000 //**** VW_SetLineWidth(40);
1001 //++++mh temp = bufferofs;
1002 //++++mh bufferofs = 0;
1003 //**** VW_SetScreen (0,0);
1013 if(scan->attributes & PURGEBITS)
1014 color = 5; // dark purple = purgable
1016 color = 9; // medium blue = non purgable
1017 if(scan->attributes & LOCKBIT)
1018 color = 12; // red = locked
1019 if(scan->start<=end)
1021 printf("MM_ShowMemory: Memory block order currupted!");
1024 end = scan->start+scan->length-1;
1025 //++++ VW_Hlin(scan->start,(unsigned)end,0,color);
1026 //++++ VW_Plot(scan->start,0,15);
1027 if(scan->next->start > end+1)
1028 //++++ VW_Hlin(end+1,scan->next->start,0,0); // black = free
1031 printf("Location:");
1032 printf("%x\t", scan->start);
1033 strcpy (scratch,"Size:");
1034 ltoa ((dword)scan->length*16,str,10);
1035 strcat (scratch,str);
1036 strcat (scratch,"\tOwner:0x");
1037 owner = (unsigned)scan->useptr;
1038 ultoa (owner,str,16);
1039 strcat (scratch,str);
1040 strcat (scratch,"\n");
1041 //++++write (debughandle,scratch,strlen(scratch));
1042 fprintf(stdout, "%s", scratch);
1051 //**** VW_SetLineWidth(64);
1052 //++++mh bufferofs = temp;
1056 //==========================================================================
1060 ======================
1064 = Returns the total free space without purging
1066 ======================
1069 dword MM_UnusedMemory(mminfo_t *mm)
1072 mmblocktype huge *scan;
1079 free += scan->next->start - (scan->start + scan->length);
1087 //==========================================================================
1091 ======================
1095 = Returns the total free space with purging
1097 ======================
1100 dword MM_TotalFree(mminfo_t *mm)
1103 mmblocktype huge *scan;
1110 if((scan->attributes&PURGEBITS) && !(scan->attributes&LOCKBIT))
1111 free += scan->length;
1112 free += scan->next->start - (scan->start + scan->length);
1120 //==========================================================================
1123 =====================
1127 =====================
1130 void MM_Report(mminfo_t *mm, mminfotype *mmi)
1132 if(MML_CheckForEMS())
1134 printf("EMM %x available\n", mm->EMSVer);
1135 printf("totalEMSpages=%u\n", mm->totalEMSpages);
1136 printf("freeEMSpages=%u\n", mm->freeEMSpages);
1137 printf("EMSpageframe=%x\n", mm->EMSpageframe);
1139 if(MML_CheckForXMS(mm)) printf("XMSaddr=%x\n", *XMSaddr);
1140 printf("near=%lu\n", mmi->nearheap);
1141 printf("far=%lu\n", mmi->farheap);
1142 printf("EMSmem=%lu\n", mmi->EMSmem);
1143 printf("XMSmem=%lu\n", mmi->XMSmem);
1144 printf("mainmem=%lu\n", mmi->mainmem);
1145 printf("UnusedMemory=%lu\n", MM_UnusedMemory(mm));
1146 printf("TotalFree=%lu\n", MM_TotalFree(mm));
1148 // printf("UnusedMemory=%lu kb\n", MM_UnusedMemory()/10248);
1149 // printf("TotalFree=%lu kb\n", MM_TotalFree()/10248);
1152 //==========================================================================
1155 =====================
1159 =====================
1174 //==========================================================================
1177 =====================
1181 =====================
1184 void MM_BombOnError(boolean bomb, mminfo_t *mm)
1186 mm->bombonerror = bomb;
1189 void MM_GetNewBlock(mminfo_t *mm)
1193 mm->mmnew=mm->mmfree;
1194 mm->mmfree=mm->mmfree->next;
1195 /*if(!(mm->mmnew=mm->mmfree))
1197 printf("MM_GETNEWBLOCK: No free blocks!");
1200 mm->mmfree=mm->mmfree->next;*/
1203 void MM_FreeBlock(mmblocktype *x, mminfo_t *mm)