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; 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
61 HC_LargestFreeBlock(size_t* Size)
\r
71 s0 = ~(size_t)0 ^ (~(size_t)0 >> 1);
\r
73 while (s0 && (p = malloc(s0)) == NULL)
\r
76 while (s0 && (p = _nmalloc(s0)) == NULL)
\r
92 if ((p = malloc(s0 + s1)) != NULL)
\r
95 if ((p = _nmalloc(s0 + s1)) != NULL)
\r
108 #ifdef __BORLANDC__
\r
109 while (s0 && (p = malloc(s0)) == NULL)
\r
112 while (s0 && (p = _nmalloc(s0)) == NULL)
\r
120 size_t HC_coreleft(void)
\r
123 void __near* pFirst = NULL;
\r
124 void __near* pLast = NULL;
\r
128 void __near* p = (void __near *)HC_LargestFreeBlock(&largest);
\r
129 if (largest < sizeof(void __near*))
\r
132 #ifdef __BORLANDC__
\r
140 *(void __near* __near*)p = NULL;
\r
142 if (pFirst == NULL)
\r
146 *(void __near* __near*)pLast = p;
\r
150 while (pFirst != NULL)
\r
152 void __near* p = *(void __near* __near*)pFirst;
\r
153 #ifdef __BORLANDC__
\r
164 void far* HC_LargestFarFreeBlock(size_t* Size)
\r
169 s0 = ~(size_t)0 ^ (~(size_t)0 >> 1);
\r
170 while (s0 && (p = _fmalloc(s0)) == NULL)
\r
179 if ((p = _fmalloc(s0 + s1)) != NULL)
\r
186 while (s0 && (p = _fmalloc(s0)) == NULL)
\r
193 size_t HC_farcoreleft_(void)
\r
196 void far* pFirst = NULL;
\r
197 void far* pLast = NULL;
\r
201 void far* p = HC_LargestFarFreeBlock(&largest);
\r
202 if (largest < sizeof(void far*))
\r
208 *(void far* far*)p = NULL;
\r
210 if (pFirst == NULL)
\r
214 *(void far* far*)pLast = p;
\r
218 while (pFirst != NULL)
\r
220 void far* p = *(void far* far*)pFirst;
\r
228 /*void huge* LargestHugeFreeBlock(size_t* Size)
\r
233 s0 = ~(size_t)0 ^ (~(size_t)0 >> 1);
\r
234 while (s0 && (p = halloc((dword)s0, 1)) == NULL)
\r
243 if ((p = halloc((dword)(s0 + s1), 1)) != NULL)
\r
250 while (s0 && (p = halloc((dword)s0, 1)) == NULL)
\r
257 size_t _hugecoreleft(void)
\r
260 void huge* pFirst = NULL;
\r
261 void huge* pLast = NULL;
\r
265 void huge* p = LargestHugeFreeBlock(&largest);
\r
266 if (largest < sizeof(void huge*))
\r
272 *(void huge* huge*)p = NULL;
\r
274 if (pFirst == NULL)
\r
278 *(void huge* huge*)pLast = p;
\r
282 while (pFirst != NULL)
\r
284 void huge* p = *(void huge* huge*)pFirst;
\r
291 void __based(__self)* LargestBasedFreeBlock(size_t* Size)
\r
295 void __based(__self)* p;
\r
297 s0 = ~(size_t)0 ^ (~(size_t)0 >> 1);
\r
298 while (s0 && (p = _bmalloc(segu, s0)) == NULL)
\r
307 if ((p = _bmalloc(segu, s0 + s1)) != NULL)
\r
314 while (s0 && (p = _bmalloc(segu, s0)) == NULL)
\r
321 size_t _basedcoreleft(void)
\r
325 void __based(segu)* pFirst = NULL;
\r
326 void __based(segu)* pLast = NULL;
\r
327 // allocate based heap
\r
328 segu = _bHC_heapseg( 1024 );
\r
329 if( segu == _NULLSEG ) {
\r
330 printf( "Unable to allocate based heap\n" );
\r
339 void __based(segu)* p = LargestBasedFreeBlock(&largest);
\r
340 if (largest < sizeof(void far*))
\r
346 *(void far* far*)p = NULL;
\r
348 if (pFirst == NULL)
\r
352 *(void far* far*)pLast = p;
\r
356 while (pFirst != NULL)
\r
358 void far* p = *(void far* far*)pFirst;
\r
365 size_t HC_GetFreeSize(void)
\r
367 struct _heapinfo h_info;
\r
369 size_t h_free=0, h_total=0, h_used=0;
\r
371 h_info._pentry = NULL;
\r
373 heap_status = _heapwalk( &h_info );
\r
374 if( heap_status != _HEAPOK ) break;
\r
375 if((h_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") h_free += h_info._size;
\r
376 if((h_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") h_used += h_info._size;
\r
377 h_total += h_info._size;
\r
379 HC_heapstat0(heap_status);
\r
383 size_t HC_GetFarFreeSize(void)
\r
385 struct _heapinfo fh_info;
\r
387 size_t fh_free=0, fh_total=0, fh_used=0;
\r
389 fh_info._pentry = NULL;
\r
391 heap_status = _fheapwalk( &fh_info );
\r
392 if( heap_status != _HEAPOK ) break;
\r
393 if((fh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") fh_free += fh_info._size;
\r
394 if((fh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") fh_used += fh_info._size;
\r
395 fh_total += fh_info._size;
\r
397 HC_heapstat0(heap_status);
\r
401 size_t HC_GetNearFreeSize(void)
\r
403 struct _heapinfo nh_info;
\r
405 size_t nh_free=0, nh_total=0, nh_used=0;
\r
407 nh_info._pentry = NULL;
\r
409 heap_status = _nheapwalk( &nh_info );
\r
410 if( heap_status != _HEAPOK ) break;
\r
411 if((nh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") nh_free += nh_info._size;
\r
412 if((nh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") nh_used += nh_info._size;
\r
413 nh_total += nh_info._size;
\r
415 HC_heapstat0(heap_status);
\r
419 void HC_heapdump(global_game_variables_t *gvar)
\r
421 struct _heapinfo fh_info, nh_info;//, h_info;
\r
423 size_t nh_free, fh_free, nh_total, fh_total, nh_used, fh_used;//, h_free, h_total, h_used;
\r
424 byte scratch[1024],str[16];
\r
426 HC_OpenDebug(gvar);
\r
429 strcpy(scratch,"\n == default ==\n\n");
\r
430 write(gvar->handle.heaphandle,scratch,strlen(scratch));
\r
431 h_info._pentry = NULL;
\r
432 h_free=0; h_total=0; h_used=0;
\r
434 heap_status = _heapwalk( &h_info );
\r
435 if( heap_status != _HEAPOK ) break;
\r
436 strcpy(scratch," "); strcat(scratch,(h_info._useflag == _USEDENTRY ? "USED" : "FREE")); strcat(scratch," block at ");
\r
437 sprintf(str, "%Fp", h_info._pentry); //ultoa((dword)h_info._pentry,str,16);
\r
438 strcat(scratch,str); strcat(scratch," of size "); ultoa(h_info._size,str,10); strcat(scratch,str); strcat(scratch,"\n");
\r
439 if((h_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") h_free += h_info._size;
\r
440 if((h_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") h_used += h_info._size;
\r
441 h_total += h_info._size;
\r
442 write(gvar->handle.heaphandle,scratch,strlen(scratch));
\r
444 HC_heapstat(gvar, heap_status, &scratch);
\r
448 strcpy(scratch,"\n == near ==\n\n");
\r
449 write(gvar->handle.heaphandle,scratch,strlen(scratch));
\r
450 nh_info._pentry = NULL;
\r
451 nh_free=0; nh_total=0; nh_used=0;
\r
453 heap_status = _nheapwalk( &nh_info );
\r
454 if( heap_status != _HEAPOK ) break;
\r
455 strcpy(scratch," "); strcat(scratch,(nh_info._useflag == _USEDENTRY ? "USED" : "FREE")); strcat(scratch," block at ");
\r
456 sprintf(str, "%Fp", nh_info._pentry); //ultoa((dword)nh_info._pentry,str,16);
\r
457 strcat(scratch,str); strcat(scratch," of size "); ultoa(nh_info._size,str,10); strcat(scratch,str); strcat(scratch,"\n");
\r
458 /* printf( " %s block at %Fp of size %4.4X\n",
\r
459 (nh_info._useflag == _USEDENTRY ? "USED" : "FREE"),
\r
460 nh_info._pentry, nh_info._size );*/
\r
461 if((nh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") nh_free += nh_info._size;
\r
462 if((nh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") nh_used += nh_info._size;
\r
463 nh_total += nh_info._size;
\r
464 write(gvar->handle.heaphandle,scratch,strlen(scratch));
\r
466 HC_heapstat(gvar, heap_status, &scratch);
\r
469 strcpy(scratch,"\n == far ==\n\n");
\r
470 write(gvar->handle.heaphandle,scratch,strlen(scratch));
\r
471 fh_info._pentry = NULL;
\r
472 fh_free=0; fh_total=0; fh_used=0;
\r
474 heap_status = _fheapwalk( &fh_info );
\r
475 if( heap_status != _HEAPOK ) break;
\r
476 strcpy(scratch," "); strcat(scratch,(fh_info._useflag == _USEDENTRY ? "USED" : "FREE")); strcat(scratch," block at ");
\r
477 sprintf(str, "%Fp", fh_info._pentry); //ultoa((dword)fh_info._pentry,str,16);
\r
478 strcat(scratch,str); strcat(scratch," of size "); ultoa(fh_info._size,str,10); strcat(scratch,str); strcat(scratch,"\n");
\r
479 /*printf( " %s block at %Fp of size %4.4X\n",
\r
480 (fh_info._useflag == _USEDENTRY ? "USED" : "FREE"),
\r
481 fh_info._pentry, fh_info._size );*/
\r
482 if((fh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") fh_free += fh_info._size;
\r
483 if((fh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") fh_used += fh_info._size;
\r
484 fh_total += fh_info._size;
\r
485 write(gvar->handle.heaphandle,scratch,strlen(scratch));
\r
487 HC_heapstat(gvar, heap_status, &scratch);
\r
489 strcpy(scratch,"\n");
\r
490 strcat(scratch,kittengets(2,0,"Memory Type Total Used Free\n"));
\r
491 strcat(scratch,"---------------- -------- -------- --------\n");
\r
492 // printmeminfoline(&scratch, "Default", h_total, h_used, h_free);
\r
493 printmeminfoline(&scratch, "Near", nh_total, nh_used, nh_free);
\r
494 printmeminfoline(&scratch, "Far", fh_total, fh_used, fh_free);
\r
495 strcat(scratch,"---------------- -------- -------- --------\n");
\r
496 strcat(scratch,"HC_coreleft = "); ultoa((dword)HC_coreleft(),str,10); strcat(scratch,str); strcat(scratch,"\n");
\r
497 strcat(scratch,"HC_farcoreleft = "); ultoa((dword)HC_farcoreleft(),str,10); strcat(scratch,str); strcat(scratch,"\n");
\r
498 strcat(scratch,"HC_GetFreeSize = "); ultoa((dword)HC_GetFreeSize(),str,10); strcat(scratch,str); strcat(scratch,"\n");
\r
499 strcat(scratch,"HC_GetNearFreeSize = "); ultoa((dword)HC_GetNearFreeSize(),str,10); strcat(scratch,str); strcat(scratch,"\n");
\r
500 strcat(scratch,"HC_GetFarFreeSize = "); ultoa((dword)HC_GetFarFreeSize(),str,10); strcat(scratch,str); strcat(scratch,"\n");
\r
501 strcat(scratch,"memavl = "); ultoa((dword)_memavl(),str,10); strcat(scratch,str); strcat(scratch,"\n");
\r
502 strcat(scratch,"stackavail = "); ultoa((dword)stackavail(),str,10); strcat(scratch,str); strcat(scratch,"\n");
\r
503 write(gvar->handle.heaphandle,scratch,strlen(scratch));
\r
504 HC_CloseDebug(gvar);
\r
507 void HC_heapstat(global_game_variables_t *gvar, int heap_status, byte *str)
\r
509 switch( heap_status ) {
\r
511 strcpy((str),"OK - end of heap\n");
\r
514 strcpy((str),"OK - heap is empty\n");
\r
517 case _HEAPBADBEGIN:
\r
518 strcpy((str),"ERROR - heap is damaged\n");
\r
521 strcpy((str),"ERROR - bad pointer to heap\n");
\r
524 strcpy((str),"ERROR - bad node in heap\n");
\r
526 write(gvar->handle.heaphandle,(str),strlen((str)));
\r
529 void HC_heapstat0(int heap_status)
\r
531 switch( heap_status ) {
\r
533 //printf("OK - end of heap\n");
\r
536 //printf("OK - heap is empty\n");
\r
538 case _HEAPBADBEGIN:
\r
539 printf("ERROR - heap is damaged\n");
\r
542 printf("ERROR - bad pointer to heap\n");
\r
545 printf("ERROR - bad node in heap\n");
\r
550 ============================
\r
552 = HC_OpenDebug / HC_CloseDebug
\r
554 = Opens a binary file with the handle "heaphandle"
\r
556 ============================
\r
558 void HC_OpenDebug(global_game_variables_t *gvar)
\r
560 #ifdef __BORLANDC__
\r
561 unlink("heap.16b");
\r
562 gvar->handle.heaphandle = open(gvar->handle.heapdumpfilename, O_CREAT | O_WRONLY | O_TEXT);
\r
565 unlink("heap.16w");
\r
566 gvar->handle.heaphandle = open(gvar->handle.heapdumpfilename, O_CREAT | O_WRONLY | O_TEXT);
\r
570 void HC_CloseDebug(global_game_variables_t *gvar)
\r
572 close(gvar->handle.heaphandle);
\r
574 #ifdef __BORLANDC__
\r
575 strcpy(gvar->handle.heapdumpfilename, "heap.16b");
\r
578 strcpy(gvar->handle.heapdumpfilename, "heap.16w");
\r
583 unsigned long farcoreleft()
\r
586 return HC_farcoreleft();
\r
589 unsigned long coreleft()
\r