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"
50 #include "src/lib/16_ca.h"
57 =============================================================================
61 =============================================================================
64 void (* beforesort) (void);
65 void (* aftersort) (void);
66 void (* XMSaddr) (void); // far pointer to XMS driver
69 =============================================================================
73 =============================================================================
76 static char *ParmStringsexmm[] = {"noems","noxms",""};
79 ======================
83 = Routine from p36 of Extending DOS
85 =======================
88 boolean MML_CheckForEMS(void)
91 static char emmname[] = "EMMXXXX0"; //fix by andrius4669
92 // mov dx,OFFSET emmname
94 mov dx,OFFSET emmname //fix by andrius4669
96 int 0x21 // try to open EMMXXXX0 device
102 int 0x21 // get device info
110 int 0x21 // get status
116 int 0x21 // close handle
146 ======================
150 =======================
153 byte MML_SetupEMS(mminfo_t *mm)
157 boolean errorflag=false;
159 unsigned int EMSVer = 0;
161 unsigned totalEMSpages,freeEMSpages,EMSpageframe,EMSpagesmapped,EMShandle;
162 totalEMSpages = freeEMSpages = EMSpageframe = EMSpagesmapped = 0;
166 int EMS_INT // make sure EMS hardware is present
168 //mov [EMS_status],ah
175 mov [EMSVer],ax // set EMSVer
176 cmp al,0x32 // only work on ems 3.2 or greater
180 int EMS_INT // find the page frame address
183 mov [EMSpageframe],bx
186 int EMS_INT // find out how much EMS is there
189 mov [totalEMSpages],dx
190 mov [freeEMSpages],bx
192 jz noEMS // no EMS at all to allocate
196 cmp bx,[freeEMSpages]
198 mov bx,[freeEMSpages]
208 jle getpages // there is only 1,2,3,or 4 pages
209 mov bx,4 // we can't use more than 4 pages
217 mov [EMSpagesmapped],bx
218 mov ah,EMS_ALLOCPAGES // allocate up to 64k of EMS
244 strcpy(str,"MM_SetupEMS: EMS error ");
249 mm->totalEMSpages=totalEMSpages;
250 mm->freeEMSpages=freeEMSpages;
251 mm->EMSpageframe=EMSpageframe;
252 mm->EMSpagesmapped=EMSpagesmapped;
253 mm->EMShandle=EMShandle;
260 ======================
264 =======================
267 void MML_ShutdownEMS(mminfo_t *mm)
269 boolean errorflag=false;
270 unsigned EMShandle=mm->EMShandle;
288 if(errorflag==true) printf("MML_ShutdownEMS: Error freeing EMS!\n"); //++++ add something
296 = Maps the 64k of EMS used by memory manager into the page frame
297 = for general use. This only needs to be called if you are keeping
298 = other things in EMS.
303 byte MM_MapEMS(mminfo_t *mm, mminfotype *mmi)
308 boolean errorflag=false;
310 EMShandle=mm->EMShandle;
312 for (i=0;i<4/*MAPPAGES*/;i++)
316 mov bx,[i] // logical page
317 mov al,bl // physical page
318 mov dx,[EMShandle] // handle
341 strcpy(str,"MM_MapEMS: EMS error ");
347 mmi->EMSmem = (i)*0x4000lu;
348 //printf(" mmi->EMSmem=%lu\n", mmi->EMSmem);
352 byte MM_MapXEMS(mminfo_t *mm, mminfotype *mmi)
354 //SUB EMS.MapXPages (PhysicalStart, LogicalStart, NumPages, Handle)
356 //Maps up to 4 logical EMS pages to physical pages in the page frame, where:
357 //PhysicalStart = Physical page first logical page is mapped to
358 //LogicalStart = First logical page to map
359 //NumPages = Number of pages to map (1 to 4)
360 //Handle = EMS handle logical pages are allocated to
362 /*//Create a buffer containing the page information
363 // FOR x = 0 TO NumPages - 1
364 // MapInfo$ = MapInfo$ + MKI$(LogicalStart + x) + MKI$(PhysicalStart + x)
367 // Regs.ax = 0x5000 //Map the pages in the buffer
368 // Regs.cx = NumPages //to the pageframe
370 // Regs.ds = VARSEG(MapInfo$)
371 // Regs.si = SADD(MapInfo$)
372 // InterruptX 0x67, Regs, Regs
373 // EMS.Error = (Regs.ax AND 0xFF00&) \ 0x100 //Store the status code
379 boolean errorflag=false;
381 EMShandle=mm->EMShandle;
386 for (i=0;i<MAPPAGES;i++)
390 mov cx,[i] // logical page
391 mov al,bl // physical page
392 mov dx,[EMShandle] // handle
415 //strcpy(str,"MM_MapXEMS: EMS error 0x");
416 strcpy(str,"MM_MapXEMS: EMS error ");
420 //printf("%s%x\n",str, err);
421 //printf("FACK! %x\n", err);
425 mmi->EMSmem = (i)*0x4000lu;
429 //==========================================================================
432 ======================
436 = Check for XMM driver
438 =======================
441 boolean MML_CheckForXMS(mminfo_t *mm)
443 boolean errorflag=false;
448 int 0x2f // query status of installed diver
459 if(errorflag==true) return false;
465 ======================
469 = Try to allocate all upper memory block
471 =======================
474 void MML_SetupXMS(mminfo_t *mm, mminfotype *mmi)
482 mov [WORD PTR XMSaddr],bx
483 mov [WORD PTR XMSaddr+2],es // function pointer to XMS driver
486 mov dx,0xffff // try for largest block possible
487 //mov ax,dx // Set available Kbytes.
488 call [DWORD PTR XMSaddr]
492 cmp bl,0xb0 // error: smaller UMB is available
496 call [DWORD PTR XMSaddr] // DX holds largest available UMB
498 jz done // another error...
515 // printf("base=%u ", base); printf("size=%u\n", size);
516 MML_UseSpace(base,size, mm);
517 mmi->XMSmem += size*16;
518 mm->UMBbase[mm->numUMBs] = base;
520 if(mm->numUMBs < MAXUMBS)
526 ======================
530 ======================
533 void MML_ShutdownXMS(mminfo_t *mm)
538 for (i=0;i<mm->numUMBs;i++)
540 base = mm->UMBbase[i];
544 call [DWORD PTR XMSaddr]
549 //==========================================================================
552 ======================
556 = Marks a range of paragraphs as usable by the memory manager
557 = This is used to mark space for the near heap, far heap, ems page frame,
558 = and upper memory blocks
560 ======================
563 void MML_UseSpace(word segstart, dword seglength, mminfo_t *mm)
565 mmblocktype huge *scan,huge *last;
571 scan = last = mm->mmhead;
572 mm->mmrover = mm->mmhead; // reset rover to start of memory
575 // search for the block that contains the range of segments
577 while(scan->start+scan->length < segstart)
581 //printf(" start=%x",scan->start);
582 //printf(" seglen=%lu\n", scan->length);
586 // take the given range out of the block
588 oldend = scan->start + scan->length;
589 extra = oldend - (segstart+seglength);
593 // find out how many blocks it spans!
595 for(;segmlen>0x10000;segmlen-=0x10000)
597 //printf(" seglen=%lu\n", segmlen);
602 if(segm>1/* || extra>=0x10000lu*/)
615 //MML_UseSpace(segstart, seglength, mm);
621 printf("MML_UseSpace: Segment spans two blocks!\n");
623 printf("========================================\n");
624 printf("segm=%u ", segm);
625 printf("ex=%lu ", extra);
626 printf("old=%u ", oldend);
627 printf("start+seglen=%lu\n", segstart+seglength);
628 printf("segsta=%x ", segstart);
629 printf("len=%lu ", scan->length);
630 printf("seglen=%lu ", seglength);
631 printf("segmlen=%lu\n", segmlen);
632 printf("========================================\n");
634 //++++todo: linked list of segment!
635 if(segstart == scan->start)
637 last->next = scan->next; // unlink block
642 scan->length = segstart-scan->start; // shorten block
647 mm->mmnew->useptr = NULL;
649 mm->mmnew->next = scan->next;
650 scan->next = mm->mmnew;
651 mm->mmnew->start = segstart+seglength;
652 mm->mmnew->length = extra;
653 mm->mmnew->attributes = LOCKBIT;
654 }//else if(segm>0) goto segu;
658 //==========================================================================
665 = We are out of blocks, so free a purgable block
670 void MML_ClearBlock(mminfo_t *mm)
672 mmblocktype huge *scan,huge *last;
674 scan = mm->mmhead->next;
678 if(!(scan->attributes&LOCKBIT) && (scan->attributes&PURGEBITS))
680 MM_FreePtr(scan->useptr, mm);
686 printf("MM_ClearBlock: No purgable blocks!\n");
690 //==========================================================================
697 = Grabs all space from turbo with malloc/farmalloc
698 = Allocates bufferseg misc buffer
703 void MM_Startup(mminfo_t *mm, mminfotype *mmi)
706 dword length,seglength;
707 //dword length; word seglength;
709 word segstart;//,endfree;
714 mm->mmstarted = true;
715 mm->bombonerror = true;
718 // set up the linked list (everything in the free list;
721 mm->mmfree = &(mm->mmblocks[0]);
722 for(i=0;i<MAXBLOCKS-1;i++)
724 mm->mmblocks[i].next = &(mm->mmblocks[i+1]);
726 mm->mmblocks[i].next = NULL;
729 // locked block of all memory until we punch out free space
732 mm->mmhead = mm->mmnew; // this will allways be the first node
733 mm->mmnew->start = 0;
734 mm->mmnew->length = 0xffff;
735 mm->mmnew->attributes = LOCKBIT;
736 mm->mmnew->next = NULL;
737 mm->mmrover = mm->mmhead;
740 // get all available near conventional memory segments
744 length=(dword)_memavl();//(dword)GetFreeSize();
745 start = (void huge *)(mm->nearheap = _nmalloc(length));
749 start = (void huge *)(mm->nearheap = malloc(length));
751 length -= 16-(FP_OFF(start)&15);
752 length -= SAVENEARHEAP;
753 seglength = length / 16; // now in paragraphs
754 segstart = FP_SEG(start)+(FP_OFF(start)+15)/16;
755 MML_UseSpace(segstart,seglength, mm);
756 mmi->nearheap = length;
757 //printf("start=%Fp segstart=%x seglen=%lu len=%lu\n", start, segstart, seglength, length);
760 // get all available far conventional memory segments
762 //printf("_FARCORELEFT %lu\n", _FCORELEFT);
767 printf("farcoreleft() %lu\n", farcoreleft());
768 printf("(farcoreleft()+32)-_FCORELEFT %d\n", (sword)((farcoreleft()+32)-_FCORELEFT));
770 length=_FCORELEFT;//_fcoreleft();//(dword)GetFarFreeSize();//0xffffUL*4UL;
771 start = mm->farheap = _fmalloc(length);
772 //start = mm->farheap = halloc(length, 1);
773 length -= 16-(FP_OFF(start)&15);
774 length -= SAVEFARHEAP;
775 seglength = length / 16; // now in paragraphs
776 segstart = FP_SEG(start)+(FP_OFF(start)+15)/16;
777 MML_UseSpace(segstart,seglength, mm);
778 mmi->farheap = length;
779 //printf("start=%Fp segstart=%x seglen=%lu len=%lu\n", start, segstart, seglength, length);
781 mmi->mainmem = mmi->nearheap + mmi->farheap;
784 // detect EMS and allocate up to 64K at page frame
787 //goto emsskip; //0000
804 ,ParmStringsexmm) == 0)
805 goto emsskip; // param NOEMS
807 if(MML_CheckForEMS())
809 MML_SetupEMS(mm); // allocate space
810 //TODO: EMS4! AND EMS 3.2 MASSIVE DATA HANDLMENT!
811 MML_UseSpace(mm->EMSpageframe,(MAPPAGES)*0x4000lu, mm);
812 //if(mm->EMSVer<0x40)
813 MM_MapEMS(mm, mmi); // map in used pages
815 //MM_MapXEMS(mm, mmi); // map in used pages
819 // detect XMS and get upper memory blocks
840 ,ParmStringsexmm) == 0)
841 goto xmsskip; // param NOXMS
843 if(MML_CheckForXMS(mm))
845 MML_SetupXMS(mm, mmi); // allocate as many UMBs as possible
849 // allocate the misc buffer
852 mm->mmrover = mm->mmhead; // start looking for space after low block
854 MM_GetPtr(&(mm->bufferseg),BUFFERSIZE, mm, mmi);
857 //==========================================================================
864 = Frees all conventional, EMS, and XMS allocated
869 void MM_Shutdown(mminfo_t *mm)
874 _ffree(mm->farheap);// printf(" far freed\n");
876 _nfree(mm->nearheap);// printf(" near freed\n");
879 free(mm->nearheap);// printf(" near freed\n");
881 if(MML_CheckForEMS()){ MML_ShutdownEMS(mm); }//printf(" EMS freed\n"); }
882 if(MML_CheckForXMS(mm)){ MML_ShutdownXMS(mm); }//printf(" XMS freed\n"); }
885 //==========================================================================
892 = Allocates an unlocked, unpurgable block
897 void MM_GetPtr (memptr *baseptr, dword size, mminfo_t *mm, mminfotype *mmi)
899 mmblocktype huge *scan,huge *lastscan,huge *endscan
900 ,huge *purge,huge *next;
902 unsigned needed,startseg;
904 needed = (size+15)/16; // convert size from bytes to paragraphs
906 GETNEWBLOCK; // fill in start and next after a spot is found
907 mm->mmnew->length = needed;
908 mm->mmnew->useptr = baseptr;
909 mm->mmnew->attributes = BASEATTRIBUTES;
912 for (search = 0; search<3; search++)
915 // first search: try to allocate right after the rover, then on up
916 // second search: search from the head pointer up to the rover
917 // third search: compress memory, then scan from start
918 if (search == 1 && mm->mmrover == mm->mmhead)
924 lastscan = mm->mmrover;
925 scan = mm->mmrover->next;
929 lastscan = mm->mmhead;
930 scan = mm->mmhead->next;
931 endscan = mm->mmrover;
935 lastscan = mm->mmhead;
936 scan = mm->mmhead->next;
941 startseg = lastscan->start + lastscan->length;
943 while (scan != endscan)
945 if (scan->start - startseg >= needed)
948 // got enough space between the end of lastscan and
949 // the start of scan, so throw out anything in the middle
950 // and allocate the new block
952 purge = lastscan->next;
953 lastscan->next = mm->mmnew;
954 mm->mmnew->start = *(unsigned *)baseptr = startseg;
955 mm->mmnew->next = scan;
956 while ( purge != scan)
957 { // free the purgable block
960 purge = next; // purge another if not at scan
962 mm->mmrover = mm->mmnew;
963 return; // good allocation!
967 // if this block is purge level zero or locked, skip past it
969 if ( (scan->attributes & LOCKBIT)
970 || !(scan->attributes & PURGEBITS) )
973 startseg = lastscan->start + lastscan->length;
977 scan=scan->next; // look at next line
986 printf(OUT_OF_MEM_MSG,(size-mmi->nearheap));
987 printf("for stability reasons the program will shut down! wwww\n");
995 //==========================================================================
1002 = Allocates an unlocked, unpurgable block
1004 ====================
1007 void MM_FreePtr(memptr *baseptr, mminfo_t *mm)
1009 mmblocktype huge *scan,huge *last;
1014 if(baseptr == mm->mmrover->useptr) // removed the last allocated block
1015 mm->mmrover = mm->mmhead;
1017 while(scan->useptr != baseptr && scan)
1025 printf("MM_FreePtr: Block not found!\n");
1029 last->next = scan->next;
1033 //==========================================================================
1036 =====================
1040 = Sets the purge level for a block (locked blocks cannot be made purgable)
1042 =====================
1045 void MM_SetPurge(memptr *baseptr, int purge, mminfo_t *mm)
1047 mmblocktype huge *start;
1049 start = mm->mmrover;
1053 if(mm->mmrover->useptr == baseptr)
1056 mm->mmrover = mm->mmrover->next;
1059 mm->mmrover = mm->mmhead;
1060 else if(mm->mmrover == start)
1062 printf("MM_SetPurge: Block not found!");
1068 mm->mmrover->attributes &= ~PURGEBITS;
1069 mm->mmrover->attributes |= purge;
1072 //==========================================================================
1075 =====================
1079 = Locks / unlocks the block
1081 =====================
1084 void MM_SetLock(memptr *baseptr, boolean locked, mminfo_t *mm)
1086 mmblocktype huge *start;
1088 start = mm->mmrover;
1092 if(mm->mmrover->useptr == baseptr)
1095 mm->mmrover = mm->mmrover->next;
1098 mm->mmrover = mm->mmhead;
1099 else if(mm->mmrover == start)
1101 printf("MM_SetLock: Block not found!");
1107 mm->mmrover->attributes &= ~LOCKBIT;
1108 mm->mmrover->attributes |= locked*LOCKBIT;
1111 //==========================================================================
1114 =====================
1118 = Throws out all purgable stuff and compresses movable blocks
1120 =====================
1123 void MM_SortMem(mminfo_t *mm)
1125 mmblocktype huge *scan,huge *last,huge *next;
1126 unsigned start,length,source,dest,oldborder;
1130 // lock down a currently playing sound
1132 /*++++ playing = SD_SoundPlaying ();
1138 playing += STARTPCSOUNDS;
1141 playing += STARTADLIBSOUNDS;
1144 MM_SetLock(&(memptr)audiosegs[playing],true);
1149 // oldborder = bordercolor;
1150 // VW_ColorBorder (15);
1157 last = NULL; // shut up compiler warning
1161 if(scan->attributes & LOCKBIT)
1164 // block is locked, so try to pile later blocks right after it
1166 start = scan->start + scan->length;
1170 if(scan->attributes & PURGEBITS)
1173 // throw out the purgable block
1177 //MM_FreeBlock(scan, mm);
1185 // push the non purgable block on top of the last moved block
1187 if(scan->start != start)
1189 length = scan->length;
1190 source = scan->start;
1192 while(length > 0xf00)
1194 movedata(source,0,dest,0,0xf00*16);
1199 movedata(source,0,dest,0,length*16);
1201 scan->start = start;
1202 *(unsigned *)scan->useptr = start;
1204 start = scan->start + scan->length;
1209 scan = scan->next; // go to next block
1212 mm->mmrover = mm->mmhead;
1217 // VW_ColorBorder (oldborder);
1220 MM_SetLock(&(memptr)audiosegs[playing],false);*/
1223 //==========================================================================
1226 =====================
1230 =====================
1233 void MM_ShowMemory(global_game_variables_t *gvar,/*page_t *page, */mminfo_t *mm)
1235 mmblocktype huge *scan;
1241 byte scratch[160],scratch0[4096],str[16];
1243 //**** VW_SetDefaultColors();
1244 //**** VW_SetLineWidth(40);
1245 //++++mh temp = bufferofs;
1246 //++++mh bufferofs = 0;
1247 //**** VW_SetScreen (0,0);
1251 CA_OpenDebug (gvar);
1255 strcpy(scratch, AARESET);
1256 if(scan->attributes & PURGEBITS)
1257 strcpy(scratch0, AAMAGENTA); // dark purple = purgable
1259 strcpy(scratch0, AABLUE); // medium blue = non purgable
1260 if(scan->attributes & LOCKBIT)
1261 strcpy(scratch0, AARED); // red = locked
1262 if(scan->start<=end)
1264 printf("\nend==%d\n\n", end);
1265 strcat(scratch, "MM_ShowMemory: Memory block order currupted!\n");
1266 strcat(scratch, "End's Size: ");
1268 strcat (scratch,str);
1269 strcat(scratch, "\nscan->start's Size: ");
1270 ultoa (scan->start,str,10);
1271 strcat (scratch,str);
1272 write(gvar->handle.debughandle,scratch,strlen(scratch));
1273 //modexprint(&page, chx, chy, 1, 0, 24, "\nMM_ShowMemory: Memory block order currupted!\n");
1276 end = scan->start+(scan->length)-1;
1277 //++++ chy = scan->start/320;
1278 //++++ chx = scan->start%320;
1279 //modexhlin(page, scan->start, (unsigned)end, chy, color);
1280 //for(chx=scan->start;chx+4>=(word)end;chx+=4)
1282 //++++ modexClearRegion(page, chx, chy, 4, 4, color);
1284 //++++ VW_Hlin(scan->start,(unsigned)end,0,color);
1285 for(w=(scan->start)/80;w<=end/80;w++)
1287 //printf("+ %u %lu\n", w, scan->length);
1288 strcat(scratch0, "+");
1290 strcat(scratch0, AARESET); strcat(scratch0, AAGREY); strcat(scratch0,"_");
1291 //++++ VW_Plot(scan->start,0,15);
1292 //++++ modexClearRegion(page, chx, chy, 4, 4, 15);
1293 //++++ VW_Hlin(end+1,scan->next->start,0,0); // black = free
1295 wwww=(dword)(scan->next->start)-(dword)scan->start;
1296 //wwww=(dword)scan->start+(dword)(scan->next->start);
1297 if (scan->next && scan->next->start >= end+1)
1299 strcat(scratch0, AARESET);
1300 strcat(scratch0, "\n");
1301 strcat(scratch0,AAGREEN);
1302 for(w=(end+1)/80;w<=(wwww/80);w++)
1304 //printf("0 %x %u %lu\n", scan->next->start, w, scan->length);
1305 strcat(scratch0,"0");
1307 //printf("==================\n");
1308 //printf("w=%x start=%x next=%x end=%u %lu\n", w, scan->start, (scan->next->start), end+1, wwww);
1309 //printf("==================\n");
1310 }/*else {//if(scan->next->start <= scan->start){
1311 scan->next->start=scan->start+0x1000;
1312 wwww=(dword)(scan->next->start)-(dword)scan->start;
1313 strcat(scratch0, AARESET);
1314 strcat(scratch0, "\n");
1315 strcat(scratch0,AAGREEN);
1316 for(w=(end+1);w<=(0x1000/80);w++)
1318 //printf("0 %x %x %u\n", scan->start, w);
1319 strcat(scratch0,"0");
1321 printf("================\n");
1322 printf("w=%x start=%x next=%x end=%u %lu\n", w, scan->start, (scan->next->start), end+1, wwww);
1323 printf("================\n");
1326 strcat(scratch0, AARESET);
1327 strcat(scratch0,"\n");
1328 //for(chx=scan->next->start;chx+4>=(word)end+1;chx+=4)
1330 // chx+=scan->next->start;
1331 // modexClearRegion(page, chx, chy, 4, 4, 2);
1333 //modexhlin(page, end+1,scan->next->start, chy, 0);
1334 /* y = scan->start/320;
1335 x = scan->start%320;
1336 VW_Hlin(x,x+end,y,color);
1338 //++++ VW_Hlin(x+end+1,x+(scan->next->start-scan->start),y,0); // black = free
1339 strcat(scratch,"Seg:");
1340 ultoa (scan->start,str,16);
1341 strcat (scratch,str);
1342 strcat (scratch,"\tSize:");
1343 ultoa ((word)scan->length,str,10);
1344 strcat (scratch,str);
1345 strcat (scratch,"\tOwner:0x");
1346 owner = (unsigned)scan->useptr;
1347 ultoa (owner,str,16);
1348 strcat (scratch,str);
1349 strcat (scratch,"\n");
1350 write(gvar->handle.debughandle,scratch,strlen(scratch));
1351 write(gvar->handle.debughandle,scratch0,strlen(scratch0));
1352 //modexprint(page, chx, chy, 1, 0, 24, &scratch);
1354 //fprintf(stdout, "%s", scratch);
1359 CA_CloseDebug (gvar);
1362 //**** VW_SetLineWidth(64);
1363 //++++mh bufferofs = temp;
1366 //==========================================================================
1369 =====================
1373 =====================
1376 void MM_DumpData(mminfo_t *mm)
1378 mmblocktype huge *scan,huge *best;
1379 long lowest,oldlowest;
1385 dumpfile = fopen ("mmdump.16","w");
1387 printf("MM_DumpData: Couldn't open MMDUMP.16!\n");
1400 owner = (word)scan->useptr;
1402 if (owner && owner<lowest && owner > oldlowest)
1411 if (lowest != 0xffff)
1413 if (best->attributes & PURGEBITS)
1417 if (best->attributes & LOCKBIT)
1421 fprintf (dumpfile,"0x%p (%c%c) = %u\n"
1422 ,(word)lowest,lock,purge,best->length);
1425 } while (lowest != 0xffff);
1428 printf("MMDUMP.16 created.\n");
1431 //==========================================================================
1435 ======================
1439 = Returns the total free space without purging
1441 ======================
1444 dword MM_UnusedMemory(mminfo_t *mm)
1447 mmblocktype huge *scan;
1454 free += scan->next->start - (scan->start + scan->length);
1462 //==========================================================================
1466 ======================
1470 = Returns the total free space with purging
1472 ======================
1475 dword MM_TotalFree(mminfo_t *mm)
1478 mmblocktype huge *scan;
1485 if((scan->attributes&PURGEBITS) && !(scan->attributes&LOCKBIT))
1486 free += scan->length;
1487 free += scan->next->start - (scan->start + scan->length);
1495 //==========================================================================
1498 =====================
1502 =====================
1505 void MM_Report(/*page_t *page, */mminfo_t *mm, mminfotype *mmi)
1507 if(MML_CheckForEMS())
1509 printf("EMM v%x.%x available\n", mm->EMSVer>>4,mm->EMSVer&0x0F);
1510 printf("totalEMSpages=%u\n", mm->totalEMSpages);
1511 printf("freeEMSpages=%u\n", mm->freeEMSpages);
1512 printf("EMSpageframe=%x\n", mm->EMSpageframe);
1514 if(MML_CheckForXMS(mm)) printf("XMSaddr=%X\n", *XMSaddr);
1515 printf("near=%lu\n", mmi->nearheap);
1516 printf("far=%lu\n", mmi->farheap);
1517 printf("EMSmem=%lu\n", mmi->EMSmem);
1518 printf("XMSmem=%lu\n", mmi->XMSmem);
1519 printf("mainmem=%lu\n", mmi->mainmem);
1520 printf("UnusedMemory=%lu\n", MM_UnusedMemory(mm));
1521 printf("TotalFree=%lu\n", MM_TotalFree(mm));
1522 printf("TotalUsed=%lu\n", mmi->mainmem+mmi->EMSmem+mmi->XMSmem+mmi->XMSmem);
1525 //==========================================================================
1528 =====================
1532 =====================
1535 void MM_EMSerr(byte *stri, byte err)
1537 //Returns a text string describing the error code in EMS.Error.
1541 strcat(stri, "successful");
1544 strcat(stri, "internal error");
1547 strcat(stri, "hardware malfunction");
1550 strcat(stri, "busy .. retry later");
1553 strcat(stri, "invalid handle");
1556 strcat(stri, "undefined function requested by application");
1559 strcat(stri, "no more handles available");
1562 strcat(stri, "error in save or restore of mapping context");
1565 strcat(stri, "insufficient memory pages in system");
1568 strcat(stri, "insufficient memory pages available");
1571 strcat(stri, "zero pages requested");
1574 strcat(stri, "invalid logical page number encountered");
1577 strcat(stri, "invalid physical page number encountered");
1580 strcat(stri, "page-mapping hardware state save area is full");
1583 strcat(stri, "save of mapping context failed");
1586 strcat(stri, "restore of mapping context failed");
1589 strcat(stri, "undefined subfunction");
1592 strcat(stri, "undefined attribute type");
1595 strcat(stri, "feature not supported");
1598 strcat(stri, "successful, but a portion of the source region has been overwritten");
1601 strcat(stri, "length of source or destination region exceeds length of region allocated to either source or destination handle");
1604 strcat(stri, "conventional and expanded memory regions overlap");
1607 strcat(stri, "offset within logical page exceeds size of logical page");
1610 strcat(stri, "region length exceeds 1 MB");
1613 strcat(stri, "source and destination EMS regions have same handle and overlap");
1616 strcat(stri, "memory source or destination type undefined");
1619 strcat(stri, "specified alternate map register or DMA register set not supported");
1622 strcat(stri, "all alternate map register or DMA register sets currently allocated");
1625 strcat(stri, "alternate map register or DMA register sets not supported");
1628 strcat(stri, "undefined or unallocated alternate map register or DMA register set");
1631 strcat(stri, "dedicated DMA channels not supported");
1634 strcat(stri, "specified dedicated DMA channel not supported");
1637 strcat(stri, "no such handle name");
1640 strcat(stri, "a handle found had no name, or duplicate handle name");
1643 strcat(stri, "attempted to wrap around 1M conventional address space");
1646 strcat(stri, "source array corrupted");
1649 strcat(stri, "operating system denied access");
1652 strcat(stri, "undefined error");
1656 //==========================================================================
1659 =====================
1663 =====================
1666 void MM_BombOnError(boolean bomb, mminfo_t *mm)
1668 mm->bombonerror = bomb;
1671 /*void MM_GetNewBlock(mminfo_t *mm)
1675 mm->mmnew=mm->mmfree;
1676 mm->mmfree=mm->mmfree->next;
1677 if(!(mm->mmnew=mm->mmfree))
1679 printf("MM_GETNEWBLOCK: No free blocks!\n");
1682 mm->mmfree=mm->mmfree->next;
1685 void MM_FreeBlock(mmblocktype *x, mminfo_t *mm)
1692 /*void MM_seguin(void)
1702 void MM_segude(void)
1710 pull data from far and put it into ds var