diff --git a/extlib/benz/gc.c b/extlib/benz/gc.c index 0ffc7bcf..4b6ee750 100644 --- a/extlib/benz/gc.c +++ b/extlib/benz/gc.c @@ -192,20 +192,18 @@ heap_free(pic_state *pic, void *ap) bp = (union header *)ap - 1; - assert(bp->s.size > 1); - for (p = pic->heap->freep; ! (bp > p && bp < p->s.ptr); p = p->s.ptr) { if (p >= p->s.ptr && (bp > p || bp < p->s.ptr)) { break; } } - if (bp + bp->s.size == p->s.ptr) { + if (bp + bp->s.size == p->s.ptr && p->s.ptr->s.size > 0) { /* don't melt base header */ bp->s.size += p->s.ptr->s.size; bp->s.ptr = p->s.ptr->s.ptr; } else { bp->s.ptr = p->s.ptr; } - if (p + p->s.size == bp && p->s.size > 1) { + if (p + p->s.size == bp) { p->s.size += bp->s.size; p->s.ptr = bp->s.ptr; } else { @@ -221,21 +219,21 @@ heap_morecore(pic_state *pic) struct heap_page *page; size_t nu; - nu = (PIC_HEAP_PAGE_SIZE + sizeof(union header) - 1) / sizeof(union header) + 1; + nu = PIC_HEAP_PAGE_SIZE / sizeof(union header); - up = pic_malloc(pic, (1 + nu + 1) * sizeof(union header)); - up->s.size = nu + 1; + assert(nu >= 2); + + up = pic_malloc(pic, PIC_HEAP_PAGE_SIZE); + up->s.size = 0; /* up is never used for allocation */ heap_free(pic, up + 1); np = up + 1; - np->s.size = nu; - np->s.ptr = up->s.ptr; - up->s.size = 1; - up->s.ptr = np; + np->s.size = nu - 1; + heap_free(pic, np + 1); page = pic_malloc(pic, sizeof(struct heap_page)); page->basep = up; - page->endp = up + nu + 1; + page->endp = up + nu; page->next = pic->heap->pages; pic->heap->pages = page; @@ -716,8 +714,9 @@ gc_sweep_page(pic_state *pic, struct heap_page *page) union header *bp, *p, *head = NULL, *tail = NULL; for (bp = page->basep; ; bp = bp->s.ptr) { - for (p = bp + bp->s.size; p != bp->s.ptr; p += p->s.size) { - if (p == page->endp) { + p = bp + (bp->s.size ? bp->s.size : 1); /* first bp's size is 0, so force advnce */ + for (; p != bp->s.ptr; p += p->s.size) { + if (p < page->basep || page->endp <= p) { goto escape; } if (gc_is_marked(p)) {