diff --git a/boot/Makefile b/boot/Makefile index 5957202..df574c4 100644 --- a/boot/Makefile +++ b/boot/Makefile @@ -5,7 +5,7 @@ PROG = boot SRCS = srt0.S gdt.S boot.c OBJS = srt0.o gdt.o boot.o -ESRCS = pccon.c sricon.c print.c fmt.c a20.c time.c mem.c cpu.c +ESRCS = pccon.c sricon.c print.c fmt.c a20.c time.c mem.c cpu.c alloc.c OBJS += $(ESRCS:.c=.o) include $(SDIR)/Makefile.inc diff --git a/boot/alloc.c b/boot/alloc.c new file mode 100644 index 0000000..20a92a8 --- /dev/null +++ b/boot/alloc.c @@ -0,0 +1,67 @@ +#include +#include "dat.h" +#include "fn.h" + +// only for booting + +typedef struct FreeList FreeList; +static struct FreeList{ + uint size; + FreeList *next; +} *freed; + +extern char end[]; +static char *top = end; + +static void +putfreed(FreeList *p) +{ + p->next = nil; + if(freed){ + p->next = freed; + freed = p; + }else + freed = p; +} + +static FreeList* +getfreed(uint n) +{ + for(FreeList *p=freed,*l=freed; p; p=p->next){ + if(p->size >= n){ + l->next = p->next; + if(p == freed) + freed = nil; + return p; + } + l = p; + } + return nil; +} + +void* +alloc(uint n) +{ + FreeList *p; + + p = getfreed(n); + if(p == nil){ + p = (FreeList*)top; + p->size = n; + p->next = nil; + memset(p+sizeof(*p), 0, n); + top += n + sizeof(*p); + } +if(debug) print("alloc %x %ud\n", &p[1], n); + return &p[1]; +} + +void +free(void *src) +{ + FreeList *p; + + p = (FreeList*)((char*)src-sizeof(FreeList)); +if(debug) print("free %x %ud\n", src, p->size); + putfreed(p); +} \ No newline at end of file diff --git a/boot/boot.c b/boot/boot.c index 2c2db99..8517d59 100644 --- a/boot/boot.c +++ b/boot/boot.c @@ -36,6 +36,7 @@ ConDev contab[CON_END] = { ConDev *con = &contab[0]; BIOSmmap biosmmap[64]; uint cnvmem, extmem; +int debug = BOOT_DEBUG; static void coninit(void) @@ -96,7 +97,8 @@ done: void boot(int bootdev) { - print("\n\n===> Hello world <===\n\tBooted on disk 0x%x\n", bootdev); + print("\n===> Hello world <===\n\t" + "Booted on disk 0x%x debug:%d\n\t", bootdev, debug); machdep(); for(;;){ diff --git a/boot/dat.h b/boot/dat.h index dedfe79..8a6ebb5 100644 --- a/boot/dat.h +++ b/boot/dat.h @@ -3,6 +3,9 @@ // 0x64 Read Status Register // 0x64 Write Command Register #define TABWIDTH 8 +#ifndef BOOT_DEBUG +#define BOOT_DEBUG 1 +#endif enum{ CON_PC, @@ -64,3 +67,4 @@ extern BIOSmmap biosmmap[64]; extern ConDev contab[CON_END]; extern ConDev *con; extern uint cnvmem, extmem; +extern int debug; \ No newline at end of file diff --git a/boot/fn.h b/boot/fn.h index 300cb28..d06fe2f 100644 --- a/boot/fn.h +++ b/boot/fn.h @@ -35,6 +35,13 @@ void cpuidprobe(void); // time.c long getsecs(void); +// alloc.c +void *alloc(uint); +void free(void*); + +// util +void* memset(void *dst, int v, int l); + static __inline int major(int x) {