2006-06-12 17:22:12 +02:00
|
|
|
/*
|
|
|
|
* physical memory allocator, intended to be used to allocate
|
|
|
|
* memory for user processes. allocates in 4096-byte "pages".
|
|
|
|
* free list is 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "param.h"
|
|
|
|
#include "types.h"
|
|
|
|
#include "defs.h"
|
2006-07-12 13:15:38 +02:00
|
|
|
#include "param.h"
|
|
|
|
#include "mmu.h"
|
|
|
|
#include "proc.h"
|
2006-07-12 03:48:35 +02:00
|
|
|
#include "spinlock.h"
|
|
|
|
|
2006-08-11 00:08:14 +02:00
|
|
|
struct spinlock kalloc_lock;
|
2006-06-12 17:22:12 +02:00
|
|
|
|
|
|
|
struct run {
|
|
|
|
struct run *next;
|
|
|
|
int len; // bytes
|
|
|
|
};
|
|
|
|
struct run *freelist;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* initialize free list of physical pages. this code
|
|
|
|
* cheats by just considering the one megabyte of pages
|
|
|
|
* after _end.
|
|
|
|
*/
|
|
|
|
void
|
2006-07-16 18:05:37 +02:00
|
|
|
kinit(void)
|
2006-06-12 17:22:12 +02:00
|
|
|
{
|
|
|
|
extern int end;
|
2006-07-17 03:52:13 +02:00
|
|
|
uint mem;
|
2006-06-12 17:22:12 +02:00
|
|
|
char *start;
|
|
|
|
|
2006-08-11 00:08:14 +02:00
|
|
|
initlock(&kalloc_lock, "kalloc");
|
2006-06-12 17:22:12 +02:00
|
|
|
start = (char *) &end;
|
2006-07-17 03:52:13 +02:00
|
|
|
start = (char *) (((uint)start + PAGE) & ~(PAGE-1));
|
2006-09-04 13:52:36 +02:00
|
|
|
mem = 256; // assume 256 pages of RAM
|
2006-06-12 17:22:12 +02:00
|
|
|
cprintf("mem = %d\n", mem * PAGE);
|
|
|
|
kfree(start, mem * PAGE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
kfree(char *cp, int len)
|
|
|
|
{
|
|
|
|
struct run **rr;
|
|
|
|
struct run *p = (struct run *) cp;
|
|
|
|
struct run *pend = (struct run *) (cp + len);
|
2006-07-01 23:26:01 +02:00
|
|
|
int i;
|
2006-06-12 17:22:12 +02:00
|
|
|
|
|
|
|
if(len % PAGE)
|
|
|
|
panic("kfree");
|
|
|
|
|
2006-07-01 23:26:01 +02:00
|
|
|
// XXX fill with junk to help debug
|
|
|
|
for(i = 0; i < len; i++)
|
|
|
|
cp[i] = 1;
|
|
|
|
|
2006-07-12 03:48:35 +02:00
|
|
|
acquire(&kalloc_lock);
|
|
|
|
|
2006-06-12 17:22:12 +02:00
|
|
|
rr = &freelist;
|
|
|
|
while(*rr){
|
|
|
|
struct run *rend = (struct run *) ((char *)(*rr) + (*rr)->len);
|
|
|
|
if(p >= *rr && p < rend)
|
|
|
|
panic("freeing free page");
|
|
|
|
if(pend == *rr){
|
|
|
|
p->len = len + (*rr)->len;
|
|
|
|
p->next = (*rr)->next;
|
|
|
|
*rr = p;
|
2006-07-12 03:48:35 +02:00
|
|
|
goto out;
|
2006-06-12 17:22:12 +02:00
|
|
|
}
|
|
|
|
if(pend < *rr){
|
|
|
|
p->len = len;
|
|
|
|
p->next = *rr;
|
|
|
|
*rr = p;
|
2006-07-12 03:48:35 +02:00
|
|
|
goto out;
|
2006-06-12 17:22:12 +02:00
|
|
|
}
|
|
|
|
if(p == rend){
|
|
|
|
(*rr)->len += len;
|
|
|
|
if((*rr)->next && (*rr)->next == pend){
|
|
|
|
(*rr)->len += (*rr)->next->len;
|
|
|
|
(*rr)->next = (*rr)->next->next;
|
|
|
|
}
|
2006-07-12 03:48:35 +02:00
|
|
|
goto out;
|
2006-06-12 17:22:12 +02:00
|
|
|
}
|
|
|
|
rr = &((*rr)->next);
|
|
|
|
}
|
|
|
|
p->len = len;
|
|
|
|
p->next = 0;
|
|
|
|
*rr = p;
|
2006-07-12 03:48:35 +02:00
|
|
|
|
|
|
|
out:
|
|
|
|
release(&kalloc_lock);
|
2006-06-12 17:22:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* allocate n bytes of physical memory.
|
|
|
|
* returns a kernel-segment pointer.
|
|
|
|
* returns 0 if there's no run that's big enough.
|
|
|
|
*/
|
2006-07-16 18:05:37 +02:00
|
|
|
char*
|
2006-06-12 17:22:12 +02:00
|
|
|
kalloc(int n)
|
|
|
|
{
|
|
|
|
struct run **rr;
|
|
|
|
|
|
|
|
if(n % PAGE)
|
|
|
|
panic("kalloc");
|
|
|
|
|
2006-07-12 03:48:35 +02:00
|
|
|
acquire(&kalloc_lock);
|
|
|
|
|
2006-06-12 17:22:12 +02:00
|
|
|
rr = &freelist;
|
|
|
|
while(*rr){
|
|
|
|
struct run *r = *rr;
|
|
|
|
if(r->len == n){
|
|
|
|
*rr = r->next;
|
2006-07-12 03:48:35 +02:00
|
|
|
release(&kalloc_lock);
|
2006-06-12 17:22:12 +02:00
|
|
|
return (char *) r;
|
|
|
|
}
|
|
|
|
if(r->len > n){
|
|
|
|
char *p = (char *)r + (r->len - n);
|
|
|
|
r->len -= n;
|
2006-07-12 03:48:35 +02:00
|
|
|
release(&kalloc_lock);
|
2006-06-12 17:22:12 +02:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
rr = &(*rr)->next;
|
|
|
|
}
|
2006-07-12 03:48:35 +02:00
|
|
|
release(&kalloc_lock);
|
2006-08-30 20:55:06 +02:00
|
|
|
cprintf("kalloc: out of memory\n");
|
2006-06-12 17:22:12 +02:00
|
|
|
return 0;
|
|
|
|
}
|