2006-09-06 17:50:20 +00:00
|
|
|
// 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.
|
2006-06-12 15:22:12 +00:00
|
|
|
|
|
|
|
#include "param.h"
|
|
|
|
#include "types.h"
|
|
|
|
#include "defs.h"
|
2006-07-12 11:15:38 +00:00
|
|
|
#include "param.h"
|
|
|
|
#include "mmu.h"
|
|
|
|
#include "proc.h"
|
2006-07-12 01:48:35 +00:00
|
|
|
#include "spinlock.h"
|
|
|
|
|
2006-08-10 22:08:14 +00:00
|
|
|
struct spinlock kalloc_lock;
|
2006-06-12 15:22:12 +00:00
|
|
|
|
|
|
|
struct run {
|
|
|
|
struct run *next;
|
|
|
|
int len; // bytes
|
|
|
|
};
|
|
|
|
struct run *freelist;
|
|
|
|
|
2006-09-06 17:50:20 +00:00
|
|
|
// Initialize free list of physical pages.
|
|
|
|
// This code cheats by just considering one megabyte of
|
|
|
|
// pages after _end. Real systems would determine the
|
|
|
|
// amount of memory available in the system and use it all.
|
2006-06-12 15:22:12 +00:00
|
|
|
void
|
2006-07-16 16:05:37 +00:00
|
|
|
kinit(void)
|
2006-06-12 15:22:12 +00:00
|
|
|
{
|
|
|
|
extern int end;
|
2006-07-17 01:52:13 +00:00
|
|
|
uint mem;
|
2006-06-12 15:22:12 +00:00
|
|
|
char *start;
|
2006-09-06 17:27:19 +00:00
|
|
|
|
2006-08-10 22:08:14 +00:00
|
|
|
initlock(&kalloc_lock, "kalloc");
|
2006-09-06 17:27:19 +00:00
|
|
|
start = (char*) &end;
|
|
|
|
start = (char*) (((uint)start + PAGE) & ~(PAGE-1));
|
2006-09-08 14:36:44 +00:00
|
|
|
mem = 256; // assume computer has 256 pages of RAM
|
2006-06-12 15:22:12 +00:00
|
|
|
cprintf("mem = %d\n", mem * PAGE);
|
|
|
|
kfree(start, mem * PAGE);
|
|
|
|
}
|
|
|
|
|
2006-09-07 14:12:30 +00:00
|
|
|
// Free the len bytes of memory pointed at by cp,
|
|
|
|
// which normally should have been returned by a
|
|
|
|
// call to kalloc(cp). (The exception is when
|
|
|
|
// initializing the allocator; see kinit above.)
|
2006-06-12 15:22:12 +00:00
|
|
|
void
|
|
|
|
kfree(char *cp, int len)
|
|
|
|
{
|
|
|
|
struct run **rr;
|
2006-09-06 17:27:19 +00:00
|
|
|
struct run *p = (struct run*) cp;
|
|
|
|
struct run *pend = (struct run*) (cp + len);
|
2006-07-01 21:26:01 +00:00
|
|
|
int i;
|
2006-06-12 15:22:12 +00:00
|
|
|
|
|
|
|
if(len % PAGE)
|
|
|
|
panic("kfree");
|
|
|
|
|
2006-09-06 18:06:04 +00:00
|
|
|
// Fill with junk to catch dangling refs.
|
2006-07-01 21:26:01 +00:00
|
|
|
for(i = 0; i < len; i++)
|
|
|
|
cp[i] = 1;
|
|
|
|
|
2006-07-12 01:48:35 +00:00
|
|
|
acquire(&kalloc_lock);
|
|
|
|
|
2006-06-12 15:22:12 +00:00
|
|
|
rr = &freelist;
|
|
|
|
while(*rr){
|
2006-09-06 17:27:19 +00:00
|
|
|
struct run *rend = (struct run*) ((char*)(*rr) + (*rr)->len);
|
2006-06-12 15:22:12 +00:00
|
|
|
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 01:48:35 +00:00
|
|
|
goto out;
|
2006-06-12 15:22:12 +00:00
|
|
|
}
|
|
|
|
if(pend < *rr){
|
|
|
|
p->len = len;
|
|
|
|
p->next = *rr;
|
|
|
|
*rr = p;
|
2006-07-12 01:48:35 +00:00
|
|
|
goto out;
|
2006-06-12 15:22:12 +00: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 01:48:35 +00:00
|
|
|
goto out;
|
2006-06-12 15:22:12 +00:00
|
|
|
}
|
|
|
|
rr = &((*rr)->next);
|
|
|
|
}
|
|
|
|
p->len = len;
|
|
|
|
p->next = 0;
|
|
|
|
*rr = p;
|
2006-07-12 01:48:35 +00:00
|
|
|
|
|
|
|
out:
|
|
|
|
release(&kalloc_lock);
|
2006-06-12 15:22:12 +00:00
|
|
|
}
|
|
|
|
|
2006-09-06 17:50:20 +00:00
|
|
|
// Allocate n bytes of physical memory.
|
|
|
|
// Returns a kernel-segment pointer.
|
|
|
|
// Returns 0 if the memory cannot be allocated.
|
2006-07-16 16:05:37 +00:00
|
|
|
char*
|
2006-06-12 15:22:12 +00:00
|
|
|
kalloc(int n)
|
|
|
|
{
|
|
|
|
struct run **rr;
|
|
|
|
|
|
|
|
if(n % PAGE)
|
|
|
|
panic("kalloc");
|
|
|
|
|
2006-07-12 01:48:35 +00:00
|
|
|
acquire(&kalloc_lock);
|
|
|
|
|
2006-06-12 15:22:12 +00:00
|
|
|
rr = &freelist;
|
|
|
|
while(*rr){
|
|
|
|
struct run *r = *rr;
|
|
|
|
if(r->len == n){
|
|
|
|
*rr = r->next;
|
2006-07-12 01:48:35 +00:00
|
|
|
release(&kalloc_lock);
|
2006-09-06 17:27:19 +00:00
|
|
|
return (char*) r;
|
2006-06-12 15:22:12 +00:00
|
|
|
}
|
|
|
|
if(r->len > n){
|
2006-09-06 17:27:19 +00:00
|
|
|
char *p = (char*)r + (r->len - n);
|
2006-06-12 15:22:12 +00:00
|
|
|
r->len -= n;
|
2006-07-12 01:48:35 +00:00
|
|
|
release(&kalloc_lock);
|
2006-06-12 15:22:12 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
rr = &(*rr)->next;
|
|
|
|
}
|
2006-07-12 01:48:35 +00:00
|
|
|
release(&kalloc_lock);
|
2006-08-30 18:55:06 +00:00
|
|
|
cprintf("kalloc: out of memory\n");
|
2006-06-12 15:22:12 +00:00
|
|
|
return 0;
|
|
|
|
}
|