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);
37 while (s0 && (p = malloc(s0)) == NULL)
40 while (s0 && (p = _nmalloc(s0)) == NULL)
56 if ((p = malloc(s0 + s1)) != NULL)
59 if ((p = _nmalloc(s0 + s1)) != NULL)
73 while (s0 && (p = malloc(s0)) == NULL)
76 while (s0 && (p = _nmalloc(s0)) == NULL)
84 size_t _coreleft(void)
87 void __near* pFirst = NULL;
88 void __near* pLast = NULL;
92 void __near* p = LargestFreeBlock(&largest);
93 if (largest < sizeof(void __near*))
104 *(void __near* __near*)p = NULL;
110 *(void __near* __near*)pLast = p;
114 while (pFirst != NULL)
116 void __near* p = *(void __near* __near*)pFirst;
128 void far* LargestFarFreeBlock(size_t* Size)
133 s0 = ~(size_t)0 ^ (~(size_t)0 >> 1);
134 while (s0 && (p = _fmalloc(s0)) == NULL)
143 if ((p = _fmalloc(s0 + s1)) != NULL)
150 while (s0 && (p = _fmalloc(s0)) == NULL)
157 size_t _farcoreleft(void)
160 void far* pFirst = NULL;
161 void far* pLast = NULL;
165 void far* p = LargestFarFreeBlock(&largest);
166 if (largest < sizeof(void far*))
172 *(void far* far*)p = NULL;
178 *(void far* far*)pLast = p;
182 while (pFirst != NULL)
184 void far* p = *(void far* far*)pFirst;
192 void huge* LargestHugeFreeBlock(size_t* Size)
197 s0 = ~(size_t)0 ^ (~(size_t)0 >> 1);
198 while (s0 && (p = halloc((dword)s0, 1)) == NULL)
207 if ((p = halloc((dword)(s0 + s1), 1)) != NULL)
214 while (s0 && (p = halloc((dword)s0, 1)) == NULL)
221 size_t _hugecoreleft(void)
224 void huge* pFirst = NULL;
225 void huge* pLast = NULL;
229 void huge* p = LargestHugeFreeBlock(&largest);
230 if (largest < sizeof(void huge*))
236 *(void huge* huge*)p = NULL;
242 *(void huge* huge*)pLast = p;
246 while (pFirst != NULL)
248 void huge* p = *(void huge* huge*)pFirst;
255 /*void __based(__self)* LargestBasedFreeBlock(size_t* Size)
259 void __based(__self)* p;
261 s0 = ~(size_t)0 ^ (~(size_t)0 >> 1);
262 while (s0 && (p = _bmalloc(segu, s0)) == NULL)
271 if ((p = _bmalloc(segu, s0 + s1)) != NULL)
278 while (s0 && (p = _bmalloc(segu, s0)) == NULL)
285 size_t _basedcoreleft(void)
289 void __based(segu)* pFirst = NULL;
290 void __based(segu)* pLast = NULL;
291 // allocate based heap
292 segu = _bheapseg( 1024 );
293 if( segu == _NULLSEG ) {
294 printf( "Unable to allocate based heap\n" );
303 void __based(segu)* p = LargestBasedFreeBlock(&largest);
304 if (largest < sizeof(void far*))
310 *(void far* far*)p = NULL;
316 *(void far* far*)pLast = p;
320 while (pFirst != NULL)
322 void far* p = *(void far* far*)pFirst;
329 size_t GetFreeSize(void)
331 struct _heapinfo h_info;
333 size_t h_free=0, h_total=0, h_used=0;
335 h_info._pentry = NULL;
337 heap_status = _heapwalk( &h_info );
338 if( heap_status != _HEAPOK ) break;
339 if((h_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") h_free += h_info._size;
340 if((h_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") h_used += h_info._size;
341 h_total += h_info._size;
343 heapstat0(heap_status);
347 size_t GetFarFreeSize(void)
349 struct _heapinfo fh_info;
351 size_t fh_free=0, fh_total=0, fh_used=0;
353 fh_info._pentry = NULL;
355 heap_status = _fheapwalk( &fh_info );
356 if( heap_status != _HEAPOK ) break;
357 if((fh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") fh_free += fh_info._size;
358 if((fh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") fh_used += fh_info._size;
359 fh_total += fh_info._size;
361 heapstat0(heap_status);
365 size_t GetNearFreeSize(void)
367 struct _heapinfo nh_info;
369 size_t nh_free=0, nh_total=0, nh_used=0;
371 nh_info._pentry = NULL;
373 heap_status = _nheapwalk( &nh_info );
374 if( heap_status != _HEAPOK ) break;
375 if((nh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") nh_free += nh_info._size;
376 if((nh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") nh_used += nh_info._size;
377 nh_total += nh_info._size;
379 heapstat0(heap_status);
385 struct _heapinfo fh_info, nh_info, h_info;
387 size_t h_free, nh_free, fh_free, h_total, nh_total, fh_total, h_used, nh_used, fh_used;
388 byte scratch[1024],str[16];
392 strcpy(scratch,"\n == default ==\n\n");
393 write(heaphandle,scratch,strlen(scratch));
394 h_info._pentry = NULL;
395 h_free=0; h_total=0; h_used=0;
397 heap_status = _heapwalk( &h_info );
398 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,10); strcat(scratch,str); strcat(scratch,"\n");
400 if((h_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") h_free += h_info._size;
401 if((h_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") h_used += h_info._size;
402 h_total += h_info._size;
403 write(heaphandle,scratch,strlen(scratch));
405 heapstat(heap_status, &scratch);
408 strcpy(scratch,"\n == near ==\n\n");
409 write(heaphandle,scratch,strlen(scratch));
410 nh_info._pentry = NULL;
411 nh_free=0; nh_total=0; nh_used=0;
413 heap_status = _nheapwalk( &nh_info );
414 if( heap_status != _HEAPOK ) break;
415 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");
416 /* printf( " %s block at %Fp of size %4.4X\n",
417 (nh_info._useflag == _USEDENTRY ? "USED" : "FREE"),
418 nh_info._pentry, nh_info._size );*/
419 if((nh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") nh_free += nh_info._size;
420 if((nh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") nh_used += nh_info._size;
421 nh_total += nh_info._size;
422 write(heaphandle,scratch,strlen(scratch));
424 heapstat(heap_status, &scratch);
427 strcpy(scratch,"\n == far ==\n\n");
428 write(heaphandle,scratch,strlen(scratch));
429 fh_info._pentry = NULL;
430 fh_free=0; fh_total=0; fh_used=0;
432 heap_status = _fheapwalk( &fh_info );
433 if( heap_status != _HEAPOK ) break;
434 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");
435 /*printf( " %s block at %Fp of size %4.4X\n",
436 (fh_info._useflag == _USEDENTRY ? "USED" : "FREE"),
437 fh_info._pentry, fh_info._size );*/
438 if((fh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="FREE") fh_free += fh_info._size;
439 if((fh_info._useflag == _USEDENTRY ? "USED" : "FREE")=="USED") fh_used += fh_info._size;
440 fh_total += fh_info._size;
441 write(heaphandle,scratch,strlen(scratch));
443 heapstat(heap_status, &scratch);
445 strcpy(scratch,"\n");
446 strcat(scratch,kittengets(2,0,"Memory Type Total Used Free\n"));
447 strcat(scratch,"---------------- -------- -------- --------\n");
448 printmeminfoline(&scratch, "Default", h_total, h_used, h_free);
449 printmeminfoline(&scratch, "Near", nh_total, nh_used, nh_free);
450 printmeminfoline(&scratch, "Far", fh_total, fh_used, fh_free);
451 strcat(scratch,"---------------- -------- -------- --------\n");
452 strcat(scratch,"coreleft = "); ultoa((dword)_coreleft(),str,10); strcat(scratch,str); strcat(scratch,"\n");
453 strcat(scratch,"farcoreleft = "); ultoa((dword)_farcoreleft(),str,10); strcat(scratch,str); strcat(scratch,"\n");
454 strcat(scratch,"GetFreeSize = "); ultoa((dword)GetFreeSize(),str,10); strcat(scratch,str); strcat(scratch,"\n");
455 strcat(scratch,"GetNearFreeSize = "); ultoa((dword)GetNearFreeSize(),str,10); strcat(scratch,str); strcat(scratch,"\n");
456 strcat(scratch,"GetFarFreeSize = "); ultoa((dword)GetFarFreeSize(),str,10); strcat(scratch,str); strcat(scratch,"\n");
457 strcat(scratch,"memavl = "); ultoa((dword)_memavl(),str,10); strcat(scratch,str); strcat(scratch,"\n");
458 strcat(scratch,"stackavail = "); ultoa((dword)stackavail(),str,10); strcat(scratch,str); strcat(scratch,"\n");
459 write(heaphandle,scratch,strlen(scratch));
463 void heapstat(int heap_status, byte *str)
465 switch( heap_status ) {
467 strcpy((str),"OK - end of heap\n");
470 strcpy((str),"OK - heap is empty\n");
474 strcpy((str),"ERROR - heap is damaged\n");
477 strcpy((str),"ERROR - bad pointer to heap\n");
480 strcpy((str),"ERROR - bad node in heap\n");
482 write(heaphandle,(str),strlen((str)));
485 void heapstat0(int heap_status)
487 switch( heap_status ) {
489 //printf("OK - end of heap\n");
492 //printf("OK - heap is empty\n");
496 printf("ERROR - heap is damaged\n");
499 printf("ERROR - bad pointer to heap\n");
502 printf("ERROR - bad node in heap\n");
507 ============================
509 = HC_OpenDebug / HC_CloseDebug
511 = Opens a binary file with the handle "heaphandle"
513 ============================
518 heaphandle = open("heap.16", O_CREAT | O_WRONLY | O_TEXT);