1 /* Project 16 Source Code~
2 * Copyright (C) 2012-2015 sparky4 & pngwen & andrius4669
4 * This file is part of Project 16.
6 * Project 16 is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * Project 16 is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>, or
18 * write to the Free Software Foundation, Inc., 51 Franklin Street,
19 * Fifth Floor, Boston, MA 02110-1301 USA.
26 #include "src/lib/16_hc.h"
35 void * LargestFreeBlock(size_t* Size)
38 void __near* LargestFreeBlock(size_t* Size)
49 s0 = ~(size_t)0 ^ (~(size_t)0 >> 1);
51 while (s0 && (p = malloc(s0)) == NULL)
54 while (s0 && (p = _nmalloc(s0)) == NULL)
70 if ((p = malloc(s0 + s1)) != NULL)
73 if ((p = _nmalloc(s0 + s1)) != NULL)
87 while (s0 && (p = malloc(s0)) == NULL)
90 while (s0 && (p = _nmalloc(s0)) == NULL)
98 size_t _coreleft(void)
101 void __near* pFirst = NULL;
102 void __near* pLast = NULL;
106 void __near* p = (void __near *)LargestFreeBlock(&largest);
107 if (largest < sizeof(void __near*))
118 *(void __near* __near*)p = NULL;
124 *(void __near* __near*)pLast = p;
128 while (pFirst != NULL)
130 void __near* p = *(void __near* __near*)pFirst;
142 void far* LargestFarFreeBlock(size_t* Size)
147 s0 = ~(size_t)0 ^ (~(size_t)0 >> 1);
148 while (s0 && (p = _fmalloc(s0)) == NULL)
157 if ((p = _fmalloc(s0 + s1)) != NULL)
164 while (s0 && (p = _fmalloc(s0)) == NULL)
171 size_t _farcoreleft(void)
174 void far* pFirst = NULL;
175 void far* pLast = NULL;
179 void far* p = LargestFarFreeBlock(&largest);
180 if (largest < sizeof(void far*))
186 *(void far* far*)p = NULL;
192 *(void far* far*)pLast = p;
196 while (pFirst != NULL)
198 void far* p = *(void far* far*)pFirst;
206 void huge* LargestHugeFreeBlock(size_t* Size)
211 s0 = ~(size_t)0 ^ (~(size_t)0 >> 1);
212 while (s0 && (p = halloc((dword)s0, 1)) == NULL)
221 if ((p = halloc((dword)(s0 + s1), 1)) != NULL)
228 while (s0 && (p = halloc((dword)s0, 1)) == NULL)
235 size_t _hugecoreleft(void)
238 void huge* pFirst = NULL;
239 void huge* pLast = NULL;
243 void huge* p = LargestHugeFreeBlock(&largest);
244 if (largest < sizeof(void huge*))
250 *(void huge* huge*)p = NULL;
256 *(void huge* huge*)pLast = p;
260 while (pFirst != NULL)
262 void huge* p = *(void huge* huge*)pFirst;
269 /*void __based(__self)* LargestBasedFreeBlock(size_t* Size)
273 void __based(__self)* p;
275 s0 = ~(size_t)0 ^ (~(size_t)0 >> 1);
276 while (s0 && (p = _bmalloc(segu, s0)) == NULL)
285 if ((p = _bmalloc(segu, s0 + s1)) != NULL)
292 while (s0 && (p = _bmalloc(segu, s0)) == NULL)
299 size_t _basedcoreleft(void)
303 void __based(segu)* pFirst = NULL;
304 void __based(segu)* pLast = NULL;
305 // allocate based heap
306 segu = _bheapseg( 1024 );
307 if( segu == _NULLSEG ) {
308 printf( "Unable to allocate based heap\n" );
317 void __based(segu)* p = LargestBasedFreeBlock(&largest);
318 if (largest < sizeof(void far*))
324 *(void far* far*)p = NULL;
330 *(void far* far*)pLast = p;
334 while (pFirst != NULL)
336 void far* p = *(void far* far*)pFirst;
343 size_t GetFreeSize(void)
345 struct _heapinfo h_info;
347 size_t h_free=0, h_total=0, h_used=0;
349 h_info._pentry = NULL;
351 heap_status = _heapwalk( &h_info );
352 if( heap_status != _HEAPOK ) break;
353 if((h_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") h_free += h_info._size;
354 if((h_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") h_used += h_info._size;
355 h_total += h_info._size;
357 heapstat0(heap_status);
361 size_t GetFarFreeSize(void)
363 struct _heapinfo fh_info;
365 size_t fh_free=0, fh_total=0, fh_used=0;
367 fh_info._pentry = NULL;
369 heap_status = _fheapwalk( &fh_info );
370 if( heap_status != _HEAPOK ) break;
371 if((fh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") fh_free += fh_info._size;
372 if((fh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") fh_used += fh_info._size;
373 fh_total += fh_info._size;
375 heapstat0(heap_status);
379 size_t GetNearFreeSize(void)
381 struct _heapinfo nh_info;
383 size_t nh_free=0, nh_total=0, nh_used=0;
385 nh_info._pentry = NULL;
387 heap_status = _nheapwalk( &nh_info );
388 if( heap_status != _HEAPOK ) break;
389 if((nh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") nh_free += nh_info._size;
390 if((nh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") nh_used += nh_info._size;
391 nh_total += nh_info._size;
393 heapstat0(heap_status);
399 struct _heapinfo fh_info, nh_info, h_info;
401 size_t h_free, nh_free, fh_free, h_total, nh_total, fh_total, h_used, nh_used, fh_used;
402 byte scratch[1024],str[16];
406 strcpy(scratch,"\n == default ==\n\n");
407 write(heaphandle,scratch,strlen(scratch));
408 h_info._pentry = NULL;
409 h_free=0; h_total=0; h_used=0;
411 heap_status = _heapwalk( &h_info );
412 if( heap_status != _HEAPOK ) break;
413 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");
414 if((h_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") h_free += h_info._size;
415 if((h_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") h_used += h_info._size;
416 h_total += h_info._size;
417 write(heaphandle,scratch,strlen(scratch));
419 heapstat(heap_status, &scratch);
422 strcpy(scratch,"\n == near ==\n\n");
423 write(heaphandle,scratch,strlen(scratch));
424 nh_info._pentry = NULL;
425 nh_free=0; nh_total=0; nh_used=0;
427 heap_status = _nheapwalk( &nh_info );
428 if( heap_status != _HEAPOK ) break;
429 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");
430 /* printf( " %s block at %Fp of size %4.4X\n",
431 (nh_info._useflag == _USEDENTRY ? "USED" : "FREE"),
432 nh_info._pentry, nh_info._size );*/
433 if((nh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") nh_free += nh_info._size;
434 if((nh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") nh_used += nh_info._size;
435 nh_total += nh_info._size;
436 write(heaphandle,scratch,strlen(scratch));
438 heapstat(heap_status, &scratch);
441 strcpy(scratch,"\n == far ==\n\n");
442 write(heaphandle,scratch,strlen(scratch));
443 fh_info._pentry = NULL;
444 fh_free=0; fh_total=0; fh_used=0;
446 heap_status = _fheapwalk( &fh_info );
447 if( heap_status != _HEAPOK ) break;
448 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");
449 /*printf( " %s block at %Fp of size %4.4X\n",
450 (fh_info._useflag == _USEDENTRY ? "USED" : "FREE"),
451 fh_info._pentry, fh_info._size );*/
452 if((fh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") fh_free += fh_info._size;
453 if((fh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") fh_used += fh_info._size;
454 fh_total += fh_info._size;
455 write(heaphandle,scratch,strlen(scratch));
457 heapstat(heap_status, &scratch);
459 strcpy(scratch,"\n");
460 strcat(scratch,kittengets(2,0,"Memory Type Total Used Free\n"));
461 strcat(scratch,"---------------- -------- -------- --------\n");
462 printmeminfoline(&scratch, "Default", h_total, h_used, h_free);
463 printmeminfoline(&scratch, "Near", nh_total, nh_used, nh_free);
464 printmeminfoline(&scratch, "Far", fh_total, fh_used, fh_free);
465 strcat(scratch,"---------------- -------- -------- --------\n");
466 strcat(scratch,"coreleft = "); ultoa((dword)_coreleft(),str,10); strcat(scratch,str); strcat(scratch,"\n");
467 strcat(scratch,"farcoreleft = "); ultoa((dword)_farcoreleft(),str,10); strcat(scratch,str); strcat(scratch,"\n");
468 strcat(scratch,"GetFreeSize = "); ultoa((dword)GetFreeSize(),str,10); strcat(scratch,str); strcat(scratch,"\n");
469 strcat(scratch,"GetNearFreeSize = "); ultoa((dword)GetNearFreeSize(),str,10); strcat(scratch,str); strcat(scratch,"\n");
470 strcat(scratch,"GetFarFreeSize = "); ultoa((dword)GetFarFreeSize(),str,10); strcat(scratch,str); strcat(scratch,"\n");
471 strcat(scratch,"memavl = "); ultoa((dword)_memavl(),str,10); strcat(scratch,str); strcat(scratch,"\n");
472 strcat(scratch,"stackavail = "); ultoa((dword)stackavail(),str,10); strcat(scratch,str); strcat(scratch,"\n");
473 write(heaphandle,scratch,strlen(scratch));
477 void heapstat(int heap_status, byte *str)
479 switch( heap_status ) {
481 strcpy((str),"OK - end of heap\n");
484 strcpy((str),"OK - heap is empty\n");
488 strcpy((str),"ERROR - heap is damaged\n");
491 strcpy((str),"ERROR - bad pointer to heap\n");
494 strcpy((str),"ERROR - bad node in heap\n");
496 write(heaphandle,(str),strlen((str)));
499 void heapstat0(int heap_status)
501 switch( heap_status ) {
503 //printf("OK - end of heap\n");
506 //printf("OK - heap is empty\n");
510 printf("ERROR - heap is damaged\n");
513 printf("ERROR - bad pointer to heap\n");
516 printf("ERROR - bad node in heap\n");
521 ============================
523 = HC_OpenDebug / HC_CloseDebug
525 = Opens a binary file with the handle "heaphandle"
527 ============================
532 heaphandle = open("heap.16", O_CREAT | O_WRONLY | O_TEXT);