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 * LargestFreeBlock(size_t* Size)
\r
33 void __near* LargestFreeBlock(size_t* Size)
\r
44 s0 = ~(size_t)0 ^ (~(size_t)0 >> 1);
\r
46 while (s0 && (p = malloc(s0)) == NULL)
\r
49 while (s0 && (p = _nmalloc(s0)) == NULL)
\r
65 if ((p = malloc(s0 + s1)) != NULL)
\r
68 if ((p = _nmalloc(s0 + s1)) != NULL)
\r
82 while (s0 && (p = malloc(s0)) == NULL)
\r
85 while (s0 && (p = _nmalloc(s0)) == NULL)
\r
93 size_t _coreleft(void)
\r
96 void __near* pFirst = NULL;
\r
97 void __near* pLast = NULL;
\r
101 void __near* p = (void __near *)LargestFreeBlock(&largest);
\r
102 if (largest < sizeof(void __near*))
\r
105 #ifdef __BORLANDC__
\r
113 *(void __near* __near*)p = NULL;
\r
115 if (pFirst == NULL)
\r
119 *(void __near* __near*)pLast = p;
\r
123 while (pFirst != NULL)
\r
125 void __near* p = *(void __near* __near*)pFirst;
\r
126 #ifdef __BORLANDC__
\r
137 void far* LargestFarFreeBlock(size_t* Size)
\r
142 s0 = ~(size_t)0 ^ (~(size_t)0 >> 1);
\r
143 while (s0 && (p = _fmalloc(s0)) == NULL)
\r
152 if ((p = _fmalloc(s0 + s1)) != NULL)
\r
159 while (s0 && (p = _fmalloc(s0)) == NULL)
\r
166 size_t _farcoreleft(void)
\r
169 void far* pFirst = NULL;
\r
170 void far* pLast = NULL;
\r
174 void far* p = LargestFarFreeBlock(&largest);
\r
175 if (largest < sizeof(void far*))
\r
181 *(void far* far*)p = NULL;
\r
183 if (pFirst == NULL)
\r
187 *(void far* far*)pLast = p;
\r
191 while (pFirst != NULL)
\r
193 void far* p = *(void far* far*)pFirst;
\r
201 void huge* LargestHugeFreeBlock(size_t* Size)
\r
206 s0 = ~(size_t)0 ^ (~(size_t)0 >> 1);
\r
207 while (s0 && (p = halloc((dword)s0, 1)) == NULL)
\r
216 if ((p = halloc((dword)(s0 + s1), 1)) != NULL)
\r
223 while (s0 && (p = halloc((dword)s0, 1)) == NULL)
\r
230 size_t _hugecoreleft(void)
\r
233 void huge* pFirst = NULL;
\r
234 void huge* pLast = NULL;
\r
238 void huge* p = LargestHugeFreeBlock(&largest);
\r
239 if (largest < sizeof(void huge*))
\r
245 *(void huge* huge*)p = NULL;
\r
247 if (pFirst == NULL)
\r
251 *(void huge* huge*)pLast = p;
\r
255 while (pFirst != NULL)
\r
257 void huge* p = *(void huge* huge*)pFirst;
\r
264 /*void __based(__self)* LargestBasedFreeBlock(size_t* Size)
\r
268 void __based(__self)* p;
\r
270 s0 = ~(size_t)0 ^ (~(size_t)0 >> 1);
\r
271 while (s0 && (p = _bmalloc(segu, s0)) == NULL)
\r
280 if ((p = _bmalloc(segu, s0 + s1)) != NULL)
\r
287 while (s0 && (p = _bmalloc(segu, s0)) == NULL)
\r
294 size_t _basedcoreleft(void)
\r
298 void __based(segu)* pFirst = NULL;
\r
299 void __based(segu)* pLast = NULL;
\r
300 // allocate based heap
\r
301 segu = _bheapseg( 1024 );
\r
302 if( segu == _NULLSEG ) {
\r
303 printf( "Unable to allocate based heap\n" );
\r
312 void __based(segu)* p = LargestBasedFreeBlock(&largest);
\r
313 if (largest < sizeof(void far*))
\r
319 *(void far* far*)p = NULL;
\r
321 if (pFirst == NULL)
\r
325 *(void far* far*)pLast = p;
\r
329 while (pFirst != NULL)
\r
331 void far* p = *(void far* far*)pFirst;
\r
338 size_t GetFreeSize(void)
\r
340 struct _heapinfo h_info;
\r
342 size_t h_free=0, h_total=0, h_used=0;
\r
344 h_info._pentry = NULL;
\r
346 heap_status = _heapwalk( &h_info );
\r
347 if( heap_status != _HEAPOK ) break;
\r
348 if((h_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") h_free += h_info._size;
\r
349 if((h_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") h_used += h_info._size;
\r
350 h_total += h_info._size;
\r
352 heapstat0(heap_status);
\r
356 size_t GetFarFreeSize(void)
\r
358 struct _heapinfo fh_info;
\r
360 size_t fh_free=0, fh_total=0, fh_used=0;
\r
362 fh_info._pentry = NULL;
\r
364 heap_status = _fheapwalk( &fh_info );
\r
365 if( heap_status != _HEAPOK ) break;
\r
366 if((fh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") fh_free += fh_info._size;
\r
367 if((fh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") fh_used += fh_info._size;
\r
368 fh_total += fh_info._size;
\r
370 heapstat0(heap_status);
\r
374 size_t GetNearFreeSize(void)
\r
376 struct _heapinfo nh_info;
\r
378 size_t nh_free=0, nh_total=0, nh_used=0;
\r
380 nh_info._pentry = NULL;
\r
382 heap_status = _nheapwalk( &nh_info );
\r
383 if( heap_status != _HEAPOK ) break;
\r
384 if((nh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") nh_free += nh_info._size;
\r
385 if((nh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") nh_used += nh_info._size;
\r
386 nh_total += nh_info._size;
\r
388 heapstat0(heap_status);
\r
392 void heapdump(global_game_variables_t *gvar)
\r
394 struct _heapinfo fh_info, nh_info, h_info;
\r
396 size_t h_free, nh_free, fh_free, h_total, nh_total, fh_total, h_used, nh_used, fh_used;
\r
397 byte scratch[1024],str[16];
\r
399 HC_OpenDebug(gvar);
\r
401 strcpy(scratch,"\n == default ==\n\n");
\r
402 write(gvar->handle.heaphandle,scratch,strlen(scratch));
\r
403 h_info._pentry = NULL;
\r
404 h_free=0; h_total=0; h_used=0;
\r
406 heap_status = _heapwalk( &h_info );
\r
407 if( heap_status != _HEAPOK ) break;
\r
408 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
409 if((h_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") h_free += h_info._size;
\r
410 if((h_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") h_used += h_info._size;
\r
411 h_total += h_info._size;
\r
412 write(gvar->handle.heaphandle,scratch,strlen(scratch));
\r
414 heapstat(gvar, heap_status, &scratch);
\r
417 strcpy(scratch,"\n == near ==\n\n");
\r
418 write(gvar->handle.heaphandle,scratch,strlen(scratch));
\r
419 nh_info._pentry = NULL;
\r
420 nh_free=0; nh_total=0; nh_used=0;
\r
422 heap_status = _nheapwalk( &nh_info );
\r
423 if( heap_status != _HEAPOK ) break;
\r
424 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
425 /* printf( " %s block at %Fp of size %4.4X\n",
\r
426 (nh_info._useflag == _USEDENTRY ? "USED" : "FREE"),
\r
427 nh_info._pentry, nh_info._size );*/
\r
428 if((nh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") nh_free += nh_info._size;
\r
429 if((nh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") nh_used += nh_info._size;
\r
430 nh_total += nh_info._size;
\r
431 write(gvar->handle.heaphandle,scratch,strlen(scratch));
\r
433 heapstat(gvar, heap_status, &scratch);
\r
436 strcpy(scratch,"\n == far ==\n\n");
\r
437 write(gvar->handle.heaphandle,scratch,strlen(scratch));
\r
438 fh_info._pentry = NULL;
\r
439 fh_free=0; fh_total=0; fh_used=0;
\r
441 heap_status = _fheapwalk( &fh_info );
\r
442 if( heap_status != _HEAPOK ) break;
\r
443 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
444 /*printf( " %s block at %Fp of size %4.4X\n",
\r
445 (fh_info._useflag == _USEDENTRY ? "USED" : "FREE"),
\r
446 fh_info._pentry, fh_info._size );*/
\r
447 if((fh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") fh_free += fh_info._size;
\r
448 if((fh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") fh_used += fh_info._size;
\r
449 fh_total += fh_info._size;
\r
450 write(gvar->handle.heaphandle,scratch,strlen(scratch));
\r
452 heapstat(gvar, heap_status, &scratch);
\r
454 strcpy(scratch,"\n");
\r
455 strcat(scratch,kittengets(2,0,"Memory Type Total Used Free\n"));
\r
456 strcat(scratch,"---------------- -------- -------- --------\n");
\r
457 printmeminfoline(&scratch, "Default", h_total, h_used, h_free);
\r
458 printmeminfoline(&scratch, "Near", nh_total, nh_used, nh_free);
\r
459 printmeminfoline(&scratch, "Far", fh_total, fh_used, fh_free);
\r
460 strcat(scratch,"---------------- -------- -------- --------\n");
\r
461 strcat(scratch,"coreleft = "); ultoa((dword)_coreleft(),str,10); strcat(scratch,str); strcat(scratch,"\n");
\r
462 strcat(scratch,"farcoreleft = "); ultoa((dword)_farcoreleft(),str,10); strcat(scratch,str); strcat(scratch,"\n");
\r
463 strcat(scratch,"GetFreeSize = "); ultoa((dword)GetFreeSize(),str,10); strcat(scratch,str); strcat(scratch,"\n");
\r
464 strcat(scratch,"GetNearFreeSize = "); ultoa((dword)GetNearFreeSize(),str,10); strcat(scratch,str); strcat(scratch,"\n");
\r
465 strcat(scratch,"GetFarFreeSize = "); ultoa((dword)GetFarFreeSize(),str,10); strcat(scratch,str); strcat(scratch,"\n");
\r
466 strcat(scratch,"memavl = "); ultoa((dword)_memavl(),str,10); strcat(scratch,str); strcat(scratch,"\n");
\r
467 strcat(scratch,"stackavail = "); ultoa((dword)stackavail(),str,10); strcat(scratch,str); strcat(scratch,"\n");
\r
468 write(gvar->handle.heaphandle,scratch,strlen(scratch));
\r
469 HC_CloseDebug(gvar);
\r
472 void heapstat(global_game_variables_t *gvar, int heap_status, byte *str)
\r
474 switch( heap_status ) {
\r
476 strcpy((str),"OK - end of heap\n");
\r
479 strcpy((str),"OK - heap is empty\n");
\r
482 case _HEAPBADBEGIN:
\r
483 strcpy((str),"ERROR - heap is damaged\n");
\r
486 strcpy((str),"ERROR - bad pointer to heap\n");
\r
489 strcpy((str),"ERROR - bad node in heap\n");
\r
491 write(gvar->handle.heaphandle,(str),strlen((str)));
\r
494 void heapstat0(int heap_status)
\r
496 switch( heap_status ) {
\r
498 //printf("OK - end of heap\n");
\r
501 //printf("OK - heap is empty\n");
\r
503 case _HEAPBADBEGIN:
\r
504 printf("ERROR - heap is damaged\n");
\r
507 printf("ERROR - bad pointer to heap\n");
\r
510 printf("ERROR - bad node in heap\n");
\r
515 ============================
\r
517 = HC_OpenDebug / HC_CloseDebug
\r
519 = Opens a binary file with the handle "heaphandle"
\r
521 ============================
\r
523 void HC_OpenDebug(global_game_variables_t *gvar)
\r
525 #ifdef __BORLANDC__
\r
526 unlink("heap.16b");
\r
527 gvar->handle.heaphandle = open("heap.16b", O_CREAT | O_WRONLY | O_TEXT);
\r
530 unlink("heap.16w");
\r
531 gvar->handle.heaphandle = open("heap.16w", O_CREAT | O_WRONLY | O_TEXT);
\r
535 void HC_CloseDebug(global_game_variables_t *gvar)
\r
537 close(gvar->handle.heaphandle);
\r