force alignment of headers

This commit is contained in:
Yuichi Nishiwaki 2013-10-13 16:02:08 +09:00
parent c250d3b07f
commit d92bd71293
2 changed files with 39 additions and 35 deletions

View File

@ -1,13 +1,16 @@
#ifndef GC_H__ #ifndef GC_H__
#define GC_H__ #define GC_H__
struct header { union header {
struct header *ptr; struct {
union header *ptr;
size_t size; size_t size;
} s;
long alignment; /* force alignment for headers to be allocated at the size of long */
}; };
struct heap_page { struct heap_page {
struct header *base, *freep; union header *base, *freep;
size_t heap_size; size_t heap_size;
}; };

View File

@ -6,44 +6,45 @@
void void
init_heap_page(struct heap_page *heap) init_heap_page(struct heap_page *heap)
{ {
struct header *base, *freep; union header *base, *freep;
void *p; void *p;
p = (struct header *)malloc(PIC_HEAP_SIZE); p = (union header *)malloc(PIC_HEAP_SIZE);
heap->base = base = (struct header *) heap->base = base = (union header *)
(((unsigned long)p + sizeof(struct header) -1) & ~(sizeof(struct header) - 1)); (((unsigned long)p + sizeof(union header) -1) & ~(sizeof(union header) - 1));
base->ptr = base + 1; base->s.ptr = base + 1;
base->size = 0; base->s.size = 0;
heap->freep = freep = base->ptr; heap->freep = freep = base->s.ptr;
freep->ptr = base; freep->s.ptr = base;
freep->size = ((char *)p + PIC_HEAP_SIZE - (char *)freep) / sizeof(struct header); freep->s.size = ((char *)p + PIC_HEAP_SIZE - (char *)freep) / sizeof(union header);
} }
void * void *
pic_alloc(pic_state *pic, size_t size) pic_alloc(pic_state *pic, size_t size)
{ {
struct header *freep, *p, *prevp; union header *freep, *p, *prevp;
size_t nunits; size_t nunits;
nunits = (size + sizeof(struct header) - 1) / sizeof(struct header) + 1; nunits = (size + sizeof(union header) - 1) / sizeof(union header) + 1;
freep = pic->heap->freep; freep = pic->heap->freep;
prevp = freep; prevp = freep;
for (p = prevp->ptr; ; prevp = p, p = p->ptr) { for (p = prevp->s.ptr; ; prevp = p, p = p->s.ptr) {
if (p->size >= nunits) if (p->s.size >= nunits)
break; break;
if (p == freep) if (p == freep) {
return 0; return 0;
} }
if (p->size == nunits) { }
prevp->ptr = p->ptr; if (p->s.size == nunits) {
prevp->s.ptr = p->s.ptr;
} }
else { else {
p->size -= nunits; p->s.size -= nunits;
p += p->size; p += p->s.size;
p->size = nunits; p->s.size = nunits;
} }
pic->heap->freep = prevp; pic->heap->freep = prevp;
@ -53,26 +54,26 @@ pic_alloc(pic_state *pic, size_t size)
void void
pic_free(pic_state *pic, void *ptr) pic_free(pic_state *pic, void *ptr)
{ {
struct header *bp, *p; union header *bp, *p;
bp = (struct header *)ptr - 1; bp = (union header *)ptr - 1;
for (p = pic->heap->freep; !(p < bp && bp < p->ptr); p = p->ptr) { for (p = pic->heap->freep; !(p < bp && bp < p->s.ptr); p = p->s.ptr) {
if (p >= p->ptr && (bp > p || bp < p->ptr)) if (p >= p->s.ptr && (bp > p || bp < p->s.ptr))
break; break;
} }
if (bp + bp->size == p->ptr) { if (bp + bp->s.size == p->s.ptr) {
bp->size += p->ptr->size; bp->s.size += p->s.ptr->s.size;
bp->ptr = p->ptr->ptr; bp->s.ptr = p->s.ptr->s.ptr;
} }
else { else {
bp->ptr = p->ptr; bp->s.ptr = p->s.ptr;
} }
if (p + p->size == bp) { if (p + p->s.size == bp) {
p->size += bp->size; p->s.size += bp->s.size;
p->ptr = bp->ptr; p->s.ptr = bp->s.ptr;
} else { } else {
p->ptr = bp; p->s.ptr = bp;
} }
pic->heap->freep = p; pic->heap->freep = p;
} }