]> 4ch.mooo.com Git - 16.git/blobdiff - src/lib/16_hc.c
ok they behave strangly!! wwww
[16.git] / src / lib / 16_hc.c
old mode 100644 (file)
new mode 100755 (executable)
index a7de12f..20a0aac
 
 #include "src/lib/16_hc.h"
 
-int heaphandle;
-
+#ifdef __BORLANDC__
+void * LargestFreeBlock(size_t* Size)
+#endif
+#ifdef __WATCOMC__
 void __near* LargestFreeBlock(size_t* Size)
+#endif
 {
        size_t s0, s1;
+#ifdef __BORLANDC__
+       void * p;
+#endif
+#ifdef __WATCOMC__
        void __near* p;
+#endif
 
        s0 = ~(size_t)0 ^ (~(size_t)0 >> 1);
+#ifdef __BORLANDC__
+       while (s0 && (p = malloc(s0)) == NULL)
+#endif
+#ifdef __WATCOMC__
        while (s0 && (p = _nmalloc(s0)) == NULL)
+#endif
                s0 >>= 1;
 
        if (p)
+#ifdef __BORLANDC__
+               free(p);
+#endif
+#ifdef __WATCOMC__
                _nfree(p);
+#endif
 
        s1 = s0 >> 1;
        while (s1)
        {
+#ifdef __BORLANDC__
+               if ((p = malloc(s0 + s1)) != NULL)
+#endif
+#ifdef __WATCOMC__
                if ((p = _nmalloc(s0 + s1)) != NULL)
+#endif
                {
                        s0 += s1;
+#ifdef __BORLANDC__
+                       free(p);
+#endif
+#ifdef __WATCOMC__
                        _nfree(p);
+#endif
                }
        s1 >>= 1;
        }
+#ifdef __BORLANDC__
+       while (s0 && (p = malloc(s0)) == NULL)
+#endif
+#ifdef __WATCOMC__
        while (s0 && (p = _nmalloc(s0)) == NULL)
+#endif
                s0 ^= s0 & -s0;
 
        *Size = s0;
@@ -64,11 +97,16 @@ size_t _coreleft(void)
        for(;;)
        {
                size_t largest;
-               void __near* p = LargestFreeBlock(&largest);
+               void __near* p = (void __near *)LargestFreeBlock(&largest);
                if (largest < sizeof(void __near*))
                {
                        if (p != NULL)
+#ifdef __BORLANDC__
+                       free(p);
+#endif
+#ifdef __WATCOMC__
                        _nfree(p);
+#endif
                        break;
                }
                *(void __near* __near*)p = NULL;
@@ -84,7 +122,12 @@ size_t _coreleft(void)
        while (pFirst != NULL)
        {
                void __near* p = *(void __near* __near*)pFirst;
+#ifdef __BORLANDC__
+               free(pFirst);
+#endif
+#ifdef __WATCOMC__
                _nfree(pFirst);
+#endif
                pFirst = p;
        }
        return total;
@@ -153,6 +196,7 @@ size_t _farcoreleft(void)
        return total;
 }
 
+#ifdef __WATCOMC__
 void huge* LargestHugeFreeBlock(size_t* Size)
 {
        size_t s0, s1;
@@ -344,70 +388,67 @@ size_t GetNearFreeSize(void)
        return nh_free;
 }
 
-void heapdump(void)
+void heapdump(global_game_variables_t *gvar)
 {
        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();
+       HC_OpenDebug(gvar);
 
        strcpy(scratch,"\n      == default ==\n\n");
-       write(heaphandle,scratch,strlen(scratch));
+       write(gvar->handle.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");
+               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,10); 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));
+               write(gvar->handle.heaphandle,scratch,strlen(scratch));
        }
-       heapstat(heap_status, &scratch);
-       
+       heapstat(gvar, heap_status, &scratch);
+
        //near
        strcpy(scratch,"\n      == near ==\n\n");
-       write(heaphandle,scratch,strlen(scratch));
+       write(gvar->handle.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");
+               strcpy(scratch,"  "); strcat(scratch,(h_info._useflag == _USEDENTRY ? "USED" : "FREE")); strcat(scratch," block at "); ultoa((dword)nh_info._pentry,str,16); strcat(scratch,str); strcat(scratch," of size "); ultoa(nh_info._size,str,10); 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));
+               write(gvar->handle.heaphandle,scratch,strlen(scratch));
        }
-       heapstat(heap_status, &scratch);
+       heapstat(gvar, heap_status, &scratch);
 
        //far
        strcpy(scratch,"\n      == far ==\n\n");
-       write(heaphandle,scratch,strlen(scratch));
+       write(gvar->handle.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");
+               strcpy(scratch,"  "); strcat(scratch,(h_info._useflag == _USEDENTRY ? "USED" : "FREE")); strcat(scratch," block at "); ultoa((dword)fh_info._pentry,str,16); strcat(scratch,str); strcat(scratch," of size "); ultoa(fh_info._size,str,10); 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));
+               write(gvar->handle.heaphandle,scratch,strlen(scratch));
        }
-       heapstat(heap_status, &scratch);
+       heapstat(gvar, heap_status, &scratch);
 
        strcpy(scratch,"\n");
        strcat(scratch,kittengets(2,0,"Memory Type         Total      Used       Free\n"));
@@ -423,11 +464,11 @@ fh_info._pentry, fh_info._size );*/
        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();
+       write(gvar->handle.heaphandle,scratch,strlen(scratch));
+       HC_CloseDebug(gvar);
 }
 
-void heapstat(int heap_status, byte *str)
+void heapstat(global_game_variables_t *gvar, int heap_status, byte *str)
 {
        switch( heap_status ) {
                case _HEAPEND:
@@ -435,7 +476,7 @@ void heapstat(int heap_status, byte *str)
                break;
                case _HEAPEMPTY:
                        strcpy((str),"OK - heap is empty\n");
-                       
+
                break;
                case _HEAPBADBEGIN:
                        strcpy((str),"ERROR - heap is damaged\n");
@@ -446,7 +487,7 @@ void heapstat(int heap_status, byte *str)
                case _HEAPBADNODE:
                        strcpy((str),"ERROR - bad node in heap\n");
        }
-       write(heaphandle,(str),strlen((str)));
+       write(gvar->handle.heaphandle,(str),strlen((str)));
 }
 
 void heapstat0(int heap_status)
@@ -457,7 +498,6 @@ void heapstat0(int heap_status)
                break;
                case _HEAPEMPTY:
                        //printf("OK - heap is empty\n");
-                       
                break;
                case _HEAPBADBEGIN:
                        printf("ERROR - heap is damaged\n");
@@ -469,7 +509,7 @@ void heapstat0(int heap_status)
                        printf("ERROR - bad node in heap\n");
        }
 }
-
+#endif
 /*
 ============================
 =
@@ -479,13 +519,13 @@ void heapstat0(int heap_status)
 =
 ============================
 */
-void HC_OpenDebug()
+void HC_OpenDebug(global_game_variables_t *gvar)
 {
        unlink("heap.16");
-       heaphandle = open("heap.16", O_CREAT | O_WRONLY | O_TEXT);
+       gvar->handle.heaphandle = open("heap.16", O_CREAT | O_WRONLY | O_TEXT);
 }
 
-void HC_CloseDebug()
+void HC_CloseDebug(global_game_variables_t *gvar)
 {
-       close(heaphandle);
+       close(gvar->handle.heaphandle);
 }