]> 4ch.mooo.com Git - 16.git/blobdiff - src/lib/16_mm.c
cleaned up the repo from debugging watcom2 ^^
[16.git] / src / lib / 16_mm.c
old mode 100644 (file)
new mode 100755 (executable)
index d058254..7cf59d3
@@ -89,7 +89,6 @@ boolean MML_CheckForEMS(void)
 {
        boolean emmcfems;
        static char     emmname[] = "EMMXXXX0"; //fix by andrius4669
-//             mov     dx,OFFSET emmname
        __asm {
                mov     dx,OFFSET emmname       //fix by andrius4669
                mov     ax,0x3d00
@@ -475,13 +474,15 @@ void MML_SetupXMS(mminfo_t *mm, mminfotype *mmi)
 {
        word    base,size;
 
-getmemory:
+
        __asm {
                mov     ax,0x4310
                int     0x2f
                mov     [WORD PTR XMSaddr],bx
                mov     [WORD PTR XMSaddr+2],es         // function pointer to XMS driver
-
+       }
+getmemory:
+       __asm {
                mov     ah,XMS_ALLOCUMB
                mov     dx,0xffff                                       // try for largest block possible
                //mov     ax,dx                                         // Set available Kbytes.
@@ -560,9 +561,9 @@ void MML_ShutdownXMS(mminfo_t *mm)
 ======================
 */
 
-void MML_UseSpace(word segstart, dword seglength, mminfo_t *mm)
+/*void MML_UseSpace(word segstart, dword seglength, mminfo_t *mm)
 {
-       mmblocktype huge *scan,huge *last;
+       //huge mmblocktype huge *scan,huge *last;
        word            segm=1;
        word    oldend;
        dword           segmlen;
@@ -578,8 +579,6 @@ void MML_UseSpace(word segstart, dword seglength, mminfo_t *mm)
        {
                last = scan;
                scan = scan->next;
-               //printf("              start=%x",scan->start);
-               //printf("              seglen=%lu\n", scan->length);
        }
 
 //
@@ -589,38 +588,17 @@ void MML_UseSpace(word segstart, dword seglength, mminfo_t *mm)
        extra = oldend - (segstart+seglength);
 
        segmlen=extra;
-//
-// find out how many blocks it spans!
-//
-       for(;segmlen>0x10000;segmlen-=0x10000)
-       {
-               //printf("      seglen=%lu\n", segmlen);
-               segm++;
-       }
 
        //++++emsver stuff!
-       if(segm>1/* || extra>=0x10000lu*/)
+       if(segm>1)/// || extra>=0x10000lu)
        //if(extra>0xfffflu)
        {
                scan->blob=segm;
-               /*__asm
-               {
-                       push    ds
-                       mov     ax,ds
-                       inc             ax
-                       mov     ds,ax
-               }*/
-
 
                //MML_UseSpace(segstart, seglength, mm);
 
-               /*__asm
-               {
-                       pop ds
-               }*/
                printf("MML_UseSpace: Segment spans two blocks!\n");
        //}
-       printf("========================================\n");
        printf("segm=%u         ", segm);
        printf("ex=%lu  ", extra);
        printf("old=%u  ", oldend);
@@ -629,7 +607,6 @@ void MML_UseSpace(word segstart, dword seglength, mminfo_t *mm)
        printf("len=%lu ", scan->length);
        printf("seglen=%lu      ", seglength);
        printf("segmlen=%lu\n", segmlen);
-       printf("========================================\n");
        }
 //++++todo: linked list of segment!
        if(segstart == scan->start)
@@ -653,6 +630,76 @@ void MML_UseSpace(word segstart, dword seglength, mminfo_t *mm)
                mm->mmnew->attributes = LOCKBIT;
        }//else if(segm>0) goto segu;
 
+}*/
+void MML_UseSpace(word segstart, dword seglength, mminfo_t *mm)
+{
+       mmblocktype far *scan,far *last;
+       word    oldend;
+       sdword          extra;
+       //word segm=1;
+
+       scan = last = mm->mmhead;
+       mm->mmrover = mm->mmhead;               // reset rover to start of memory
+
+//
+// search for the block that contains the range of segments
+//
+       while (scan->start+scan->length < segstart)
+       {
+               last = scan;
+               scan = scan->next;
+       }
+
+//
+// find out how many blocks it spans!
+//
+       /*for(;seglength>=0x10000;seglength-=0xFFFF)
+       {
+               //printf("      seglen=%lu\n", segmlen);
+               segm++;
+       }*/
+
+//
+// take the given range out of the block
+//
+       oldend = scan->start + scan->length;
+       extra = oldend - (segstart+((unsigned)seglength));
+       if (extra < 0)
+       {
+               printf("========================================\n");
+               printf("start=%x        ", scan->start);
+               printf("old=%u  ", oldend);
+               printf("start+seglen=%lu\n", segstart+seglength);
+               printf("segsta=%x       ", segstart);
+               printf("len=%lu ", scan->length);
+               printf("seglen=%lu      ", seglength);
+               printf("\n");
+               printf("MML_UseSpace: Segment spans two blocks! %d\n", extra);
+               printf("========================================\n");
+               //return;
+       }
+
+       if (segstart == scan->start)
+       {
+               last->next = scan->next;                        // unlink block
+               FREEBLOCK(scan);
+               scan = last;
+       }
+       else
+               scan->length = segstart-scan->start;    // shorten block
+
+       if (extra > 0)
+       {
+               GETNEWBLOCK;
+               mm->mmnew->useptr = NULL;
+
+               mm->mmnew->next = scan->next;
+               scan->next = mm->mmnew;
+               mm->mmnew->start = segstart+seglength;
+               mm->mmnew->length = extra;
+               mm->mmnew->attributes = LOCKBIT;
+       }
+
 }
 
 //==========================================================================
@@ -669,7 +716,8 @@ void MML_UseSpace(word segstart, dword seglength, mminfo_t *mm)
 
 void MML_ClearBlock(mminfo_t *mm)
 {
-       mmblocktype huge *scan,huge *last;
+       //huge mmblocktype huge *scan,huge *last;
+       mmblocktype far *scan,far *last;
 
        scan = mm->mmhead->next;
 
@@ -703,10 +751,12 @@ void MML_ClearBlock(mminfo_t *mm)
 void MM_Startup(mminfo_t *mm, mminfotype *mmi)
 {
        int i;
-       dword length,seglength;
-       //dword length; word seglength;
-       void huge       *start;
+       //dword length,seglength;
+       dword length; word seglength;
+       //huge void huge        *start;
+       void far        *start;
        word    segstart;//,endfree;
+       //memptr *peeonself;
 
 //     if(mm->mmstarted)
 //             MM_Shutdown(mm);
@@ -734,19 +784,24 @@ void MM_Startup(mminfo_t *mm, mminfotype *mmi)
        mm->mmnew->length = 0xffff;
        mm->mmnew->attributes = LOCKBIT;
        mm->mmnew->next = NULL;
+       //mm->mmnew->useptr = peeonself;
        mm->mmrover = mm->mmhead;
 
+       //printf("              %x\n", peeonself);
+       //printf("              %x\n", *peeonself);
 //
 // get all available near conventional memory segments
 //
 #ifdef __WATCOMC__
        _nheapgrow();
        length=(dword)_memavl();//(dword)GetFreeSize();
-       start = (void huge *)(mm->nearheap = _nmalloc(length));
+       //huge start = (void huge *)(mm->nearheap = _nmalloc(length));
+       start = (void far *)(mm->nearheap = _nmalloc(length));
 #endif
 #ifdef __BORLANDC__
        length=coreleft();
-       start = (void huge *)(mm->nearheap = malloc(length));
+       //huge start = (void huge *)(mm->nearheap = malloc(length));
+       start = (void far *)(mm->nearheap = malloc(length));
 #endif
        length -= 16-(FP_OFF(start)&15);
        length -= SAVENEARHEAP;
@@ -896,8 +951,8 @@ void MM_Shutdown(mminfo_t *mm)
 
 void MM_GetPtr (memptr *baseptr, dword size, mminfo_t *mm, mminfotype *mmi)
 {
-       mmblocktype huge *scan,huge *lastscan,huge *endscan
-                               ,huge *purge,huge *next;
+       //huge mmblocktype huge *scan,huge *lastscan,huge *endscan,huge *purge,huge *next;
+       mmblocktype far *scan,far *lastscan,far *endscan,far *purge,far *next;
        int                     search;
        unsigned        needed,startseg;
 
@@ -906,6 +961,11 @@ void MM_GetPtr (memptr *baseptr, dword size, mminfo_t *mm, mminfotype *mmi)
        GETNEWBLOCK;                            // fill in start and next after a spot is found
        mm->mmnew->length = needed;
        mm->mmnew->useptr = baseptr;
+       //if(mm->mmnew->useptr==NULL){
+       printf("baseptr=%04x    ", baseptr); printf("useptr=%04x\n", mm->mmnew->useptr);
+       printf("*baseptr=%04x   ", *baseptr); printf("*useptr=%04x\n", *(mm->mmnew->useptr));
+       //printf("*baseptr=%Fp  ", *baseptr); printf("*useptr=%Fp\n", *(mm->mmnew->useptr));
+       //exit(-5); }
        mm->mmnew->attributes = BASEATTRIBUTES;
 
 //tryagain:
@@ -1006,7 +1066,8 @@ void MM_GetPtr (memptr *baseptr, dword size, mminfo_t *mm, mminfotype *mmi)
 
 void MM_FreePtr(memptr *baseptr, mminfo_t *mm)
 {
-       mmblocktype huge *scan,huge *last;
+       //huge mmblocktype huge *scan,huge *last;
+       mmblocktype far *scan,far *last;
 
        last = mm->mmhead;
        scan = last->next;
@@ -1044,7 +1105,8 @@ void MM_FreePtr(memptr *baseptr, mminfo_t *mm)
 
 void MM_SetPurge(memptr *baseptr, int purge, mminfo_t *mm)
 {
-       mmblocktype huge *start;
+       //huge mmblocktype huge *start;
+       mmblocktype far *start;
 
        start = mm->mmrover;
 
@@ -1083,7 +1145,8 @@ void MM_SetPurge(memptr *baseptr, int purge, mminfo_t *mm)
 
 void MM_SetLock(memptr *baseptr, boolean locked, mminfo_t *mm)
 {
-       mmblocktype huge *start;
+       //huge mmblocktype huge *start;
+       mmblocktype far *start;
 
        start = mm->mmrover;
 
@@ -1122,7 +1185,8 @@ void MM_SetLock(memptr *baseptr, boolean locked, mminfo_t *mm)
 
 void MM_SortMem(mminfo_t *mm)
 {
-       mmblocktype huge *scan,huge *last,huge *next;
+       //huge mmblocktype huge *scan,huge *last,huge *next;
+       mmblocktype far *scan,far *last,far *next;
        unsigned        start,length,source,dest,oldborder;
        int                     playing;
 
@@ -1232,13 +1296,14 @@ void MM_SortMem(mminfo_t *mm)
 
 void MM_ShowMemory(global_game_variables_t *gvar,/*page_t *page, */mminfo_t *mm)
 {
-       mmblocktype huge *scan;
+       //huge mmblocktype huge *scan;
+       mmblocktype far *scan;
        word temp;
        sdword  end,owner;
        //word chx,chy;
        word w;
-       dword wwww;
-       byte    scratch[160],scratch0[4096],str[16];
+       //dword wwww;
+       byte    scratch[160],scratch0[4096],scratch1[160],str[16];
        //byte d = '#';
 //**** VW_SetDefaultColors();
 //**** VW_SetLineWidth(40);
@@ -1292,21 +1357,25 @@ void MM_ShowMemory(global_game_variables_t *gvar,/*page_t *page, */mminfo_t *mm)
 //++++                         modexClearRegion(page, chx, chy, 4, 4, 15);
 //++++                 VW_Hlin(end+1,scan->next->start,0,0);   // black = free
 
-               wwww=(dword)(scan->next->start)-(dword)scan->start;
+               //wwww=(dword)(scan->next->start)-(dword)scan->start;
                //wwww=(dword)scan->start+(dword)(scan->next->start);
                if (scan->next && scan->next->start >= end+1)
                {
                        strcat(scratch0, AARESET);
                        strcat(scratch0, "\n");
                        strcat(scratch0,AAGREEN);
-                       for(w=(end+1)/80;w<=(wwww/80);w++)
+                       for(w=(end+1)/80;w<=((scan->next->start-scan->start)/80);w++)
+                       //for(w=(wwww)/80;w<=((end+1)/80);w++)
+                       //for(w=(end+1)/80;w<=((wwww)/80);w++)
                        {
                                //printf("0     %x      %u      %lu\n", scan->next->start, w, scan->length);
                                strcat(scratch0,"0");
                        }
                        //printf("==================\n");
-                       //printf("w=%x  start=%x        next=%x end=%u  %lu\n", w, scan->start, (scan->next->start), end+1, wwww);
+                       //printf("w=%u  wwww=%lu        start=%04x      next=%04x       end=%lu\n", w/80, wwww/80, scan->start, (scan->next->start), end+1);
                        //printf("==================\n");
+                       strcat(scratch0, "\n");
+                       //getch();
                }/*else {//if(scan->next->start <= scan->start){
                        scan->next->start=scan->start+0x1000;
                        wwww=(dword)(scan->next->start)-(dword)scan->start;
@@ -1321,10 +1390,10 @@ void MM_ShowMemory(global_game_variables_t *gvar,/*page_t *page, */mminfo_t *mm)
                        printf("================\n");
                        printf("w=%x    start=%x        next=%x end=%u  %lu\n", w, scan->start, (scan->next->start), end+1, wwww);
                        printf("================\n");
-//                     getch();
+                       getch();
                }*/
                strcat(scratch0, AARESET);
-               strcat(scratch0,"\n");
+               //strcat(scratch0,"\n");
                        //for(chx=scan->next->start;chx+4>=(word)end+1;chx+=4)
                        //{
 //                             chx+=scan->next->start;
@@ -1340,7 +1409,7 @@ void MM_ShowMemory(global_game_variables_t *gvar,/*page_t *page, */mminfo_t *mm)
                ultoa (scan->start,str,16);
                strcat (scratch,str);
                strcat (scratch,"\tSize:");
-               ultoa ((word)scan->length,str,10);
+               ultoa ((unsigned)scan->length,str,10);
                strcat (scratch,str);
                strcat (scratch,"\tOwner:0x");
                owner = (unsigned)scan->useptr;
@@ -1355,6 +1424,21 @@ void MM_ShowMemory(global_game_variables_t *gvar,/*page_t *page, */mminfo_t *mm)
 
                scan = scan->next;
        }
+       /*strcpy(scratch1, AARESET);
+       strcat(scratch1, "========================================\n");
+       strcat(scratch1, "near=  ");
+       ultoa (*(mm->nearheap),str,10);
+       strcat (scratch1,str);
+       strcat(scratch1, "      far= ");
+       ultoa (*(mm->farheap),str,10);
+       strcat (scratch1,str);
+       strcat(scratch1, "\n");
+       //strcat(scratch1, "&near=      %Fp ", &(mm->nearheap));
+       //strcat(scratch1, "&far=       %Fp", &(mm->farheap));
+       //strcat(scratch1, "\n");
+       strcat(scratch1, "========================================\n");
+       write(gvar->handle.debughandle,scratch1,strlen(scratch1));*/
+
 
        CA_CloseDebug (gvar);
 
@@ -1375,14 +1459,20 @@ void MM_ShowMemory(global_game_variables_t *gvar,/*page_t *page, */mminfo_t *mm)
 
 void MM_DumpData(mminfo_t *mm)
 {
-       mmblocktype huge *scan,huge *best;
+       //huge mmblocktype huge *scan,huge *best;
+       mmblocktype far *scan,far *best;
        long    lowest,oldlowest;
        word    owner;
        byte    lock,purge;
        FILE    *dumpfile;
 
        free(mm->nearheap);
-       dumpfile = fopen ("mmdump.16","w");
+#ifdef __BORLANDC__
+               dumpfile = fopen ("mmdump.16b","w");
+#endif
+#ifdef __WATCOMC__
+               dumpfile = fopen ("mmdump.16w","w");
+#endif
        if (!dumpfile){
                printf("MM_DumpData: Couldn't open MMDUMP.16!\n");
                return;
@@ -1397,7 +1487,7 @@ void MM_DumpData(mminfo_t *mm)
                scan = mm->mmhead;
                while (scan)
                {
-                       owner = (word)scan->useptr;
+                       owner = (unsigned)scan->useptr;
 
                        if (owner && owner<lowest && owner > oldlowest)
                        {
@@ -1419,7 +1509,7 @@ void MM_DumpData(mminfo_t *mm)
                        else
                                lock = '-';
                        fprintf (dumpfile,"0x%p (%c%c) = %u\n"
-                       ,(word)lowest,lock,purge,best->length);
+                       ,(unsigned)lowest,lock,purge,best->length);
                }
 
        } while (lowest != 0xffff);
@@ -1444,7 +1534,8 @@ void MM_DumpData(mminfo_t *mm)
 dword MM_UnusedMemory(mminfo_t *mm)
 {
        dword free;
-       mmblocktype huge *scan;
+       //huge mmblocktype huge *scan;
+       mmblocktype far *scan;
 
        free = 0;
        scan = mm->mmhead;
@@ -1475,7 +1566,8 @@ dword MM_UnusedMemory(mminfo_t *mm)
 dword MM_TotalFree(mminfo_t *mm)
 {
        dword free;
-       mmblocktype huge *scan;
+       //huge mmblocktype huge *scan;
+       mmblocktype far *scan;
 
        free = 0;
        scan = mm->mmhead;