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
180 cmp bx,[freeEMSpages]
182 mov bx,[freeEMSpages]
187 jle getpages // there is only 1,2,3,or 4 pages
188 mov bx,4 // we can't use more than 4 pages
191 mov [EMSpagesmapped],bx
192 mov ah,EMS_ALLOCPAGES // allocate up to 64k of EMS
207 strcpy(str,"MML_SetupEMS: EMS error 0x");
213 mm->totalEMSpages=totalEMSpages;
214 mm->freeEMSpages=freeEMSpages;
215 mm->EMSpageframe=EMSpageframe;
216 mm->EMSpagesmapped=EMSpagesmapped;
217 mm->EMShandle=EMShandle;
224 ======================
228 =======================
231 void MML_ShutdownEMS(mminfo_t *mm)
233 boolean errorflag=false;
234 unsigned EMShandle=mm->EMShandle;
248 if(errorflag==true) printf("MML_ShutdownEMS: Error freeing EMS!"); //++++ add something
256 = Maps the 64k of EMS used by memory manager into the page frame
257 = for general use. This only needs to be called if you are keeping
258 = other things in EMS.
263 unsigned MM_MapEMS(mminfo_t *mm)
265 char str[80],str2[10];
266 unsigned err, EMShandle;
267 boolean errorflag=false;
270 EMShandle=mm->EMShandle;
272 for (i=0;i<mm->EMSpagesmapped;i++)
277 mov bx,[i] // logical page
278 mov al,bl // physical page
279 mov dx,[EMShandle] // handle
291 strcpy(str,"MM_MapEMS: EMS error 0x");
301 //==========================================================================
304 ======================
308 = Check for XMM driver
310 =======================
313 boolean MML_CheckForXMS(mminfo_t *mm)
315 boolean errorflag=false;
321 int 0x2f // query status of installed diver
327 if(errorflag==true) return false;
333 ======================
337 = Try to allocate all upper memory block
339 =======================
342 void MML_SetupXMS(mminfo_t *mm, mminfotype *mmi)
351 mov [WORD PTR XMSaddr],bx
352 mov [WORD PTR XMSaddr+2],es // function pointer to XMS driver
355 mov dx,0xffff // try for largest block possible
356 //mov ax,dx // Set available Kbytes.
357 call [DWORD PTR XMSaddr]
361 cmp bl,0xb0 // error: smaller UMB is available
365 call [DWORD PTR XMSaddr] // DX holds largest available UMB
367 jz done // another error...
374 printf("base=%u ", base); printf("size=%u\n", size);
375 MML_UseSpace(base,size, mm);
376 mmi->XMSmem += size*16;
377 mm->UMBbase[mm->numUMBs] = base;
379 if(mm->numUMBs < MAXUMBS)
385 ======================
389 ======================
392 void MML_ShutdownXMS(mminfo_t *mm)
397 for (i=0;i<mm->numUMBs;i++)
399 base = mm->UMBbase[i];
404 call [DWORD PTR XMSaddr]
409 //==========================================================================
412 ======================
416 = Marks a range of paragraphs as usable by the memory manager
417 = This is used to mark space for the near heap, far heap, ems page frame,
418 = and upper memory blocks
420 ======================
423 void MML_UseSpace(/*d*/word segstart, dword seglength, mminfo_t *mm)
425 mmblocktype huge *scan,huge *last;
430 scan = last = mm->mmhead;
431 mm->mmrover = mm->mmhead; // reset rover to start of memory
433 scan->segmlen=seglength;
436 // search for the block that contains the range of segments
438 while(scan->start+scan->length < segstart)
444 //find out how many blocks it span!
445 if(seglength>0xfffflu)
447 scan->segm=seglength/0xfffflu;
451 // take the given range out of the block
453 oldend = scan->start + scan->length;
454 extra = oldend - (segstart+seglength);
465 printf("segm=%u ", scan->segm);
466 printf("ex=%lu ", extra);
467 printf("len=%u ", scan->length);
468 printf("segsta=%u ", segstart);
469 printf("seglen=%lu\n", seglength);
471 //MML_UseSpace(?segstart?, ?length?, mm);
477 //printf("MML_UseSpace: Segment spans two blocks!\n");
481 //++++todo: linked list of segment!
482 //printf("segm=%lu\n", segm);
483 if(segstart == scan->start)
485 last->next = scan->next; // unlink block
486 MM_FreeBlock(scan, mm);
490 scan->length = segstart-scan->start; // shorten block
497 mm->mmnew->next = scan->next;
498 scan->next = mm->mmnew;
499 mm->mmnew->start = segstart+seglength;
500 mm->mmnew->length = extra;
501 mm->mmnew->attributes = LOCKBIT;
502 }//else if(segm>0) goto segu;
506 //==========================================================================
513 = We are out of blocks, so free a purgable block
518 void MML_ClearBlock(mminfo_t *mm)
520 mmblocktype huge *scan,huge *last;
522 scan = mm->mmhead->next;
526 if(!(scan->attributes&LOCKBIT) && (scan->attributes&PURGEBITS))
528 MM_FreePtr(scan->useptr, mm);
534 printf("MM_ClearBlock: No purgable blocks!\n");
538 //==========================================================================
545 = Grabs all space from turbo with malloc/farmalloc
546 = Allocates bufferseg misc buffer
551 void MM_Startup(mminfo_t *mm, mminfotype *mmi)
554 dword length;//,farlen;
556 unsigned segstart,seglength,endfree;
561 mm->mmstarted = true;
562 mm->bombonerror = true;
564 // set up the linked list (everything in the free list;
567 mm->mmfree = &(mm->mmblocks[0]);
568 for(i=0;i<MAXBLOCKS-1;i++)
570 mm->mmblocks[i].next = &(mm->mmblocks[i+1]);
572 mm->mmblocks[i].next = NULL;
575 // locked block of all memory until we punch out free space
578 mm->mmhead = mm->mmnew; // this will allways be the first node
579 mm->mmnew->start = 0;
580 mm->mmnew->length = 0xffff; //todo: mm make it fucking massive as fuck!~
581 mm->mmnew->attributes = LOCKBIT;
582 mm->mmnew->next = NULL;
583 mm->mmrover = mm->mmhead;
585 // farlen=_bios_memsize()*1024;
588 // get all available near conventional memory segments
590 //---- length=coreleft();
593 start = (void huge *)(mm->nearheap = malloc(length));
595 length -= 16-(FP_OFF(start)&15);
596 length -= SAVENEARHEAP;
597 seglength = length / 16; // now in paragraphs
598 segstart = FP_SEG(start)+(FP_OFF(start)+15)/16;
599 MML_UseSpace(segstart,seglength, mm);
600 mmi->nearheap = length;
603 // get all available far conventional memory segments
605 //---- length=farcoreleft();
609 start = mm->farheap = halloc(length, sizeof(dword));
610 //start = mm->farheap = _fmalloc(length);
611 length -= 16-(FP_OFF(start)&15);
612 length -= SAVEFARHEAP;
613 seglength = length / 16; // now in paragraphs
614 segstart = FP_SEG(start)+(FP_OFF(start)+15)/16;
615 MML_UseSpace(segstart,seglength, mm);
616 mmi->farheap = length;
617 mmi->mainmem = mmi->nearheap + mmi->farheap;
621 // detect EMS and allocate up to 64K at page frame
623 printf("EMS!\n\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!
625 for(i = 1;i < __argc;i++)
627 if(US_CheckParm(__argv[i],ParmStringsexmm) == 0)
628 goto emsskip; // param NOEMS
630 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!
631 if(MML_CheckForEMS())
634 MML_SetupEMS(mm); // allocate space
635 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!
636 //TODO: EMS4! AND EMS 3.2 MASSIVE DATA HANDLMENT!
637 MML_UseSpace(mm->EMSpageframe,(mm->EMSpagesmapped)*0x4000lu, mm);
639 MM_MapEMS(mm); // map in used pages
641 mmi->EMSmem = (mm->EMSpagesmapped)*0x4000lu;
645 // detect XMS and get upper memory blocks
649 for(i = 1;i < __argc;i++)
651 if(US_CheckParm(__argv[i],ParmStringsexmm) == 0)
652 goto xmsskip; // param NOXMS
654 // 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!
655 if(MML_CheckForXMS(mm))
658 MML_SetupXMS(mm, mmi); // allocate as many UMBs as possible
662 // allocate the misc buffer
665 mm->mmrover = mm->mmhead; // start looking for space after low block
667 MM_GetPtr(&(mm->bufferseg),BUFFERSIZE, mm, mmi);
670 //==========================================================================
677 = Frees all conventional, EMS, and XMS allocated
682 void MM_Shutdown(mminfo_t *mm)
688 printf("far freed\n");
690 printf("near freed\n");
691 //hfree(mm->hugeheap);
692 //printf("huge freed\n");
693 if(MML_CheckForEMS()){ MML_ShutdownEMS(mm); printf("EMS freed\n"); }
694 if(MML_CheckForXMS(mm)){ MML_ShutdownXMS(mm); printf("XMS freed\n"); }
697 //==========================================================================
704 = Allocates an unlocked, unpurgable block
709 void MM_GetPtr(memptr *baseptr,dword size, mminfo_t *mm, mminfotype *mmi)
711 mmblocktype huge *scan,huge *lastscan,huge *endscan,huge *purge,huge *next;
713 unsigned needed,startseg;
715 needed = (size+15)/16; // convert size from bytes to paragraphs
717 MM_GetNewBlock(mm); // fill in start and next after a spot is found
718 mm->mmnew->length = needed;
719 mm->mmnew->useptr = baseptr;
720 mm->mmnew->attributes = BASEATTRIBUTES;
722 for(search = 0; search<3; search++)
725 // first search: try to allocate right after the rover, then on up
726 // second search: search from the head pointer up to the rover
727 // third search: compress memory, then scan from start
728 if(search == 1 && mm->mmrover == mm->mmhead)
734 lastscan = mm->mmrover;
735 scan = mm->mmrover->next;
739 lastscan = mm->mmhead;
740 scan = mm->mmhead->next;
741 endscan = mm->mmrover;
745 lastscan = mm->mmhead;
746 scan = mm->mmhead->next;
751 startseg = lastscan->start + lastscan->length;
753 while(scan != endscan)
755 if(scan->start - startseg >= needed)
758 // got enough space between the end of lastscan and
759 // the start of scan, so throw out anything in the middle
760 // and allocate the new block
762 purge = lastscan->next;
763 lastscan->next = mm->mmnew;
764 mm->mmnew->start = *(unsigned *)baseptr = startseg;
765 mm->mmnew->next = scan;
767 { // free the purgable block
769 MM_FreeBlock(purge, mm);
770 purge = next; // purge another if not at scan
772 mm->mmrover = mm->mmnew;
773 return; // good allocation!
777 // if this block is purge level zero or locked, skip past it
779 if((scan->attributes & LOCKBIT)
780 || !(scan->attributes & PURGEBITS) )
783 startseg = lastscan->start + lastscan->length;
787 scan=scan->next; // look at next line
792 printf(OUT_OF_MEM_MSG,(size-mmi->nearheap));
797 //==========================================================================
804 = Allocates an unlocked, unpurgable block
809 void MM_FreePtr(memptr *baseptr, mminfo_t *mm)
811 mmblocktype huge *scan,huge *last;
816 if(baseptr == mm->mmrover->useptr) // removed the last allocated block
817 mm->mmrover = mm->mmhead;
819 while(scan->useptr != baseptr && scan)
827 printf("MM_FreePtr: Block not found!");
831 last->next = scan->next;
833 MM_FreeBlock(scan, mm);
835 //==========================================================================
838 =====================
842 = Sets the purge level for a block (locked blocks cannot be made purgable)
844 =====================
847 void MM_SetPurge(memptr *baseptr, int purge, mminfo_t *mm)
849 mmblocktype huge *start;
855 if(mm->mmrover->useptr == baseptr)
858 mm->mmrover = mm->mmrover->next;
861 mm->mmrover = mm->mmhead;
862 else if(mm->mmrover == start)
864 printf("MM_SetPurge: Block not found!");
870 mm->mmrover->attributes &= ~PURGEBITS;
871 mm->mmrover->attributes |= purge;
874 //==========================================================================
877 =====================
881 = Locks / unlocks the block
883 =====================
886 void MM_SetLock(memptr *baseptr, boolean locked, mminfo_t *mm)
888 mmblocktype huge *start;
894 if(mm->mmrover->useptr == baseptr)
897 mm->mmrover = mm->mmrover->next;
900 mm->mmrover = mm->mmhead;
901 else if(mm->mmrover == start)
903 printf("MM_SetLock: Block not found!");
909 mm->mmrover->attributes &= ~LOCKBIT;
910 mm->mmrover->attributes |= locked*LOCKBIT;
913 //==========================================================================
916 =====================
920 = Throws out all purgable stuff and compresses movable blocks
922 =====================
925 void MM_SortMem(mminfo_t *mm)
927 mmblocktype huge *scan,huge *last,huge *next;
928 unsigned start,length,source,dest,oldborder;
932 // lock down a currently playing sound
934 /*++++ playing = SD_SoundPlaying ();
940 playing += STARTPCSOUNDS;
943 playing += STARTADLIBSOUNDS;
946 MM_SetLock(&(memptr)audiosegs[playing],true);
951 // oldborder = bordercolor;
952 // VW_ColorBorder (15);
959 last = NULL; // shut up compiler warning
963 if(scan->attributes & LOCKBIT)
966 // block is locked, so try to pile later blocks right after it
968 start = scan->start + scan->length;
972 if(scan->attributes & PURGEBITS)
975 // throw out the purgable block
978 MM_FreeBlock(scan, mm);
986 // push the non purgable block on top of the last moved block
988 if(scan->start != start)
990 length = scan->length;
991 source = scan->start;
993 while(length > 0xf00)
995 movedata(source,0,dest,0,0xf00*16);
1000 movedata(source,0,dest,0,length*16);
1002 scan->start = start;
1003 *(unsigned *)scan->useptr = start;
1005 start = scan->start + scan->length;
1010 scan = scan->next; // go to next block
1013 mm->mmrover = mm->mmhead;
1018 // VW_ColorBorder (oldborder);
1021 MM_SetLock(&(memptr)audiosegs[playing],false);*/
1025 //==========================================================================
1029 =====================
1033 =====================
1036 void MM_ShowMemory(mminfo_t *mm)
1038 mmblocktype huge *scan;
1039 unsigned color,temp;//, i;
1041 char scratch[160],str[16];
1043 //**** VW_SetDefaultColors();
1044 //**** VW_SetLineWidth(40);
1045 //++++mh temp = bufferofs;
1046 //++++mh bufferofs = 0;
1047 //**** VW_SetScreen (0,0);
1057 if(scan->attributes & PURGEBITS)
1058 color = 5; // dark purple = purgable
1060 color = 9; // medium blue = non purgable
1061 if(scan->attributes & LOCKBIT)
1062 color = 12; // red = locked
1063 if(scan->start<=end)
1065 printf("\nMM_ShowMemory: Memory block order currupted!\n");
1068 end = scan->start+scan->length-1;
1069 //++++ VW_Hlin(scan->start,(unsigned)end,0,color);
1070 //++++ VW_Plot(scan->start,0,15);
1071 if(scan->next->start > end+1)
1072 //++++ VW_Hlin(end+1,scan->next->start,0,0); // black = free
1075 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!
1076 strcpy(scratch,"Location:");
1077 ultoa (scan->start,str,16);
1078 strcat (scratch,str);
1079 strcat (scratch,"\tSize:");
1080 ltoa ((dword)scan->length*16,str,10);
1081 strcat (scratch,str);
1082 strcat (scratch,"\tOwner:0x");
1083 owner = (unsigned)scan->useptr;
1084 ultoa (owner,str,16);
1085 strcat (scratch,str);
1086 strcat (scratch,"\n");
1087 //++++write (debughandle,scratch,strlen(scratch));
1088 fprintf(stdout, "%s", scratch);
1097 //**** VW_SetLineWidth(64);
1098 //++++mh bufferofs = temp;
1102 //==========================================================================
1106 ======================
1110 = Returns the total free space without purging
1112 ======================
1115 dword MM_UnusedMemory(mminfo_t *mm)
1118 mmblocktype huge *scan;
1125 free += scan->next->start - (scan->start + scan->length);
1133 //==========================================================================
1137 ======================
1141 = Returns the total free space with purging
1143 ======================
1146 dword MM_TotalFree(mminfo_t *mm)
1149 mmblocktype huge *scan;
1156 if((scan->attributes&PURGEBITS) && !(scan->attributes&LOCKBIT))
1157 free += scan->length;
1158 free += scan->next->start - (scan->start + scan->length);
1166 //==========================================================================
1169 =====================
1173 =====================
1176 void MM_Report(mminfo_t *mm, mminfotype *mmi)
1178 if(MML_CheckForEMS())
1180 printf("EMM %x available\n", mm->EMSVer);
1181 printf("totalEMSpages=%u\n", mm->totalEMSpages);
1182 printf("freeEMSpages=%u\n", mm->freeEMSpages);
1183 printf("EMSpageframe=%x\n", mm->EMSpageframe);
1185 if(MML_CheckForXMS(mm)) printf("XMSaddr=%Fp\n", *XMSaddr);
1186 printf("near=%lu\n", mmi->nearheap);
1187 printf("far=%lu\n", mmi->farheap);
1188 printf("EMSmem=%lu\n", mmi->EMSmem);
1189 printf("XMSmem=%lu\n", mmi->XMSmem);
1190 printf("mainmem=%lu\n", mmi->mainmem);
1191 printf("UnusedMemory=%lu\n", MM_UnusedMemory(mm));
1192 printf("TotalFree=%lu\n", MM_TotalFree(mm));
1194 // printf("UnusedMemory=%lu kb\n", MM_UnusedMemory()/10248);
1195 // printf("TotalFree=%lu kb\n", MM_TotalFree()/10248);
1198 //==========================================================================
1201 =====================
1205 =====================
1220 //==========================================================================
1223 =====================
1227 =====================
1230 void MM_BombOnError(boolean bomb, mminfo_t *mm)
1232 mm->bombonerror = bomb;
1235 void MM_GetNewBlock(mminfo_t *mm)
1239 mm->mmnew=mm->mmfree;
1240 mm->mmfree=mm->mmfree->next;
1241 /*if(!(mm->mmnew=mm->mmfree))
1243 printf("MM_GETNEWBLOCK: No free blocks!");
1246 mm->mmfree=mm->mmfree->next;*/
1249 void MM_FreeBlock(mmblocktype *x, mminfo_t *mm)
1256 void MM_seguin(void)
1267 void MM_segude(void)
1276 pull data from far and put it into ds var