bug in sbrk

test malloc
This commit is contained in:
kaashoek 2006-08-24 19:24:36 +00:00
parent bcfb84b6a9
commit 81d5219998
4 changed files with 49 additions and 8 deletions

5
proc.c
View file

@ -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.

View file

@ -26,6 +26,8 @@ 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)

2
user.h
View file

@ -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 *);

View file

@ -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();