+ }
+ 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->mmrover = mm->mmhead;
+
+//
+// get all available near conventional memory segments
+//
+#ifdef __WATCOMC__
+ _nheapgrow();
+ length=(dword)_memavl();//(dword)GetFreeSize();
+ start = (void huge *)(mm->nearheap = _nmalloc(length));
+#endif
+#ifdef __BORLANDC__
+ length=coreleft();
+ start = (void huge *)(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;
+ 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)
+{
+ mmblocktype huge *scan,huge *lastscan,huge *endscan,huge *purge,huge *next;
+ int search;
+ dword needed;
+ word startseg;
+
+ needed = (size+15)/16; // convert size from bytes to paragraphs
+printf("."); //0000
+ GETNEWBLOCK; // fill in start and next after a spot is found
+ mm->mmnew->length = needed;
+ mm->mmnew->useptr = baseptr;
+ mm->mmnew->attributes = BASEATTRIBUTES;
+printf("."); //0000
+ for(search = 0; search<mm->endid; search++)
+ {
+printf(" [case]"); //0000
+ //
+ // 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:
+printf("0 "); //0000
+ lastscan = mm->mmrover;
+ scan = mm->mmrover->next;
+ endscan = NULL;
+ break;
+ case 1:
+printf("1 "); //0000
+ lastscan = mm->mmhead;
+ scan = mm->mmhead->next;
+ endscan = mm->mmrover;
+ break;
+ case 2:
+printf("2 "); //0000
+ MM_SortMem(mm);
+ lastscan = mm->mmhead;
+ scan = mm->mmhead->next;
+ endscan = NULL;
+ break;
+ }
+
+ startseg = lastscan->start + (word)lastscan->length;
+
+ while(scan != endscan)
+ {
+//printf(","); //0000
+ if(scan->start - startseg >= needed)
+ {
+printf("."); //0000
+ //
+ // 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 = *(word *)baseptr = startseg;
+ mm->mmnew->next = scan;
+ while(purge != scan)
+ { // free the purgable block
+printf(" freeing block~\n"); //0000
+ next = purge->next;
+ FREEBLOCK(purge);
+ //MM_FreeBlock(purge, mm);
+ 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) )
+ {
+printf(" [lock] "); //0000
+printf("len=%lu ", scan->length);
+ lastscan = scan;
+ startseg = lastscan->start + (word)lastscan->length;
+ }
+
+printf("\n");
+ 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");
+ printf(" endid=%u\n",(mm->endid));
+ MM_Shutdown(mm);
+ exit(-1);
+ }
+ else
+ mm->mmerror = true;
+}
+
+//==========================================================================
+
+/*
+====================
+=
+= MM_FreePtr
+=
+= Allocates an unlocked, unpurgable block
+=
+====================
+*/
+
+void MM_FreePtr(memptr *baseptr, mminfo_t *mm)
+{
+ mmblocktype huge *scan,huge *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)
+{
+ mmblocktype huge *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)
+{
+ mmblocktype huge *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)
+{
+ mmblocktype huge *scan,huge *last,huge *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)
+{
+ mmblocktype huge *scan;
+ word temp;
+ sdword end,owner;
+ //word chx,chy;
+ word w;
+ byte scratch[160],scratch0[4096],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;
+ }
+ //for(;scan->length>0xfffflu;scan->length-=0xfffflu);
+ 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
+ if((scan->next->start != 0xe000) > end)
+ {
+ strcat(scratch0, AARESET);
+ strcat(scratch0,AAGREEN);
+ for(w=0;w<=((scan->length+1)/80);w++)
+ {
+ //printf("0 %x %u %lu\n", scan->next->start, w, scan->length);
+ strcat(scratch0,"0");
+ }
+ }else{
+ strcat(scratch0, AARESET);
+ strcat(scratch0,AAGREEN);
+ for(w=0;w<=((scan->length+1)/80);w++)
+ {
+ //printf("0 %x %u %lu\n", scan->next->start, w, scan->length);
+ strcat(scratch0,"0");
+ }
+ printf("================\nstart=%x length==%lu\n next=%x end=%x\nscan->start+(scan->length)==%x\n================\n", scan->start, scan->length, scan->next->start, end, scan->start+(scan->length));
+ printf("w=%u (scan->length+1)/80)=%lu\n\n", w, (scan->length+1)/80);
+ //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 ((word)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;
+ }
+
+ CA_CloseDebug (gvar);
+
+//++++mh IN_Ack();
+//**** VW_SetLineWidth(64);
+//++++mh bufferofs = temp;
+}
+
+//==========================================================================
+
+/*
+=====================
+=
+= MM_DumpData
+=
+=====================
+*/
+
+void MM_DumpData(mminfo_t *mm)
+{
+ mmblocktype huge *scan,huge *best;
+ long lowest,oldlowest;
+ word owner;
+ byte lock,purge;
+ FILE *dumpfile;
+