kalloc是用于分配物理内存,比较简单,but to make it clear,这里还是做一个笔记
struct run {
struct run *next;
};
struct {
struct spinlock lock;
struct run *freelist;
} kmem;
首先这里定义了两个结构体,但是有一些细节需要注意:第一个结构体有名字,struct run,第二个没有,第二个的kmem是其定义的第一个结构体变量
在系统启动时,会调用kinit,初始化这个内存分配器
void
kinit()
{
initlock(&kmem.lock, "kmem");
freerange(end, (void*)PHYSTOP);
}
void
freerange(void *pa_start, void *pa_end)
{
char *p;
p = (char*)PGROUNDUP((uint64)pa_start);
for(; p + PGSIZE <= (char*)pa_end; p += PGSIZE)
kfree(p);
}
// Free the page of physical memory pointed at by v,
// which normally should have been returned by a
// call to kalloc(). (The exception is when
// initializing the allocator; see kinit above.)
void
kfree(void *pa)
{
struct run *r;
if(((uint64)pa % PGSIZE) != 0 || (char*)pa < end || (ui