diff --git a/kernel.c b/kernel.c index 1e38709..f58fcd6 100644 --- a/kernel.c +++ b/kernel.c @@ -19,7 +19,10 @@ void k_init(); #include "video.c" dword timer = 0; -dword pagesa = 0; +word index = 0; +dword ptrs[512]; +dword goon = 1; +dword req = 0; void k_init() { @@ -28,9 +31,12 @@ void k_init() outportb(0x43, 0x34); outportb(0x40, 0x9c); //lsb outportb(0x40, 0x2e); //msb - enable_ints(); - video_init((ModeInfoBlock *) 0x90306); +// video_init((ModeInfoBlock *) 0x90306); mm_init(); + int a; + for (a=0; a<512; a++) + ptrs[a] = 0; + enable_ints(); console_cls(); printf("Memory available to OS: %d MB", mm_totalmem/0x100000); /* pageblock *pb = first_pageblock; @@ -51,8 +57,28 @@ void isr(dword num) (*(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++; + if (goon) + { + if(ptrs[index] = (dword)mm_palloc(1024)) + printf("\nReq:%d\tAddr: %x\tEntries: %x\tMem: %x\tIndex: %x", req++, ptrs[index++], mm_freeentries(), mm_freemem(), index); + else + { + goon = 0; + index = 0; + } + } + else + { + dword reslt = mm_pfree((void *)ptrs[index]); //zero returned means chunk freed successfully + printf("\nReq:%d\tFree(): %x\tEntries: %x\tMem: %x\tIndex: %x", req++, reslt, mm_freeentries(), mm_freemem(), index++); + if (reslt) + { + for (index=0; index<512; index++) + ptrs[index] = 0; + index = 0; + goon = 1; + } + } } eoi(); } diff --git a/mm.c b/mm.c index c0000a6..53bd308 100644 --- a/mm.c +++ b/mm.c @@ -157,14 +157,27 @@ int mm_pfree(void *ptr) dword tofree = (dword) ptr; for (;;) { - if (pb->base == tofree) + if ((pb->base == tofree) && (pb->flags == MM_PB_USED)) { pb->flags = MM_PB_AVAIL; //found block, mark available / coalesce it pageblock *pbc = first_pageblock; for (;;) { - - + if ((pbc->base + (pbc->length * 4096)) == pb->base) //pbc ends where pb starts + { + pbc->length += pb->length; //extend pbc's length by pb's length + pb->flags = MM_PB_NP; //mark pb as an unused entry + return 0; + } + if ((pb->base + (pb->length * 4096)) == pbc->base) //pb ends where pb starts + { + pb->length += pbc->length; + pbc->flags = MM_PB_NP; + return 0; + } + if (pbc->link == 0) //no entry found to consolidate... + return 0; + pbc = (pageblock *) pbc->link; //next entry to test } } if (pb->link == 0)