Newer
Older
// Physical memory allocator, intended to allocate
// memory for user processes. Allocates in 4096-byte "pages".
// Free list is kept sorted and combines adjacent pages into
// long runs, to make it easier to allocate big segments.
// One reason the page size is 4k is that the x86 segment size
// granularity is 4k.
// Initialize free list of physical pages.
// This code cheats by just considering one megabyte of
// pages after end. Real systems would determine the
extern char end[];
uint len;
char *p;
p = (char*)(((uint)end + PAGE) & ~(PAGE-1));
len = 256*PAGE; // assume computer has 256 pages of RAM, 1 MB
cprintf("mem = %d\n", len);
kfree(p, len);
rend = (struct run*)((char*)r + r->len);
if(r <= p && p < rend)
if(rend == p){ // r before p: expand r to include p
r->len += len;
if(r->next && r->next == pend){ // r now next to r->next?
r->len += r->next->len;
r->next = r->next->next;
if(pend == r){ // p before r: expand p to include, replace r
p->len = len + r->len;
p->next = r->next;
*rp = p;
goto out;
}
// Allocate n bytes of physical memory.
// Returns a kernel-segment pointer.
// Returns 0 if the memory cannot be allocated.