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"
28 void __near* LargestFreeBlock(size_t* Size)
33 s0 = ~(size_t)0 ^ (~(size_t)0 >> 1);
34 while (s0 && (p = _nmalloc(s0)) == NULL)
43 if ((p = _nmalloc(s0 + s1)) != NULL)
50 while (s0 && (p = _nmalloc(s0)) == NULL)
57 size_t _coreleft(void)
60 void __near* pFirst = NULL;
61 void __near* pLast = NULL;
65 void __near* p = LargestFreeBlock(&largest);
66 if (largest < sizeof(void __near*))
72 *(void __near* __near*)p = NULL;
78 *(void __near* __near*)pLast = p;
82 while (pFirst != NULL)
84 void __near* p = *(void __near* __near*)pFirst;
91 void far* LargestFarFreeBlock(size_t* Size)
96 s0 = ~(size_t)0 ^ (~(size_t)0 >> 1);
97 while (s0 && (p = _fmalloc(s0)) == NULL)
106 if ((p = _fmalloc(s0 + s1)) != NULL)
113 while (s0 && (p = _fmalloc(s0)) == NULL)
120 size_t _farcoreleft(void)
123 void far* pFirst = NULL;
124 void far* pLast = NULL;
128 void far* p = LargestFarFreeBlock(&largest);
129 if (largest < sizeof(void far*))
135 *(void far* far*)p = NULL;
141 *(void far* far*)pLast = p;
145 while (pFirst != NULL)
147 void far* p = *(void far* far*)pFirst;
154 void huge* LargestHugeFreeBlock(size_t* Size)
159 s0 = ~(size_t)0 ^ (~(size_t)0 >> 1);
160 while (s0 && (p = halloc((dword)s0, 1)) == NULL)
169 if ((p = halloc((dword)(s0 + s1), 1)) != NULL)
176 while (s0 && (p = halloc((dword)s0, 1)) == NULL)
183 size_t _hugecoreleft(void)
186 void huge* pFirst = NULL;
187 void huge* pLast = NULL;
191 void huge* p = LargestHugeFreeBlock(&largest);
192 if (largest < sizeof(void huge*))
198 *(void huge* huge*)p = NULL;
204 *(void huge* huge*)pLast = p;
208 while (pFirst != NULL)
210 void huge* p = *(void huge* huge*)pFirst;
217 /*void __based(__self)* LargestBasedFreeBlock(size_t* Size)
221 void __based(__self)* p;
223 s0 = ~(size_t)0 ^ (~(size_t)0 >> 1);
224 while (s0 && (p = _bmalloc(segu, s0)) == NULL)
233 if ((p = _bmalloc(segu, s0 + s1)) != NULL)
240 while (s0 && (p = _bmalloc(segu, s0)) == NULL)
247 size_t _basedcoreleft(void)
251 void __based(segu)* pFirst = NULL;
252 void __based(segu)* pLast = NULL;
253 // allocate based heap
254 segu = _bheapseg( 1024 );
255 if( segu == _NULLSEG ) {
256 printf( "Unable to allocate based heap\n" );
265 void __based(segu)* p = LargestBasedFreeBlock(&largest);
266 if (largest < sizeof(void far*))
272 *(void far* far*)p = NULL;
278 *(void far* far*)pLast = p;
282 while (pFirst != NULL)
284 void far* p = *(void far* far*)pFirst;
291 size_t GetFreeSize(void)
293 struct _heapinfo h_info;
295 size_t h_free=0, h_total=0, h_used=0;
297 h_info._pentry = NULL;
299 heap_status = _heapwalk( &h_info );
300 if( heap_status != _HEAPOK ) break;
301 if((h_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") h_free += h_info._size;
302 if((h_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") h_used += h_info._size;
303 h_total += h_info._size;
305 heapstat0(heap_status);
309 size_t GetFarFreeSize(void)
311 struct _heapinfo fh_info;
313 size_t fh_free=0, fh_total=0, fh_used=0;
315 fh_info._pentry = NULL;
317 heap_status = _fheapwalk( &fh_info );
318 if( heap_status != _HEAPOK ) break;
319 if((fh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") fh_free += fh_info._size;
320 if((fh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") fh_used += fh_info._size;
321 fh_total += fh_info._size;
323 heapstat0(heap_status);
327 size_t GetNearFreeSize(void)
329 struct _heapinfo nh_info;
331 size_t nh_free=0, nh_total=0, nh_used=0;
333 nh_info._pentry = NULL;
335 heap_status = _nheapwalk( &nh_info );
336 if( heap_status != _HEAPOK ) break;
337 if((nh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") nh_free += nh_info._size;
338 if((nh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") nh_used += nh_info._size;
339 nh_total += nh_info._size;
341 heapstat0(heap_status);
347 struct _heapinfo fh_info, nh_info, h_info;
349 size_t h_free, nh_free, fh_free, h_total, nh_total, fh_total, h_used, nh_used, fh_used;
350 byte scratch[1024],str[16];
354 strcpy(scratch,"\n == default ==\n\n");
355 write(heaphandle,scratch,strlen(scratch));
356 h_info._pentry = NULL;
357 h_free=0; h_total=0; h_used=0;
359 heap_status = _heapwalk( &h_info );
360 if( heap_status != _HEAPOK ) break;
362 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,16); strcat(scratch,str); strcat(scratch,"\n");
363 if((h_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") h_free += h_info._size;
364 if((h_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") h_used += h_info._size;
365 h_total += h_info._size;
366 write(heaphandle,scratch,strlen(scratch));
368 heapstat(heap_status, &scratch);
371 strcpy(scratch,"\n == near ==\n\n");
372 write(heaphandle,scratch,strlen(scratch));
373 nh_info._pentry = NULL;
374 nh_free=0; nh_total=0; nh_used=0;
376 heap_status = _nheapwalk( &nh_info );
377 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,16); 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;
399 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,16); strcat(scratch,str); strcat(scratch,"\n");
400 /*printf( " %s block at %Fp of size %4.4X\n",
401 (fh_info._useflag == _USEDENTRY ? "USED" : "FREE"),
402 fh_info._pentry, fh_info._size );*/
403 if((fh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") fh_free += fh_info._size;
404 if((fh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") fh_used += fh_info._size;
405 fh_total += fh_info._size;
406 write(heaphandle,scratch,strlen(scratch));
408 heapstat(heap_status, &scratch);
410 strcpy(scratch,"\n");
411 strcat(scratch,kittengets(2,0,"Memory Type Total Used Free\n"));
412 strcat(scratch,"---------------- -------- -------- --------\n");
413 printmeminfoline(&scratch, "Default", h_total, h_used, h_free);
414 printmeminfoline(&scratch, "Near", nh_total, nh_used, nh_free);
415 printmeminfoline(&scratch, "Far", fh_total, fh_used, fh_free);
416 strcat(scratch,"---------------- -------- -------- --------\n");
417 strcat(scratch,"coreleft = "); ultoa((dword)_coreleft(),str,10); strcat(scratch,str); strcat(scratch,"\n");
418 strcat(scratch,"farcoreleft = "); ultoa((dword)_farcoreleft(),str,10); strcat(scratch,str); strcat(scratch,"\n");
419 strcat(scratch,"GetFreeSize = "); ultoa((dword)GetFreeSize(),str,10); strcat(scratch,str); strcat(scratch,"\n");
420 strcat(scratch,"GetNearFreeSize = "); ultoa((dword)GetNearFreeSize(),str,10); strcat(scratch,str); strcat(scratch,"\n");
421 strcat(scratch,"GetFarFreeSize = "); ultoa((dword)GetFarFreeSize(),str,10); strcat(scratch,str); strcat(scratch,"\n");
422 strcat(scratch,"memavl = "); ultoa((dword)_memavl(),str,10); strcat(scratch,str); strcat(scratch,"\n");
423 strcat(scratch,"stackavail = "); ultoa((dword)stackavail(),str,10); strcat(scratch,str); strcat(scratch,"\n");
424 write(heaphandle,scratch,strlen(scratch));
428 void heapstat(int heap_status, byte *str)
430 switch( heap_status ) {
432 strcpy((str),"OK - end of heap\n");
435 strcpy((str),"OK - heap is empty\n");
439 strcpy((str),"ERROR - heap is damaged\n");
442 strcpy((str),"ERROR - bad pointer to heap\n");
445 strcpy((str),"ERROR - bad node in heap\n");
447 write(heaphandle,(str),strlen((str)));
450 void heapstat0(int heap_status)
452 switch( heap_status ) {
454 //printf("OK - end of heap\n");
457 //printf("OK - heap is empty\n");
461 printf("ERROR - heap is damaged\n");
464 printf("ERROR - bad pointer to heap\n");
467 printf("ERROR - bad node in heap\n");
472 ============================
474 = HC_OpenDebug / HC_CloseDebug
476 = Opens a binary file with the handle "heaphandle"
478 ============================
483 heaphandle = open("heap.16", O_CREAT | O_WRONLY | O_TEXT);