diff --git a/src/sys/include/ubixos/kmalloc.h b/src/sys/include/ubixos/kmalloc.h index 5e69750..0b9000c 100755 --- a/src/sys/include/ubixos/kmalloc.h +++ b/src/sys/include/ubixos/kmalloc.h @@ -27,6 +27,6 @@ #include void *kmalloc(uInt len); -void kfree(void *); +void kfree(void *baseAddr); #endif diff --git a/src/sys/init/main.c b/src/sys/init/main.c index 7a68402..7977ac1 100755 --- a/src/sys/init/main.c +++ b/src/sys/init/main.c @@ -78,6 +78,7 @@ } int main() { + char *test; clearScreen(); outputVersion(); //Display Version Info init8259(); //Initialize PIC @@ -88,6 +89,17 @@ initScheduler(); //Initialize Scheduler initFloppy(); //Initialize Floppy Controller initUbixFS(); + test = kmalloc(sizeof(char)); + test[0] = 'c'; + kprintf("[%i][%i]\n",test,*test); + test = kmalloc(sizeof(char)); + kprintf("[%i][%i]\n",test,*test); + test[0] = 'b'; + kprintf("[%i][%i]\n",test,*test); + kfree(test); + test = kmalloc(sizeof(char)); + kprintf("[%i][%i]\n",test,*test); + while(1); execThread(idleThread,0xAFFF,"Idle Thread"); execFile("init"); enableIrq(0); diff --git a/src/sys/kernel/kmalloc.c b/src/sys/kernel/kmalloc.c index 5f8c404..63c3746 100755 --- a/src/sys/kernel/kmalloc.c +++ b/src/sys/kernel/kmalloc.c @@ -22,14 +22,91 @@ **************************************************************************************/ #include +#include + +struct memDescriptor { + void *page; + void *base; + uShort limit; + uShort counter; + struct memDescriptor *next; + }; + +struct memDescriptor *kernDesc = 0x0; + +void initMalloc() { + int i = 0; + struct memDescriptor *tmpDesc = 0x0; + kernDesc = allocPage(); + tmpDesc = kernDesc; + for (i=0;i<4096;i+=16) { + tmpDesc->page = 0x0; + tmpDesc->base = 0x0; + tmpDesc->limit = 0x0; + tmpDesc->counter = 0x0; + if (i == 4096) { + tmpDesc->next = 0x0; + } + else { + tmpDesc->next = tmpDesc+16; + } + tmpDesc = tmpDesc->next; + } + } + +void *findFreeDesc() { + struct memDescriptor *findDesc = 0x0; + for (findDesc=(struct memDescriptor *)kernDesc;findDesc;findDesc=(struct memDescriptor *)findDesc->next) { + if (findDesc->base == 0) { + return(findDesc); + } + } + return(0); + } void *kmalloc(uInt len) { void *ret; - + struct memDescriptor *tmpDesc = 0x0,*newDesc = 0x0; + if (!kernDesc) { + initMalloc(); + } + for(tmpDesc=kernDesc;tmpDesc;tmpDesc=tmpDesc->next) { + if (tmpDesc->counter == 0) { + if (tmpDesc->limit >= len) { + if (tmpDesc->limit > len+4) { + newDesc = findFreeDesc(); + newDesc->base = tmpDesc->base+tmpDesc->limit + 1; + newDesc->limit = tmpDesc->limit - len; + } + ret = tmpDesc->base; + break; + } + if (tmpDesc->page == 0) { + tmpDesc->page = allocPage(); + tmpDesc->base = tmpDesc->page; + tmpDesc->limit = len; + tmpDesc->counter = 1; + newDesc = findFreeDesc(); + newDesc->page = tmpDesc->page; + newDesc->base = tmpDesc->base + tmpDesc->limit + 1; + newDesc->limit = 4096 - tmpDesc->limit; + ret = tmpDesc->base; + break; + } + } + } return(ret); } + -void kfree(void * addr) -{ - return; -} +void kfree(void *baseAddr) { + struct memDescriptor *tmpDesc = 0x0; + for (tmpDesc=kernDesc;tmpDesc;tmpDesc=tmpDesc->next) { + if (tmpDesc->base == baseAddr) { + tmpDesc->counter = 0; + return; + } + } + kprintf("Error Freeing!!!\n"); + return; + }