OSDN Git Service

i feel horrendous! i should stop! i fixed up showmemorydetail function ^^. and initia...
[proj16/16.git] / src / lib / 16_hc.c
index 1d6bd4c..f7f5d52 100755 (executable)
@@ -70,48 +70,23 @@ HC_LargestFreeBlock(size_t* Size)
 #endif\r
 \r
        s0 = ~(size_t)0 ^ (~(size_t)0 >> 1);\r
-#ifdef __BORLANDC__\r
-       while (s0 && (p = malloc(s0)) == NULL)\r
-#endif\r
-#ifdef __WATCOMC__\r
        while (s0 && (p = _nmalloc(s0)) == NULL)\r
-#endif\r
                s0 >>= 1;\r
 \r
        if (p)\r
-#ifdef __BORLANDC__\r
-               free(p);\r
-#endif\r
-#ifdef __WATCOMC__\r
                _nfree(p);\r
-#endif\r
 \r
        s1 = s0 >> 1;\r
        while (s1)\r
        {\r
-#ifdef __BORLANDC__\r
-               if ((p = malloc(s0 + s1)) != NULL)\r
-#endif\r
-#ifdef __WATCOMC__\r
                if ((p = _nmalloc(s0 + s1)) != NULL)\r
-#endif\r
                {\r
                        s0 += s1;\r
-#ifdef __BORLANDC__\r
-                       free(p);\r
-#endif\r
-#ifdef __WATCOMC__\r
                        _nfree(p);\r
-#endif\r
                }\r
        s1 >>= 1;\r
        }\r
-#ifdef __BORLANDC__\r
-       while (s0 && (p = malloc(s0)) == NULL)\r
-#endif\r
-#ifdef __WATCOMC__\r
        while (s0 && (p = _nmalloc(s0)) == NULL)\r
-#endif\r
                s0 ^= s0 & -s0;\r
 \r
        *Size = s0;\r
@@ -131,12 +106,7 @@ size_t HC_coreleft(void)
                if (largest < sizeof(void __near*))\r
                {\r
                        if (p != NULL)\r
-#ifdef __BORLANDC__\r
-                       free(p);\r
-#endif\r
-#ifdef __WATCOMC__\r
                        _nfree(p);\r
-#endif\r
                        break;\r
                }\r
                *(void __near* __near*)p = NULL;\r
@@ -152,12 +122,7 @@ size_t HC_coreleft(void)
        while (pFirst != NULL)\r
        {\r
                void __near* p = *(void __near* __near*)pFirst;\r
-#ifdef __BORLANDC__\r
-               free(pFirst);\r
-#endif\r
-#ifdef __WATCOMC__\r
                _nfree(pFirst);\r
-#endif\r
                pFirst = p;\r
        }\r
        return total;\r
@@ -228,7 +193,7 @@ unsigned long HC_farcoreleft(void)
        return total;\r
 }\r
 \r
-#ifdef __WATCOMC__\r
+//==#ifdef __WATCOMC__\r
 /*void huge* LargestHugeFreeBlock(size_t* Size)\r
 {\r
        size_t s0, s1;\r
@@ -364,8 +329,8 @@ size_t _basedcoreleft(void)
                pFirst = p;\r
        }\r
        return total;\r
-}*/\r
-#if 0\r
+}\r
+\r
 size_t HC_GetFreeSize(void)\r
 {\r
        struct _heapinfo h_info;\r
@@ -380,13 +345,34 @@ size_t HC_GetFreeSize(void)
                if((h_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") h_used += h_info._size;\r
                h_total += h_info._size;\r
        }\r
-       HCL_heapstat0(heap_status);\r
+       HCL_heapstat(heap_status);\r
        return h_free;\r
 }\r
-#endif\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
 unsigned long 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
        unsigned long fh_free=0, fh_total=0, fh_used=0;\r
@@ -399,12 +385,18 @@ unsigned long HC_GetFarFreeSize(void)
                if((fh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") fh_used += fh_info._size;\r
                fh_total += fh_info._size;\r
        }\r
-       HCL_heapstat0(heap_status);\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
@@ -417,8 +409,9 @@ size_t HC_GetNearFreeSize(void)
                if((nh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") nh_used += nh_info._size;\r
                nh_total += nh_info._size;\r
        }\r
-       HCL_heapstat0(heap_status);\r
+       HCL_heapstat(heap_status);\r
        return nh_free;\r
+#endif\r
 }\r
 \r
 void HC_heapdump(global_game_variables_t *gvar)\r
@@ -446,7 +439,7 @@ void HC_heapdump(global_game_variables_t *gvar)
                h_total += h_info._size;\r
                write(gvar->handle.heaphandle,scratch,strlen(scratch));\r
        }\r
-       HCL_heapstat(gvar, heap_status, &scratch);\r
+       HCL_heapstatLogWrite(gvar, heap_status, scratch);\r
 #endif\r
 \r
        //near\r
@@ -468,7 +461,7 @@ nh_info._pentry, nh_info._size );*/
                nh_total += nh_info._size;\r
                write(gvar->handle.heaphandle,scratch,strlen(scratch));\r
        }\r
-       HCL_heapstat(gvar, heap_status, &scratch);\r
+       HCL_heapstatLogWrite(gvar, heap_status, scratch);\r
 \r
        //far\r
        strcpy(scratch,"\n      == far ==\n\n");\r
@@ -489,14 +482,16 @@ fh_info._pentry, fh_info._size );*/
                fh_total += fh_info._size;\r
                write(gvar->handle.heaphandle,scratch,strlen(scratch));\r
        }\r
-       HCL_heapstat(gvar, heap_status, &scratch);\r
+       HCL_heapstatLogWrite(gvar, heap_status, scratch);\r
 \r
        strcpy(scratch,"\n");\r
+#ifdef __WATCOMC__\r
        strcat(scratch,kittengets(2,0,"Memory Type         Total      Used       Free\n"));\r
+#endif\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
+       printmeminfoline(scratch, "Near", nh_total, nh_used, nh_free);\r
+       printmeminfoline(scratch, "Far", fh_total, fh_used, fh_free);\r
        strcat(scratch,"----------------  --------   --------   --------\n");\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
@@ -511,7 +506,7 @@ fh_info._pentry, fh_info._size );*/
        HC_CloseDebug(gvar);\r
 }\r
 \r
-void HCL_heapstat(global_game_variables_t *gvar, int heap_status, byte *str)\r
+void HCL_heapstatLogWrite(global_game_variables_t *gvar, int heap_status, byte *str)\r
 {\r
        switch( heap_status ) {\r
                case _HEAPEND:\r
@@ -524,16 +519,18 @@ void HCL_heapstat(global_game_variables_t *gvar, int heap_status, byte *str)
                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_heapstat0(int heap_status)\r
+void HCL_heapstat(int heap_status)\r
 {\r
        switch( heap_status ) {\r
                case _HEAPEND:\r
@@ -545,14 +542,18 @@ void HCL_heapstat0(int heap_status)
                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
 unsigned long farcoreleft()\r
 {\r
 //---- _fheapgrow();\r