kalloc/kfree now only a page at a time
do not keep sorted contiguous free list
This commit is contained in:
parent
81b30b14d6
commit
7d7dc9331b
5
defs.h
5
defs.h
|
@ -60,9 +60,8 @@ extern uchar ioapicid;
|
||||||
void ioapicinit(void);
|
void ioapicinit(void);
|
||||||
|
|
||||||
// kalloc.c
|
// kalloc.c
|
||||||
extern int nfreemem;
|
char* kalloc(void);
|
||||||
char* kalloc(int);
|
void kfree(char*);
|
||||||
void kfree(char*, int);
|
|
||||||
void kinit(char*,uint);
|
void kinit(char*,uint);
|
||||||
|
|
||||||
// kbd.c
|
// kbd.c
|
||||||
|
|
89
kalloc.c
89
kalloc.c
|
@ -1,8 +1,6 @@
|
||||||
// Physical memory allocator, intended to allocate
|
// Physical memory allocator, intended to allocate
|
||||||
// memory for user processes. Allocates in 4096-byte pages.
|
// memory for user processes, kernel stacks, page table pages,
|
||||||
// Free list is kept sorted and combines adjacent pages into
|
// and pipe buffers. Allocates 4096-byte pages.
|
||||||
// long runs, to make it easier to allocate big segments.
|
|
||||||
// This combining is not useful now that xv6 uses paging.
|
|
||||||
|
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
#include "defs.h"
|
#include "defs.h"
|
||||||
|
@ -12,7 +10,6 @@
|
||||||
|
|
||||||
struct run {
|
struct run {
|
||||||
struct run *next;
|
struct run *next;
|
||||||
int len; // bytes
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct {
|
struct {
|
||||||
|
@ -20,92 +17,52 @@ struct {
|
||||||
struct run *freelist;
|
struct run *freelist;
|
||||||
} kmem;
|
} kmem;
|
||||||
|
|
||||||
int nfreemem;
|
|
||||||
|
|
||||||
// Initialize free list of physical pages.
|
// Initialize free list of physical pages.
|
||||||
void
|
void
|
||||||
kinit(char *p, uint len)
|
kinit(char *p, uint len)
|
||||||
{
|
{
|
||||||
initlock(&kmem.lock, "kmem");
|
initlock(&kmem.lock, "kmem");
|
||||||
nfreemem = 0;
|
char *p1 = (char*)PGROUNDUP((uint)p);
|
||||||
kfree(p, len);
|
char *p2 = PGROUNDDOWN(p + len);
|
||||||
|
for( ; p1 < p2; p1 += 4096)
|
||||||
|
kfree(p1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Free the len bytes of memory pointed at by v,
|
// Free the page of physical memory pointed at by v,
|
||||||
// which normally should have been returned by a
|
// which normally should have been returned by a
|
||||||
// call to kalloc(len). (The exception is when
|
// call to kalloc(). (The exception is when
|
||||||
// initializing the allocator; see kinit above.)
|
// initializing the allocator; see kinit above.)
|
||||||
void
|
void
|
||||||
kfree(char *v, int len)
|
kfree(char *v)
|
||||||
{
|
{
|
||||||
struct run *r, *rend, **rp, *p, *pend;
|
struct run *r;
|
||||||
|
|
||||||
if(len <= 0 || len % PGSIZE)
|
if(((uint) v) % PGSIZE || (uint)v < 1024*1024 || (uint)v >= PHYSTOP)
|
||||||
panic("kfree");
|
panic("kfree");
|
||||||
|
|
||||||
// Fill with junk to catch dangling refs.
|
// Fill with junk to catch dangling refs.
|
||||||
memset(v, 1, len);
|
memset(v, 1, PGSIZE);
|
||||||
|
|
||||||
acquire(&kmem.lock);
|
acquire(&kmem.lock);
|
||||||
nfreemem += len;
|
r = (struct run *) v;
|
||||||
p = (struct run*)v;
|
r->next = kmem.freelist;
|
||||||
pend = (struct run*)(v + len);
|
kmem.freelist = r;
|
||||||
for(rp=&kmem.freelist; (r=*rp) != 0 && r <= pend; rp=&r->next){
|
|
||||||
rend = (struct run*)((char*)r + r->len);
|
|
||||||
if(r <= p && p < rend) {
|
|
||||||
cprintf("freeing a free page: r = 0x%x p = 0x%x rend = 0x%x\n",
|
|
||||||
r, p, rend);
|
|
||||||
panic("freeing free page");
|
|
||||||
}
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Insert p before r in list.
|
|
||||||
p->len = len;
|
|
||||||
p->next = r;
|
|
||||||
*rp = p;
|
|
||||||
|
|
||||||
out:
|
|
||||||
release(&kmem.lock);
|
release(&kmem.lock);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Allocate n bytes of physical memory.
|
// Allocate one 4096-byte page of physical memory.
|
||||||
// Returns a kernel-segment pointer.
|
// Returns a pointer that the kernel can use.
|
||||||
// Returns 0 if the memory cannot be allocated.
|
// Returns 0 if the memory cannot be allocated.
|
||||||
char*
|
char*
|
||||||
kalloc(int n)
|
kalloc()
|
||||||
{
|
{
|
||||||
char *p;
|
struct run *r;
|
||||||
struct run *r, **rp;
|
|
||||||
|
|
||||||
if(n % PGSIZE || n <= 0)
|
|
||||||
panic("kalloc");
|
|
||||||
|
|
||||||
acquire(&kmem.lock);
|
acquire(&kmem.lock);
|
||||||
for(rp=&kmem.freelist; (r=*rp) != 0; rp=&r->next){
|
r = kmem.freelist;
|
||||||
if(r->len >= n){
|
if(r)
|
||||||
r->len -= n;
|
kmem.freelist = r->next;
|
||||||
p = (char*)r + r->len;
|
|
||||||
if(r->len == 0)
|
|
||||||
*rp = r->next;
|
|
||||||
nfreemem -= n;
|
|
||||||
release(&kmem.lock);
|
|
||||||
return p;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
release(&kmem.lock);
|
release(&kmem.lock);
|
||||||
return 0;
|
return (char*) r;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
4
main.c
4
main.c
|
@ -28,7 +28,7 @@ main(void)
|
||||||
void
|
void
|
||||||
jkstack(void)
|
jkstack(void)
|
||||||
{
|
{
|
||||||
char *kstack = kalloc(PGSIZE);
|
char *kstack = kalloc();
|
||||||
if (!kstack)
|
if (!kstack)
|
||||||
panic("jkstack\n");
|
panic("jkstack\n");
|
||||||
char *top = kstack + PGSIZE;
|
char *top = kstack + PGSIZE;
|
||||||
|
@ -92,7 +92,7 @@ bootothers(void)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
// Fill in %esp, %eip and start code on cpu.
|
// Fill in %esp, %eip and start code on cpu.
|
||||||
stack = kalloc(KSTACKSIZE);
|
stack = kalloc();
|
||||||
*(void**)(code-4) = stack + KSTACKSIZE;
|
*(void**)(code-4) = stack + KSTACKSIZE;
|
||||||
*(void**)(code-8) = mpmain;
|
*(void**)(code-8) = mpmain;
|
||||||
lapicstartap(c->id, (uint)code);
|
lapicstartap(c->id, (uint)code);
|
||||||
|
|
2
mmu.h
2
mmu.h
|
@ -112,7 +112,7 @@ struct segdesc {
|
||||||
#define PDXSHIFT 22 // offset of PDX in a linear address
|
#define PDXSHIFT 22 // offset of PDX in a linear address
|
||||||
|
|
||||||
#define PGROUNDUP(sz) (((sz)+PGSIZE-1) & ~(PGSIZE-1))
|
#define PGROUNDUP(sz) (((sz)+PGSIZE-1) & ~(PGSIZE-1))
|
||||||
#define PGROUNDDOWN(a) ((char*)((((unsigned int)a) & ~(PGSIZE-1))))
|
#define PGROUNDDOWN(a) ((char*)((((unsigned int)(a)) & ~(PGSIZE-1))))
|
||||||
|
|
||||||
// Page table/directory entry flags.
|
// Page table/directory entry flags.
|
||||||
#define PTE_P 0x001 // Present
|
#define PTE_P 0x001 // Present
|
||||||
|
|
4
param.h
4
param.h
|
@ -1,6 +1,5 @@
|
||||||
#define NPROC 64 // maximum number of processes
|
#define NPROC 64 // maximum number of processes
|
||||||
#define PAGE 4096 // conveniently chosen to be equal to PGSIZE
|
#define KSTACKSIZE 4096 // size of per-process kernel stack
|
||||||
#define KSTACKSIZE PAGE // size of per-process kernel stack
|
|
||||||
#define NCPU 8 // maximum number of CPUs
|
#define NCPU 8 // maximum number of CPUs
|
||||||
#define NOFILE 16 // open files per process
|
#define NOFILE 16 // open files per process
|
||||||
#define NFILE 100 // open files per system
|
#define NFILE 100 // open files per system
|
||||||
|
@ -8,3 +7,4 @@
|
||||||
#define NINODE 50 // maximum number of active i-nodes
|
#define NINODE 50 // maximum number of active i-nodes
|
||||||
#define NDEV 10 // maximum major device number
|
#define NDEV 10 // maximum major device number
|
||||||
#define ROOTDEV 1 // device number of file system root disk
|
#define ROOTDEV 1 // device number of file system root disk
|
||||||
|
#define PHYSTOP 0x1000000 // use phys mem up to here as free pool
|
||||||
|
|
6
pipe.c
6
pipe.c
|
@ -27,7 +27,7 @@ pipealloc(struct file **f0, struct file **f1)
|
||||||
*f0 = *f1 = 0;
|
*f0 = *f1 = 0;
|
||||||
if((*f0 = filealloc()) == 0 || (*f1 = filealloc()) == 0)
|
if((*f0 = filealloc()) == 0 || (*f1 = filealloc()) == 0)
|
||||||
goto bad;
|
goto bad;
|
||||||
if((p = (struct pipe*)kalloc(PAGE)) == 0)
|
if((p = (struct pipe*)kalloc()) == 0)
|
||||||
goto bad;
|
goto bad;
|
||||||
p->readopen = 1;
|
p->readopen = 1;
|
||||||
p->writeopen = 1;
|
p->writeopen = 1;
|
||||||
|
@ -47,7 +47,7 @@ pipealloc(struct file **f0, struct file **f1)
|
||||||
//PAGEBREAK: 20
|
//PAGEBREAK: 20
|
||||||
bad:
|
bad:
|
||||||
if(p)
|
if(p)
|
||||||
kfree((char*)p, PAGE);
|
kfree((char*)p);
|
||||||
if(*f0)
|
if(*f0)
|
||||||
fileclose(*f0);
|
fileclose(*f0);
|
||||||
if(*f1)
|
if(*f1)
|
||||||
|
@ -68,7 +68,7 @@ pipeclose(struct pipe *p, int writable)
|
||||||
}
|
}
|
||||||
if(p->readopen == 0 && p->writeopen == 0) {
|
if(p->readopen == 0 && p->writeopen == 0) {
|
||||||
release(&p->lock);
|
release(&p->lock);
|
||||||
kfree((char*)p, PAGE);
|
kfree((char*)p);
|
||||||
} else
|
} else
|
||||||
release(&p->lock);
|
release(&p->lock);
|
||||||
}
|
}
|
||||||
|
|
6
proc.c
6
proc.c
|
@ -84,7 +84,7 @@ found:
|
||||||
release(&ptable.lock);
|
release(&ptable.lock);
|
||||||
|
|
||||||
// Allocate kernel stack if possible.
|
// Allocate kernel stack if possible.
|
||||||
if((p->kstack = kalloc(KSTACKSIZE)) == 0){
|
if((p->kstack = kalloc()) == 0){
|
||||||
p->state = UNUSED;
|
p->state = UNUSED;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -169,7 +169,7 @@ fork(void)
|
||||||
|
|
||||||
// Copy process state from p.
|
// Copy process state from p.
|
||||||
if (!(np->pgdir = copyuvm(proc->pgdir, proc->sz))) {
|
if (!(np->pgdir = copyuvm(proc->pgdir, proc->sz))) {
|
||||||
kfree(np->kstack, KSTACKSIZE);
|
kfree(np->kstack);
|
||||||
np->kstack = 0;
|
np->kstack = 0;
|
||||||
np->state = UNUSED;
|
np->state = UNUSED;
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -418,7 +418,7 @@ wait(void)
|
||||||
if(p->state == ZOMBIE){
|
if(p->state == ZOMBIE){
|
||||||
// Found one.
|
// Found one.
|
||||||
pid = p->pid;
|
pid = p->pid;
|
||||||
kfree(p->kstack, KSTACKSIZE);
|
kfree(p->kstack);
|
||||||
p->kstack = 0;
|
p->kstack = 0;
|
||||||
freevm(p->pgdir);
|
freevm(p->pgdir);
|
||||||
p->state = UNUSED;
|
p->state = UNUSED;
|
||||||
|
|
|
@ -49,8 +49,8 @@ morecore(uint nu)
|
||||||
char *p;
|
char *p;
|
||||||
Header *hp;
|
Header *hp;
|
||||||
|
|
||||||
if(nu < PAGE)
|
if(nu < 4096)
|
||||||
nu = PAGE;
|
nu = 4096;
|
||||||
p = sbrk(nu * sizeof(Header));
|
p = sbrk(nu * sizeof(Header));
|
||||||
if(p == (char*) -1)
|
if(p == (char*) -1)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
17
vm.c
17
vm.c
|
@ -29,7 +29,6 @@
|
||||||
// (both in physical memory and in the kernel's virtual address
|
// (both in physical memory and in the kernel's virtual address
|
||||||
// space).
|
// space).
|
||||||
|
|
||||||
#define PHYSTOP 0x1000000
|
|
||||||
#define USERTOP 0xA0000
|
#define USERTOP 0xA0000
|
||||||
|
|
||||||
static uint kerntext; // Linker starts kernel at 1MB
|
static uint kerntext; // Linker starts kernel at 1MB
|
||||||
|
@ -53,7 +52,7 @@ walkpgdir(pde_t *pgdir, const void *va, int create)
|
||||||
pde = &pgdir[PDX(va)];
|
pde = &pgdir[PDX(va)];
|
||||||
if (*pde & PTE_P) {
|
if (*pde & PTE_P) {
|
||||||
pgtab = (pte_t*) PTE_ADDR(*pde);
|
pgtab = (pte_t*) PTE_ADDR(*pde);
|
||||||
} else if (!create || !(r = (uint) kalloc(PGSIZE)))
|
} else if (!create || !(r = (uint) kalloc()))
|
||||||
return 0;
|
return 0;
|
||||||
else {
|
else {
|
||||||
pgtab = (pte_t*) r;
|
pgtab = (pte_t*) r;
|
||||||
|
@ -156,7 +155,7 @@ setupkvm(void)
|
||||||
pde_t *pgdir;
|
pde_t *pgdir;
|
||||||
|
|
||||||
// Allocate page directory
|
// Allocate page directory
|
||||||
if (!(pgdir = (pde_t *) kalloc(PGSIZE)))
|
if (!(pgdir = (pde_t *) kalloc()))
|
||||||
return 0;
|
return 0;
|
||||||
memset(pgdir, 0, PGSIZE);
|
memset(pgdir, 0, PGSIZE);
|
||||||
// Map IO space from 640K to 1Mbyte
|
// Map IO space from 640K to 1Mbyte
|
||||||
|
@ -206,7 +205,7 @@ allocuvm(pde_t *pgdir, char *addr, uint sz)
|
||||||
for(a = first; a <= last; a += PGSIZE){
|
for(a = first; a <= last; a += PGSIZE){
|
||||||
pte_t *pte = walkpgdir(pgdir, a, 0);
|
pte_t *pte = walkpgdir(pgdir, a, 0);
|
||||||
if(pte == 0 || (*pte & PTE_P) == 0){
|
if(pte == 0 || (*pte & PTE_P) == 0){
|
||||||
char *mem = kalloc(PGSIZE);
|
char *mem = kalloc();
|
||||||
if(mem == 0){
|
if(mem == 0){
|
||||||
// XXX clean up?
|
// XXX clean up?
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -235,7 +234,7 @@ deallocuvm(pde_t *pgdir, char *addr, uint sz)
|
||||||
uint pa = PTE_ADDR(*pte);
|
uint pa = PTE_ADDR(*pte);
|
||||||
if(pa == 0)
|
if(pa == 0)
|
||||||
panic("deallocuvm");
|
panic("deallocuvm");
|
||||||
kfree((void *) pa, PGSIZE);
|
kfree((void *) pa);
|
||||||
*pte = 0;
|
*pte = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -260,15 +259,15 @@ freevm(pde_t *pgdir)
|
||||||
uint pa = PTE_ADDR(pgtab[j]);
|
uint pa = PTE_ADDR(pgtab[j]);
|
||||||
uint va = PGADDR(i, j, 0);
|
uint va = PGADDR(i, j, 0);
|
||||||
if (va < USERTOP) // user memory
|
if (va < USERTOP) // user memory
|
||||||
kfree((void *) pa, PGSIZE);
|
kfree((void *) pa);
|
||||||
pgtab[j] = 0;
|
pgtab[j] = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
kfree((void *) da, PGSIZE);
|
kfree((void *) da);
|
||||||
pgdir[i] = 0;
|
pgdir[i] = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
kfree((void *) pgdir, PGSIZE);
|
kfree((void *) pgdir);
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
|
@ -324,7 +323,7 @@ copyuvm(pde_t *pgdir, uint sz)
|
||||||
panic("copyuvm: pte should exist\n");
|
panic("copyuvm: pte should exist\n");
|
||||||
if(*pte & PTE_P){
|
if(*pte & PTE_P){
|
||||||
pa = PTE_ADDR(*pte);
|
pa = PTE_ADDR(*pte);
|
||||||
if (!(mem = kalloc(PGSIZE)))
|
if (!(mem = kalloc()))
|
||||||
return 0;
|
return 0;
|
||||||
memmove(mem, (char *)pa, PGSIZE);
|
memmove(mem, (char *)pa, PGSIZE);
|
||||||
if (!mappages(d, (void *)i, PGSIZE, PADDR(mem), PTE_W|PTE_U))
|
if (!mappages(d, (void *)i, PGSIZE, PADDR(mem), PTE_W|PTE_U))
|
||||||
|
|
Loading…
Reference in a new issue