From 82da40c9f05f2fa306e5a112abbc80bb2bdd5ff2 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Mon, 29 Sep 2003 22:00:00 -0400 Subject: [PATCH] Import backup from 2003-09-29 --- desc.txt | 1 + k_defines.h | 2 ++ kernel.c | 40 ++++++++++++++++++------- mm.c | 85 ++++++++++++++++++++++++++++++----------------------- mm.h | 7 ++--- 5 files changed, 84 insertions(+), 51 deletions(-) create mode 100644 desc.txt diff --git a/desc.txt b/desc.txt new file mode 100644 index 0000000..0fa68d3 --- /dev/null +++ b/desc.txt @@ -0,0 +1 @@ +mm_palloc() and mm_pfree() working, coalescing successfully!! \ No newline at end of file diff --git a/k_defines.h b/k_defines.h index ee8da76..9621cbe 100644 --- a/k_defines.h +++ b/k_defines.h @@ -30,6 +30,8 @@ #define FREERAM_START 0x268000 +#define KERNEL_PID 0x02 + typedef unsigned char byte; typedef unsigned short word; typedef unsigned int dword; diff --git a/kernel.c b/kernel.c index 63584c8..0a0651c 100644 --- a/kernel.c +++ b/kernel.c @@ -23,8 +23,10 @@ void k_init(); #include "functions.c" #include "video.c" -dword timer = 0; -dword addresses[256]; +dword timer = 0; +dword index = 0; +dword keepgoing = 1; +dword addresses[256]; void k_init() { @@ -45,14 +47,6 @@ void k_init() enable_ints(); console_cls(); printf("Memory available to OS: %d MB (Bytes: %d)\n", mm_totalmem/0x100000, mm_totalmem); - int a = 0; - for (;;) - { - addresses[a] = (dword)mm_palloc(256); - printf("%x\t", addresses[a]); - if (addresses[a++] == 0) - break; - } dword key = 0; for (;;) { @@ -68,6 +62,32 @@ void isr(dword num) case 0x20: // IRQ0 - timer interrupt timer++; (*(byte *)0xb8000)++; + if ((timer%25)==0) + { + if (keepgoing) + { + addresses[index] = (dword) mm_palloc(256, KERNEL_PID); + printf("Addr: 0x%x\tEntries:\t%d\n", addresses[index], mm_freeentries()); + if (addresses[index] == 0) + { + index = -1; + keepgoing = 0; + } + index++; + } + else + { + printf("Freeing %x\t", addresses[index]); + addresses[index] = mm_pfree((void *)addresses[index]); + printf("Free:\t%d\tEntries:\t%d\n", addresses[index], mm_freeentries()); + if (addresses[index] != 0) + { + index = -1; + keepgoing = 1; + } + index++; + } + } eoi(); break; case 0x21: // IRQ1 - keyboard interrupt diff --git a/mm.c b/mm.c index 810a13b..ac3d9a2 100644 --- a/mm.c +++ b/mm.c @@ -32,21 +32,21 @@ void mm_init() mm_init_pageblockpage(first_pageblock); first_pageblock->base = maps[a].base.lowdword; first_pageblock->length = maps[a].limit.lowdword / 4096; - first_pageblock->flags = MM_PB_AVAIL; + first_pageblock->pid = MM_PB_AVAIL; } else //first_pageblock already set up, add on segment { pageblock *pb = first_pageblock; for (;;) { - if (pb->flags == MM_PB_NP) + if (pb->pid == MM_PB_NP) break; else pb++; } pb->base = maps[a].base.lowdword; pb->length = maps[a].limit.lowdword / 4096; - pb->flags = MM_PB_AVAIL; + pb->pid = MM_PB_AVAIL; } } } @@ -71,7 +71,7 @@ void mm_init_pageblockpage(pageblock *pbp) { pbp[a].base = 0; pbp[a].length = 0; - pbp[a].flags = MM_PB_NP; + pbp[a].pid = MM_PB_NP; if (a<255) pbp[a].link = (dword)(&pbp[a+1]); else @@ -80,8 +80,10 @@ void mm_init_pageblockpage(pageblock *pbp) } -void *mm_palloc(dword numpages) +void *mm_palloc(dword numpages, dword proc_pid) { + if (proc_pid < KERNEL_PID) + return 0; pageblock *pb = first_pageblock; if (mm_freeentries() < 2) { @@ -91,7 +93,7 @@ void *mm_palloc(dword numpages) pageblock *newentry = mm_nextpageblockentry(); for (;;) { - if ((pb->flags == MM_PB_AVAIL) && (pb->length >= numpages)) + if ((pb->pid == MM_PB_AVAIL) && (pb->length >= numpages)) break; if (pb->link == 0) return 0; @@ -99,14 +101,14 @@ void *mm_palloc(dword numpages) } if (pb->length == numpages) //dont need a new entry, just mark this one as used :) { - pb->flags = MM_PB_USED; - return pb; + pb->pid = proc_pid; + return (void *)pb->base; } else //subtract out allocated number of pages from length { newentry->base = pb->base; newentry->length = numpages; - newentry->flags = MM_PB_USED; + newentry->pid = proc_pid; pb->base += (numpages * 4096); pb->length -= numpages; return (void *)newentry->base; @@ -120,7 +122,7 @@ dword mm_freeentries() dword counter = 0; for (;;) { - if (pb->flags == MM_PB_NP) + if (pb->pid == MM_PB_NP) counter++; if (pb->link == 0) return counter; @@ -134,7 +136,7 @@ pageblock *mm_new_pageblock_page() //as of 09/26/03 this method leaks 4kb main pageblock *pb = first_pageblock; for (;;) { - if ((pb->flags == MM_PB_AVAIL) && (pb->length > 1)) + if ((pb->pid == MM_PB_AVAIL) && (pb->length > 1)) { pageblock *retval = (pageblock *)pb->base; pb->base += 4096; @@ -158,31 +160,11 @@ int mm_pfree(void *ptr) dword tofree = (dword) ptr; for (;;) { - if (pb->base == tofree) // && (pb->flags == MM_PB_USED)) + if (pb->base == tofree) // && (pb->pid == MM_PB_USED)) { - pb->flags = MM_PB_AVAIL; //found block, mark available / coalesce it - pageblock *pbc = first_pageblock; - for (;;) - { - if (pbc->flags == MM_PB_AVAIL) - { - 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 - } + pb->pid = MM_PB_AVAIL; //found block, mark available / coalesce it + mm_coalesce(pb); + return 0; } if (pb->link == 0) return 1; @@ -208,7 +190,7 @@ pageblock *mm_nextpageblockentry() pageblock *pb = first_pageblock; for (;;) { - if (pb->flags == MM_PB_NP) + if (pb->pid == MM_PB_NP) return pb; if (pb->link == 0) return 0; @@ -223,7 +205,7 @@ dword mm_freemem() pageblock *pb = first_pageblock; for (;;) { - if (pb->flags == MM_PB_AVAIL) + if (pb->pid == MM_PB_AVAIL) amount += (pb->length)*4096; if (pb->link == 0) return amount; @@ -232,6 +214,35 @@ dword mm_freemem() } +void mm_coalesce(pageblock *pb) +{ + pageblock *pbc = first_pageblock; + for (;;) + { + if (pbc->pid == pb->pid) //pid fields must match, both same process, both avail, or both not present + { + if ((pbc->base + (pbc->length * 4096)) == pb->base) //pbc ends where pb starts + { + pbc->length += pb->length; + pb->pid = MM_PB_NP; + mm_coalesce(pbc); //recursion: checks for any more page blocks to coalesce if one found + return; //exit this recursion... + } + if ((pb->base + (pb->length * 4096)) == pbc->base) //pb ends where pbc starts + { + pb->length += pbc->length; + pbc->pid = MM_PB_NP; + mm_coalesce(pb); + return; + } + } + if (pbc->link == 0) + return; + pbc = (pageblock *)pbc->link; + } +} + + diff --git a/mm.h b/mm.h index a1ce0b7..34d3343 100644 --- a/mm.h +++ b/mm.h @@ -10,14 +10,15 @@ typedef struct { typedef struct { dword base; dword length; //in pages - dword flags; + dword pid; //normally PID of process, use NP, AVAIL for special flags dword link; //leave dword instead of pointer so i dont have to worry about pointer arithmetic } __attribute__ ((packed)) pageblock; //16 byte pageblock entry - 256 entries = 1 page void mm_init(); void mm_init_pageblockpage(pageblock *pbp); -void *mm_palloc(dword numpages); +void *mm_palloc(dword numpages, dword proc_pid); +void mm_coalesce(pageblock *pb); int mm_pfree(void *ptr); dword mm_freeentries(); dword mm_freemem(); @@ -26,10 +27,8 @@ pageblock *mm_lastpageblockentry(); pageblock *mm_nextpageblockentry(); -#define MM_PB_FLAGMASK 0x03 //00000011 #define MM_PB_NP 0x00 //00000000 #define MM_PB_AVAIL 0x01 //00000001 -#define MM_PB_USED 0x02 //00000010