formatting, simplify

This commit is contained in:
rsc 2007-08-14 19:05:48 +00:00
parent 8d0a83565a
commit 4bc5056c19

View file

@ -47,9 +47,9 @@ kinit(void)
void void
kfree(char *v, int len) kfree(char *v, int len)
{ {
struct run **rr, *p, *pend; struct run *r, *rend, **rp, *p, *pend;
if(len % PAGE) if(len <= 0 || len % PAGE)
panic("kfree"); panic("kfree");
// Fill with junk to catch dangling refs. // Fill with junk to catch dangling refs.
@ -58,36 +58,29 @@ kfree(char *v, int len)
acquire(&kalloc_lock); acquire(&kalloc_lock);
p = (struct run*)v; p = (struct run*)v;
pend = (struct run*)(v + len); pend = (struct run*)(v + len);
rr = &freelist; for(rp=&freelist; (r=*rp) != 0 && r <= pend; rp=&r->next){
while(*rr){ rend = (struct run*)((char*)r + r->len);
struct run *rend = (struct run*) ((char*)(*rr) + (*rr)->len); if(r <= p && p < rend)
if(p >= *rr && p < rend)
panic("freeing free page"); panic("freeing free page");
if(pend == *rr){ if(pend == r){ // p next to r: replace r with p
p->len = len + (*rr)->len; p->len = len + r->len;
p->next = (*rr)->next; p->next = r->next;
*rr = p; *rp = p;
goto out; goto out;
} }
if(pend < *rr){ if(rend == p){ // r next to p: replace p with r
p->len = len; r->len += len;
p->next = *rr; if(r->next && r->next == pend){ // r now next to r->next?
*rr = p; r->len += r->next->len;
goto out; r->next = r->next->next;
}
if(p == rend){
(*rr)->len += len;
if((*rr)->next && (*rr)->next == pend){
(*rr)->len += (*rr)->next->len;
(*rr)->next = (*rr)->next->next;
} }
goto out; goto out;
} }
rr = &((*rr)->next);
} }
// Insert p before r in list.
p->len = len; p->len = len;
p->next = 0; p->next = r;
*rr = p; *rp = p;
out: out:
release(&kalloc_lock); release(&kalloc_lock);