diff --git a/kernel.c b/kernel.c index 11b58f7..1e38709 100644 --- a/kernel.c +++ b/kernel.c @@ -19,6 +19,7 @@ void k_init(); #include "video.c" dword timer = 0; +dword pagesa = 0; void k_init() { @@ -32,18 +33,14 @@ void k_init() mm_init(); console_cls(); printf("Memory available to OS: %d MB", mm_totalmem/0x100000); - pageblock *pb = first_pageblock; +/* pageblock *pb = first_pageblock; for (;;) { if (pb->flags == MM_PB_NP) break; printf("\nBase: 0x%x; Limit: 0x%x; Flags: 0x%x, Link: 0x%x", pb->base, pb->length, pb->flags, pb->link); pb = (pageblock *) pb->link; - } - int a; - for (a = -1024; a < 778; a += 98) - printf("\n%u\t%d\t%x", a, a, a); - //printf("\n%d\t%d", -123, -23456); + } */ } void isr(dword num) @@ -51,6 +48,12 @@ void isr(dword num) if (num == 0x20) { timer++; + (*(byte *)0xb8000)++; + if ((timer%50)==0) + { + printf("\nReq: 0x%x\t Addr: 0x%x\tEntries: 0x%x\tMem: 0x%x", pagesa, (dword)mm_palloc(1024), mm_freeentries(), mm_freemem()); + pagesa++; + } eoi(); } //if (num == 0x21) diff --git a/mm.c b/mm.c index b9a276f..c0000a6 100644 --- a/mm.c +++ b/mm.c @@ -72,25 +72,146 @@ void mm_init_pageblockpage(pageblock *pbp) pb->length = 0; pb->flags = MM_PB_NP; if (a<511) - pb->link = (dword)++pb + sizeof(pageblock); + pb->link = (dword)pb + sizeof(pageblock); else pb->link = 0; + pb++; } } void *mm_palloc(dword numpages) { + pageblock *pb = first_pageblock; + dword freeentries = mm_freeentries(); + if (freeentries < 2) + { + if(mm_new_pageblock_page() == 0) + return 0; + } + pageblock *newentry = mm_nextpageblockentry(); + for (;;) + { + if ((pb->flags == MM_PB_AVAIL) && (pb->length >= numpages)) + break; + if (pb->link == 0) + return 0; + pb = (pageblock *)pb->link; + } + if (pb->length == numpages) //dont need a new entry, just mark this one as used :) + { + pb->flags = MM_PB_USED; + return pb; + } + else //subtract out allocated number of pages from length + { + newentry->base = pb->base; + newentry->length = numpages; + newentry->flags = MM_PB_USED; + pb->base += (numpages * 4096); + pb->length -= numpages; + return (void *)newentry->base; + } +} +dword mm_freeentries() +{ + pageblock *pb = first_pageblock; + dword counter = 0; + for (;;) + { + if (pb->flags == MM_PB_NP) + counter++; + if (pb->link == 0) + return counter; + pb = (pageblock *) pb->link; + } +} + + +pageblock *mm_new_pageblock_page() +{ + pageblock *pb = first_pageblock; + for (;;) + { + if ((pb->flags == MM_PB_AVAIL) && (pb->length > 1)) + { + pageblock *retval = (pageblock *)pb->base; + pb->base += 4096; + pb->length--; + mm_init_pageblockpage(retval); //zeros out, links new pageblock page + mm_lastpageblockentry()->link = (dword)retval; //set up link to new pageblock page + return retval; + } + pb = (pageblock *) pb->link; + if (pb == 0) + return 0; + } } int mm_pfree(void *ptr) { + pageblock *pb = first_pageblock; + dword tofree = (dword) ptr; + for (;;) + { + if (pb->base == tofree) + { + pb->flags = MM_PB_AVAIL; //found block, mark available / coalesce it + pageblock *pbc = first_pageblock; + for (;;) + { + } + } + if (pb->link == 0) + return 1; + pb = (pageblock *) pb->link; + } +} + +pageblock *mm_lastpageblockentry() +{ + pageblock *pb = first_pageblock; + for (;;) + { + if (pb->link == 0) + return pb; + pb = (pageblock *)pb->link; + } +} + + +pageblock *mm_nextpageblockentry() +{ + pageblock *pb = first_pageblock; + for (;;) + { + if (pb->flags == MM_PB_NP) + return pb; + if (pb->link == 0) + return 0; + pb = (pageblock *)pb->link; + } +} + + +dword mm_freemem() +{ + dword amount = 0; + pageblock *pb = first_pageblock; + for (;;) + { + if (pb->flags == MM_PB_AVAIL) + amount += (pb->length)*4096; + if (pb->link == 0) + return amount; + pb = (pageblock *) pb->link; + } } diff --git a/mm.h b/mm.h index e52f12b..9c7b6d1 100644 --- a/mm.h +++ b/mm.h @@ -19,6 +19,11 @@ void mm_init(); void mm_init_pageblockpage(pageblock *pbp); void *mm_palloc(dword numpages); int mm_pfree(void *ptr); +dword mm_freeentries(); +pageblock *mm_new_pageblock_page(); +pageblock *mm_lastpageblockentry(); +pageblock *mm_nextpageblockentry(); +dword mm_freemem(); #define MM_PB_FLAGMASK 0x03 //00000011