1 /* Project 16 Source Code~
\r
2 * Copyright (C) 2012-2017 sparky4 & pngwen & andrius4669 & joncampbell123 & yakui-lover
\r
4 * This file is part of Project 16.
\r
6 * Project 16 is free software; you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation; either version 3 of the License, or
\r
9 * (at your option) any later version.
\r
11 * Project 16 is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with this program. If not, see <http://www.gnu.org/licenses/>, or
\r
18 * write to the Free Software Foundation, Inc., 51 Franklin Street,
\r
19 * Fifth Floor, Boston, MA 02110-1301 USA.
\r
26 #include "src/lib/16_hc.h"
\r
30 void CHIKAKU* HC_LargestFreeBlock(size_t* Size)
\r
35 s0 = ~(size_t)0 ^ (~(size_t)0 >> 1);
\r
36 while (s0 && (p = _nmalloc(s0)) == NULL)
\r
45 if ((p = _nmalloc(s0 + s1)) != NULL)
\r
52 while (s0 && (p = _nmalloc(s0)) == NULL)
\r
59 size_t HC_coreleft(void)
\r
62 void CHIKAKU* pFirst = NULL;
\r
63 void CHIKAKU* pLast = NULL;
\r
67 void CHIKAKU* p = (void CHIKAKU *)HC_LargestFreeBlock(&largest);
\r
68 if (largest < sizeof(void CHIKAKU*))
\r
75 *(void CHIKAKU* CHIKAKU*)p = NULL;
\r
81 *(void CHIKAKU* CHIKAKU*)pLast = p;
\r
85 while (pFirst != NULL)
\r
87 void CHIKAKU* p = *(void CHIKAKU* CHIKAKU*)pFirst;
\r
95 void far* HC_LargestFarFreeBlock(size_t* Size)
\r
100 s0 = ~(size_t)0 ^ (~(size_t)0 >> 1);
\r
101 while (s0 && (p = _fmalloc(s0)) == NULL)
\r
110 if ((p = _fmalloc(s0 + s1)) != NULL)
\r
117 while (s0 && (p = _fmalloc(s0)) == NULL)
\r
124 size_t HC_farcoreleft(void)
\r
127 void far* pFirst = NULL;
\r
128 void far* pLast = NULL;
\r
132 void far* p = HC_LargestFarFreeBlock(&largest);
\r
133 if (largest < sizeof(void far*))
\r
139 *(void far* far*)p = NULL;
\r
141 if (pFirst == NULL)
\r
145 *(void far* far*)pLast = p;
\r
149 while (pFirst != NULL)
\r
151 void far* p = *(void far* far*)pFirst;
\r
160 void huge* HC_LargestHugeFreeBlock(size_t* Size)
\r
165 s0 = ~(size_t)0 ^ (~(size_t)0 >> 1);
\r
166 while (s0 && (p = halloc((dword)s0, 1)) == NULL)
\r
175 if ((p = halloc((dword)(s0 + s1), 1)) != NULL)
\r
182 while (s0 && (p = halloc((dword)s0, 1)) == NULL)
\r
189 size_t HC_hugecoreleft(void)
\r
192 void huge* pFirst = NULL;
\r
193 void huge* pLast = NULL;
\r
197 void huge* p = HC_LargestHugeFreeBlock(&largest);
\r
198 if (largest < sizeof(void huge*))
\r
204 *(void huge* huge*)p = NULL;
\r
206 if (pFirst == NULL)
\r
210 *(void huge* huge*)pLast = p;
\r
214 while (pFirst != NULL)
\r
216 void huge* p = *(void huge* huge*)pFirst;
\r
223 void __based(__self)* LargestBasedFreeBlock(size_t* Size)
\r
227 void __based(__self)* p;
\r
229 s0 = ~(size_t)0 ^ (~(size_t)0 >> 1);
\r
230 while (s0 && (p = _bmalloc(segu, s0)) == NULL)
\r
239 if ((p = _bmalloc(segu, s0 + s1)) != NULL)
\r
246 while (s0 && (p = _bmalloc(segu, s0)) == NULL)
\r
253 size_t _basedcoreleft(void)
\r
257 void __based(segu)* pFirst = NULL;
\r
258 void __based(segu)* pLast = NULL;
\r
259 // allocate based heap
\r
260 segu = _bheapseg( 1024 );
\r
261 if( segu == _NULLSEG ) {
\r
262 printf( "Unable to allocate based heap\n" );
\r
271 void __based(segu)* p = LargestBasedFreeBlock(&largest);
\r
272 if (largest < sizeof(void far*))
\r
278 *(void far* far*)p = NULL;
\r
280 if (pFirst == NULL)
\r
284 *(void far* far*)pLast = p;
\r
288 while (pFirst != NULL)
\r
290 void far* p = *(void far* far*)pFirst;
\r
297 size_t HC_GetFreeSize(void)
\r
299 struct _heapinfo h_info;
\r
301 size_t h_free=0, h_total=0, h_used=0;
\r
303 h_info._pentry = NULL;
\r
305 heap_status = _heapwalk( &h_info );
\r
306 if( heap_status != _HEAPOK ) break;
\r
307 if((h_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") h_free += h_info._size;
\r
308 if((h_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") h_used += h_info._size;
\r
309 h_total += h_info._size;
\r
311 HC_heapstat0(heap_status);
\r
315 size_t HC_GetFarFreeSize(void)
\r
317 struct _heapinfo fh_info;
\r
319 size_t fh_free=0, fh_total=0, fh_used=0;
\r
321 fh_info._pentry = NULL;
\r
323 heap_status = _fheapwalk( &fh_info );
\r
324 if( heap_status != _HEAPOK ) break;
\r
325 if((fh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") fh_free += fh_info._size;
\r
326 if((fh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") fh_used += fh_info._size;
\r
327 fh_total += fh_info._size;
\r
329 HC_heapstat0(heap_status);
\r
333 size_t HC_GetNearFreeSize(void)
\r
335 struct _heapinfo nh_info;
\r
337 size_t nh_free=0, nh_total=0, nh_used=0;
\r
339 nh_info._pentry = NULL;
\r
341 heap_status = _nheapwalk( &nh_info );
\r
342 if( heap_status != _HEAPOK ) break;
\r
343 if((nh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") nh_free += nh_info._size;
\r
344 if((nh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") nh_used += nh_info._size;
\r
345 nh_total += nh_info._size;
\r
347 HC_heapstat0(heap_status);
\r
351 void HC_heapdump(global_game_variables_t *gvar)
\r
353 struct _heapinfo fh_info, nh_info, h_info;
\r
355 size_t h_free, nh_free, fh_free, h_total, nh_total, fh_total, h_used, nh_used, fh_used;
\r
356 byte scratch[1024],str[16];
\r
358 HC_OpenDebug(gvar);
\r
360 strcpy(scratch,"\n == default ==\n\n");
\r
361 write(gvar->handle.heaphandle,scratch,strlen(scratch));
\r
362 h_info._pentry = NULL;
\r
363 h_free=0; h_total=0; h_used=0;
\r
365 heap_status = _heapwalk( &h_info );
\r
366 if( heap_status != _HEAPOK ) break;
\r
367 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");
\r
368 if((h_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") h_free += h_info._size;
\r
369 if((h_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") h_used += h_info._size;
\r
370 h_total += h_info._size;
\r
371 write(gvar->handle.heaphandle,scratch,strlen(scratch));
\r
373 HC_heapstat(gvar, heap_status, &scratch);
\r
376 strcpy(scratch,"\n == near ==\n\n");
\r
377 write(gvar->handle.heaphandle,scratch,strlen(scratch));
\r
378 nh_info._pentry = NULL;
\r
379 nh_free=0; nh_total=0; nh_used=0;
\r
381 heap_status = _nheapwalk( &nh_info );
\r
382 if( heap_status != _HEAPOK ) break;
\r
383 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");
\r
384 /* printf( " %s block at %Fp of size %4.4X\n",
\r
385 (nh_info._useflag == _USEDENTRY ? "USED" : "FREE"),
\r
386 nh_info._pentry, nh_info._size );*/
\r
387 if((nh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") nh_free += nh_info._size;
\r
388 if((nh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") nh_used += nh_info._size;
\r
389 nh_total += nh_info._size;
\r
390 write(gvar->handle.heaphandle,scratch,strlen(scratch));
\r
392 HC_heapstat(gvar, heap_status, &scratch);
\r
395 strcpy(scratch,"\n == far ==\n\n");
\r
396 write(gvar->handle.heaphandle,scratch,strlen(scratch));
\r
397 fh_info._pentry = NULL;
\r
398 fh_free=0; fh_total=0; fh_used=0;
\r
400 heap_status = _fheapwalk( &fh_info );
\r
401 if( heap_status != _HEAPOK ) break;
\r
402 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");
\r
403 /*printf( " %s block at %Fp of size %4.4X\n",
\r
404 (fh_info._useflag == _USEDENTRY ? "USED" : "FREE"),
\r
405 fh_info._pentry, fh_info._size );*/
\r
406 if((fh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") fh_free += fh_info._size;
\r
407 if((fh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") fh_used += fh_info._size;
\r
408 fh_total += fh_info._size;
\r
409 write(gvar->handle.heaphandle,scratch,strlen(scratch));
\r
411 HC_heapstat(gvar, heap_status, &scratch);
\r
413 strcpy(scratch,"\n");
\r
414 strcat(scratch,kittengets(2,0,"Memory Type Total Used Free\n"));
\r
415 strcat(scratch,"---------------- -------- -------- --------\n");
\r
416 printmeminfoline(&scratch, "Default", h_total, h_used, h_free);
\r
417 printmeminfoline(&scratch, "Near", nh_total, nh_used, nh_free);
\r
418 printmeminfoline(&scratch, "Far", fh_total, fh_used, fh_free);
\r
419 strcat(scratch,"---------------- -------- -------- --------\n");
\r
420 strcat(scratch,"coreleft = "); ultoa((dword)HC_coreleft(),str,10); strcat(scratch,str); strcat(scratch,"\n");
\r
421 strcat(scratch,"farcoreleft = "); ultoa((dword)HC_farcoreleft(),str,10); strcat(scratch,str); strcat(scratch,"\n");
\r
422 strcat(scratch,"GetFreeSize = "); ultoa((dword)HC_GetFreeSize(),str,10); strcat(scratch,str); strcat(scratch,"\n");
\r
423 strcat(scratch,"GetNearFreeSize = "); ultoa((dword)HC_GetNearFreeSize(),str,10); strcat(scratch,str); strcat(scratch,"\n");
\r
424 strcat(scratch,"GetFarFreeSize = "); ultoa((dword)HC_GetFarFreeSize(),str,10); strcat(scratch,str); strcat(scratch,"\n");
\r
425 strcat(scratch,"memavl = "); ultoa((dword)_memavl(),str,10); strcat(scratch,str); strcat(scratch,"\n");
\r
426 strcat(scratch,"stackavail = "); ultoa((dword)stackavail(),str,10); strcat(scratch,str); strcat(scratch,"\n");
\r
427 write(gvar->handle.heaphandle,scratch,strlen(scratch));
\r
428 HC_CloseDebug(gvar);
\r
431 void HC_heapstat(global_game_variables_t *gvar, int heap_status, byte *str)
\r
433 switch( heap_status ) {
\r
435 strcpy((str),"OK - end of heap\n");
\r
438 strcpy((str),"OK - heap is empty\n");
\r
441 case _HEAPBADBEGIN:
\r
442 strcpy((str),"ERROR - heap is damaged\n");
\r
445 strcpy((str),"ERROR - bad pointer to heap\n");
\r
448 strcpy((str),"ERROR - bad node in heap\n");
\r
450 write(gvar->handle.heaphandle,(str),strlen((str)));
\r
453 void HC_heapstat0(int heap_status)
\r
455 switch( heap_status ) {
\r
457 //printf("OK - end of heap\n");
\r
460 //printf("OK - heap is empty\n");
\r
462 case _HEAPBADBEGIN:
\r
463 printf("ERROR - heap is damaged\n");
\r
466 printf("ERROR - bad pointer to heap\n");
\r
469 printf("ERROR - bad node in heap\n");
\r
474 ============================
\r
476 = HC_OpenDebug / HC_CloseDebug
\r
478 = Opens a binary file with the handle "heaphandle"
\r
480 ============================
\r
482 void HC_OpenDebug(global_game_variables_t *gvar)
\r
484 #ifdef __BORLANDC__
\r
485 unlink("heap.16b");
\r
486 gvar->handle.heaphandle = open("heap.16b", O_CREAT | O_WRONLY | O_TEXT);
\r
489 unlink("heap.16w");
\r
490 gvar->handle.heaphandle = open("heap.16w", O_CREAT | O_WRONLY | O_TEXT);
\r
494 void HC_CloseDebug(global_game_variables_t *gvar)
\r
496 close(gvar->handle.heaphandle);
\r