diff options
Diffstat (limited to 'kalloc.c')
-rw-r--r-- | kalloc.c | 35 |
1 files changed, 16 insertions, 19 deletions
@@ -1,13 +1,13 @@ // Physical memory allocator, intended to allocate -// memory for user processes. Allocates in 4096-byte "pages". +// 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. +// This combining is not useful now that xv6 uses paging. #include "types.h" #include "defs.h" #include "param.h" +#include "mmu.h" #include "spinlock.h" struct run { @@ -20,21 +20,14 @@ struct { struct run *freelist; } kmem; +int nfreemem; + // 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. void -kinit(void) +kinit(char *p, uint len) { - extern char end[]; - uint len; - char *p; - initlock(&kmem.lock, "kmem"); - p = (char*)(((uint)end + PAGE) & ~(PAGE-1)); - len = 256*PAGE; // assume computer has 256 pages of RAM, 1 MB - cprintf("mem = %d\n", len); + nfreemem = 0; kfree(p, len); } @@ -47,19 +40,23 @@ kfree(char *v, int len) { struct run *r, *rend, **rp, *p, *pend; - if(len <= 0 || len % PAGE) + if(len <= 0 || len % PGSIZE) panic("kfree"); // Fill with junk to catch dangling refs. memset(v, 1, len); acquire(&kmem.lock); + nfreemem += len; p = (struct run*)v; pend = (struct run*)(v + len); for(rp=&kmem.freelist; (r=*rp) != 0 && r <= pend; rp=&r->next){ rend = (struct run*)((char*)r + r->len); - if(r <= p && p < rend) + 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? @@ -93,7 +90,7 @@ kalloc(int n) char *p; struct run *r, **rp; - if(n % PAGE || n <= 0) + if(n % PGSIZE || n <= 0) panic("kalloc"); acquire(&kmem.lock); @@ -103,12 +100,12 @@ kalloc(int n) p = (char*)r + r->len; if(r->len == 0) *rp = r->next; + nfreemem -= n; release(&kmem.lock); return p; } } release(&kmem.lock); - - cprintf("kalloc: out of memory\n"); return 0; } + |