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