diff --git a/proc.c b/proc.c index b65bb70..4767827 100644 --- a/proc.c +++ b/proc.c @@ -151,10 +151,9 @@ growproc(int n) oldmem = cp->mem; cp->mem = newmem; kfree(oldmem, cp->sz); + cprintf("growproc: added %d bytes to %d bytes\n", n, cp->sz); cp->sz += n; - cprintf("growproc: added %d bytes starting at address 0x%x\n", n, - newmem + cp->sz - n); - return newmem + cp->sz - n; + return cp->sz - n; } // Per-CPU process scheduler. diff --git a/umalloc.c b/umalloc.c index c0fc4ca..11d816a 100644 --- a/umalloc.c +++ b/umalloc.c @@ -25,7 +25,9 @@ void free(void *ap) { Header *bp, *p; - + + printf(1, "free\n"); + bp = (Header *) ap - 1; for (p = freep; !(bp > p && bp < p->s.ptr); p = p->s.ptr) if (p >= p->s.ptr && (bp > p || bp < p->s.ptr)) @@ -51,6 +53,7 @@ morecore(uint nu) if (nu < PAGE) nu = PAGE; + printf(1, "call sbrk\n"); cp = sbrk(nu * sizeof(Header)); if (cp == (char *) -1) return 0; @@ -66,6 +69,8 @@ malloc(uint nbytes) Header *p, *prevp; uint nunits; + printf(1, "malloc %d\n", nbytes); + nunits = (nbytes + sizeof(Header) - 1)/sizeof(Header) + 1; if ((prevp = freep) == 0) { base.s.ptr = freep = prevp = &base; @@ -81,6 +86,7 @@ malloc(uint nbytes) p->s.size = nunits; } freep = prevp; + printf(1, "malloc returns: %d\n", (int) (p+1)); return (void *) (p + 1); } if (p == freep) diff --git a/user.h b/user.h index b5ea91d..428d786 100644 --- a/user.h +++ b/user.h @@ -30,5 +30,5 @@ void printf(int fd, char *fmt, ...); char *gets(char *, int max); unsigned int strlen(char *); void * memset(void *dst, int c, unsigned int n); -void *mallic(uint); +void *malloc(uint); void free(void *); diff --git a/usertests.c b/usertests.c index 0d3e2bc..cb01c92 100644 --- a/usertests.c +++ b/usertests.c @@ -117,14 +117,50 @@ exitwait(void) puts("exitwait ok\n"); } +void +mem(void) +{ + void *m = malloc(4096); + void *m1, *m2; + + free(m + 3*1024); + free(m + 2*1024); + free(m + 1024); + free(m); + m1 = malloc(4096); + if (m1 != m) { + puts("didn't coalesce\n"); + exit(); + } + free(m1); + + m1 = 0; + while ((m2 = malloc(1024)) != 0) { + *(char **) m2 = m1; + m1 = m2; + } + while (m1) { + m2 = *(char **)m1; + free(m1); + m1 = m2; + } + m1 = malloc(1024*20); + if (m1 == 0) { + puts("couldn't allocate mem?!!\n"); + exit(); + } + free(m1); +} + int main(int argc, char *argv[]) { puts("usertests starting\n"); - pipe1(); - preempt(); - exitwait(); + // pipe1(); + // preempt(); + // exitwait(); + mem(); puts("usertests finished\n"); exit();