]> 4ch.mooo.com Git - 16.git/blobdiff - src/lib/16_hc.c
updating copy left. i am super fuckin busy with school, and trying to get life back...
[16.git] / src / lib / 16_hc.c
old mode 100644 (file)
new mode 100755 (executable)
index af747fc..388133e
-/* Project 16 Source Code~
- * Copyright (C) 2012-2015 sparky4 & pngwen & andrius4669
- *
- * This file is part of Project 16.
- *
- * Project 16 is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 3 of the License, or
- * (at your option) any later version.
- *
- * Project 16 is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>, or
- * write to the Free Software Foundation, Inc., 51 Franklin Street,
- * Fifth Floor, Boston, MA 02110-1301 USA.
- *
- */
-/*
-       heap test stuff
-*/
-
-#include "src/lib/16_hc.h"
-
-void __near* LargestFreeBlock(size_t* Size)
-{
-       size_t s0, s1;
-       void __near* p;
-
-       s0 = ~(size_t)0 ^ (~(size_t)0 >> 1);
-       while (s0 && (p = _nmalloc(s0)) == NULL)
-               s0 >>= 1;
-
-       if (p)
-               _nfree(p);
-
-       s1 = s0 >> 1;
-       while (s1)
-       {
-               if ((p = _nmalloc(s0 + s1)) != NULL)
-               {
-                       s0 += s1;
-                       _nfree(p);
-               }
-       s1 >>= 1;
-       }
-       while (s0 && (p = _nmalloc(s0)) == NULL)
-               s0 ^= s0 & -s0;
-
-       *Size = s0;
-       return p;
-}
-
-size_t _coreleft(void)
-{
-       size_t total = 0;
-       void __near* pFirst = NULL;
-       void __near* pLast = NULL;
-       for(;;)
-       {
-               size_t largest;
-               void __near* p = LargestFreeBlock(&largest);
-               if (largest < sizeof(void __near*))
-               {
-                       if (p != NULL)
-                       _nfree(p);
-                       break;
-               }
-               *(void __near* __near*)p = NULL;
-               total += largest;
-               if (pFirst == NULL)
-                       pFirst = p;
-
-               if (pLast != NULL)
-                       *(void __near* __near*)pLast = p;
-               pLast = p;
-       }
-
-       while (pFirst != NULL)
-       {
-               void __near* p = *(void __near* __near*)pFirst;
-               _nfree(pFirst);
-               pFirst = p;
-       }
-       return total;
-}
-
-void far* LargestFarFreeBlock(size_t* Size)
-{
-       size_t s0, s1;
-       void far* p;
-
-       s0 = ~(size_t)0 ^ (~(size_t)0 >> 1);
-       while (s0 && (p = _fmalloc(s0)) == NULL)
-               s0 >>= 1;
-
-       if (p)
-               _ffree(p);
-
-       s1 = s0 >> 1;
-       while (s1)
-       {
-               if ((p = _fmalloc(s0 + s1)) != NULL)
-               {
-                       s0 += s1;
-                       _ffree(p);
-               }
-       s1 >>= 1;
-       }
-       while (s0 && (p = _fmalloc(s0)) == NULL)
-               s0 ^= s0 & -s0;
-
-       *Size = s0;
-       return p;
-}
-
-size_t _farcoreleft(void)
-{
-       size_t total = 0;
-       void far* pFirst = NULL;
-       void far* pLast = NULL;
-       for(;;)
-       {
-               size_t largest;
-               void far* p = LargestFarFreeBlock(&largest);
-               if (largest < sizeof(void far*))
-               {
-                       if (p != NULL)
-                       _ffree(p);
-                       break;
-               }
-               *(void far* far*)p = NULL;
-               total += largest;
-               if (pFirst == NULL)
-                       pFirst = p;
-
-               if (pLast != NULL)
-                       *(void far* far*)pLast = p;
-               pLast = p;
-       }
-
-       while (pFirst != NULL)
-       {
-               void far* p = *(void far* far*)pFirst;
-               _ffree(pFirst);
-               pFirst = p;
-       }
-       return total;
-}
-
-void huge* LargestHugeFreeBlock(size_t* Size)
-{
-       size_t s0, s1;
-       void huge* p;
-
-       s0 = ~(size_t)0 ^ (~(size_t)0 >> 1);
-       while (s0 && (p = halloc((dword)s0, 1)) == NULL)
-               s0 >>= 1;
-
-       if (p)
-               hfree(p);
-
-       s1 = s0 >> 1;
-       while (s1)
-       {
-               if ((p = halloc((dword)(s0 + s1), 1)) != NULL)
-               {
-                       s0 += s1;
-                       hfree(p);
-               }
-       s1 >>= 1;
-       }
-       while (s0 && (p = halloc((dword)s0, 1)) == NULL)
-               s0 ^= s0 & -s0;
-
-       *Size = s0;
-       return p;
-}
-
-size_t _hugecoreleft(void)
-{
-       size_t total = 0;
-       void huge* pFirst = NULL;
-       void huge* pLast = NULL;
-       for(;;)
-       {
-               size_t largest;
-               void huge* p = LargestHugeFreeBlock(&largest);
-               if (largest < sizeof(void huge*))
-               {
-                       if (p != NULL)
-                       hfree(p);
-                       break;
-               }
-               *(void huge* huge*)p = NULL;
-               total += largest;
-               if (pFirst == NULL)
-                       pFirst = p;
-
-               if (pLast != NULL)
-                       *(void huge* huge*)pLast = p;
-               pLast = p;
-       }
-
-       while (pFirst != NULL)
-       {
-               void huge* p = *(void huge* huge*)pFirst;
-               hfree(pFirst);
-               pFirst = p;
-       }
-       return total;
-}
-
-/*void __based(__self)* LargestBasedFreeBlock(size_t* Size)
-{
-       __segment segu;
-       size_t s0, s1;
-       void __based(__self)* p;
-
-       s0 = ~(size_t)0 ^ (~(size_t)0 >> 1);
-       while (s0 && (p = _bmalloc(segu, s0)) == NULL)
-               s0 >>= 1;
-
-       if (p)
-               _ffree(p);
-
-       s1 = s0 >> 1;
-       while (s1)
-       {
-               if ((p = _bmalloc(segu, s0 + s1)) != NULL)
-               {
-                       s0 += s1;
-                       _ffree(p);
-               }
-       s1 >>= 1;
-       }
-       while (s0 && (p = _bmalloc(segu, s0)) == NULL)
-               s0 ^= s0 & -s0;
-
-       *Size = s0;
-       return p;
-}
-
-size_t _basedcoreleft(void)
-{
-       __segment segu;
-       size_t total = 0;
-       void __based(segu)* pFirst = NULL;
-       void __based(segu)* pLast = NULL;
-       // allocate based heap
-       segu = _bheapseg( 1024 );
-       if( segu == _NULLSEG ) {
-               printf( "Unable to allocate based heap\n" );
-               return 0;
-               //exit( 1 );
-       }
-       else
-
-       for(;;)
-       {
-               size_t largest;
-               void __based(segu)* p = LargestBasedFreeBlock(&largest);
-               if (largest < sizeof(void far*))
-               {
-                       if (p != NULL)
-                       _ffree(p);
-                       break;
-               }
-               *(void far* far*)p = NULL;
-               total += largest;
-               if (pFirst == NULL)
-                       pFirst = p;
-
-               if (pLast != NULL)
-                       *(void far* far*)pLast = p;
-               pLast = p;
-       }
-
-       while (pFirst != NULL)
-       {
-               void far* p = *(void far* far*)pFirst;
-               _ffree(pFirst);
-               pFirst = p;
-       }
-       return total;
-}*/
-
-size_t GetFreeSize(void)
-{
-       struct _heapinfo h_info;
-       int heap_status;
-       size_t h_free=0, h_total=0, h_used=0;
-
-       h_info._pentry = NULL;
-       for(;;) {
-               heap_status = _heapwalk( &h_info );
-               if( heap_status != _HEAPOK ) break;
-               if((h_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") h_free += h_info._size;
-               if((h_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") h_used += h_info._size;
-               h_total += h_info._size;
-       }
-       heapstat0(heap_status);
-       return h_free;
-}
-
-size_t GetFarFreeSize(void)
-{
-       struct _heapinfo fh_info;
-       int heap_status;
-       size_t fh_free=0, fh_total=0, fh_used=0;
-
-       fh_info._pentry = NULL;
-       for(;;) {
-               heap_status = _fheapwalk( &fh_info );
-               if( heap_status != _HEAPOK ) break;
-               if((fh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") fh_free += fh_info._size;
-               if((fh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") fh_used += fh_info._size;
-               fh_total += fh_info._size;
-       }
-       heapstat0(heap_status);
-       return fh_free;
-}
-
-size_t GetNearFreeSize(void)
-{
-       struct _heapinfo nh_info;
-       int heap_status;
-       size_t nh_free=0, nh_total=0, nh_used=0;
-
-       nh_info._pentry = NULL;
-       for(;;) {
-               heap_status = _nheapwalk( &nh_info );
-               if( heap_status != _HEAPOK ) break;
-               if((nh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") nh_free += nh_info._size;
-               if((nh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") nh_used += nh_info._size;
-               nh_total += nh_info._size;
-       }
-       heapstat0(heap_status);
-       return nh_free;
-}
-
-void heapdump(void)
-{
-       struct _heapinfo fh_info, nh_info, h_info;
-       int heap_status;
-       size_t h_free, nh_free, fh_free, h_total, nh_total, fh_total, h_used, nh_used, fh_used;
-       byte    scratch[1024],str[16];
-
-       HC_OpenDebug();
-
-       strcpy(scratch,"\n      == default ==\n\n");
-       write(heaphandle,scratch,strlen(scratch));
-       h_info._pentry = NULL;
-       h_free=0; h_total=0; h_used=0;
-       for(;;) {
-               heap_status = _heapwalk( &h_info );
-               if( heap_status != _HEAPOK ) break;
-
-               strcpy(scratch,"  "); strcat(scratch,(h_info._useflag == _USEDENTRY ? "USED" : "FREE")); strcat(scratch," block at "); ultoa((dword)h_info._pentry,str,16); strcat(scratch,str); strcat(scratch," of size "); ultoa(h_info._size,str,16); strcat(scratch,str); strcat(scratch,"\n");
-               if((h_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") h_free += h_info._size;
-               if((h_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") h_used += h_info._size;
-               h_total += h_info._size;
-               write(heaphandle,scratch,strlen(scratch));
-       }
-       heapstat(heap_status, &scratch);
-       
-       //near
-       strcpy(scratch,"\n      == near ==\n\n");
-       write(heaphandle,scratch,strlen(scratch));
-       nh_info._pentry = NULL;
-       nh_free=0; nh_total=0; nh_used=0;
-       for(;;) {
-               heap_status = _nheapwalk( &nh_info );
-               if( heap_status != _HEAPOK ) break;
-               
-               strcpy(scratch,"  "); strcat(scratch,(h_info._useflag == _USEDENTRY ? "USED" : "FREE")); strcat(scratch," block at "); ultoa((dword)h_info._pentry,str,16); strcat(scratch,str); strcat(scratch," of size "); ultoa(h_info._size,str,16); strcat(scratch,str); strcat(scratch,"\n");
-/*             printf( "  %s block at %Fp of size %4.4X\n",
-(nh_info._useflag == _USEDENTRY ? "USED" : "FREE"),
-nh_info._pentry, nh_info._size );*/
-               if((nh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") nh_free += nh_info._size;
-               if((nh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") nh_used += nh_info._size;
-               nh_total += nh_info._size;
-               write(heaphandle,scratch,strlen(scratch));
-       }
-       heapstat(heap_status, &scratch);
-
-       //far
-       strcpy(scratch,"\n      == far ==\n\n");
-       write(heaphandle,scratch,strlen(scratch));
-       fh_info._pentry = NULL;
-       fh_free=0; fh_total=0; fh_used=0;
-       for(;;) {
-               heap_status = _fheapwalk( &fh_info );
-               if( heap_status != _HEAPOK ) break;
-
-               strcpy(scratch,"  "); strcat(scratch,(h_info._useflag == _USEDENTRY ? "USED" : "FREE")); strcat(scratch," block at "); ultoa((dword)h_info._pentry,str,16); strcat(scratch,str); strcat(scratch," of size "); ultoa(h_info._size,str,16); strcat(scratch,str); strcat(scratch,"\n");
-               /*printf( "  %s block at %Fp of size %4.4X\n",
-(fh_info._useflag == _USEDENTRY ? "USED" : "FREE"),
-fh_info._pentry, fh_info._size );*/
-               if((fh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") fh_free += fh_info._size;
-               if((fh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") fh_used += fh_info._size;
-               fh_total += fh_info._size;
-               write(heaphandle,scratch,strlen(scratch));
-       }
-       heapstat(heap_status, &scratch);
-
-       strcpy(scratch,"\n");
-       strcat(scratch,kittengets(2,0,"Memory Type         Total      Used       Free\n"));
-       strcat(scratch,"----------------  --------   --------   --------\n");
-       printmeminfoline(&scratch, "Default", h_total, h_used, h_free);
-       printmeminfoline(&scratch, "Near", nh_total, nh_used, nh_free);
-       printmeminfoline(&scratch, "Far", fh_total, fh_used, fh_free);
-       strcat(scratch,"----------------  --------   --------   --------\n");
-       strcat(scratch,"coreleft = ");                  ultoa((dword)_coreleft(),str,10);               strcat(scratch,str);    strcat(scratch,"\n");
-       strcat(scratch,"farcoreleft = ");               ultoa((dword)_farcoreleft(),str,10);    strcat(scratch,str);    strcat(scratch,"\n");
-       strcat(scratch,"GetFreeSize = ");               ultoa((dword)GetFreeSize(),str,10);             strcat(scratch,str);    strcat(scratch,"\n");
-       strcat(scratch,"GetNearFreeSize = ");   ultoa((dword)GetNearFreeSize(),str,10); strcat(scratch,str);    strcat(scratch,"\n");
-       strcat(scratch,"GetFarFreeSize = ");    ultoa((dword)GetFarFreeSize(),str,10);  strcat(scratch,str);    strcat(scratch,"\n");
-       strcat(scratch,"memavl = ");                    ultoa((dword)_memavl(),str,10);                 strcat(scratch,str);    strcat(scratch,"\n");
-       strcat(scratch,"stackavail = ");                ultoa((dword)stackavail(),str,10);              strcat(scratch,str);    strcat(scratch,"\n");
-       write(heaphandle,scratch,strlen(scratch));
-       HC_CloseDebug();
-}
-
-void heapstat(int heap_status, byte *str)
-{
-       switch( heap_status ) {
-               case _HEAPEND:
-                       strcpy((str),"OK - end of heap\n");
-               break;
-               case _HEAPEMPTY:
-                       strcpy((str),"OK - heap is empty\n");
-                       
-               break;
-               case _HEAPBADBEGIN:
-                       strcpy((str),"ERROR - heap is damaged\n");
-               break;
-               case _HEAPBADPTR:
-                       strcpy((str),"ERROR - bad pointer to heap\n");
-               break;
-               case _HEAPBADNODE:
-                       strcpy((str),"ERROR - bad node in heap\n");
-       }
-       write(heaphandle,(str),strlen((str)));
-}
-
-void heapstat0(int heap_status)
-{
-       switch( heap_status ) {
-               case _HEAPEND:
-                       //printf("OK - end of heap\n");
-               break;
-               case _HEAPEMPTY:
-                       //printf("OK - heap is empty\n");
-                       
-               break;
-               case _HEAPBADBEGIN:
-                       printf("ERROR - heap is damaged\n");
-               break;
-               case _HEAPBADPTR:
-                       printf("ERROR - bad pointer to heap\n");
-               break;
-               case _HEAPBADNODE:
-                       printf("ERROR - bad node in heap\n");
-       }
-}
-
-/*
-============================
-=
-= HC_OpenDebug / HC_CloseDebug
-=
-= Opens a binary file with the handle "heaphandle"
-=
-============================
-*/
-void HC_OpenDebug()
-{
-       unlink("heap.16");
-       heaphandle = open("heap.16", O_CREAT | O_WRONLY | O_TEXT);
-}
-
-void HC_CloseDebug()
-{
-       close(heaphandle);
-}
+/* Project 16 Source Code~\r
+ * Copyright (C) 2012-2019 sparky4 & pngwen & andrius4669 & joncampbell123 & yakui-lover\r
+ *\r
+ * This file is part of Project 16.\r
+ *\r
+ * Project 16 is free software; you can redistribute it and/or modify\r
+ * it under the terms of the GNU General Public License as published by\r
+ * the Free Software Foundation; either version 3 of the License, or\r
+ * (at your option) any later version.\r
+ *\r
+ * Project 16 is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+ * GNU General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU General Public License\r
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>, or\r
+ * write to the Free Software Foundation, Inc., 51 Franklin Street,\r
+ * Fifth Floor, Boston, MA 02110-1301 USA.\r
+ *\r
+ */\r
+/*\r
+       heap check\r
+*/\r
+\r
+#include "src/lib/16_hc.h"\r
+#include <malloc.h>\r
+\r
+//from ftp://213.85.246.177/pub/FreeBSD/ports/archivers/arj/work/arj-3.10.22/environ.c\r
+#if 0\r
+//#ifdef __WATCOMC__\r
+long HC_Newfarcoreleft()\r
+{\r
+       void __huge *hp;                static long rc=736L;    long s_rc;\r
+\r
+       s_rc=rc;        rc+=2L;\r
+       do\r
+               hp=halloc(rc-=2L, 1024);\r
+       while(hp==NULL&&rc>0L);\r
+       if(hp!=NULL)\r
+               hfree(hp);\r
+       if(rc<s_rc)\r
+               return(rc*1024L);\r
+       do\r
+       {\r
+               hp=halloc(rc+=16L, 1024);\r
+               if(hp!=NULL)\r
+                       hfree(hp);\r
+       } while(hp!=NULL);\r
+       return((rc-16L)*1024L);\r
+}\r
+#endif\r
+\r
+//from: https://stackoverflow.com/questions/14386856/c-check-available-ram\r
+void NPTR* HC_LargestFreeBlock(size_t* Size)\r
+{\r
+       size_t s0, s1;\r
+       void NPTR* p;\r
+\r
+       s0 = ~(size_t)0 ^ (~(size_t)0 >> 1);\r
+       while (s0 && (p = _nmalloc(s0)) == NULL)\r
+               s0 >>= 1;\r
+\r
+       if (p)\r
+               _nfree(p);\r
+\r
+       s1 = s0 >> 1;\r
+       while (s1)\r
+       {\r
+               if ((p = _nmalloc(s0 + s1)) != NULL)\r
+               {\r
+                       s0 += s1;\r
+                       _nfree(p);\r
+               }\r
+       s1 >>= 1;\r
+       }\r
+       while (s0 && (p = _nmalloc(s0)) == NULL)\r
+               s0 ^= s0 & -s0;\r
+\r
+       *Size = s0;\r
+       return p;\r
+}\r
+\r
+//from: https://stackoverflow.com/questions/14386856/c-check-available-ram\r
+size_t HC_coreleft(void)\r
+{\r
+       size_t total = 0;\r
+       void __near* pFirst = NULL;\r
+       void __near* pLast = NULL;\r
+       for(;;)\r
+       {\r
+               size_t largest;\r
+               void __near* p = (void __near *)HC_LargestFreeBlock(&largest);\r
+               if (largest < sizeof(void __near*))\r
+               {\r
+                       if (p != NULL)\r
+                       _nfree(p);\r
+                       break;\r
+               }\r
+               *(void __near* __near*)p = NULL;\r
+               total += largest;\r
+               if (pFirst == NULL)\r
+                       pFirst = p;\r
+\r
+               if (pLast != NULL)\r
+                       *(void __near* __near*)pLast = p;\r
+               pLast = p;\r
+       }\r
+\r
+       while (pFirst != NULL)\r
+       {\r
+               void __near* p = *(void __near* __near*)pFirst;\r
+               _nfree(pFirst);\r
+               pFirst = p;\r
+       }\r
+       return total;\r
+}\r
+\r
+//far version of above\r
+void __far* HC_LargestFarFreeBlock(dword* Size)\r
+{\r
+       dword s0, s1;\r
+       void __far* p;\r
+\r
+       s0 = ~(dword)0 ^ (~(dword)0 >> 1);\r
+       while (s0 && (p = _fmalloc(s0)) == NULL)\r
+               s0 >>= 1;\r
+\r
+       if (p)\r
+               _ffree(p);\r
+\r
+       s1 = s0 >> 1;\r
+       while (s1)\r
+       {\r
+               if ((p = _fmalloc(s0 + s1)) != NULL)\r
+               {\r
+                       s0 += s1;\r
+                       _ffree(p);\r
+               }\r
+       s1 >>= 1;\r
+       }\r
+       while (s0 && (p = _fmalloc(s0)) == NULL)\r
+               s0 ^= s0 & -s0;\r
+\r
+       *Size = s0;\r
+       return p;\r
+}\r
+\r
+//far version of above\r
+dword HC_farcoreleft(void)\r
+{\r
+       dword total = 0UL;\r
+       void __far* pFirst = NULL;\r
+       void __far* pLast = NULL;\r
+       for(;;)\r
+       {\r
+               dword largest;\r
+               void __far* p = HC_LargestFarFreeBlock(&largest);\r
+               if (largest < sizeof(void __far*))\r
+               {\r
+                       if (p != NULL)\r
+                       _ffree(p);\r
+                       break;\r
+               }\r
+               *(void __far* __far*)p = NULL;\r
+               total += largest;\r
+               if (pFirst == NULL)\r
+                       pFirst = p;\r
+\r
+               if (pLast != NULL)\r
+                       *(void __far* __far*)pLast = p;\r
+               pLast = p;\r
+       }\r
+\r
+       while (pFirst != NULL)\r
+       {\r
+               void __far* p = *(void __far* __far*)pFirst;\r
+               _ffree(pFirst);\r
+               pFirst = p;\r
+       }\r
+\r
+       //if(total>16) total+=16;       total &= 0xfffffff0UL;\r
+       return total;\r
+}\r
+\r
+//==#ifdef __WATCOMC__\r
+/*void huge* LargestHugeFreeBlock(size_t* Size)\r
+{\r
+       size_t s0, s1;\r
+       void huge* p;\r
+\r
+       s0 = ~(size_t)0 ^ (~(size_t)0 >> 1);\r
+       while (s0 && (p = halloc((dword)s0, 1)) == NULL)\r
+               s0 >>= 1;\r
+\r
+       if (p)\r
+               hfree(p);\r
+\r
+       s1 = s0 >> 1;\r
+       while (s1)\r
+       {\r
+               if ((p = halloc((dword)(s0 + s1), 1)) != NULL)\r
+               {\r
+                       s0 += s1;\r
+                       hfree(p);\r
+               }\r
+       s1 >>= 1;\r
+       }\r
+       while (s0 && (p = halloc((dword)s0, 1)) == NULL)\r
+               s0 ^= s0 & -s0;\r
+\r
+       *Size = s0;\r
+       return p;\r
+}\r
+\r
+size_t _hugecoreleft(void)\r
+{\r
+       size_t total = 0;\r
+       void huge* pFirst = NULL;\r
+       void huge* pLast = NULL;\r
+       for(;;)\r
+       {\r
+               size_t largest;\r
+               void huge* p = LargestHugeFreeBlock(&largest);\r
+               if (largest < sizeof(void huge*))\r
+               {\r
+                       if (p != NULL)\r
+                       hfree(p);\r
+                       break;\r
+               }\r
+               *(void huge* huge*)p = NULL;\r
+               total += largest;\r
+               if (pFirst == NULL)\r
+                       pFirst = p;\r
+\r
+               if (pLast != NULL)\r
+                       *(void huge* huge*)pLast = p;\r
+               pLast = p;\r
+       }\r
+\r
+       while (pFirst != NULL)\r
+       {\r
+               void huge* p = *(void huge* huge*)pFirst;\r
+               hfree(pFirst);\r
+               pFirst = p;\r
+       }\r
+       return total;\r
+}\r
+\r
+void __based(__self)* LargestBasedFreeBlock(size_t* Size)\r
+{\r
+       __segment segu;\r
+       size_t s0, s1;\r
+       void __based(__self)* p;\r
+\r
+       s0 = ~(size_t)0 ^ (~(size_t)0 >> 1);\r
+       while (s0 && (p = _bmalloc(segu, s0)) == NULL)\r
+               s0 >>= 1;\r
+\r
+       if (p)\r
+               _ffree(p);\r
+\r
+       s1 = s0 >> 1;\r
+       while (s1)\r
+       {\r
+               if ((p = _bmalloc(segu, s0 + s1)) != NULL)\r
+               {\r
+                       s0 += s1;\r
+                       _ffree(p);\r
+               }\r
+       s1 >>= 1;\r
+       }\r
+       while (s0 && (p = _bmalloc(segu, s0)) == NULL)\r
+               s0 ^= s0 & -s0;\r
+\r
+       *Size = s0;\r
+       return p;\r
+}\r
+\r
+size_t _basedcoreleft(void)\r
+{\r
+       __segment segu;\r
+       size_t total = 0;\r
+       void __based(segu)* pFirst = NULL;\r
+       void __based(segu)* pLast = NULL;\r
+       // allocate based heap\r
+       segu = _bHC_heapseg( 1024 );\r
+       if( segu == _NULLSEG ) {\r
+               printf( "Unable to allocate based heap\n" );\r
+               return 0;\r
+\r
+       }\r
+       else\r
+\r
+       for(;;)\r
+       {\r
+               size_t largest;\r
+               void __based(segu)* p = LargestBasedFreeBlock(&largest);\r
+               if (largest < sizeof(void __far*))\r
+               {\r
+                       if (p != NULL)\r
+                       _ffree(p);\r
+                       break;\r
+               }\r
+               *(void __far* __far*)p = NULL;\r
+               total += largest;\r
+               if (pFirst == NULL)\r
+                       pFirst = p;\r
+\r
+               if (pLast != NULL)\r
+                       *(void __far* __far*)pLast = p;\r
+               pLast = p;\r
+       }\r
+\r
+       while (pFirst != NULL)\r
+       {\r
+               void __far* p = *(void __far* __far*)pFirst;\r
+               _ffree(pFirst);\r
+               pFirst = p;\r
+       }\r
+       return total;\r
+}\r
+\r
+size_t HC_GetFreeSize(void)\r
+{\r
+       struct _heapinfo h_info;\r
+       int heap_status;\r
+       size_t h_free=0, h_total=0, h_used=0;\r
+\r
+       h_info._pentry = NULL;\r
+       for(;;) {\r
+               heap_status = _heapwalk( &h_info );\r
+               if( heap_status != _HEAPOK ) break;\r
+               if((h_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") h_free += h_info._size;\r
+               if((h_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") h_used += h_info._size;\r
+               h_total += h_info._size;\r
+       }\r
+       HCL_heapstat(heap_status);\r
+       return h_free;\r
+}\r
+*/\r
+\r
+void HCL_HeapWalking (struct _heapinfo *h_info, hc_use_t *hu, unsigned nearfarswitch)\r
+{\r
+       hu->h_free=0; hu->h_total=0; hu->h_used=0;\r
+\r
+       h_info->_pentry = NULL;\r
+       for(;;) {\r
+               if(nearfarswitch==0) hu->heap_status = _nheapwalk( h_info );\r
+               else if(nearfarswitch==1) hu->heap_status = _fheapwalk( h_info );\r
+               if( hu->heap_status != _HEAPOK ) break;\r
+               if((h_info->_useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") hu->h_free += h_info->_size;\r
+               if((h_info->_useflag == _USEDENTRY ? "USED" : "FREE")=="USED") hu->h_used += h_info->_size;\r
+               hu->h_total += h_info->_size;\r
+       }\r
+       HCL_heapstat(hu->heap_status);\r
+}\r
+\r
+dword HC_GetFarFreeSize(void)\r
+{\r
+       struct _heapinfo h_info;\r
+       hc_use_t hu;\r
+       HCL_HeapWalking (&h_info, &hu, 1);\r
+       return hu.h_free;\r
+#if 0\r
+       struct _heapinfo fh_info;\r
+       int heap_status;\r
+       dword fh_free=0, fh_total=0, fh_used=0;\r
+\r
+       fh_info._pentry = NULL;\r
+       for(;;) {\r
+               heap_status = _fheapwalk( &fh_info );\r
+               if( heap_status != _HEAPOK ) break;\r
+               if((fh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") fh_free += fh_info._size;\r
+               if((fh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") fh_used += fh_info._size;\r
+               fh_total += fh_info._size;\r
+       }\r
+       HCL_heapstat(heap_status);\r
+       return fh_free;\r
+#endif\r
+}\r
+\r
+size_t HC_GetNearFreeSize(void)\r
+{\r
+       struct _heapinfo h_info;\r
+       hc_use_t hu;\r
+       HCL_HeapWalking (&h_info, &hu, 0);\r
+       return hu.h_free;\r
+#if 0\r
+       struct _heapinfo nh_info;\r
+       int heap_status;\r
+       size_t nh_free=0, nh_total=0, nh_used=0;\r
+\r
+       nh_info._pentry = NULL;\r
+       for(;;) {\r
+               heap_status = _nheapwalk( &nh_info );\r
+               if( heap_status != _HEAPOK ) break;\r
+               if((nh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") nh_free += nh_info._size;\r
+               if((nh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") nh_used += nh_info._size;\r
+               nh_total += nh_info._size;\r
+       }\r
+       HCL_heapstat(heap_status);\r
+       return nh_free;\r
+#endif\r
+}\r
+\r
+void HC_heapdump(global_game_variables_t *gvar)\r
+{\r
+       struct _heapinfo fh_info, nh_info;//, h_info;\r
+       int heap_status;\r
+       size_t nh_free, fh_free, nh_total, fh_total, nh_used, fh_used;//,       h_free, h_total, h_used;\r
+       byte    scratch[1024],str[16];\r
+\r
+       HC_OpenDebug(gvar);\r
+\r
+#if 0\r
+       strcpy(scratch,"\n      == default ==\n\n");\r
+       write(gvar->handle.heaphandle,scratch,strlen(scratch));\r
+       h_info._pentry = NULL;\r
+       h_free=0; h_total=0; h_used=0;\r
+       for(;;) {\r
+               heap_status = _heapwalk( &h_info );\r
+               if( heap_status != _HEAPOK ) break;\r
+               strcpy(scratch,"  "); strcat(scratch,(h_info._useflag == _USEDENTRY ? "USED" : "FREE")); strcat(scratch," block at ");\r
+               sprintf(str, "%Fp", h_info._pentry); //ultoa((dword)h_info._pentry,str,16);\r
+                       strcat(scratch,str); strcat(scratch," of size "); ultoa(h_info._size,str,10); strcat(scratch,str); strcat(scratch,"\n");\r
+               if((h_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") h_free += h_info._size;\r
+               if((h_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") h_used += h_info._size;\r
+               h_total += h_info._size;\r
+               write(gvar->handle.heaphandle,scratch,strlen(scratch));\r
+       }\r
+       HCL_heapstatLogWrite(gvar, heap_status, scratch);\r
+#endif\r
+\r
+       //near\r
+       strcpy(scratch,"\n      == near ==\n\n");\r
+       write(gvar->handle.heaphandle,scratch,strlen(scratch));\r
+       nh_info._pentry = NULL;\r
+       nh_free=0; nh_total=0; nh_used=0;\r
+       for(;;) {\r
+               heap_status = _nheapwalk( &nh_info );\r
+               if( heap_status != _HEAPOK ) break;\r
+               strcpy(scratch,"  "); strcat(scratch,(nh_info._useflag == _USEDENTRY ? "USED" : "FREE")); strcat(scratch," block at ");\r
+               sprintf(str, "%Fp", nh_info._pentry); //ultoa((dword)nh_info._pentry,str,16);\r
+                       strcat(scratch,str); strcat(scratch," of size "); ultoa(nh_info._size,str,10); strcat(scratch,str); strcat(scratch,"\n");\r
+/*             printf( "  %s block at %Fp of size %4.4X\n",\r
+(nh_info._useflag == _USEDENTRY ? "USED" : "FREE"),\r
+nh_info._pentry, nh_info._size );*/\r
+               if((nh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") nh_free += nh_info._size;\r
+               if((nh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") nh_used += nh_info._size;\r
+               nh_total += nh_info._size;\r
+               write(gvar->handle.heaphandle,scratch,strlen(scratch));\r
+       }\r
+       HCL_heapstatLogWrite(gvar, heap_status, scratch);\r
+\r
+       //far\r
+       strcpy(scratch,"\n      == far ==\n\n");\r
+       write(gvar->handle.heaphandle,scratch,strlen(scratch));\r
+       fh_info._pentry = NULL;\r
+       fh_free=0; fh_total=0; fh_used=0;\r
+       for(;;) {\r
+               heap_status = _fheapwalk( &fh_info );\r
+               if( heap_status != _HEAPOK ) break;\r
+               strcpy(scratch,"  "); strcat(scratch,(fh_info._useflag == _USEDENTRY ? "USED" : "FREE")); strcat(scratch," block at ");\r
+               sprintf(str, "%Fp", fh_info._pentry); //ultoa((dword)fh_info._pentry,str,16);\r
+                       strcat(scratch,str); strcat(scratch," of size "); ultoa(fh_info._size,str,10); strcat(scratch,str); strcat(scratch,"\n");\r
+               /*printf( "  %s block at %Fp of size %4.4X\n",\r
+(fh_info._useflag == _USEDENTRY ? "USED" : "FREE"),\r
+fh_info._pentry, fh_info._size );*/\r
+               if((fh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") fh_free += fh_info._size;\r
+               if((fh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") fh_used += fh_info._size;\r
+               fh_total += fh_info._size;\r
+               write(gvar->handle.heaphandle,scratch,strlen(scratch));\r
+       }\r
+       HCL_heapstatLogWrite(gvar, heap_status, scratch);\r
+\r
+       strcpy(scratch,"\n");\r
+       strcat(scratch,kittengets(2,0,"Memory Type         Total      Used       Free\n"));\r
+       strcat(scratch,"----------------  --------   --------   --------\n");\r
+//--   printmeminfoline(&scratch, "Default", h_total, h_used, h_free);\r
+       printmeminfoline(scratch, "Near", nh_total, nh_used, nh_free);\r
+       printmeminfoline(scratch, "Far", fh_total, fh_used, fh_free);\r
+       strcat(scratch,"----------------  --------   --------   --------\n");\r
+#if defined(__LARGE__) || defined(__COMPACT__) || defined(__HUGE__)\r
+       strcat(scratch,"HC_coreleft = ");                       ultoa((dword)HC_coreleft(),str,10);                     strcat(scratch,str);    strcat(scratch,"\n");\r
+       strcat(scratch,"HC_farcoreleft = ");                    ultoa((dword)HC_farcoreleft(),str,10);          strcat(scratch,str);    strcat(scratch,"\n");\r
+#endif\r
+//--   strcat(scratch,"HC_Newfarcoreleft = ");         ultoa((dword)HC_Newfarcoreleft(),str,10);               strcat(scratch,str);    strcat(scratch,"\n");\r
+//--   strcat(scratch,"HC_GetFreeSize = ");            ultoa((dword)HC_GetFreeSize(),str,10);          strcat(scratch,str);    strcat(scratch,"\n");\r
+//00   strcat(scratch,"HC_GetNearFreeSize = ");        ultoa((dword)HC_GetNearFreeSize(),str,10);      strcat(scratch,str);    strcat(scratch,"\n");\r
+//00   strcat(scratch,"HC_GetFarFreeSize = ");         ultoa((dword)HC_GetFarFreeSize(),str,10);       strcat(scratch,str);    strcat(scratch,"\n");\r
+       strcat(scratch,"coreleft = ");                          ultoa((dword)coreleft(),str,10);                                strcat(scratch,str);    strcat(scratch,"\n");\r
+       strcat(scratch,"farcoreleft = ");                       ultoa((dword)farcoreleft(),str,10);                     strcat(scratch,str);    strcat(scratch,"\n");\r
+       strcat(scratch,"stackavail = ");                        ultoa((dword)stackavail(),str,10);                      strcat(scratch,str);    strcat(scratch,"\n");\r
+       write(gvar->handle.heaphandle,scratch,strlen(scratch));\r
+       HC_CloseDebug(gvar);\r
+}\r
+\r
+void HCL_heapstatLogWrite(global_game_variables_t *gvar, int heap_status, byte *str)\r
+{\r
+       switch( heap_status ) {\r
+               case _HEAPEND:\r
+                       strcpy((str),"OK - end of heap\n");\r
+               break;\r
+               case _HEAPEMPTY:\r
+                       strcpy((str),"OK - heap is empty\n");\r
+\r
+               break;\r
+               case _HEAPBADBEGIN:\r
+                       strcpy((str),"ERROR - heap is damaged\n");\r
+               break;\r
+#ifdef __WATCOMC__\r
+               case _HEAPBADPTR:\r
+                       strcpy((str),"ERROR - bad pointer to heap\n");\r
+               break;\r
+#endif\r
+               case _HEAPBADNODE:\r
+                       strcpy((str),"ERROR - bad node in heap\n");\r
+       }\r
+       write(gvar->handle.heaphandle,(str),strlen((str)));\r
+}\r
+\r
+void HCL_heapstat(int heap_status)\r
+{\r
+       switch( heap_status ) {\r
+               case _HEAPEND:\r
+                       //printf("OK - end of heap\n");\r
+               break;\r
+               case _HEAPEMPTY:\r
+                       //printf("OK - heap is empty\n");\r
+               break;\r
+               case _HEAPBADBEGIN:\r
+                       printf("ERROR - heap is damaged\n");\r
+               break;\r
+#ifdef __WATCOMC__\r
+               case _HEAPBADPTR:\r
+                       printf("ERROR - bad pointer to heap\n");\r
+               break;\r
+#endif\r
+               case _HEAPBADNODE:\r
+                       printf("ERROR - bad node in heap\n");\r
+       }\r
+}\r
+\r
+//++\r
+#ifdef __WATCOMC__\r
+dword farcoreleft()\r
+{\r
+//---- _fheapgrow();\r
+// #ifdef __BORLANDC__\r
+//     return 0x90000UL-16UL;\r
+// #endif\r
+\r
+#if !defined(__LARGE__) && !defined(__COMPACT__) && !defined(__HUGE__)\r
+//----\r
+       return 0x90000UL+16UL;\r
+//---- return 589824UL+16UL;\r
+#else\r
+//++++\r
+       return HC_farcoreleft();\r
+//stack overflows      return HC_GetFarFreeSize();\r
+#endif\r
+}\r
+\r
+dword coreleft()\r
+{\r
+       _nheapgrow();\r
+       return _memavl();\r
+//     return HC_GetNearFreeSize();\r
+}\r
+#endif\r
+\r
+/*\r
+============================\r
+=\r
+= HC_OpenDebug / HC_CloseDebug\r
+=\r
+= Opens a binary file with the handle "heaphandle"\r
+=\r
+============================\r
+*/\r
+void HC_OpenDebug(global_game_variables_t *gvar)\r
+{\r
+#ifdef __BORLANDC__\r
+       unlink("heap.16b");\r
+       gvar->handle.heaphandle = open(gvar->handle.heapdumpfilename, O_CREAT | O_WRONLY | O_TEXT);\r
+#endif\r
+#ifdef __WATCOMC__\r
+       unlink("heap.16w");\r
+       gvar->handle.heaphandle = open(gvar->handle.heapdumpfilename, O_CREAT | O_WRONLY | O_TEXT);\r
+#endif\r
+}\r
+\r
+void HC_CloseDebug(global_game_variables_t *gvar)\r
+{\r
+       close(gvar->handle.heaphandle);\r
+\r
+#ifdef __BORLANDC__\r
+       strcpy(gvar->handle.heapdumpfilename, "heap.16b");\r
+#endif\r
+#ifdef __WATCOMC__\r
+       strcpy(gvar->handle.heapdumpfilename, "heap.16w");\r
+#endif\r
+}\r