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
31 //#ifdef __WATCOMC__
\r
32 long HC_Newfarcoreleft()
\r
34 void __huge *hp; static long rc=736L; long s_rc;
\r
38 hp=halloc(rc-=2L, 1024);
\r
39 while(hp==NULL&&rc>0L);
\r
46 hp=halloc(rc+=16L, 1024);
\r
50 return((rc-16L)*1024L);
\r
54 //from: https://stackoverflow.com/questions/14386856/c-check-available-ram
\r
55 void NPTR* HC_LargestFreeBlock(size_t* Size)
\r
60 s0 = ~(size_t)0 ^ (~(size_t)0 >> 1);
\r
61 while (s0 && (p = _nmalloc(s0)) == NULL)
\r
70 if ((p = _nmalloc(s0 + s1)) != NULL)
\r
77 while (s0 && (p = _nmalloc(s0)) == NULL)
\r
84 //from: https://stackoverflow.com/questions/14386856/c-check-available-ram
\r
85 size_t HC_coreleft(void)
\r
88 void __near* pFirst = NULL;
\r
89 void __near* pLast = NULL;
\r
93 void __near* p = (void __near *)HC_LargestFreeBlock(&largest);
\r
94 if (largest < sizeof(void __near*))
\r
100 *(void __near* __near*)p = NULL;
\r
102 if (pFirst == NULL)
\r
106 *(void __near* __near*)pLast = p;
\r
110 while (pFirst != NULL)
\r
112 void __near* p = *(void __near* __near*)pFirst;
\r
119 //far version of above
\r
120 void __far* HC_LargestFarFreeBlock(dword* Size)
\r
125 s0 = ~(dword)0 ^ (~(dword)0 >> 1);
\r
126 while (s0 && (p = _fmalloc(s0)) == NULL)
\r
135 if ((p = _fmalloc(s0 + s1)) != NULL)
\r
142 while (s0 && (p = _fmalloc(s0)) == NULL)
\r
149 //far version of above
\r
150 dword HC_farcoreleft(void)
\r
153 void __far* pFirst = NULL;
\r
154 void __far* pLast = NULL;
\r
158 void __far* p = HC_LargestFarFreeBlock(&largest);
\r
159 if (largest < sizeof(void __far*))
\r
165 *(void __far* __far*)p = NULL;
\r
167 if (pFirst == NULL)
\r
171 *(void __far* __far*)pLast = p;
\r
175 while (pFirst != NULL)
\r
177 void __far* p = *(void __far* __far*)pFirst;
\r
182 //if(total>16) total+=16; total &= 0xfffffff0UL;
\r
186 //==#ifdef __WATCOMC__
\r
187 /*void huge* LargestHugeFreeBlock(size_t* Size)
\r
192 s0 = ~(size_t)0 ^ (~(size_t)0 >> 1);
\r
193 while (s0 && (p = halloc((dword)s0, 1)) == NULL)
\r
202 if ((p = halloc((dword)(s0 + s1), 1)) != NULL)
\r
209 while (s0 && (p = halloc((dword)s0, 1)) == NULL)
\r
216 size_t _hugecoreleft(void)
\r
219 void huge* pFirst = NULL;
\r
220 void huge* pLast = NULL;
\r
224 void huge* p = LargestHugeFreeBlock(&largest);
\r
225 if (largest < sizeof(void huge*))
\r
231 *(void huge* huge*)p = NULL;
\r
233 if (pFirst == NULL)
\r
237 *(void huge* huge*)pLast = p;
\r
241 while (pFirst != NULL)
\r
243 void huge* p = *(void huge* huge*)pFirst;
\r
250 void __based(__self)* LargestBasedFreeBlock(size_t* Size)
\r
254 void __based(__self)* p;
\r
256 s0 = ~(size_t)0 ^ (~(size_t)0 >> 1);
\r
257 while (s0 && (p = _bmalloc(segu, s0)) == NULL)
\r
266 if ((p = _bmalloc(segu, s0 + s1)) != NULL)
\r
273 while (s0 && (p = _bmalloc(segu, s0)) == NULL)
\r
280 size_t _basedcoreleft(void)
\r
284 void __based(segu)* pFirst = NULL;
\r
285 void __based(segu)* pLast = NULL;
\r
286 // allocate based heap
\r
287 segu = _bHC_heapseg( 1024 );
\r
288 if( segu == _NULLSEG ) {
\r
289 printf( "Unable to allocate based heap\n" );
\r
298 void __based(segu)* p = LargestBasedFreeBlock(&largest);
\r
299 if (largest < sizeof(void __far*))
\r
305 *(void __far* __far*)p = NULL;
\r
307 if (pFirst == NULL)
\r
311 *(void __far* __far*)pLast = p;
\r
315 while (pFirst != NULL)
\r
317 void __far* p = *(void __far* __far*)pFirst;
\r
324 size_t HC_GetFreeSize(void)
\r
326 struct _heapinfo h_info;
\r
328 size_t h_free=0, h_total=0, h_used=0;
\r
330 h_info._pentry = NULL;
\r
332 heap_status = _heapwalk( &h_info );
\r
333 if( heap_status != _HEAPOK ) break;
\r
334 if((h_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") h_free += h_info._size;
\r
335 if((h_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") h_used += h_info._size;
\r
336 h_total += h_info._size;
\r
338 HCL_heapstat(heap_status);
\r
343 void HCL_HeapWalking (struct _heapinfo *h_info, hc_use_t *hu, unsigned nearfarswitch)
\r
345 hu->h_free=0; hu->h_total=0; hu->h_used=0;
\r
347 h_info->_pentry = NULL;
\r
349 if(nearfarswitch==0) hu->heap_status = _nheapwalk( h_info );
\r
350 else if(nearfarswitch==1) hu->heap_status = _fheapwalk( h_info );
\r
351 if( hu->heap_status != _HEAPOK ) break;
\r
352 if((h_info->_useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") hu->h_free += h_info->_size;
\r
353 if((h_info->_useflag == _USEDENTRY ? "USED" : "FREE")=="USED") hu->h_used += h_info->_size;
\r
354 hu->h_total += h_info->_size;
\r
356 HCL_heapstat(hu->heap_status);
\r
359 dword HC_GetFarFreeSize(void)
\r
361 struct _heapinfo h_info;
\r
363 HCL_HeapWalking (&h_info, &hu, 1);
\r
366 struct _heapinfo fh_info;
\r
368 dword fh_free=0, fh_total=0, fh_used=0;
\r
370 fh_info._pentry = NULL;
\r
372 heap_status = _fheapwalk( &fh_info );
\r
373 if( heap_status != _HEAPOK ) break;
\r
374 if((fh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") fh_free += fh_info._size;
\r
375 if((fh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") fh_used += fh_info._size;
\r
376 fh_total += fh_info._size;
\r
378 HCL_heapstat(heap_status);
\r
383 size_t HC_GetNearFreeSize(void)
\r
385 struct _heapinfo h_info;
\r
387 HCL_HeapWalking (&h_info, &hu, 0);
\r
390 struct _heapinfo nh_info;
\r
392 size_t nh_free=0, nh_total=0, nh_used=0;
\r
394 nh_info._pentry = NULL;
\r
396 heap_status = _nheapwalk( &nh_info );
\r
397 if( heap_status != _HEAPOK ) break;
\r
398 if((nh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") nh_free += nh_info._size;
\r
399 if((nh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") nh_used += nh_info._size;
\r
400 nh_total += nh_info._size;
\r
402 HCL_heapstat(heap_status);
\r
407 void HC_heapdump(global_game_variables_t *gvar)
\r
409 struct _heapinfo fh_info, nh_info;//, h_info;
\r
411 size_t nh_free, fh_free, nh_total, fh_total, nh_used, fh_used;//, h_free, h_total, h_used;
\r
412 byte scratch[1024],str[16];
\r
414 HC_OpenDebug(gvar);
\r
417 strcpy(scratch,"\n == default ==\n\n");
\r
418 write(gvar->handle.heaphandle,scratch,strlen(scratch));
\r
419 h_info._pentry = NULL;
\r
420 h_free=0; h_total=0; h_used=0;
\r
422 heap_status = _heapwalk( &h_info );
\r
423 if( heap_status != _HEAPOK ) break;
\r
424 strcpy(scratch," "); strcat(scratch,(h_info._useflag == _USEDENTRY ? "USED" : "FREE")); strcat(scratch," block at ");
\r
425 sprintf(str, "%Fp", h_info._pentry); //ultoa((dword)h_info._pentry,str,16);
\r
426 strcat(scratch,str); strcat(scratch," of size "); ultoa(h_info._size,str,10); strcat(scratch,str); strcat(scratch,"\n");
\r
427 if((h_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") h_free += h_info._size;
\r
428 if((h_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") h_used += h_info._size;
\r
429 h_total += h_info._size;
\r
430 write(gvar->handle.heaphandle,scratch,strlen(scratch));
\r
432 HCL_heapstatLogWrite(gvar, heap_status, scratch);
\r
436 strcpy(scratch,"\n == near ==\n\n");
\r
437 write(gvar->handle.heaphandle,scratch,strlen(scratch));
\r
438 nh_info._pentry = NULL;
\r
439 nh_free=0; nh_total=0; nh_used=0;
\r
441 heap_status = _nheapwalk( &nh_info );
\r
442 if( heap_status != _HEAPOK ) break;
\r
443 strcpy(scratch," "); strcat(scratch,(nh_info._useflag == _USEDENTRY ? "USED" : "FREE")); strcat(scratch," block at ");
\r
444 sprintf(str, "%Fp", nh_info._pentry); //ultoa((dword)nh_info._pentry,str,16);
\r
445 strcat(scratch,str); strcat(scratch," of size "); ultoa(nh_info._size,str,10); strcat(scratch,str); strcat(scratch,"\n");
\r
446 /* printf( " %s block at %Fp of size %4.4X\n",
\r
447 (nh_info._useflag == _USEDENTRY ? "USED" : "FREE"),
\r
448 nh_info._pentry, nh_info._size );*/
\r
449 if((nh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") nh_free += nh_info._size;
\r
450 if((nh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") nh_used += nh_info._size;
\r
451 nh_total += nh_info._size;
\r
452 write(gvar->handle.heaphandle,scratch,strlen(scratch));
\r
454 HCL_heapstatLogWrite(gvar, heap_status, scratch);
\r
457 strcpy(scratch,"\n == far ==\n\n");
\r
458 write(gvar->handle.heaphandle,scratch,strlen(scratch));
\r
459 fh_info._pentry = NULL;
\r
460 fh_free=0; fh_total=0; fh_used=0;
\r
462 heap_status = _fheapwalk( &fh_info );
\r
463 if( heap_status != _HEAPOK ) break;
\r
464 strcpy(scratch," "); strcat(scratch,(fh_info._useflag == _USEDENTRY ? "USED" : "FREE")); strcat(scratch," block at ");
\r
465 sprintf(str, "%Fp", fh_info._pentry); //ultoa((dword)fh_info._pentry,str,16);
\r
466 strcat(scratch,str); strcat(scratch," of size "); ultoa(fh_info._size,str,10); strcat(scratch,str); strcat(scratch,"\n");
\r
467 /*printf( " %s block at %Fp of size %4.4X\n",
\r
468 (fh_info._useflag == _USEDENTRY ? "USED" : "FREE"),
\r
469 fh_info._pentry, fh_info._size );*/
\r
470 if((fh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") fh_free += fh_info._size;
\r
471 if((fh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") fh_used += fh_info._size;
\r
472 fh_total += fh_info._size;
\r
473 write(gvar->handle.heaphandle,scratch,strlen(scratch));
\r
475 HCL_heapstatLogWrite(gvar, heap_status, scratch);
\r
477 strcpy(scratch,"\n");
\r
478 strcat(scratch,kittengets(2,0,"Memory Type Total Used Free\n"));
\r
479 strcat(scratch,"---------------- -------- -------- --------\n");
\r
480 //-- printmeminfoline(&scratch, "Default", h_total, h_used, h_free);
\r
481 printmeminfoline(scratch, "Near", nh_total, nh_used, nh_free);
\r
482 printmeminfoline(scratch, "Far", fh_total, fh_used, fh_free);
\r
483 strcat(scratch,"---------------- -------- -------- --------\n");
\r
484 #if defined(__LARGE__) || defined(__COMPACT__) || defined(__HUGE__)
\r
485 strcat(scratch,"HC_coreleft = "); ultoa((dword)HC_coreleft(),str,10); strcat(scratch,str); strcat(scratch,"\n");
\r
486 strcat(scratch,"HC_farcoreleft = "); ultoa((dword)HC_farcoreleft(),str,10); strcat(scratch,str); strcat(scratch,"\n");
\r
488 //-- strcat(scratch,"HC_Newfarcoreleft = "); ultoa((dword)HC_Newfarcoreleft(),str,10); strcat(scratch,str); strcat(scratch,"\n");
\r
489 //-- strcat(scratch,"HC_GetFreeSize = "); ultoa((dword)HC_GetFreeSize(),str,10); strcat(scratch,str); strcat(scratch,"\n");
\r
490 //00 strcat(scratch,"HC_GetNearFreeSize = "); ultoa((dword)HC_GetNearFreeSize(),str,10); strcat(scratch,str); strcat(scratch,"\n");
\r
491 //00 strcat(scratch,"HC_GetFarFreeSize = "); ultoa((dword)HC_GetFarFreeSize(),str,10); strcat(scratch,str); strcat(scratch,"\n");
\r
492 strcat(scratch,"coreleft = "); ultoa((dword)coreleft(),str,10); strcat(scratch,str); strcat(scratch,"\n");
\r
493 strcat(scratch,"farcoreleft = "); ultoa((dword)farcoreleft(),str,10); strcat(scratch,str); strcat(scratch,"\n");
\r
494 strcat(scratch,"stackavail = "); ultoa((dword)stackavail(),str,10); strcat(scratch,str); strcat(scratch,"\n");
\r
495 write(gvar->handle.heaphandle,scratch,strlen(scratch));
\r
496 HC_CloseDebug(gvar);
\r
499 void HCL_heapstatLogWrite(global_game_variables_t *gvar, int heap_status, byte *str)
\r
501 switch( heap_status ) {
\r
503 strcpy((str),"OK - end of heap\n");
\r
506 strcpy((str),"OK - heap is empty\n");
\r
509 case _HEAPBADBEGIN:
\r
510 strcpy((str),"ERROR - heap is damaged\n");
\r
514 strcpy((str),"ERROR - bad pointer to heap\n");
\r
518 strcpy((str),"ERROR - bad node in heap\n");
\r
520 write(gvar->handle.heaphandle,(str),strlen((str)));
\r
523 void HCL_heapstat(int heap_status)
\r
525 switch( heap_status ) {
\r
527 //printf("OK - end of heap\n");
\r
530 //printf("OK - heap is empty\n");
\r
532 case _HEAPBADBEGIN:
\r
533 printf("ERROR - heap is damaged\n");
\r
537 printf("ERROR - bad pointer to heap\n");
\r
541 printf("ERROR - bad node in heap\n");
\r
547 dword farcoreleft()
\r
549 //---- _fheapgrow();
\r
550 // #ifdef __BORLANDC__
\r
551 // return 0x90000UL-16UL;
\r
554 #if !defined(__LARGE__) && !defined(__COMPACT__) && !defined(__HUGE__)
\r
556 return 0x90000UL+16UL;
\r
557 //---- return 589824UL+16UL;
\r
560 return HC_farcoreleft();
\r
561 //stack overflows return HC_GetFarFreeSize();
\r
569 // return HC_GetNearFreeSize();
\r
574 ============================
\r
576 = HC_OpenDebug / HC_CloseDebug
\r
578 = Opens a binary file with the handle "heaphandle"
\r
580 ============================
\r
582 void HC_OpenDebug(global_game_variables_t *gvar)
\r
584 #ifdef __BORLANDC__
\r
585 unlink("heap.16b");
\r
586 gvar->handle.heaphandle = open(gvar->handle.heapdumpfilename, O_CREAT | O_WRONLY | O_TEXT);
\r
589 unlink("heap.16w");
\r
590 gvar->handle.heaphandle = open(gvar->handle.heapdumpfilename, O_CREAT | O_WRONLY | O_TEXT);
\r
594 void HC_CloseDebug(global_game_variables_t *gvar)
\r
596 close(gvar->handle.heaphandle);
\r
598 #ifdef __BORLANDC__
\r
599 strcpy(gvar->handle.heapdumpfilename, "heap.16b");
\r
602 strcpy(gvar->handle.heapdumpfilename, "heap.16w");
\r