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
29 //from ftp://213.85.246.177/pub/FreeBSD/ports/archivers/arj/work/arj-3.10.22/environ.c
\r
30 //#ifdef __WATCOMC__
\r
31 #define FARCORELEFTPTR __huge
\r
32 long HC_farcoreleft()
\r
34 void FARCORELEFTPTR *hp;
\r
35 static long rc=736L;
\r
41 hp=halloc(rc-=2L, 1024);
\r
42 while(hp==NULL&&rc>0L);
\r
49 hp=halloc(rc+=16L, 1024);
\r
53 return((rc-16L)*1024L);
\r
64 HC_LargestFreeBlock(size_t* Size)
\r
74 s0 = ~(size_t)0 ^ (~(size_t)0 >> 1);
\r
76 while (s0 && (p = malloc(s0)) == NULL)
\r
79 while (s0 && (p = _nmalloc(s0)) == NULL)
\r
95 if ((p = malloc(s0 + s1)) != NULL)
\r
98 if ((p = _nmalloc(s0 + s1)) != NULL)
\r
102 #ifdef __BORLANDC__
\r
111 #ifdef __BORLANDC__
\r
112 while (s0 && (p = malloc(s0)) == NULL)
\r
115 while (s0 && (p = _nmalloc(s0)) == NULL)
\r
123 size_t HC_coreleft(void)
\r
126 void __near* pFirst = NULL;
\r
127 void __near* pLast = NULL;
\r
131 void __near* p = (void __near *)HC_LargestFreeBlock(&largest);
\r
132 if (largest < sizeof(void __near*))
\r
135 #ifdef __BORLANDC__
\r
143 *(void __near* __near*)p = NULL;
\r
145 if (pFirst == NULL)
\r
149 *(void __near* __near*)pLast = p;
\r
153 while (pFirst != NULL)
\r
155 void __near* p = *(void __near* __near*)pFirst;
\r
156 #ifdef __BORLANDC__
\r
167 void far* HC_LargestFarFreeBlock(size_t* Size)
\r
172 s0 = ~(size_t)0 ^ (~(size_t)0 >> 1);
\r
173 while (s0 && (p = _fmalloc(s0)) == NULL)
\r
182 if ((p = _fmalloc(s0 + s1)) != NULL)
\r
189 while (s0 && (p = _fmalloc(s0)) == NULL)
\r
196 size_t HC_farcoreleft_(void)
\r
199 void far* pFirst = NULL;
\r
200 void far* pLast = NULL;
\r
204 void far* p = HC_LargestFarFreeBlock(&largest);
\r
205 if (largest < sizeof(void far*))
\r
211 *(void far* far*)p = NULL;
\r
213 if (pFirst == NULL)
\r
217 *(void far* far*)pLast = p;
\r
221 while (pFirst != NULL)
\r
223 void far* p = *(void far* far*)pFirst;
\r
231 /*void huge* LargestHugeFreeBlock(size_t* Size)
\r
236 s0 = ~(size_t)0 ^ (~(size_t)0 >> 1);
\r
237 while (s0 && (p = halloc((dword)s0, 1)) == NULL)
\r
246 if ((p = halloc((dword)(s0 + s1), 1)) != NULL)
\r
253 while (s0 && (p = halloc((dword)s0, 1)) == NULL)
\r
260 size_t _hugecoreleft(void)
\r
263 void huge* pFirst = NULL;
\r
264 void huge* pLast = NULL;
\r
268 void huge* p = LargestHugeFreeBlock(&largest);
\r
269 if (largest < sizeof(void huge*))
\r
275 *(void huge* huge*)p = NULL;
\r
277 if (pFirst == NULL)
\r
281 *(void huge* huge*)pLast = p;
\r
285 while (pFirst != NULL)
\r
287 void huge* p = *(void huge* huge*)pFirst;
\r
294 void __based(__self)* LargestBasedFreeBlock(size_t* Size)
\r
298 void __based(__self)* p;
\r
300 s0 = ~(size_t)0 ^ (~(size_t)0 >> 1);
\r
301 while (s0 && (p = _bmalloc(segu, s0)) == NULL)
\r
310 if ((p = _bmalloc(segu, s0 + s1)) != NULL)
\r
317 while (s0 && (p = _bmalloc(segu, s0)) == NULL)
\r
324 size_t _basedcoreleft(void)
\r
328 void __based(segu)* pFirst = NULL;
\r
329 void __based(segu)* pLast = NULL;
\r
330 // allocate based heap
\r
331 segu = _bHC_heapseg( 1024 );
\r
332 if( segu == _NULLSEG ) {
\r
333 printf( "Unable to allocate based heap\n" );
\r
342 void __based(segu)* p = LargestBasedFreeBlock(&largest);
\r
343 if (largest < sizeof(void far*))
\r
349 *(void far* far*)p = NULL;
\r
351 if (pFirst == NULL)
\r
355 *(void far* far*)pLast = p;
\r
359 while (pFirst != NULL)
\r
361 void far* p = *(void far* far*)pFirst;
\r
368 size_t HC_GetFreeSize(void)
\r
370 struct _heapinfo h_info;
\r
372 size_t h_free=0, h_total=0, h_used=0;
\r
374 h_info._pentry = NULL;
\r
376 heap_status = _heapwalk( &h_info );
\r
377 if( heap_status != _HEAPOK ) break;
\r
378 if((h_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") h_free += h_info._size;
\r
379 if((h_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") h_used += h_info._size;
\r
380 h_total += h_info._size;
\r
382 HC_heapstat0(heap_status);
\r
386 size_t HC_GetFarFreeSize(void)
\r
388 struct _heapinfo fh_info;
\r
390 size_t fh_free=0, fh_total=0, fh_used=0;
\r
392 fh_info._pentry = NULL;
\r
394 heap_status = _fheapwalk( &fh_info );
\r
395 if( heap_status != _HEAPOK ) break;
\r
396 if((fh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") fh_free += fh_info._size;
\r
397 if((fh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") fh_used += fh_info._size;
\r
398 fh_total += fh_info._size;
\r
400 HC_heapstat0(heap_status);
\r
404 size_t HC_GetNearFreeSize(void)
\r
406 struct _heapinfo nh_info;
\r
408 size_t nh_free=0, nh_total=0, nh_used=0;
\r
410 nh_info._pentry = NULL;
\r
412 heap_status = _nheapwalk( &nh_info );
\r
413 if( heap_status != _HEAPOK ) break;
\r
414 if((nh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") nh_free += nh_info._size;
\r
415 if((nh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") nh_used += nh_info._size;
\r
416 nh_total += nh_info._size;
\r
418 HC_heapstat0(heap_status);
\r
422 void HC_heapdump(global_game_variables_t *gvar)
\r
424 struct _heapinfo fh_info, nh_info;//, h_info;
\r
426 size_t nh_free, fh_free, nh_total, fh_total, nh_used, fh_used;//, h_free, h_total, h_used;
\r
427 byte scratch[1024],str[16];
\r
429 HC_OpenDebug(gvar);
\r
432 strcpy(scratch,"\n == default ==\n\n");
\r
433 write(gvar->handle.heaphandle,scratch,strlen(scratch));
\r
434 h_info._pentry = NULL;
\r
435 h_free=0; h_total=0; h_used=0;
\r
437 heap_status = _heapwalk( &h_info );
\r
438 if( heap_status != _HEAPOK ) break;
\r
439 strcpy(scratch," "); strcat(scratch,(h_info._useflag == _USEDENTRY ? "USED" : "FREE")); strcat(scratch," block at ");
\r
440 sprintf(str, "%Fp", h_info._pentry); //ultoa((dword)h_info._pentry,str,16);
\r
441 strcat(scratch,str); strcat(scratch," of size "); ultoa(h_info._size,str,10); strcat(scratch,str); strcat(scratch,"\n");
\r
442 if((h_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") h_free += h_info._size;
\r
443 if((h_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") h_used += h_info._size;
\r
444 h_total += h_info._size;
\r
445 write(gvar->handle.heaphandle,scratch,strlen(scratch));
\r
447 HC_heapstat(gvar, heap_status, &scratch);
\r
451 strcpy(scratch,"\n == near ==\n\n");
\r
452 write(gvar->handle.heaphandle,scratch,strlen(scratch));
\r
453 nh_info._pentry = NULL;
\r
454 nh_free=0; nh_total=0; nh_used=0;
\r
456 heap_status = _nheapwalk( &nh_info );
\r
457 if( heap_status != _HEAPOK ) break;
\r
458 strcpy(scratch," "); strcat(scratch,(nh_info._useflag == _USEDENTRY ? "USED" : "FREE")); strcat(scratch," block at ");
\r
459 sprintf(str, "%Fp", nh_info._pentry); //ultoa((dword)nh_info._pentry,str,16);
\r
460 strcat(scratch,str); strcat(scratch," of size "); ultoa(nh_info._size,str,10); strcat(scratch,str); strcat(scratch,"\n");
\r
461 /* printf( " %s block at %Fp of size %4.4X\n",
\r
462 (nh_info._useflag == _USEDENTRY ? "USED" : "FREE"),
\r
463 nh_info._pentry, nh_info._size );*/
\r
464 if((nh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") nh_free += nh_info._size;
\r
465 if((nh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") nh_used += nh_info._size;
\r
466 nh_total += nh_info._size;
\r
467 write(gvar->handle.heaphandle,scratch,strlen(scratch));
\r
469 HC_heapstat(gvar, heap_status, &scratch);
\r
472 strcpy(scratch,"\n == far ==\n\n");
\r
473 write(gvar->handle.heaphandle,scratch,strlen(scratch));
\r
474 fh_info._pentry = NULL;
\r
475 fh_free=0; fh_total=0; fh_used=0;
\r
477 heap_status = _fheapwalk( &fh_info );
\r
478 if( heap_status != _HEAPOK ) break;
\r
479 strcpy(scratch," "); strcat(scratch,(fh_info._useflag == _USEDENTRY ? "USED" : "FREE")); strcat(scratch," block at ");
\r
480 sprintf(str, "%Fp", fh_info._pentry); //ultoa((dword)fh_info._pentry,str,16);
\r
481 strcat(scratch,str); strcat(scratch," of size "); ultoa(fh_info._size,str,10); strcat(scratch,str); strcat(scratch,"\n");
\r
482 /*printf( " %s block at %Fp of size %4.4X\n",
\r
483 (fh_info._useflag == _USEDENTRY ? "USED" : "FREE"),
\r
484 fh_info._pentry, fh_info._size );*/
\r
485 if((fh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") fh_free += fh_info._size;
\r
486 if((fh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") fh_used += fh_info._size;
\r
487 fh_total += fh_info._size;
\r
488 write(gvar->handle.heaphandle,scratch,strlen(scratch));
\r
490 HC_heapstat(gvar, heap_status, &scratch);
\r
492 strcpy(scratch,"\n");
\r
493 strcat(scratch,kittengets(2,0,"Memory Type Total Used Free\n"));
\r
494 strcat(scratch,"---------------- -------- -------- --------\n");
\r
495 // printmeminfoline(&scratch, "Default", h_total, h_used, h_free);
\r
496 printmeminfoline(&scratch, "Near", nh_total, nh_used, nh_free);
\r
497 printmeminfoline(&scratch, "Far", fh_total, fh_used, fh_free);
\r
498 strcat(scratch,"---------------- -------- -------- --------\n");
\r
499 strcat(scratch,"HC_coreleft = "); ultoa((dword)HC_coreleft(),str,10); strcat(scratch,str); strcat(scratch,"\n");
\r
500 strcat(scratch,"HC_farcoreleft = "); ultoa((dword)HC_farcoreleft(),str,10); strcat(scratch,str); strcat(scratch,"\n");
\r
501 strcat(scratch,"HC_GetFreeSize = "); ultoa((dword)HC_GetFreeSize(),str,10); strcat(scratch,str); strcat(scratch,"\n");
\r
502 strcat(scratch,"HC_GetNearFreeSize = "); ultoa((dword)HC_GetNearFreeSize(),str,10); strcat(scratch,str); strcat(scratch,"\n");
\r
503 strcat(scratch,"HC_GetFarFreeSize = "); ultoa((dword)HC_GetFarFreeSize(),str,10); strcat(scratch,str); strcat(scratch,"\n");
\r
504 strcat(scratch,"memavl = "); ultoa((dword)_memavl(),str,10); strcat(scratch,str); strcat(scratch,"\n");
\r
505 strcat(scratch,"stackavail = "); ultoa((dword)stackavail(),str,10); strcat(scratch,str); strcat(scratch,"\n");
\r
506 write(gvar->handle.heaphandle,scratch,strlen(scratch));
\r
507 HC_CloseDebug(gvar);
\r
510 void HC_heapstat(global_game_variables_t *gvar, int heap_status, byte *str)
\r
512 switch( heap_status ) {
\r
514 strcpy((str),"OK - end of heap\n");
\r
517 strcpy((str),"OK - heap is empty\n");
\r
520 case _HEAPBADBEGIN:
\r
521 strcpy((str),"ERROR - heap is damaged\n");
\r
524 strcpy((str),"ERROR - bad pointer to heap\n");
\r
527 strcpy((str),"ERROR - bad node in heap\n");
\r
529 write(gvar->handle.heaphandle,(str),strlen((str)));
\r
532 void HC_heapstat0(int heap_status)
\r
534 switch( heap_status ) {
\r
536 //printf("OK - end of heap\n");
\r
539 //printf("OK - heap is empty\n");
\r
541 case _HEAPBADBEGIN:
\r
542 printf("ERROR - heap is damaged\n");
\r
545 printf("ERROR - bad pointer to heap\n");
\r
548 printf("ERROR - bad node in heap\n");
\r
553 ============================
\r
555 = HC_OpenDebug / HC_CloseDebug
\r
557 = Opens a binary file with the handle "heaphandle"
\r
559 ============================
\r
561 void HC_OpenDebug(global_game_variables_t *gvar)
\r
563 #ifdef __BORLANDC__
\r
564 unlink("heap.16b");
\r
565 gvar->handle.heaphandle = open(gvar->handle.heapdumpfilename, O_CREAT | O_WRONLY | O_TEXT);
\r
568 unlink("heap.16w");
\r
569 gvar->handle.heaphandle = open(gvar->handle.heapdumpfilename, O_CREAT | O_WRONLY | O_TEXT);
\r
573 void HC_CloseDebug(global_game_variables_t *gvar)
\r
575 close(gvar->handle.heaphandle);
\r
577 #ifdef __BORLANDC__
\r
578 strcpy(gvar->handle.heapdumpfilename, "heap.16b");
\r
581 strcpy(gvar->handle.heapdumpfilename, "heap.16w");
\r