use malloc for allocating managed objects

This commit is contained in:
Yuichi Nishiwaki 2017-05-13 23:23:57 +09:00
parent e8f4bd250a
commit f69bc42187
5 changed files with 150 additions and 394 deletions

515
lib/gc.c
View File

@ -7,11 +7,6 @@
#include "object.h" #include "object.h"
#include "state.h" #include "state.h"
#define PAGE_UNITS ((PIC_HEAP_PAGE_SIZE - offsetof(struct heap_page, u)) / sizeof(union header))
#define alignof(type) offsetof(struct { char c; type m; }, m)
#define roundup(n,unit) (((n) + (unit) - 1) / (unit) * (unit))
struct object { struct object {
union { union {
struct basic basic; struct basic basic;
@ -30,62 +25,6 @@ struct object {
} u; } u;
}; };
struct free_region {
union header *ptr;
size_t size;
};
union header {
struct free_region s;
char alignment[roundup(sizeof(struct free_region), alignof(struct object))];
};
struct heap_page {
struct heap_page *next;
union {
union header basep[1];
struct object alignment;
} u;
};
struct heap {
union header base, *freep;
struct heap_page *pages;
struct attr *attrs; /* weak map chain */
};
#define unitsof(type) ((type2size(type) + sizeof(union header) - 1) / sizeof(union header))
struct heap *
pic_heap_open(pic_state *pic)
{
struct heap *heap;
heap = pic_malloc(pic, sizeof(struct heap));
heap->base.s.ptr = &heap->base;
heap->base.s.size = 0; /* not 1, since it must never be used for allocation */
heap->freep = &heap->base;
heap->pages = NULL;
heap->attrs = NULL;
return heap;
}
void
pic_heap_close(pic_state *pic, struct heap *heap)
{
struct heap_page *page;
while (heap->pages) {
page = heap->pages;
heap->pages = heap->pages->next;
pic_free(pic, page);
}
pic_free(pic, heap);
}
#if PIC_USE_LIBC #if PIC_USE_LIBC
void * void *
pic_default_allocf(void *PIC_UNUSED(userdata), void *ptr, size_t size) pic_default_allocf(void *PIC_UNUSED(userdata), void *ptr, size_t size)
@ -99,44 +38,30 @@ pic_default_allocf(void *PIC_UNUSED(userdata), void *ptr, size_t size)
#endif #endif
void * void *
pic_malloc(pic_state *pic, size_t size) pic_realloc(pic_state *pic, void *ptr, size_t size)
{ {
void *ptr; void *p;
retry: retry:
ptr = pic->allocf(pic->userdata, NULL, size); p = pic->allocf(pic->userdata, ptr, size);
if (ptr == NULL && size > 0) { if (p == NULL && size > 0) {
pic->panicf(pic, "out of memory", 0, NULL); pic->panicf(pic, "out of memory", 0, NULL);
goto retry; goto retry;
} }
return ptr; return p;
} }
void * void *
pic_realloc(pic_state *pic, void *ptr, size_t size) pic_malloc(pic_state *pic, size_t size)
{ {
retry: return pic_realloc(pic, NULL, size);
ptr = pic->allocf(pic->userdata, ptr, size);
if (ptr == NULL && size > 0) {
pic->panicf(pic, "out of memory", 0, NULL);
goto retry;
}
return ptr;
} }
void * void *
pic_calloc(pic_state *pic, size_t count, size_t size) pic_calloc(pic_state *pic, size_t count, size_t size)
{ {
void *ptr; void *ptr = pic_malloc(pic, count * size);
memset(ptr, 0, count * size);
size *= count;
retry:
ptr = pic->allocf(pic->userdata, NULL, size);
if (ptr == NULL && size > 0) {
pic->panicf(pic, "out of memory", 0, NULL);
goto retry;
}
memset(ptr, 0, size);
return ptr; return ptr;
} }
@ -146,14 +71,16 @@ pic_free(pic_state *pic, void *ptr)
pic->allocf(pic->userdata, ptr, 0); pic->allocf(pic->userdata, ptr, 0);
} }
static void size_t
gc_protect(pic_state *pic, struct object *obj) pic_enter(pic_state *pic)
{ {
if (pic->ai >= pic->arena_size) { return pic->ai;
pic->arena_size = pic->arena_size * 2 + 1; }
pic->arena = pic_realloc(pic, pic->arena, sizeof(struct object *) * pic->arena_size);
} void
pic->arena[pic->ai++] = obj; pic_leave(pic_state *pic, size_t ai)
{
pic->ai = ai;
} }
pic_value pic_value
@ -162,23 +89,14 @@ pic_protect(pic_state *pic, pic_value v)
if (! pic_obj_p(pic, v)) if (! pic_obj_p(pic, v))
return v; return v;
gc_protect(pic, pic_ptr(pic, v)); if (pic->ai >= pic->arena_size) {
pic->arena_size = pic->arena_size * 2 + 1;
pic->arena = pic_realloc(pic, pic->arena, sizeof(struct object *) * pic->arena_size);
}
pic->arena[pic->ai++] = pic_ptr(pic, v);
return v; return v;
} }
size_t
pic_enter(pic_state *pic)
{
return pic->ai;
}
void
pic_leave(pic_state *pic, size_t state)
{
pic->ai = state;
}
void * void *
pic_alloca(pic_state *pic, size_t n) pic_alloca(pic_state *pic, size_t n)
{ {
@ -187,19 +105,12 @@ pic_alloca(pic_state *pic, size_t n)
return pic_data(pic, pic_data_value(pic, pic_malloc(pic, n), &t)); return pic_data(pic, pic_data_value(pic, pic_malloc(pic, n), &t));
} }
/* MARK */ /* GC */
PIC_STATIC_INLINE bool #define is_alive(obj) ((obj)->u.basic.tt & GC_MARK)
is_alive(struct object *obj) #define mark(obj) ((obj)->u.basic.tt |= GC_MARK)
{ #define unmark(obj) ((obj)->u.basic.tt &= ~GC_MARK)
return obj->u.basic.tt & GC_MARK;
}
PIC_STATIC_INLINE void
mark(struct object *obj)
{
obj->u.basic.tt |= GC_MARK;
}
static void gc_mark_object(pic_state *, struct object *); static void gc_mark_object(pic_state *, struct object *);
static void static void
@ -265,9 +176,6 @@ gc_mark_object(pic_state *pic, struct object *obj)
} }
break; break;
} }
case PIC_TYPE_STRING: {
break;
}
case PIC_TYPE_VECTOR: { case PIC_TYPE_VECTOR: {
int i; int i;
for (i = 0; i < obj->u.vec.len; ++i) { for (i = 0; i < obj->u.vec.len; ++i) {
@ -275,12 +183,6 @@ gc_mark_object(pic_state *pic, struct object *obj)
} }
break; break;
} }
case PIC_TYPE_BLOB: {
break;
}
case PIC_TYPE_DATA: {
break;
}
case PIC_TYPE_DICT: { case PIC_TYPE_DICT: {
pic_value key, val; pic_value key, val;
int it = 0; int it = 0;
@ -303,74 +205,21 @@ gc_mark_object(pic_state *pic, struct object *obj)
case PIC_TYPE_ATTR: { case PIC_TYPE_ATTR: {
struct attr *attr = (struct attr *)obj; struct attr *attr = (struct attr *)obj;
attr->prev = pic->heap->attrs; attr->prev = pic->gc_attrs;
pic->heap->attrs = attr; pic->gc_attrs = attr;
break; break;
} }
case PIC_TYPE_STRING:
case PIC_TYPE_BLOB:
case PIC_TYPE_DATA:
break;
default: default:
PIC_UNREACHABLE(); PIC_UNREACHABLE();
} }
} }
static void
gc_mark_phase(pic_state *pic)
{
struct context *cxt;
size_t j;
assert(pic->heap->attrs == NULL);
/* context */
for (cxt = pic->cxt; cxt != NULL; cxt = cxt->prev) {
if (cxt->fp) gc_mark_object(pic, (struct object *)cxt->fp);
if (cxt->sp) gc_mark_object(pic, (struct object *)cxt->sp);
if (cxt->irep) gc_mark_object(pic, (struct object *)cxt->irep);
gc_mark(pic, cxt->conts);
}
/* arena */
for (j = 0; j < pic->ai; ++j) {
gc_mark_object(pic, (struct object *)pic->arena[j]);
}
/* global variables */
gc_mark(pic, pic->globals);
/* top continuation */
gc_mark(pic, pic->halt);
/* weak maps */
do {
struct object *key;
pic_value val;
int it;
khash_t(attr) *h;
struct attr *attr;
j = 0;
attr = pic->heap->attrs;
while (attr != NULL) {
h = &attr->hash;
for (it = kh_begin(h); it != kh_end(h); ++it) {
if (! kh_exist(h, it))
continue;
key = kh_key(h, it);
val = kh_val(h, it);
if (is_alive(key)) {
if (pic_obj_p(pic, val) && ! is_alive(pic_ptr(pic, val))) {
gc_mark(pic, val);
++j;
}
}
}
attr = attr->prev;
}
} while (j > 0);
}
/* SWEEP */
static void static void
gc_finalize_object(pic_state *pic, struct object *obj) gc_finalize_object(pic_state *pic, struct object *obj)
{ {
@ -430,168 +279,72 @@ gc_finalize_object(pic_state *pic, struct object *obj)
} }
} }
static size_t void
type2size(int type) pic_gc(pic_state *pic)
{ {
switch (type) { struct context *cxt;
case PIC_TYPE_VECTOR: return sizeof(struct vector); size_t j;
case PIC_TYPE_BLOB: return sizeof(struct blob);
case PIC_TYPE_STRING: return sizeof(struct string);
case PIC_TYPE_DATA: return sizeof(struct data);
case PIC_TYPE_DICT: return sizeof(struct dict);
case PIC_TYPE_SYMBOL: return sizeof(struct symbol);
case PIC_TYPE_ATTR: return sizeof(struct attr);
case PIC_TYPE_IREP: return sizeof(struct irep);
case PIC_TYPE_PAIR: return sizeof(struct pair);
case PIC_TYPE_FRAME: return sizeof(struct frame);
case PIC_TYPE_RECORD: return sizeof(struct record);
case PIC_TYPE_PROC_FUNC: return sizeof(struct proc);
case PIC_TYPE_PROC_IREP: return sizeof(struct proc);
default: PIC_UNREACHABLE();
}
}
static struct object *
obj_alloc(pic_state *pic, int type)
{
union header *p, *prevp;
struct object *obj;
size_t nunits;
nunits = unitsof(type);
prevp = pic->heap->freep;
for (p = prevp->s.ptr; ; prevp = p, p = p->s.ptr) {
if (p->s.size >= nunits)
break;
if (p == pic->heap->freep) {
return NULL;
}
}
if (p->s.size == nunits) {
prevp->s.ptr = p->s.ptr;
}
else {
p->s.size -= nunits;
p += p->s.size;
p->s.size = nunits;
}
pic->heap->freep = prevp;
obj = (struct object *) p;
obj->u.basic.tt = type;
return obj;
}
static void
free_chunk(pic_state *pic, union header *bp)
{
union header *p;
assert(bp != NULL);
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 && 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 && bp->s.size > 0) { /* don't melt base header */
p->s.size += bp->s.size;
p->s.ptr = bp->s.ptr;
} else {
p->s.ptr = bp;
}
pic->heap->freep = p;
}
static void
heap_morecore(pic_state *pic)
{
union header *bp, *np;
struct heap_page *page;
assert(PAGE_UNITS >= 2);
page = pic_malloc(pic, PIC_HEAP_PAGE_SIZE);
page->next = pic->heap->pages;
bp = page->u.basep;
bp->s.size = 0; /* bp is never used for allocation */
free_chunk(pic, bp);
np = page->u.basep + 1;
np->s.size = PAGE_UNITS - 1;
free_chunk(pic, np);
pic->heap->pages = page;
}
static size_t
gc_sweep_page(pic_state *pic, struct heap_page *page)
{
union header *bp, *p, *head = NULL, *tail = NULL;
struct object *obj;
size_t alive = 0, nunits;
for (bp = page->u.basep; ; bp = bp->s.ptr) {
p = bp + (bp->s.size ? bp->s.size : 1); /* first bp's size is 0, so force advnce */
while (p != bp->s.ptr) {
if (p < page->u.basep || page->u.basep + PAGE_UNITS <= p) {
goto escape;
}
obj = (struct object *) p;
nunits = unitsof(obj_type(obj));
if (obj->u.basic.tt & GC_MARK) {
obj->u.basic.tt &= ~GC_MARK;
alive += nunits;
} else {
gc_finalize_object(pic, obj);
if (head == NULL) {
head = p;
}
if (tail != NULL) {
tail->s.ptr = p;
}
tail = p;
tail->s.size = nunits;
tail->s.ptr = NULL; /* We can safely reuse ptr field of dead object */
}
p += nunits;
}
}
escape:
/* free! */
while (head != NULL) {
p = head;
head = head->s.ptr;
free_chunk(pic, p);
}
return alive;
}
static void
gc_sweep_phase(pic_state *pic)
{
struct heap_page *page;
int it;
khash_t(attr) *h;
khash_t(oblist) *s = &pic->oblist; khash_t(oblist) *s = &pic->oblist;
struct symbol *sym; struct symbol *sym;
struct object *obj; int it;
size_t total = 0, inuse = 0; struct object *obj, *prev, *next;
/* weak maps */ assert(pic->gc_attrs == NULL);
while (pic->heap->attrs != NULL) {
h = &pic->heap->attrs->hash; if (! pic->gc_enable) {
return;
}
/* scan objects */
for (cxt = pic->cxt; cxt != NULL; cxt = cxt->prev) {
if (cxt->fp) gc_mark_object(pic, (struct object *)cxt->fp);
if (cxt->sp) gc_mark_object(pic, (struct object *)cxt->sp);
if (cxt->irep) gc_mark_object(pic, (struct object *)cxt->irep);
gc_mark(pic, cxt->conts);
}
for (j = 0; j < pic->ai; ++j) {
gc_mark_object(pic, (struct object *)pic->arena[j]);
}
gc_mark(pic, pic->globals);
gc_mark(pic, pic->halt);
/* scan weak references */
do {
struct object *key;
pic_value val;
int it;
khash_t(attr) *h;
struct attr *attr;
j = 0;
attr = pic->gc_attrs;
while (attr != NULL) {
h = &attr->hash;
for (it = kh_begin(h); it != kh_end(h); ++it) {
if (! kh_exist(h, it))
continue;
key = kh_key(h, it);
val = kh_val(h, it);
if (is_alive(key)) {
if (pic_obj_p(pic, val) && ! is_alive((struct object *) pic_ptr(pic, val))) {
gc_mark(pic, val);
++j;
}
}
}
attr = attr->prev;
}
} while (j > 0);
/* reclaim dead weak references */
while (pic->gc_attrs != NULL) {
khash_t(attr) *h = &pic->gc_attrs->hash;
for (it = kh_begin(h); it != kh_end(h); ++it) { for (it = kh_begin(h); it != kh_end(h); ++it) {
if (! kh_exist(h, it)) if (! kh_exist(h, it))
continue; continue;
@ -600,10 +353,9 @@ gc_sweep_phase(pic_state *pic)
kh_del(attr, h, it); kh_del(attr, h, it);
} }
} }
pic->heap->attrs = pic->heap->attrs->prev; pic->gc_attrs = pic->gc_attrs->prev;
} }
/* symbol table */
for (it = kh_begin(s); it != kh_end(s); ++it) { for (it = kh_begin(s); it != kh_end(s); ++it) {
if (! kh_exist(s, it)) if (! kh_exist(s, it))
continue; continue;
@ -613,53 +365,60 @@ gc_sweep_phase(pic_state *pic)
} }
} }
page = pic->heap->pages; /* reclaim dead objects */
while (page) {
inuse += gc_sweep_page(pic, page);
total += PAGE_UNITS;
page = page->next;
}
if (PIC_PAGE_REQUEST_THRESHOLD(total) <= inuse) { for (prev = (struct object *) &pic->gc_head, obj = prev->u.basic.next; obj != (struct object *) &pic->gc_head; prev = obj, obj = next) {
heap_morecore(pic); next = obj->u.basic.next;
if (is_alive(obj)) {
unmark(obj);
} else {
gc_finalize_object(pic, obj);
pic_free(pic, obj);
prev->u.basic.next = next;
obj = prev;
}
} }
} }
void static size_t
pic_gc(pic_state *pic) type2size(int type)
{ {
if (! pic->gc_enable) { switch (type) {
return; case PIC_TYPE_VECTOR: return sizeof(struct vector);
case PIC_TYPE_BLOB: return sizeof(struct blob);
case PIC_TYPE_STRING: return sizeof(struct string);
case PIC_TYPE_DATA: return sizeof(struct data);
case PIC_TYPE_DICT: return sizeof(struct dict);
case PIC_TYPE_SYMBOL: return sizeof(struct symbol);
case PIC_TYPE_ATTR: return sizeof(struct attr);
case PIC_TYPE_IREP: return sizeof(struct irep);
case PIC_TYPE_PAIR: return sizeof(struct pair);
case PIC_TYPE_FRAME: return sizeof(struct frame);
case PIC_TYPE_RECORD: return sizeof(struct record);
case PIC_TYPE_PROC_FUNC: return sizeof(struct proc);
case PIC_TYPE_PROC_IREP: return sizeof(struct proc);
default: PIC_UNREACHABLE();
} }
gc_mark_phase(pic);
gc_sweep_phase(pic);
} }
struct object * struct object *
pic_obj_alloc_unsafe(pic_state *pic, int type) pic_obj_alloc_unsafe(pic_state *pic, int type)
{ {
struct object *obj; struct object *obj;
size_t size = type2size(type);
if (pic->heap->pages == NULL) { if (pic->gc_count > PIC_GC_PERIOD) {
heap_morecore(pic);
}
#if GC_STRESS
pic_gc(pic);
#endif
obj = obj_alloc(pic, type);
if (obj == NULL) {
pic_gc(pic); pic_gc(pic);
retry: pic->gc_count -= PIC_GC_PERIOD;
obj = obj_alloc(pic, type);
if (obj == NULL) {
pic->panicf(pic, "out of memory", 0, NULL);
goto retry;
}
} }
obj = pic_malloc(pic, size);
obj->u.basic.tt = type;
obj->u.basic.next = pic->gc_head.next;
pic->gc_head.next = obj;
pic->gc_count += size;
return obj; return obj;
} }
@ -670,6 +429,6 @@ pic_obj_alloc(pic_state *pic, int type)
obj = pic_obj_alloc_unsafe(pic, type); obj = pic_obj_alloc_unsafe(pic, type);
gc_protect(pic, obj); pic_protect(pic, obj_value(pic, obj));
return obj; return obj;
} }

View File

@ -72,12 +72,8 @@
# define PIC_ARENA_SIZE (8 * 1024) # define PIC_ARENA_SIZE (8 * 1024)
#endif #endif
#ifndef PIC_HEAP_PAGE_SIZE #ifndef PIC_GC_PERIOD
# define PIC_HEAP_PAGE_SIZE (4 * 1024 * 1024) # define PIC_GC_PERIOD (8 * 1024 * 1024)
#endif
#ifndef PIC_PAGE_REQUEST_THRESHOLD
# define PIC_PAGE_REQUEST_THRESHOLD(total) ((total) * 77 / 100)
#endif #endif
/* check compatibility */ /* check compatibility */

View File

@ -12,6 +12,7 @@ extern "C" {
#include "khash.h" #include "khash.h"
#define OBJECT_HEADER \ #define OBJECT_HEADER \
struct object *next; \
unsigned char tt; unsigned char tt;
#define TYPE_MASK 0x7f #define TYPE_MASK 0x7f

View File

@ -130,8 +130,10 @@ pic_open(pic_allocf allocf, void *userdata, pic_panicf panicf)
/* turn off GC */ /* turn off GC */
pic->gc_enable = false; pic->gc_enable = false;
/* memory heap */ /* gc */
pic->heap = pic_heap_open(pic); pic->gc_head.next = (struct object *) &pic->gc_head;
pic->gc_attrs = NULL;
pic->gc_count = 0;
/* symbol table */ /* symbol table */
kh_init(oblist, &pic->oblist); kh_init(oblist, &pic->oblist);
@ -196,8 +198,7 @@ pic_close(pic_state *pic)
/* free all heap objects */ /* free all heap objects */
pic_gc(pic); pic_gc(pic);
/* free heaps */ assert(pic->gc_head.next == (struct object *) &pic->gc_head);
pic_heap_close(pic, pic->heap);
/* free global stacks */ /* free global stacks */
kh_destroy(oblist, &pic->oblist); kh_destroy(oblist, &pic->oblist);

View File

@ -36,25 +36,24 @@ struct pic_state {
void *userdata; void *userdata;
struct context *cxt, default_cxt; struct context *cxt, default_cxt;
size_t ai; size_t ai;
khash_t(oblist) oblist; /* string to symbol */ khash_t(oblist) oblist; /* string to symbol */
pic_value globals; /* dict */ pic_value globals; /* dict */
bool gc_enable;
struct heap *heap;
struct object **arena; struct object **arena;
size_t arena_size; size_t arena_size;
bool gc_enable;
struct basic gc_head;
struct attr *gc_attrs;
size_t gc_count;
pic_value halt; /* top continuation */ pic_value halt; /* top continuation */
pic_panicf panicf; pic_panicf panicf;
}; };
struct heap *pic_heap_open(pic_state *);
void pic_heap_close(pic_state *, struct heap *);
pic_value pic_global_ref(pic_state *pic, pic_value uid); pic_value pic_global_ref(pic_state *pic, pic_value uid);
void pic_global_set(pic_state *pic, pic_value uid, pic_value value); void pic_global_set(pic_state *pic, pic_value uid, pic_value value);