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 __near* 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(unsigned long* Size)
\r
122 unsigned long s0, s1;
\r
125 s0 = ~(unsigned long)0 ^ (~(unsigned long)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 unsigned long HC_farcoreleft(void)
\r
152 unsigned long total = 0UL;
\r
153 void far* pFirst = NULL;
\r
154 void far* pLast = NULL;
\r
157 unsigned long largest;
\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
184 //==#ifdef __WATCOMC__
\r
185 /*void huge* LargestHugeFreeBlock(size_t* Size)
\r
190 s0 = ~(size_t)0 ^ (~(size_t)0 >> 1);
\r
191 while (s0 && (p = halloc((dword)s0, 1)) == NULL)
\r
200 if ((p = halloc((dword)(s0 + s1), 1)) != NULL)
\r
207 while (s0 && (p = halloc((dword)s0, 1)) == NULL)
\r
214 size_t _hugecoreleft(void)
\r
217 void huge* pFirst = NULL;
\r
218 void huge* pLast = NULL;
\r
222 void huge* p = LargestHugeFreeBlock(&largest);
\r
223 if (largest < sizeof(void huge*))
\r
229 *(void huge* huge*)p = NULL;
\r
231 if (pFirst == NULL)
\r
235 *(void huge* huge*)pLast = p;
\r
239 while (pFirst != NULL)
\r
241 void huge* p = *(void huge* huge*)pFirst;
\r
248 void __based(__self)* LargestBasedFreeBlock(size_t* Size)
\r
252 void __based(__self)* p;
\r
254 s0 = ~(size_t)0 ^ (~(size_t)0 >> 1);
\r
255 while (s0 && (p = _bmalloc(segu, s0)) == NULL)
\r
264 if ((p = _bmalloc(segu, s0 + s1)) != NULL)
\r
271 while (s0 && (p = _bmalloc(segu, s0)) == NULL)
\r
278 size_t _basedcoreleft(void)
\r
282 void __based(segu)* pFirst = NULL;
\r
283 void __based(segu)* pLast = NULL;
\r
284 // allocate based heap
\r
285 segu = _bHC_heapseg( 1024 );
\r
286 if( segu == _NULLSEG ) {
\r
287 printf( "Unable to allocate based heap\n" );
\r
296 void __based(segu)* p = LargestBasedFreeBlock(&largest);
\r
297 if (largest < sizeof(void far*))
\r
303 *(void far* far*)p = NULL;
\r
305 if (pFirst == NULL)
\r
309 *(void far* far*)pLast = p;
\r
313 while (pFirst != NULL)
\r
315 void far* p = *(void far* far*)pFirst;
\r
322 size_t HC_GetFreeSize(void)
\r
324 struct _heapinfo h_info;
\r
326 size_t h_free=0, h_total=0, h_used=0;
\r
328 h_info._pentry = NULL;
\r
330 heap_status = _heapwalk( &h_info );
\r
331 if( heap_status != _HEAPOK ) break;
\r
332 if((h_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") h_free += h_info._size;
\r
333 if((h_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") h_used += h_info._size;
\r
334 h_total += h_info._size;
\r
336 HCL_heapstat(heap_status);
\r
341 void HCL_HeapWalking (struct _heapinfo *h_info, hc_use_t *hu, unsigned nearfarswitch)
\r
343 hu->h_free=0; hu->h_total=0; hu->h_used=0;
\r
345 h_info->_pentry = NULL;
\r
347 if(nearfarswitch==0) hu->heap_status = _nheapwalk( h_info );
\r
348 else if(nearfarswitch==1) hu->heap_status = _fheapwalk( h_info );
\r
349 if( hu->heap_status != _HEAPOK ) break;
\r
350 if((h_info->_useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") hu->h_free += h_info->_size;
\r
351 if((h_info->_useflag == _USEDENTRY ? "USED" : "FREE")=="USED") hu->h_used += h_info->_size;
\r
352 hu->h_total += h_info->_size;
\r
354 HCL_heapstat(hu->heap_status);
\r
357 unsigned long HC_GetFarFreeSize(void)
\r
359 struct _heapinfo h_info;
\r
361 HCL_HeapWalking (&h_info, &hu, 1);
\r
364 struct _heapinfo fh_info;
\r
366 unsigned long fh_free=0, fh_total=0, fh_used=0;
\r
368 fh_info._pentry = NULL;
\r
370 heap_status = _fheapwalk( &fh_info );
\r
371 if( heap_status != _HEAPOK ) break;
\r
372 if((fh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") fh_free += fh_info._size;
\r
373 if((fh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") fh_used += fh_info._size;
\r
374 fh_total += fh_info._size;
\r
376 HCL_heapstat(heap_status);
\r
381 size_t HC_GetNearFreeSize(void)
\r
383 struct _heapinfo h_info;
\r
385 HCL_HeapWalking (&h_info, &hu, 0);
\r
388 struct _heapinfo nh_info;
\r
390 size_t nh_free=0, nh_total=0, nh_used=0;
\r
392 nh_info._pentry = NULL;
\r
394 heap_status = _nheapwalk( &nh_info );
\r
395 if( heap_status != _HEAPOK ) break;
\r
396 if((nh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") nh_free += nh_info._size;
\r
397 if((nh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") nh_used += nh_info._size;
\r
398 nh_total += nh_info._size;
\r
400 HCL_heapstat(heap_status);
\r
405 void HC_heapdump(global_game_variables_t *gvar)
\r
407 struct _heapinfo fh_info, nh_info;//, h_info;
\r
409 size_t nh_free, fh_free, nh_total, fh_total, nh_used, fh_used;//, h_free, h_total, h_used;
\r
410 byte scratch[1024],str[16];
\r
412 HC_OpenDebug(gvar);
\r
415 strcpy(scratch,"\n == default ==\n\n");
\r
416 write(gvar->handle.heaphandle,scratch,strlen(scratch));
\r
417 h_info._pentry = NULL;
\r
418 h_free=0; h_total=0; h_used=0;
\r
420 heap_status = _heapwalk( &h_info );
\r
421 if( heap_status != _HEAPOK ) break;
\r
422 strcpy(scratch," "); strcat(scratch,(h_info._useflag == _USEDENTRY ? "USED" : "FREE")); strcat(scratch," block at ");
\r
423 sprintf(str, "%Fp", h_info._pentry); //ultoa((dword)h_info._pentry,str,16);
\r
424 strcat(scratch,str); strcat(scratch," of size "); ultoa(h_info._size,str,10); strcat(scratch,str); strcat(scratch,"\n");
\r
425 if((h_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") h_free += h_info._size;
\r
426 if((h_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") h_used += h_info._size;
\r
427 h_total += h_info._size;
\r
428 write(gvar->handle.heaphandle,scratch,strlen(scratch));
\r
430 HCL_heapstatLogWrite(gvar, heap_status, scratch);
\r
434 strcpy(scratch,"\n == near ==\n\n");
\r
435 write(gvar->handle.heaphandle,scratch,strlen(scratch));
\r
436 nh_info._pentry = NULL;
\r
437 nh_free=0; nh_total=0; nh_used=0;
\r
439 heap_status = _nheapwalk( &nh_info );
\r
440 if( heap_status != _HEAPOK ) break;
\r
441 strcpy(scratch," "); strcat(scratch,(nh_info._useflag == _USEDENTRY ? "USED" : "FREE")); strcat(scratch," block at ");
\r
442 sprintf(str, "%Fp", nh_info._pentry); //ultoa((dword)nh_info._pentry,str,16);
\r
443 strcat(scratch,str); strcat(scratch," of size "); ultoa(nh_info._size,str,10); strcat(scratch,str); strcat(scratch,"\n");
\r
444 /* printf( " %s block at %Fp of size %4.4X\n",
\r
445 (nh_info._useflag == _USEDENTRY ? "USED" : "FREE"),
\r
446 nh_info._pentry, nh_info._size );*/
\r
447 if((nh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") nh_free += nh_info._size;
\r
448 if((nh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") nh_used += nh_info._size;
\r
449 nh_total += nh_info._size;
\r
450 write(gvar->handle.heaphandle,scratch,strlen(scratch));
\r
452 HCL_heapstatLogWrite(gvar, heap_status, scratch);
\r
455 strcpy(scratch,"\n == far ==\n\n");
\r
456 write(gvar->handle.heaphandle,scratch,strlen(scratch));
\r
457 fh_info._pentry = NULL;
\r
458 fh_free=0; fh_total=0; fh_used=0;
\r
460 heap_status = _fheapwalk( &fh_info );
\r
461 if( heap_status != _HEAPOK ) break;
\r
462 strcpy(scratch," "); strcat(scratch,(fh_info._useflag == _USEDENTRY ? "USED" : "FREE")); strcat(scratch," block at ");
\r
463 sprintf(str, "%Fp", fh_info._pentry); //ultoa((dword)fh_info._pentry,str,16);
\r
464 strcat(scratch,str); strcat(scratch," of size "); ultoa(fh_info._size,str,10); strcat(scratch,str); strcat(scratch,"\n");
\r
465 /*printf( " %s block at %Fp of size %4.4X\n",
\r
466 (fh_info._useflag == _USEDENTRY ? "USED" : "FREE"),
\r
467 fh_info._pentry, fh_info._size );*/
\r
468 if((fh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") fh_free += fh_info._size;
\r
469 if((fh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") fh_used += fh_info._size;
\r
470 fh_total += fh_info._size;
\r
471 write(gvar->handle.heaphandle,scratch,strlen(scratch));
\r
473 HCL_heapstatLogWrite(gvar, heap_status, scratch);
\r
475 strcpy(scratch,"\n");
\r
476 strcat(scratch,kittengets(2,0,"Memory Type Total Used Free\n"));
\r
477 strcat(scratch,"---------------- -------- -------- --------\n");
\r
478 // printmeminfoline(&scratch, "Default", h_total, h_used, h_free);
\r
479 printmeminfoline(scratch, "Near", nh_total, nh_used, nh_free);
\r
480 printmeminfoline(scratch, "Far", fh_total, fh_used, fh_free);
\r
481 strcat(scratch,"---------------- -------- -------- --------\n");
\r
482 strcat(scratch,"HC_coreleft = "); ultoa((dword)HC_coreleft(),str,10); strcat(scratch,str); strcat(scratch,"\n");
\r
483 strcat(scratch,"HC_farcoreleft = "); ultoa((dword)HC_farcoreleft(),str,10); strcat(scratch,str); strcat(scratch,"\n");
\r
484 // strcat(scratch,"HC_Newfarcoreleft = "); ultoa((dword)HC_Newfarcoreleft(),str,10); strcat(scratch,str); strcat(scratch,"\n");
\r
485 // strcat(scratch,"HC_GetFreeSize = "); ultoa((dword)HC_GetFreeSize(),str,10); strcat(scratch,str); strcat(scratch,"\n");
\r
486 strcat(scratch,"HC_GetNearFreeSize = "); ultoa((dword)HC_GetNearFreeSize(),str,10); strcat(scratch,str); strcat(scratch,"\n");
\r
487 strcat(scratch,"HC_GetFarFreeSize = "); ultoa((dword)HC_GetFarFreeSize(),str,10); strcat(scratch,str); strcat(scratch,"\n");
\r
488 strcat(scratch,"coreleft = "); ultoa((dword)coreleft(),str,10); strcat(scratch,str); strcat(scratch,"\n");
\r
489 strcat(scratch,"farcoreleft = "); ultoa((dword)farcoreleft(),str,10); strcat(scratch,str); strcat(scratch,"\n");
\r
490 strcat(scratch,"stackavail = "); ultoa((dword)stackavail(),str,10); strcat(scratch,str); strcat(scratch,"\n");
\r
491 write(gvar->handle.heaphandle,scratch,strlen(scratch));
\r
492 HC_CloseDebug(gvar);
\r
495 void HCL_heapstatLogWrite(global_game_variables_t *gvar, int heap_status, byte *str)
\r
497 switch( heap_status ) {
\r
499 strcpy((str),"OK - end of heap\n");
\r
502 strcpy((str),"OK - heap is empty\n");
\r
505 case _HEAPBADBEGIN:
\r
506 strcpy((str),"ERROR - heap is damaged\n");
\r
510 strcpy((str),"ERROR - bad pointer to heap\n");
\r
514 strcpy((str),"ERROR - bad node in heap\n");
\r
516 write(gvar->handle.heaphandle,(str),strlen((str)));
\r
519 void HCL_heapstat(int heap_status)
\r
521 switch( heap_status ) {
\r
523 //printf("OK - end of heap\n");
\r
526 //printf("OK - heap is empty\n");
\r
528 case _HEAPBADBEGIN:
\r
529 printf("ERROR - heap is damaged\n");
\r
533 printf("ERROR - bad pointer to heap\n");
\r
537 printf("ERROR - bad node in heap\n");
\r
543 unsigned long farcoreleft()
\r
545 //---- _fheapgrow();
\r
546 return HC_farcoreleft();
\r
547 //stack overflows return HC_GetFarFreeSize();
\r
550 unsigned long coreleft()
\r
554 // return HC_GetNearFreeSize();
\r
559 ============================
\r
561 = HC_OpenDebug / HC_CloseDebug
\r
563 = Opens a binary file with the handle "heaphandle"
\r
565 ============================
\r
567 void HC_OpenDebug(global_game_variables_t *gvar)
\r
569 #ifdef __BORLANDC__
\r
570 unlink("heap.16b");
\r
571 gvar->handle.heaphandle = open(gvar->handle.heapdumpfilename, O_CREAT | O_WRONLY | O_TEXT);
\r
574 unlink("heap.16w");
\r
575 gvar->handle.heaphandle = open(gvar->handle.heapdumpfilename, O_CREAT | O_WRONLY | O_TEXT);
\r
579 void HC_CloseDebug(global_game_variables_t *gvar)
\r
581 close(gvar->handle.heaphandle);
\r
583 #ifdef __BORLANDC__
\r
584 strcpy(gvar->handle.heapdumpfilename, "heap.16b");
\r
587 strcpy(gvar->handle.heapdumpfilename, "heap.16w");
\r