]> 4ch.mooo.com Git - 16.git/blob - src/lib/16_mm.c
exmm test~
[16.git] / src / lib / 16_mm.c
1 /* Catacomb Armageddon Source Code
2  * Copyright (C) 1993-2014 Flat Rock Software
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 // NEWMM.C
20
21 /*
22 =============================================================================
23
24                         ID software memory manager
25                         --------------------------
26
27 Primary coder: John Carmack
28
29 RELIES ON
30 ---------
31 Quit (char *error) function
32
33
34 WORK TO DO
35 ----------
36 MM_SizePtr to change the size of a given pointer
37
38 Multiple purge levels utilized
39
40 EMS / XMS unmanaged routines
41
42 =============================================================================
43 */
44
45 #include "src/lib/16_mm.h"
46
47 /*
48 =============================================================================
49
50                                                         LOCAL INFO
51
52 =============================================================================
53 */
54
55 #define LOCKBIT         0x80    // if set in attributes, block cannot be moved
56 #define PURGEBITS       3               // 0-3 level, 0= unpurgable, 3= purge first
57 #define PURGEMASK       0xfffc
58 #define BASEATTRIBUTES  0       // unlocked, non purgable
59
60 #define MAXUMBS         10
61
62 typedef struct mmblockstruct
63 {
64         unsigned        start,length;
65         unsigned        attributes;
66         memptr          *useptr;        // pointer to the segment start
67         struct mmblockstruct far *next;
68 } mmblocktype;
69
70
71 //#define GETNEWBLOCK {if(!(mmnew=mmfree))Quit("MM_GETNEWBLOCK: No free blocks!");mmfree=mmfree->next;}
72 #define GETNEWBLOCK {if(!mmfree)MML_ClearBlock();mmnew=mmfree;mmfree=mmfree->next;}
73 #define FREEBLOCK(x) {*x->useptr=NULL;x->next=mmfree;mmfree=x;}
74
75 /*
76 =============================================================================
77
78                                                  GLOBAL VARIABLES
79
80 =============================================================================
81 */
82
83 mminfotype      mminfo;
84 memptr          bufferseg;
85 boolean         mmerror;
86
87 void            (* beforesort) (void);
88 void            (* aftersort) (void);
89
90 /*
91 =============================================================================
92
93                                                  LOCAL VARIABLES
94
95 =============================================================================
96 */
97
98 boolean         mmstarted;
99
100 void huge       *hugeheap;
101 void far        *farheap;
102 void            *nearheap;
103
104 mmblocktype     far mmblocks[MAXBLOCKS]
105                         ,far *mmhead,far *mmfree,far *mmrover,far *mmnew;
106
107 boolean         bombonerror;
108
109 unsigned        totalEMSpages,freeEMSpages,EMSpageframe,EMSpagesmapped,EMShandle;
110
111 void            (* XMSaddr) (void);             // far pointer to XMS driver
112
113 unsigned        numUMBs,UMBbase[MAXUMBS];
114
115
116 /*
117 ======================
118 =
119 = MML_CheckForEMS
120 =
121 = Routine from p36 of Extending DOS
122 =
123 =======================
124 */
125
126 boolean MML_CheckForEMS (void)
127 {
128         boolean emmcfems;
129         char    emmname[] = "EMMXXXX0";
130 //              mov     dx,OFFSET emmname
131         __asm {
132                 LEA     DX, emmname     //fix by andrius4669
133                 mov     ax,0x3d00
134                 int     0x21            // try to open EMMXXXX0 device
135                 jc      error
136
137                 mov     bx,ax
138                 mov     ax,0x4400
139
140                 int     0x21            // get device info
141                 jc      error
142
143                 and     dx,0x80
144                 jz      error
145
146                 mov     ax,0x4407
147
148                 int     0x21            // get status
149                 jc      error
150                 or      al,al
151                 jz      error
152
153                 mov     ah,0x3e
154                 int     0x21            // close handle
155                 jc      error
156                 //
157                 // EMS is good
158                 //
159                 mov     emmcfems,1
160                 jmp End
161                 error:
162                 //
163                 // EMS is bad
164                 //
165                 mov     emmcfems,0
166                 End:
167         }
168         return(emmcfems);
169 }
170
171
172 /*
173 ======================
174 =
175 = MML_SetupEMS
176 =
177 =======================
178 */
179
180 void MML_SetupEMS (void)
181 {
182         char    str[80],str2[10];
183         unsigned        err;
184         boolean errorflag=false;
185         union REGS CPURegs;
186
187         totalEMSpages = freeEMSpages = EMSpageframe = EMSpagesmapped = 0;
188
189         __asm
190                 {
191                 mov     ah,EMS_STATUS
192                 int     EMS_INT                                         // make sure EMS hardware is present
193                 or      ah,ah
194                 jnz     error
195
196                 mov     ah,EMS_VERSION
197                 int     EMS_INT
198                 or      ah,ah
199                 jnz     error
200                 cmp     al,0x32                                         // only work on ems 3.2 or greater
201                 jb      error
202
203                 mov     ah,EMS_GETFRAME
204                 int     EMS_INT                                         // find the page frame address
205                 or      ah,ah
206                 jnz     error
207                 mov     [EMSpageframe],bx
208
209                 mov     ah,EMS_GETPAGES
210                 int     EMS_INT                                         // find out how much EMS is there
211                 or      ah,ah
212                 jnz     error
213                 mov     [totalEMSpages],dx
214                 mov     [freeEMSpages],bx
215                 or      bx,bx
216                 jz      noEMS                                           // no EMS at all to allocate
217
218                 cmp     bx,4
219                 jle     getpages                                        // there is only 1,2,3,or 4 pages
220                 mov     bx,4                                            // we can't use more than 4 pages
221
222 getpages:
223                 mov     [EMSpagesmapped],bx
224                 mov     ah,EMS_ALLOCPAGES                       // allocate up to 64k of EMS
225                 int     EMS_INT
226                 or      ah,ah
227                 jnz     error
228                 mov     [EMShandle],dx
229                 jmp End
230 error:
231                 mov     errorflag,1
232                 jmp End
233 noEMS:
234 End:
235         }
236         if(errorflag==true)
237         {
238                 err = CPURegs.h.ah;
239                 strcpy(str,"MML_SetupEMS: EMS error 0x");
240                 itoa(err,str2,16);
241                 strcpy(str,str2);
242                 printf("%s\n",str);
243         }
244 }
245
246
247 /*
248 ======================
249 =
250 = MML_ShutdownEMS
251 =
252 =======================
253 */
254
255 void MML_ShutdownEMS (void)
256 {
257         boolean errorflag=false;
258         if (!EMShandle)
259                 return;
260         __asm
261         {
262                 mov     ah,EMS_FREEPAGES
263                 mov     dx,[EMShandle]
264                 int     EMS_INT
265                 or      ah,ah
266                 jz      ok
267                 mov     errorflag,1
268                 ok:
269         }
270         if(errorflag==true) printf("MML_ShutdownEMS: Error freeing EMS!");      //++++ add something
271 }
272
273 /*
274 ====================
275 =
276 = MM_MapEMS
277 =
278 = Maps the 64k of EMS used by memory manager into the page frame
279 = for general use.  This only needs to be called if you are keeping
280 = other things in EMS.
281 =
282 ====================
283 */
284
285 void MM_MapEMS (void)
286 {
287         char    str[80],str2[10];
288         unsigned        err;
289         boolean errorflag=false;
290         int     i;
291         union REGS CPURegs;
292
293         for (i=0;i<EMSpagesmapped;i++)
294         {
295                 __asm
296                 {
297                         mov     ah,EMS_MAPPAGE
298                         mov     bx,[i]                  // logical page
299                         mov     al,bl                   // physical page
300                         mov     dx,[EMShandle]  // handle
301                         int     EMS_INT
302                         or      ah,ah
303                         jnz     error
304                         jmp End
305                         error:
306                         mov     errorflag,1
307                         End:
308                 }
309                 if(errorflag==true)
310                 {
311                         err = CPURegs.h.ah;
312                         strcpy(str,"MM_MapEMS: EMS error 0x");
313                         itoa(err,str2,16);
314                         strcpy(str,str2);
315                         printf("%s\n",str);
316                 }
317         }
318         return;
319 }
320
321 //==========================================================================
322
323 /*
324 ======================
325 =
326 = MML_CheckForXMS
327 =
328 = Check for XMM driver
329 =
330 =======================
331 */
332
333 boolean MML_CheckForXMS (void)
334 {
335         boolean errorflag=false;
336         numUMBs = 0;
337
338         __asm
339         {
340                 mov     ax,0x4300
341                 int     0x2f                            // query status of installed diver
342                 cmp     al,0x80
343                 je      good
344                 mov     errorflag,1
345                 good:
346         }
347         if(errorflag==true) return false;
348         else return true;
349 }
350
351
352 /*
353 ======================
354 =
355 = MML_SetupXMS
356 =
357 = Try to allocate all upper memory block
358 =
359 =======================
360 */
361
362 void MML_SetupXMS (void)
363 {
364         unsigned        base,size;
365
366         __asm
367         {
368                 mov     ax,0x4310
369                 int     0x2f
370                 mov     [WORD PTR XMSaddr],bx
371                 mov     [WORD PTR XMSaddr+2],es         // function pointer to XMS driver
372         }
373 getmemory:
374         __asm
375         {
376                 mov     ah,XMS_ALLOCUMB
377                 mov     dx,0xffff                                       // try for largest block possible
378                 call    [DWORD PTR XMSaddr]
379                 or      ax,ax
380                 jnz     gotone
381
382                 cmp     bl,0xb0                                         // error: smaller UMB is available
383                 jne     done;
384
385                 mov     ah,XMS_ALLOCUMB
386                 call    [DWORD PTR XMSaddr]             // DX holds largest available UMB
387                 or      ax,ax
388                 jz      done                                            // another error...
389
390 gotone:
391                 mov     [base],bx
392                 mov     [size],dx
393 done:
394         }
395         MML_UseSpace (base,size);
396         mminfo.XMSmem += size*16;
397         UMBbase[numUMBs] = base;
398         numUMBs++;
399         if (numUMBs < MAXUMBS)
400                 goto getmemory;
401 }
402
403
404 /*
405 ======================
406 =
407 = MML_ShutdownXMS
408 =
409 ======================
410 */
411
412 void MML_ShutdownXMS (void)
413 {
414         int     i;
415         unsigned        base;
416
417         for (i=0;i<numUMBs;i++)
418         {
419                 base = UMBbase[i];
420                 __asm
421                 {
422                         mov     ah,XMS_FREEUMB
423                         mov     dx,[base]
424                         call    [DWORD PTR XMSaddr]
425                 }
426         }
427 }
428
429 //==========================================================================
430
431 /*
432 ======================
433 =
434 = MML_UseSpace
435 =
436 = Marks a range of paragraphs as usable by the memory manager
437 = This is used to mark space for the near heap, far heap, ems page frame,
438 = and upper memory blocks
439 =
440 ======================
441 */
442
443 void MML_UseSpace (unsigned segstart, unsigned seglength)
444 {
445         mmblocktype far *scan,far *last;
446         unsigned        oldend;
447         long            extra;
448
449         scan = last = mmhead;
450         mmrover = mmhead;               // reset rover to start of memory
451
452 //
453 // search for the block that contains the range of segments
454 //
455         while (scan->start+scan->length < segstart)
456         {
457                 last = scan;
458                 scan = scan->next;
459         }
460
461 //
462 // take the given range out of the block
463 //
464         oldend = scan->start + scan->length;
465         extra = oldend - (segstart+seglength);
466         if (extra < 0)
467         {
468                 printf("MML_UseSpace: Segment spans two blocks!");
469                 return;
470         }
471                 
472
473         if (segstart == scan->start)
474         {
475                 last->next = scan->next;                        // unlink block
476                 FREEBLOCK(scan);
477                 scan = last;
478         }
479         else
480                 scan->length = segstart-scan->start;    // shorten block
481
482         if (extra > 0)
483         {
484                 GETNEWBLOCK;
485                 mmnew->next = scan->next;
486                 scan->next = mmnew;
487                 mmnew->start = segstart+seglength;
488                 mmnew->length = extra;
489                 mmnew->attributes = LOCKBIT;
490         }
491
492 }
493
494 //==========================================================================
495
496 /*
497 ====================
498 =
499 = MML_ClearBlock
500 =
501 = We are out of blocks, so free a purgable block
502 =
503 ====================
504 */
505
506 void MML_ClearBlock (void)
507 {
508         mmblocktype far *scan,far *last;
509
510         scan = mmhead->next;
511
512         while (scan)
513         {
514                 if (!(scan->attributes&LOCKBIT) && (scan->attributes&PURGEBITS) )
515                 {
516                         MM_FreePtr(scan->useptr);
517                         return;
518                 }
519                 scan = scan->next;
520         }
521
522         printf("MM_ClearBlock: No purgable blocks!");
523 }
524
525
526 //==========================================================================
527
528 /*
529 ===================
530 =
531 = MM_Startup
532 =
533 = Grabs all space from turbo with malloc/farmalloc
534 = Allocates bufferseg misc buffer
535 =
536 ===================
537 */
538
539 static  char *ParmStringsexmm[] = {"noems","noxms",""};
540
541 void MM_Startup (void)
542 {
543         int i;
544         dword length;
545         void far        *start;
546         unsigned        segstart,seglength,endfree;
547
548         if (mmstarted)
549                 MM_Shutdown ();
550
551
552         mmstarted = true;
553         bombonerror = true;
554 //
555 // set up the linked list (everything in the free list;
556 //
557         mmhead = NULL;
558         mmfree = &mmblocks[0];
559         for (i=0;i<MAXBLOCKS-1;i++)
560                 mmblocks[i].next = &mmblocks[i+1];
561         mmblocks[i].next = NULL;
562
563 //
564 // locked block of all memory until we punch out free space
565 //
566         GETNEWBLOCK;
567         mmhead = mmnew;                         // this will allways be the first node
568         mmnew->start = 0;
569         mmnew->length = 0xffff;
570         mmnew->attributes = LOCKBIT;
571         mmnew->next = NULL;
572         mmrover = mmhead;
573
574
575 //
576 // get all available near conventional memory segments
577 //
578 //----  length=coreleft();
579         length=_memavl();
580         start = (void far *)(nearheap = malloc(length));
581
582         length -= 16-(FP_OFF(start)&15);
583         length -= SAVENEARHEAP;
584         seglength = length / 16;                        // now in paragraphs
585         segstart = FP_SEG(start)+(FP_OFF(start)+15)/16;
586         MML_UseSpace (segstart,seglength);
587         mminfo.nearheap = length;
588
589 //
590 // get all available far conventional memory segments
591 //
592 //----  length=farcoreleft();
593         length=_memmax();
594         start = farheap = _fmalloc(length);
595         length -= 16-(FP_OFF(start)&15);
596         length -= SAVEFARHEAP;
597         seglength = length / 16;                        // now in paragraphs
598         segstart = FP_SEG(start)+(FP_OFF(start)+15)/16;
599         MML_UseSpace (segstart,seglength);
600         mminfo.farheap = length;
601         mminfo.mainmem = mminfo.nearheap + mminfo.farheap;
602
603
604 //
605 // detect EMS and allocate up to 64K at page frame
606 //
607         mminfo.EMSmem = 0;
608         for (i = 1;i < __argc;i++)
609         {
610                 if ( US_CheckParm(__argv[i],ParmStringsexmm) == 0)
611                         goto emsskip;                           // param NOEMS
612         }
613
614         if (MML_CheckForEMS())
615         {
616                 MML_SetupEMS();                                 // allocate space
617                 MML_UseSpace (EMSpageframe,EMSpagesmapped*0x400);
618                 MM_MapEMS();                                    // map in used pages
619                 mminfo.EMSmem = EMSpagesmapped*0x4000l;
620         }
621
622 //
623 // detect XMS and get upper memory blocks
624 //
625 emsskip:
626         mminfo.XMSmem = 0;
627         for (i = 1;i < __argc;i++)
628         {
629                 if ( US_CheckParm(__argv[i],ParmStringsexmm) == 0)
630                         goto xmsskip;                           // param NOXMS
631         }
632
633         if (MML_CheckForXMS())
634                 MML_SetupXMS();                                 // allocate as many UMBs as possible
635
636 //
637 // allocate the misc buffer
638 //
639 xmsskip:
640         mmrover = mmhead;               // start looking for space after low block
641
642         MM_GetPtr (&bufferseg,BUFFERSIZE);
643 }
644
645 //==========================================================================
646
647 /*
648 ====================
649 =
650 = MM_Shutdown
651 =
652 = Frees all conventional, EMS, and XMS allocated
653 =
654 ====================
655 */
656
657 void MM_Shutdown (void)
658 {
659   if (!mmstarted)
660         return;
661
662   _ffree (farheap);
663   free (nearheap);
664   hfree(hugeheap);
665   MML_ShutdownEMS ();
666   MML_ShutdownXMS ();
667 }
668
669 //==========================================================================
670
671 /*
672 ====================
673 =
674 = MM_GetPtr
675 =
676 = Allocates an unlocked, unpurgable block
677 =
678 ====================
679 */
680
681 void MM_GetPtr (memptr *baseptr,dword size)
682 {
683         mmblocktype far *scan,far *lastscan,far *endscan
684                                 ,far *purge,far *next;
685         int                     search;
686         unsigned        needed,startseg;
687
688         needed = (size+15)/16;          // convert size from bytes to paragraphs
689
690         GETNEWBLOCK;                            // fill in start and next after a spot is found
691         mmnew->length = needed;
692         mmnew->useptr = baseptr;
693         mmnew->attributes = BASEATTRIBUTES;
694
695         for (search = 0; search<3; search++)
696         {
697         //
698         // first search:        try to allocate right after the rover, then on up
699         // second search:       search from the head pointer up to the rover
700         // third search:        compress memory, then scan from start
701                 if (search == 1 && mmrover == mmhead)
702                         search++;
703
704                 switch (search)
705                 {
706                 case 0:
707                         lastscan = mmrover;
708                         scan = mmrover->next;
709                         endscan = NULL;
710                         break;
711                 case 1:
712                         lastscan = mmhead;
713                         scan = mmhead->next;
714                         endscan = mmrover;
715                         break;
716                 case 2:
717                         MM_SortMem ();
718                         lastscan = mmhead;
719                         scan = mmhead->next;
720                         endscan = NULL;
721                         break;
722                 }
723
724                 startseg = lastscan->start + lastscan->length;
725
726                 while (scan != endscan)
727                 {
728                         if (scan->start - startseg >= needed)
729                         {
730                         //
731                         // got enough space between the end of lastscan and
732                         // the start of scan, so throw out anything in the middle
733                         // and allocate the new block
734                         //
735                                 purge = lastscan->next;
736                                 lastscan->next = mmnew;
737                                 mmnew->start = *(unsigned *)baseptr = startseg;
738                                 mmnew->next = scan;
739                                 while ( purge != scan)
740                                 {       // free the purgable block
741                                         next = purge->next;
742                                         FREEBLOCK(purge);
743                                         purge = next;           // purge another if not at scan
744                                 }
745                                 mmrover = mmnew;
746                                 return; // good allocation!
747                         }
748
749                         //
750                         // if this block is purge level zero or locked, skip past it
751                         //
752                         if ( (scan->attributes & LOCKBIT)
753                                 || !(scan->attributes & PURGEBITS) )
754                         {
755                                 lastscan = scan;
756                                 startseg = lastscan->start + lastscan->length;
757                         }
758
759
760                         scan=scan->next;                // look at next line
761                 }
762         }
763
764         if (bombonerror)
765                 printf(OUT_OF_MEM_MSG,(size-mminfo.nearheap));
766         else
767                 mmerror = true;
768 }
769
770 //==========================================================================
771
772 /*
773 ====================
774 =
775 = MM_FreePtr
776 =
777 = Allocates an unlocked, unpurgable block
778 =
779 ====================
780 */
781
782 void MM_FreePtr (memptr *baseptr)
783 {
784         mmblocktype far *scan,far *last;
785
786         last = mmhead;
787         scan = last->next;
788
789         if (baseptr == mmrover->useptr) // removed the last allocated block
790                 mmrover = mmhead;
791
792         while (scan->useptr != baseptr && scan)
793         {
794                 last = scan;
795                 scan = scan->next;
796         }
797
798         if (!scan)
799         {
800                 printf("MM_FreePtr: Block not found!");
801                 return;
802         }
803
804         last->next = scan->next;
805
806         FREEBLOCK(scan);
807 }
808 //==========================================================================
809
810 /*
811 =====================
812 =
813 = MM_SetPurge
814 =
815 = Sets the purge level for a block (locked blocks cannot be made purgable)
816 =
817 =====================
818 */
819
820 void MM_SetPurge (memptr *baseptr, int purge)
821 {
822         mmblocktype far *start;
823
824         start = mmrover;
825
826         do
827         {
828                 if (mmrover->useptr == baseptr)
829                         break;
830
831                 mmrover = mmrover->next;
832
833                 if (!mmrover)
834                         mmrover = mmhead;
835                 else if (mmrover == start)
836                 {
837                         printf("MM_SetPurge: Block not found!");
838                         return;
839                 }
840
841         } while (1);
842
843         mmrover->attributes &= ~PURGEBITS;
844         mmrover->attributes |= purge;
845 }
846
847 //==========================================================================
848
849 /*
850 =====================
851 =
852 = MM_SetLock
853 =
854 = Locks / unlocks the block
855 =
856 =====================
857 */
858
859 void MM_SetLock (memptr *baseptr, boolean locked)
860 {
861         mmblocktype far *start;
862
863         start = mmrover;
864
865         do
866         {
867                 if (mmrover->useptr == baseptr)
868                         break;
869
870                 mmrover = mmrover->next;
871
872                 if (!mmrover)
873                         mmrover = mmhead;
874                 else if (mmrover == start)
875                 {
876                         printf("MM_SetLock: Block not found!");
877                         return;
878                 }
879
880         } while (1);
881
882         mmrover->attributes &= ~LOCKBIT;
883         mmrover->attributes |= locked*LOCKBIT;
884 }
885
886 //==========================================================================
887
888 /*
889 =====================
890 =
891 = MM_SortMem
892 =
893 = Throws out all purgable stuff and compresses movable blocks
894 =
895 =====================
896 */
897
898 void MM_SortMem (void)
899 {
900         mmblocktype far *scan,far *last,far *next;
901         unsigned        start,length,source,dest,oldborder;
902         int                     playing;
903
904         //
905         // lock down a currently playing sound
906         //
907 /*++++  playing = SD_SoundPlaying ();
908         if (playing)
909         {
910                 switch (SoundMode)
911                 {
912                 case sdm_PC:
913                         playing += STARTPCSOUNDS;
914                         break;
915                 case sdm_AdLib:
916                         playing += STARTADLIBSOUNDS;
917                         break;
918                 }
919                 MM_SetLock(&(memptr)audiosegs[playing],true);
920         }
921
922
923         SD_StopSound();*/
924 //      oldborder = bordercolor;
925 //      VW_ColorBorder (15);
926
927         if (beforesort)
928                 beforesort();
929
930         scan = mmhead;
931
932         last = NULL;            // shut up compiler warning
933
934         while (scan)
935         {
936                 if (scan->attributes & LOCKBIT)
937                 {
938                 //
939                 // block is locked, so try to pile later blocks right after it
940                 //
941                         start = scan->start + scan->length;
942                 }
943                 else
944                 {
945                         if (scan->attributes & PURGEBITS)
946                         {
947                         //
948                         // throw out the purgable block
949                         //
950                                 next = scan->next;
951                                 FREEBLOCK(scan);
952                                 last->next = next;
953                                 scan = next;
954                                 continue;
955                         }
956                         else
957                         {
958                         //
959                         // push the non purgable block on top of the last moved block
960                         //
961                                 if (scan->start != start)
962                                 {
963                                         length = scan->length;
964                                         source = scan->start;
965                                         dest = start;
966                                         while (length > 0xf00)
967                                         {
968                                                 movedata(source,0,dest,0,0xf00*16);
969                                                 length -= 0xf00;
970                                                 source += 0xf00;
971                                                 dest += 0xf00;
972                                         }
973                                         movedata(source,0,dest,0,length*16);
974
975                                         scan->start = start;
976                                         *(unsigned *)scan->useptr = start;
977                                 }
978                                 start = scan->start + scan->length;
979                         }
980                 }
981
982                 last = scan;
983                 scan = scan->next;              // go to next block
984         }
985
986         mmrover = mmhead;
987
988         if (aftersort)
989                 aftersort();
990
991 //      VW_ColorBorder (oldborder);
992
993 /*++++  if (playing)
994                 MM_SetLock(&(memptr)audiosegs[playing],false);*/
995 }
996
997
998 //==========================================================================
999
1000 //****
1001 #if 0
1002 /*
1003 =====================
1004 =
1005 = MM_ShowMemory
1006 =
1007 =====================
1008 */
1009
1010 void MM_ShowMemory (void)
1011 {
1012         mmblocktype far *scan;
1013         unsigned color,temp;
1014         long    end,owner;
1015         char    scratch[80],str[10];
1016
1017 //****  VW_SetDefaultColors();
1018 //****  VW_SetLineWidth(40);
1019         temp = bufferofs;
1020         bufferofs = 0;
1021 //****  VW_SetScreen (0,0);
1022
1023         scan = mmhead;
1024
1025         end = -1;
1026
1027 //CA_OpenDebug ();
1028
1029         while (scan)
1030         {
1031                 if (scan->attributes & PURGEBITS)
1032                         color = 5;              // dark purple = purgable
1033                 else
1034                         color = 9;              // medium blue = non purgable
1035                 if (scan->attributes & LOCKBIT)
1036                         color = 12;             // red = locked
1037                 if (scan->start<=end)
1038                 {
1039                         printf("MM_ShowMemory: Memory block order currupted!");
1040                         return;
1041                 }
1042                 end = scan->start+scan->length-1;
1043 //****          VW_Hlin(scan->start,(unsigned)end,0,color);
1044 //****          VW_Plot(scan->start,0,15);
1045 //****          if (scan->next->start > end+1)
1046 //****                  VW_Hlin(end+1,scan->next->start,0,0);   // black = free
1047
1048 //****#if 0
1049 strcpy (scratch,"Size:");
1050 ltoa ((long)scan->length*16,str,10);
1051 strcat (scratch,str);
1052 strcat (scratch,"\tOwner:0x");
1053 owner = (unsigned)scan->useptr;
1054 ultoa (owner,str,16);
1055 strcat (scratch,str);
1056 strcat (scratch,"\n");
1057 //++++write (debughandle,scratch,strlen(scratch));
1058 printf("%s\n", scratch);
1059 //****#endif
1060
1061                 scan = scan->next;
1062         }
1063
1064 //CA_CloseDebug ();
1065
1066 //++++mh        IN_Ack();
1067 //****  VW_SetLineWidth(64);
1068         bufferofs = temp;
1069 }
1070 //****
1071 #endif
1072
1073 //==========================================================================
1074
1075
1076 /*
1077 ======================
1078 =
1079 = MM_UnusedMemory
1080 =
1081 = Returns the total free space without purging
1082 =
1083 ======================
1084 */
1085
1086 long MM_UnusedMemory (void)
1087 {
1088         unsigned free;
1089         mmblocktype far *scan;
1090
1091         free = 0;
1092         scan = mmhead;
1093
1094         while (scan->next)
1095         {
1096                 free += scan->next->start - (scan->start + scan->length);
1097                 scan = scan->next;
1098         }
1099
1100         return free*16l;
1101 }
1102
1103 //==========================================================================
1104
1105
1106 /*
1107 ======================
1108 =
1109 = MM_TotalFree
1110 =
1111 = Returns the total free space with purging
1112 =
1113 ======================
1114 */
1115
1116 long MM_TotalFree (void)
1117 {
1118         unsigned free;
1119         mmblocktype far *scan;
1120
1121         free = 0;
1122         scan = mmhead;
1123
1124         while (scan->next)
1125         {
1126                 if ((scan->attributes&PURGEBITS) && !(scan->attributes&LOCKBIT))
1127                         free += scan->length;
1128                 free += scan->next->start - (scan->start + scan->length);
1129                 scan = scan->next;
1130         }
1131
1132         return free*16l;
1133 }
1134
1135 //==========================================================================
1136
1137 /*
1138 =====================
1139 =
1140 = MM_BombOnError
1141 =
1142 =====================
1143 */
1144
1145 void MM_BombOnError (boolean bomb)
1146 {
1147         bombonerror = bomb;
1148 }
1149
1150 ///////////////////////////////////////////////////////////////////////////
1151 //
1152 //      US_CheckParm() - checks to see if a string matches one of a set of
1153 //              strings. The check is case insensitive. The routine returns the
1154 //              index of the string that matched, or -1 if no matches were found
1155 //
1156 ///////////////////////////////////////////////////////////////////////////
1157 int
1158 US_CheckParm(char *parm,char **strings)
1159 {
1160         char    cp,cs,
1161                         *p,*s;
1162         int             i;
1163
1164         while (!isalpha(*parm)) // Skip non-alphas
1165                 parm++;
1166
1167         for (i = 0;*strings && **strings;i++)
1168         {
1169                 for (s = *strings++,p = parm,cs = cp = 0;cs == cp;)
1170                 {
1171                         cs = *s++;
1172                         if (!cs)
1173                                 return(i);
1174                         cp = *p++;
1175
1176                         if (isupper(cs))
1177                                 cs = tolower(cs);
1178                         if (isupper(cp))
1179                                 cp = tolower(cp);
1180                 }
1181         }
1182         return(-1);
1183 }
1184