+ }
+ mm->mmblocks[i].next = NULL;
+
+//
+// locked block of all memory until we punch out free space
+//
+ GETNEWBLOCK;
+ mm->mmhead = mm->mmnew; // this will allways be the first node
+ mm->mmnew->start = 0;
+ 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();
+ //huge start = (void huge *)(mm->nearheap = _nmalloc(length));
+ start = (void far *)(mm->nearheap = _nmalloc(length));
+#endif
+#ifdef __BORLANDC__
+ length=coreleft();
+ //huge start = (void huge *)(mm->nearheap = malloc(length));
+ start = (void far *)(mm->nearheap = malloc(length));
+#endif
+ length -= 16-(FP_OFF(start)&15);
+ length -= SAVENEARHEAP;
+ seglength = length / 16; // now in paragraphs
+ segstart = FP_SEG(start)+(FP_OFF(start)+15)/16;
+ MML_UseSpace(segstart,seglength, mm);
+ mmi->nearheap = length;
+ //printf("start=%Fp segstart=%x seglen=%lu len=%lu\n", start, segstart, seglength, length);
+
+//
+// get all available far conventional memory segments
+//
+ //printf("_FARCORELEFT %lu\n", _FCORELEFT);
+#ifdef __WATCOMC__
+ _fheapgrow();
+#endif
+#ifdef __BORLANDC__
+ printf("farcoreleft() %lu\n", farcoreleft());
+ printf("(farcoreleft()+32)-_FCORELEFT %d\n", (sword)((farcoreleft()+32)-_FCORELEFT));
+#endif
+ length=_FCORELEFT;//_fcoreleft();//(dword)GetFarFreeSize();//0xffffUL*4UL;
+ start = mm->farheap = _fmalloc(length);
+ //start = mm->farheap = halloc(length, 1);
+ length -= 16-(FP_OFF(start)&15);
+ length -= SAVEFARHEAP;
+ seglength = length / 16; // now in paragraphs
+ segstart = FP_SEG(start)+(FP_OFF(start)+15)/16;
+ MML_UseSpace(segstart,seglength, mm);
+ mmi->farheap = length;
+ //printf("start=%Fp segstart=%x seglen=%lu len=%lu\n", start, segstart, seglength, length);
+
+ mmi->mainmem = mmi->nearheap + mmi->farheap;
+
+//
+// detect EMS and allocate up to 64K at page frame
+//
+ mmi->EMSmem = 0;
+//goto emsskip; //0000
+ for(i = 1;i <
+#ifdef __WATCOMC__
+ __argc
+#endif
+#ifdef __BORLANDC__
+ _argc
+#endif
+ ;i++)
+ {
+ if(US_CheckParm(
+#ifdef __WATCOMC__
+ __argv[i]
+#endif
+#ifdef __BORLANDC__
+ _argv[i]
+#endif
+ ,ParmStringsexmm) == 0)
+ goto emsskip; // param NOEMS
+ }
+ if(MML_CheckForEMS())
+ {
+ MML_SetupEMS(mm); // allocate space
+ //TODO: EMS4! AND EMS 3.2 MASSIVE DATA HANDLMENT!
+ MML_UseSpace(mm->EMSpageframe,(MAPPAGES)*0x4000lu, mm);
+ //if(mm->EMSVer<0x40)
+ MM_MapEMS(mm, mmi); // map in used pages
+ //else
+ //MM_MapXEMS(mm, mmi); // map in used pages
+ }
+
+//
+// detect XMS and get upper memory blocks
+//
+emsskip:
+ mmi->XMSmem = 0;
+goto xmsskip;//0000
+ for(i = 1;i <
+#ifdef __WATCOMC__
+ __argc
+#endif
+#ifdef __BORLANDC__
+ _argc
+#endif
+ ;i++)
+ {
+ if(US_CheckParm(
+#ifdef __WATCOMC__
+ __argv[i]
+#endif
+#ifdef __BORLANDC__
+ _argv[i]
+#endif
+ ,ParmStringsexmm) == 0)
+ goto xmsskip; // param NOXMS
+ }
+ if(MML_CheckForXMS(mm))
+ {
+ MML_SetupXMS(mm, mmi); // allocate as many UMBs as possible
+ }
+
+//
+// allocate the misc buffer
+//
+xmsskip:
+ mm->mmrover = mm->mmhead; // start looking for space after low block
+
+ MM_GetPtr(&(mm->bufferseg),BUFFERSIZE, mm, mmi);
+}
+
+//==========================================================================
+
+/*
+====================
+=
+= MM_Shutdown
+=
+= Frees all conventional, EMS, and XMS allocated
+=
+====================
+*/
+
+void MM_Shutdown(mminfo_t *mm)
+{
+ if(!(mm->mmstarted))
+ return;
+
+ _ffree(mm->farheap);// printf(" far freed\n");
+#ifdef __WATCOMC__
+ _nfree(mm->nearheap);// printf(" near freed\n");
+#endif
+#ifdef __BORLANDC__
+ free(mm->nearheap);// printf(" near freed\n");
+#endif
+ if(MML_CheckForEMS()){ MML_ShutdownEMS(mm); }//printf(" EMS freed\n"); }
+ if(MML_CheckForXMS(mm)){ MML_ShutdownXMS(mm); }//printf(" XMS freed\n"); }
+}
+
+//==========================================================================
+
+/*
+====================
+=
+= MM_GetPtr
+=
+= Allocates an unlocked, unpurgable block
+=
+====================
+*/
+
+void MM_GetPtr (memptr *baseptr, dword size, mminfo_t *mm, mminfotype *mmi)
+{
+ //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;
+
+ needed = (size+15)/16; // convert size from bytes to paragraphs
+
+ 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:
+ for (search = 0; search<3; search++)
+ {
+ //
+ // first search: try to allocate right after the rover, then on up
+ // second search: search from the head pointer up to the rover
+ // third search: compress memory, then scan from start
+ if (search == 1 && mm->mmrover == mm->mmhead)
+ search++;
+
+ switch (search)
+ {
+ case 0:
+ lastscan = mm->mmrover;
+ scan = mm->mmrover->next;
+ endscan = NULL;
+ break;
+ case 1:
+ lastscan = mm->mmhead;
+ scan = mm->mmhead->next;
+ endscan = mm->mmrover;
+ break;
+ case 2:
+ MM_SortMem (mm);
+ lastscan = mm->mmhead;
+ scan = mm->mmhead->next;
+ endscan = NULL;
+ break;
+ }
+
+ startseg = lastscan->start + lastscan->length;
+
+ while (scan != endscan)
+ {
+ if (scan->start - startseg >= needed)
+ {
+ //
+ // got enough space between the end of lastscan and
+ // the start of scan, so throw out anything in the middle
+ // and allocate the new block
+ //
+ purge = lastscan->next;
+ lastscan->next = mm->mmnew;
+ mm->mmnew->start = *(unsigned *)baseptr = startseg;
+ mm->mmnew->next = scan;
+ while ( purge != scan)
+ { // free the purgable block
+ next = purge->next;
+ FREEBLOCK(purge);
+ purge = next; // purge another if not at scan
+ }
+ mm->mmrover = mm->mmnew;
+ return; // good allocation!
+ }
+
+ //
+ // if this block is purge level zero or locked, skip past it
+ //
+ if ( (scan->attributes & LOCKBIT)
+ || !(scan->attributes & PURGEBITS) )
+ {
+ lastscan = scan;
+ startseg = lastscan->start + lastscan->length;
+ }
+
+
+ scan=scan->next; // look at next line
+ }
+ }
+
+ if (mm->bombonerror)
+ {
+#ifdef __WATCOMC__
+ //heapdump();
+#endif
+ printf(OUT_OF_MEM_MSG,(size-mmi->nearheap));
+ printf("for stability reasons the program will shut down! wwww\n");
+ MM_Shutdown(mm);
+ exit(-1);
+ }
+ else
+ mm->mmerror = true;
+}
+
+//==========================================================================
+
+/*
+====================
+=
+= MM_FreePtr
+=
+= Allocates an unlocked, unpurgable block
+=
+====================
+*/
+
+void MM_FreePtr(memptr *baseptr, mminfo_t *mm)
+{
+ //huge mmblocktype huge *scan,huge *last;
+ mmblocktype far *scan,far *last;
+
+ last = mm->mmhead;
+ scan = last->next;
+
+ if(baseptr == mm->mmrover->useptr) // removed the last allocated block
+ mm->mmrover = mm->mmhead;
+
+ while(scan->useptr != baseptr && scan)
+ {
+ last = scan;
+ scan = scan->next;
+ }
+
+ if(!scan)
+ {
+ printf("MM_FreePtr: Block not found!\n");
+ return;
+ }
+
+ last->next = scan->next;
+
+ FREEBLOCK(scan);
+}
+//==========================================================================
+
+/*
+=====================
+=
+= MM_SetPurge
+=
+= Sets the purge level for a block (locked blocks cannot be made purgable)
+=
+=====================
+*/
+
+void MM_SetPurge(memptr *baseptr, int purge, mminfo_t *mm)
+{
+ //huge mmblocktype huge *start;
+ mmblocktype far *start;
+
+ start = mm->mmrover;
+
+ do
+ {
+ if(mm->mmrover->useptr == baseptr)
+ break;
+
+ mm->mmrover = mm->mmrover->next;
+
+ if(!mm->mmrover)
+ mm->mmrover = mm->mmhead;
+ else if(mm->mmrover == start)
+ {
+ printf("MM_SetPurge: Block not found!");
+ return;
+ }
+
+ } while(1);
+
+ mm->mmrover->attributes &= ~PURGEBITS;
+ mm->mmrover->attributes |= purge;
+}
+
+//==========================================================================
+
+/*
+=====================
+=
+= MM_SetLock
+=
+= Locks / unlocks the block
+=
+=====================
+*/
+
+void MM_SetLock(memptr *baseptr, boolean locked, mminfo_t *mm)
+{
+ //huge mmblocktype huge *start;
+ mmblocktype far *start;
+
+ start = mm->mmrover;
+
+ do
+ {
+ if(mm->mmrover->useptr == baseptr)
+ break;
+
+ mm->mmrover = mm->mmrover->next;
+
+ if(!mm->mmrover)
+ mm->mmrover = mm->mmhead;
+ else if(mm->mmrover == start)
+ {
+ printf("MM_SetLock: Block not found!");
+ return;
+ }
+
+ } while(1);
+
+ mm->mmrover->attributes &= ~LOCKBIT;
+ mm->mmrover->attributes |= locked*LOCKBIT;
+}
+
+//==========================================================================
+
+/*
+=====================
+=
+= MM_SortMem
+=
+= Throws out all purgable stuff and compresses movable blocks
+=
+=====================
+*/
+
+void MM_SortMem(mminfo_t *mm)
+{
+ //huge mmblocktype huge *scan,huge *last,huge *next;
+ mmblocktype far *scan,far *last,far *next;
+ unsigned start,length,source,dest,oldborder;
+ int playing;
+
+ //
+ // lock down a currently playing sound
+ //
+/*++++ playing = SD_SoundPlaying ();
+ if(playing)
+ {
+ switch (SoundMode)
+ {
+ case sdm_PC:
+ playing += STARTPCSOUNDS;
+ break;
+ case sdm_AdLib:
+ playing += STARTADLIBSOUNDS;
+ break;
+ }
+ MM_SetLock(&(memptr)audiosegs[playing],true);
+ }
+
+
+ SD_StopSound();*/
+// oldborder = bordercolor;
+// VW_ColorBorder (15);
+
+ if(beforesort)
+ beforesort();
+
+ scan = mm->mmhead;
+
+ last = NULL; // shut up compiler warning
+
+ while(scan)
+ {
+ if(scan->attributes & LOCKBIT)
+ {
+ //
+ // block is locked, so try to pile later blocks right after it
+ //
+ start = scan->start + scan->length;
+ }
+ else
+ {
+ if(scan->attributes & PURGEBITS)
+ {
+ //
+ // throw out the purgable block
+ //
+ next = scan->next;
+ FREEBLOCK(scan);
+ //MM_FreeBlock(scan, mm);
+ last->next = next;
+ scan = next;
+ continue;
+ }
+ else
+ {
+ //
+ // push the non purgable block on top of the last moved block
+ //
+ if(scan->start != start)
+ {
+ length = scan->length;
+ source = scan->start;
+ dest = start;
+ while(length > 0xf00)
+ {
+ movedata(source,0,dest,0,0xf00*16);
+ length -= 0xf00;
+ source += 0xf00;
+ dest += 0xf00;
+ }
+ movedata(source,0,dest,0,length*16);
+
+ scan->start = start;
+ *(unsigned *)scan->useptr = start;
+ }
+ start = scan->start + scan->length;
+ }
+ }
+
+ last = scan;
+ scan = scan->next; // go to next block
+ }
+
+ mm->mmrover = mm->mmhead;
+
+ if(aftersort)
+ aftersort();
+
+// VW_ColorBorder (oldborder);
+
+/*++++ if(playing)
+ MM_SetLock(&(memptr)audiosegs[playing],false);*/
+}
+
+//==========================================================================
+
+/*
+=====================
+=
+= MM_ShowMemory
+=
+=====================
+*/
+
+void MM_ShowMemory(global_game_variables_t *gvar,/*page_t *page, */mminfo_t *mm)
+{
+ //huge mmblocktype huge *scan;
+ mmblocktype far *scan;
+ word temp;
+ sdword end,owner;
+ //word chx,chy;
+ word w;
+ //dword wwww;
+ byte scratch[160],scratch0[4096],scratch1[160],str[16];
+ //byte d = '#';
+//**** VW_SetDefaultColors();
+//**** VW_SetLineWidth(40);
+//++++mh temp = bufferofs;
+//++++mh bufferofs = 0;
+//**** VW_SetScreen (0,0);
+ scan = mm->mmhead;
+ end = -1;
+
+ CA_OpenDebug (gvar);
+ w=0;
+ while(scan)
+ {
+ strcpy(scratch, AARESET);
+ if(scan->attributes & PURGEBITS)
+ strcpy(scratch0, AAMAGENTA); // dark purple = purgable
+ else
+ strcpy(scratch0, AABLUE); // medium blue = non purgable
+ if(scan->attributes & LOCKBIT)
+ strcpy(scratch0, AARED); // red = locked
+ if(scan->start<=end)
+ {
+ printf("\nend==%d\n\n", end);
+ strcat(scratch, "MM_ShowMemory: Memory block order currupted!\n");
+ strcat(scratch, "End's Size: ");
+ ultoa (end,str,10);
+ strcat (scratch,str);
+ strcat(scratch, "\nscan->start's Size: ");
+ ultoa (scan->start,str,10);
+ strcat (scratch,str);
+ write(gvar->handle.debughandle,scratch,strlen(scratch));
+ //modexprint(&page, chx, chy, 1, 0, 24, "\nMM_ShowMemory: Memory block order currupted!\n");
+ break;
+ }
+ end = scan->start+(scan->length)-1;
+//++++ chy = scan->start/320;
+//++++ chx = scan->start%320;
+ //modexhlin(page, scan->start, (unsigned)end, chy, color);
+ //for(chx=scan->start;chx+4>=(word)end;chx+=4)
+ //{
+//++++ modexClearRegion(page, chx, chy, 4, 4, color);
+ //}
+//++++ VW_Hlin(scan->start,(unsigned)end,0,color);
+ for(w=(scan->start)/80;w<=end/80;w++)
+ {
+ //printf("+ %u %lu\n", w, scan->length);
+ strcat(scratch0, "+");
+ }
+ strcat(scratch0, AARESET); strcat(scratch0, AAGREY); strcat(scratch0,"_");
+//++++ VW_Plot(scan->start,0,15);
+//++++ 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->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<=((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=%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;
+ strcat(scratch0, AARESET);
+ strcat(scratch0, "\n");
+ strcat(scratch0,AAGREEN);
+ for(w=(end+1);w<=(0x1000/80);w++)
+ {
+ //printf("0 %x %x %u\n", scan->start, w);
+ 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("================\n");
+ getch();
+ }*/
+ strcat(scratch0, AARESET);
+ //strcat(scratch0,"\n");
+ //for(chx=scan->next->start;chx+4>=(word)end+1;chx+=4)
+ //{
+// chx+=scan->next->start;
+// modexClearRegion(page, chx, chy, 4, 4, 2);
+ //}
+ //modexhlin(page, end+1,scan->next->start, chy, 0);
+/* y = scan->start/320;
+ x = scan->start%320;
+ VW_Hlin(x,x+end,y,color);
+ VW_Plot(x,y,15);*/
+//++++ VW_Hlin(x+end+1,x+(scan->next->start-scan->start),y,0); // black = free
+ strcat(scratch,"Seg:");
+ ultoa (scan->start,str,16);
+ strcat (scratch,str);
+ strcat (scratch,"\tSize:");
+ ultoa ((unsigned)scan->length,str,10);
+ strcat (scratch,str);
+ strcat (scratch,"\tOwner:0x");
+ owner = (unsigned)scan->useptr;
+ ultoa (owner,str,16);
+ strcat (scratch,str);
+ strcat (scratch,"\n");
+ write(gvar->handle.debughandle,scratch,strlen(scratch));
+ write(gvar->handle.debughandle,scratch0,strlen(scratch0));
+//modexprint(page, chx, chy, 1, 0, 24, &scratch);
+//++++chy+=4;
+//fprintf(stdout, "%s", scratch);
+
+ 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);
+
+//++++mh IN_Ack();
+//**** VW_SetLineWidth(64);
+//++++mh bufferofs = temp;
+}
+
+//==========================================================================
+
+/*
+=====================
+=
+= MM_DumpData
+=
+=====================
+*/
+
+void MM_DumpData(mminfo_t *mm)
+{
+ //huge mmblocktype huge *scan,huge *best;
+ mmblocktype far *scan,far *best;
+ long lowest,oldlowest;
+ word owner;
+ byte lock,purge;
+ FILE *dumpfile;
+