Skip to content
Snippets Groups Projects
kalloc.c 2.74 KiB
Newer Older
rsc's avatar
rsc committed
// 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.
rtm's avatar
rtm committed

#include "param.h"
#include "types.h"
#include "defs.h"
#include "param.h"
#include "mmu.h"
#include "proc.h"
rtm's avatar
rtm committed
#include "spinlock.h"

struct spinlock kalloc_lock;
rtm's avatar
rtm committed

struct run {
  struct run *next;
  int len; // bytes
};
struct run *freelist;

rsc's avatar
rsc committed
// 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.
rtm's avatar
rtm committed
void
rsc's avatar
rsc committed
kinit(void)
rtm's avatar
rtm committed
{
  extern int end;
rtm's avatar
rtm committed
  char *start;
  initlock(&kalloc_lock, "kalloc");
rsc's avatar
rsc committed
  start = (char*) &end;
  start = (char*) (((uint)start + PAGE) & ~(PAGE-1));
kaashoek's avatar
kaashoek committed
  mem = 256; // assume computer has 256 pages of RAM
rtm's avatar
rtm committed
  cprintf("mem = %d\n", mem * PAGE);
  kfree(start, mem * PAGE);
}

rsc's avatar
rsc committed
// Free the len bytes of memory pointed at by v,
rsc's avatar
rsc committed
// which normally should have been returned by a
rsc's avatar
rsc committed
// call to kalloc(len).  (The exception is when
rsc's avatar
rsc committed
// initializing the allocator; see kinit above.)
rtm's avatar
rtm committed
void
rsc's avatar
rsc committed
kfree(char *v, int len)
rtm's avatar
rtm committed
{
rsc's avatar
rsc committed
  struct run *r, *rend, **rp, *p, *pend;
rtm's avatar
rtm committed

rsc's avatar
rsc committed
  if(len <= 0 || len % PAGE)
rtm's avatar
rtm committed
    panic("kfree");

rsc's avatar
rsc committed
  // Fill with junk to catch dangling refs.
rsc's avatar
rsc committed
  memset(v, 1, len);
rtm's avatar
rtm committed
  acquire(&kalloc_lock);
rsc's avatar
rsc committed
  p = (struct run*)v;
  pend = (struct run*)(v + len);
rsc's avatar
rsc committed
  for(rp=&freelist; (r=*rp) != 0 && r <= pend; rp=&r->next){
    rend = (struct run*)((char*)r + r->len);
    if(r <= p && p < rend)
rtm's avatar
rtm committed
      panic("freeing free page");
rsc's avatar
rsc committed
    if(pend == r){  // p next to r: replace r with p
      p->len = len + r->len;
      p->next = r->next;
      *rp = p;
rtm's avatar
rtm committed
      goto out;
rtm's avatar
rtm committed
    }
rsc's avatar
rsc committed
    if(rend == p){  // r next to p: replace p with r
      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;
rtm's avatar
rtm committed
      }
rtm's avatar
rtm committed
      goto out;
rtm's avatar
rtm committed
    }
  }
rsc's avatar
rsc committed
  // Insert p before r in list.
rtm's avatar
rtm committed
  p->len = len;
rsc's avatar
rsc committed
  p->next = r;
  *rp = p;
rtm's avatar
rtm committed

 out:
  release(&kalloc_lock);
rtm's avatar
rtm committed
}

rsc's avatar
rsc committed
// Allocate n bytes of physical memory.
// Returns a kernel-segment pointer.
// Returns 0 if the memory cannot be allocated.
rsc's avatar
rsc committed
char*
rtm's avatar
rtm committed
kalloc(int n)
{
rsc's avatar
rsc committed
  char *p;
  struct run *r, **rr;
rtm's avatar
rtm committed

  if(n % PAGE)
    panic("kalloc");

rtm's avatar
rtm committed
  acquire(&kalloc_lock);

rtm's avatar
rtm committed
  rr = &freelist;
  while(*rr){
rsc's avatar
rsc committed
    r = *rr;
rtm's avatar
rtm committed
    if(r->len == n){
      *rr = r->next;
rtm's avatar
rtm committed
      release(&kalloc_lock);
rsc's avatar
rsc committed
      return (char*) r;
rtm's avatar
rtm committed
    }
    if(r->len > n){
      r->len -= n;
rsc's avatar
rsc committed
      p = (char*)r + r->len;
rtm's avatar
rtm committed
      release(&kalloc_lock);
rtm's avatar
rtm committed
      return p;
    }
    rr = &(*rr)->next;
  }
rtm's avatar
rtm committed
  release(&kalloc_lock);
rtm's avatar
rtm committed
  cprintf("kalloc: out of memory\n");
rtm's avatar
rtm committed
  return 0;
}