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"
30 void __near* LargestFreeBlock(size_t* Size)
35 s0 = ~(size_t)0 ^ (~(size_t)0 >> 1);
36 while (s0 && (p = _nmalloc(s0)) == NULL)
45 if ((p = _nmalloc(s0 + s1)) != NULL)
52 while (s0 && (p = _nmalloc(s0)) == NULL)
59 size_t _coreleft(void)
62 void __near* pFirst = NULL;
63 void __near* pLast = NULL;
67 void __near* p = LargestFreeBlock(&largest);
68 if (largest < sizeof(void __near*))
74 *(void __near* __near*)p = NULL;
80 *(void __near* __near*)pLast = p;
84 while (pFirst != NULL)
86 void __near* p = *(void __near* __near*)pFirst;
93 void far* LargestFarFreeBlock(size_t* Size)
98 s0 = ~(size_t)0 ^ (~(size_t)0 >> 1);
99 while (s0 && (p = _fmalloc(s0)) == NULL)
108 if ((p = _fmalloc(s0 + s1)) != NULL)
115 while (s0 && (p = _fmalloc(s0)) == NULL)
122 size_t _farcoreleft(void)
125 void far* pFirst = NULL;
126 void far* pLast = NULL;
130 void far* p = LargestFarFreeBlock(&largest);
131 if (largest < sizeof(void far*))
137 *(void far* far*)p = NULL;
143 *(void far* far*)pLast = p;
147 while (pFirst != NULL)
149 void far* p = *(void far* far*)pFirst;
156 void huge* LargestHugeFreeBlock(size_t* Size)
161 s0 = ~(size_t)0 ^ (~(size_t)0 >> 1);
162 while (s0 && (p = halloc((dword)s0, 1)) == NULL)
171 if ((p = halloc((dword)(s0 + s1), 1)) != NULL)
178 while (s0 && (p = halloc((dword)s0, 1)) == NULL)
185 size_t _hugecoreleft(void)
188 void huge* pFirst = NULL;
189 void huge* pLast = NULL;
193 void huge* p = LargestHugeFreeBlock(&largest);
194 if (largest < sizeof(void huge*))
200 *(void huge* huge*)p = NULL;
206 *(void huge* huge*)pLast = p;
210 while (pFirst != NULL)
212 void huge* p = *(void huge* huge*)pFirst;
219 /*void __based(__self)* LargestBasedFreeBlock(size_t* Size)
223 void __based(__self)* p;
225 s0 = ~(size_t)0 ^ (~(size_t)0 >> 1);
226 while (s0 && (p = _bmalloc(segu, s0)) == NULL)
235 if ((p = _bmalloc(segu, s0 + s1)) != NULL)
242 while (s0 && (p = _bmalloc(segu, s0)) == NULL)
249 size_t _basedcoreleft(void)
253 void __based(segu)* pFirst = NULL;
254 void __based(segu)* pLast = NULL;
255 // allocate based heap
256 segu = _bheapseg( 1024 );
257 if( segu == _NULLSEG ) {
258 printf( "Unable to allocate based heap\n" );
267 void __based(segu)* p = LargestBasedFreeBlock(&largest);
268 if (largest < sizeof(void far*))
274 *(void far* far*)p = NULL;
280 *(void far* far*)pLast = p;
284 while (pFirst != NULL)
286 void far* p = *(void far* far*)pFirst;
293 size_t GetFreeSize(void)
295 struct _heapinfo h_info;
297 size_t h_free=0, h_total=0, h_used=0;
299 h_info._pentry = NULL;
301 heap_status = _heapwalk( &h_info );
302 if( heap_status != _HEAPOK ) break;
303 if((h_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") h_free += h_info._size;
304 if((h_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") h_used += h_info._size;
305 h_total += h_info._size;
307 heapstat0(heap_status);
311 size_t GetFarFreeSize(void)
313 struct _heapinfo fh_info;
315 size_t fh_free=0, fh_total=0, fh_used=0;
317 fh_info._pentry = NULL;
319 heap_status = _fheapwalk( &fh_info );
320 if( heap_status != _HEAPOK ) break;
321 if((fh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") fh_free += fh_info._size;
322 if((fh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") fh_used += fh_info._size;
323 fh_total += fh_info._size;
325 heapstat0(heap_status);
329 size_t GetNearFreeSize(void)
331 struct _heapinfo nh_info;
333 size_t nh_free=0, nh_total=0, nh_used=0;
335 nh_info._pentry = NULL;
337 heap_status = _nheapwalk( &nh_info );
338 if( heap_status != _HEAPOK ) break;
339 if((nh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") nh_free += nh_info._size;
340 if((nh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") nh_used += nh_info._size;
341 nh_total += nh_info._size;
343 heapstat0(heap_status);
349 struct _heapinfo fh_info, nh_info, h_info;
351 size_t h_free, nh_free, fh_free, h_total, nh_total, fh_total, h_used, nh_used, fh_used;
352 byte scratch[1024],str[16];
356 strcpy(scratch,"\n == default ==\n\n");
357 write(heaphandle,scratch,strlen(scratch));
358 h_info._pentry = NULL;
359 h_free=0; h_total=0; h_used=0;
361 heap_status = _heapwalk( &h_info );
362 if( heap_status != _HEAPOK ) break;
363 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");
364 if((h_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") h_free += h_info._size;
365 if((h_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") h_used += h_info._size;
366 h_total += h_info._size;
367 write(heaphandle,scratch,strlen(scratch));
369 heapstat(heap_status, &scratch);
372 strcpy(scratch,"\n == near ==\n\n");
373 write(heaphandle,scratch,strlen(scratch));
374 nh_info._pentry = NULL;
375 nh_free=0; nh_total=0; nh_used=0;
377 heap_status = _nheapwalk( &nh_info );
378 if( heap_status != _HEAPOK ) break;
379 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");
380 /* printf( " %s block at %Fp of size %4.4X\n",
381 (nh_info._useflag == _USEDENTRY ? "USED" : "FREE"),
382 nh_info._pentry, nh_info._size );*/
383 if((nh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") nh_free += nh_info._size;
384 if((nh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") nh_used += nh_info._size;
385 nh_total += nh_info._size;
386 write(heaphandle,scratch,strlen(scratch));
388 heapstat(heap_status, &scratch);
391 strcpy(scratch,"\n == far ==\n\n");
392 write(heaphandle,scratch,strlen(scratch));
393 fh_info._pentry = NULL;
394 fh_free=0; fh_total=0; fh_used=0;
396 heap_status = _fheapwalk( &fh_info );
397 if( heap_status != _HEAPOK ) break;
398 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");
399 /*printf( " %s block at %Fp of size %4.4X\n",
400 (fh_info._useflag == _USEDENTRY ? "USED" : "FREE"),
401 fh_info._pentry, fh_info._size );*/
402 if((fh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") fh_free += fh_info._size;
403 if((fh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") fh_used += fh_info._size;
404 fh_total += fh_info._size;
405 write(heaphandle,scratch,strlen(scratch));
407 heapstat(heap_status, &scratch);
409 strcpy(scratch,"\n");
410 strcat(scratch,kittengets(2,0,"Memory Type Total Used Free\n"));
411 strcat(scratch,"---------------- -------- -------- --------\n");
412 printmeminfoline(&scratch, "Default", h_total, h_used, h_free);
413 printmeminfoline(&scratch, "Near", nh_total, nh_used, nh_free);
414 printmeminfoline(&scratch, "Far", fh_total, fh_used, fh_free);
415 strcat(scratch,"---------------- -------- -------- --------\n");
416 strcat(scratch,"coreleft = "); ultoa((dword)_coreleft(),str,10); strcat(scratch,str); strcat(scratch,"\n");
417 strcat(scratch,"farcoreleft = "); ultoa((dword)_farcoreleft(),str,10); strcat(scratch,str); strcat(scratch,"\n");
418 strcat(scratch,"GetFreeSize = "); ultoa((dword)GetFreeSize(),str,10); strcat(scratch,str); strcat(scratch,"\n");
419 strcat(scratch,"GetNearFreeSize = "); ultoa((dword)GetNearFreeSize(),str,10); strcat(scratch,str); strcat(scratch,"\n");
420 strcat(scratch,"GetFarFreeSize = "); ultoa((dword)GetFarFreeSize(),str,10); strcat(scratch,str); strcat(scratch,"\n");
421 strcat(scratch,"memavl = "); ultoa((dword)_memavl(),str,10); strcat(scratch,str); strcat(scratch,"\n");
422 strcat(scratch,"stackavail = "); ultoa((dword)stackavail(),str,10); strcat(scratch,str); strcat(scratch,"\n");
423 write(heaphandle,scratch,strlen(scratch));
427 void heapstat(int heap_status, byte *str)
429 switch( heap_status ) {
431 strcpy((str),"OK - end of heap\n");
434 strcpy((str),"OK - heap is empty\n");
438 strcpy((str),"ERROR - heap is damaged\n");
441 strcpy((str),"ERROR - bad pointer to heap\n");
444 strcpy((str),"ERROR - bad node in heap\n");
446 write(heaphandle,(str),strlen((str)));
449 void heapstat0(int heap_status)
451 switch( heap_status ) {
453 //printf("OK - end of heap\n");
456 //printf("OK - heap is empty\n");
460 printf("ERROR - heap is damaged\n");
463 printf("ERROR - bad pointer to heap\n");
466 printf("ERROR - bad node in heap\n");
471 ============================
473 = HC_OpenDebug / HC_CloseDebug
475 = Opens a binary file with the handle "heaphandle"
477 ============================
482 heaphandle = open("heap.16", O_CREAT | O_WRONLY | O_TEXT);