add heap alloc function

Physically contiguous memory
This commit is contained in:
Hojun-Cho 2024-12-12 14:06:06 +09:00
parent a9a1ad666d
commit e74cf22714
5 changed files with 82 additions and 2 deletions

View File

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

67
boot/alloc.c Normal file
View File

@ -0,0 +1,67 @@
#include <u.h>
#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);
}

View File

@ -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(;;){

View File

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

View File

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