Import backup from 2003-09-29

This commit is contained in:
Josh Holtrop 2003-09-29 22:00:00 -04:00
parent 8d5b28f823
commit 82da40c9f0
5 changed files with 84 additions and 51 deletions

1
desc.txt Normal file
View File

@ -0,0 +1 @@
mm_palloc() and mm_pfree() working, coalescing successfully!!

View File

@ -30,6 +30,8 @@
#define FREERAM_START 0x268000
#define KERNEL_PID 0x02
typedef unsigned char byte;
typedef unsigned short word;
typedef unsigned int dword;

View File

@ -24,6 +24,8 @@ void k_init();
#include "video.c"
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

83
mm.c
View File

@ -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,32 +160,12 @@ 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
pb->pid = MM_PB_AVAIL; //found block, mark available / coalesce it
mm_coalesce(pb);
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)
return 1;
pb = (pageblock *) pb->link;
@ -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;
}
}

7
mm.h
View File

@ -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