2013-10-10 03:44:51 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "picrin.h"
|
2013-10-13 02:14:15 -04:00
|
|
|
#include "picrin/gc.h"
|
2013-10-13 06:00:39 -04:00
|
|
|
#include "picrin/irep.h"
|
2013-10-13 02:14:15 -04:00
|
|
|
|
|
|
|
void
|
|
|
|
init_heap_page(struct heap_page *heap)
|
|
|
|
{
|
2013-10-13 03:02:08 -04:00
|
|
|
union header *base, *freep;
|
2013-10-13 02:14:15 -04:00
|
|
|
void *p;
|
|
|
|
|
2013-10-13 03:02:08 -04:00
|
|
|
p = (union header *)malloc(PIC_HEAP_SIZE);
|
2013-10-13 02:14:15 -04:00
|
|
|
|
2013-10-13 03:02:08 -04:00
|
|
|
heap->base = base = (union header *)
|
|
|
|
(((unsigned long)p + sizeof(union header) -1) & ~(sizeof(union header) - 1));
|
|
|
|
base->s.ptr = base + 1;
|
|
|
|
base->s.size = 0;
|
2013-10-13 02:14:15 -04:00
|
|
|
|
2013-10-13 03:02:08 -04:00
|
|
|
heap->freep = freep = base->s.ptr;
|
|
|
|
freep->s.ptr = base;
|
|
|
|
freep->s.size = ((char *)p + PIC_HEAP_SIZE - (char *)freep) / sizeof(union header);
|
2013-10-14 05:21:56 -04:00
|
|
|
|
|
|
|
heap->endp = freep + freep->s.size;
|
2013-10-13 02:14:15 -04:00
|
|
|
}
|
2013-10-10 03:44:51 -04:00
|
|
|
|
|
|
|
void *
|
|
|
|
pic_alloc(pic_state *pic, size_t size)
|
2013-10-13 03:56:30 -04:00
|
|
|
{
|
|
|
|
void *ptr;
|
|
|
|
|
|
|
|
ptr = malloc(size);
|
|
|
|
if (ptr == NULL) {
|
|
|
|
pic_raise(pic, "memory exhausted");
|
|
|
|
}
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
pic_free(pic_state *pic, void *ptr)
|
|
|
|
{
|
|
|
|
free(ptr);
|
|
|
|
}
|
|
|
|
|
2013-10-13 04:25:36 -04:00
|
|
|
static void
|
|
|
|
gc_protect(pic_state *pic, struct pic_object *obj)
|
|
|
|
{
|
|
|
|
if (pic->arena_idx >= PIC_ARENA_SIZE) {
|
|
|
|
pic_raise(pic, "arena overflow");
|
|
|
|
}
|
|
|
|
pic->arena[pic->arena_idx++] = obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
pic_gc_protect(pic_state *pic, pic_value v)
|
|
|
|
{
|
|
|
|
struct pic_object *obj;
|
|
|
|
|
|
|
|
if (v.type != PIC_VTYPE_HEAP) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
obj = pic_object_ptr(v);
|
|
|
|
|
|
|
|
gc_protect(pic, obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
pic_gc_arena_preserve(pic_state *pic)
|
|
|
|
{
|
|
|
|
return pic->arena_idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
pic_gc_arena_restore(pic_state *pic, int state)
|
|
|
|
{
|
|
|
|
pic->arena_idx = state;
|
|
|
|
}
|
|
|
|
|
2013-10-13 03:56:30 -04:00
|
|
|
static void *
|
2013-10-13 06:00:12 -04:00
|
|
|
gc_alloc(pic_state *pic, size_t size)
|
2013-10-10 03:44:51 -04:00
|
|
|
{
|
2013-10-13 03:02:08 -04:00
|
|
|
union header *freep, *p, *prevp;
|
2013-10-13 02:14:15 -04:00
|
|
|
size_t nunits;
|
|
|
|
|
2013-10-13 03:02:08 -04:00
|
|
|
nunits = (size + sizeof(union header) - 1) / sizeof(union header) + 1;
|
2013-10-13 02:14:15 -04:00
|
|
|
|
|
|
|
freep = pic->heap->freep;
|
|
|
|
prevp = freep;
|
2013-10-13 03:02:08 -04:00
|
|
|
for (p = prevp->s.ptr; ; prevp = p, p = p->s.ptr) {
|
|
|
|
if (p->s.size >= nunits)
|
2013-10-13 02:14:15 -04:00
|
|
|
break;
|
2013-10-13 03:02:08 -04:00
|
|
|
if (p == freep) {
|
2013-10-13 02:14:15 -04:00
|
|
|
return 0;
|
2013-10-13 03:02:08 -04:00
|
|
|
}
|
2013-10-13 02:14:15 -04:00
|
|
|
}
|
2013-10-13 03:02:08 -04:00
|
|
|
if (p->s.size == nunits) {
|
|
|
|
prevp->s.ptr = p->s.ptr;
|
2013-10-13 02:14:15 -04:00
|
|
|
}
|
|
|
|
else {
|
2013-10-13 03:02:08 -04:00
|
|
|
p->s.size -= nunits;
|
|
|
|
p += p->s.size;
|
|
|
|
p->s.size = nunits;
|
2013-10-13 02:14:15 -04:00
|
|
|
}
|
|
|
|
pic->heap->freep = prevp;
|
|
|
|
|
2013-10-13 06:00:12 -04:00
|
|
|
p->s.mark = PIC_GC_UNMARK;
|
2013-10-13 02:14:15 -04:00
|
|
|
return (void *)(p + 1);
|
|
|
|
}
|
|
|
|
|
2013-10-13 06:00:12 -04:00
|
|
|
static void gc_mark(pic_state *, pic_value);
|
|
|
|
|
2013-10-13 03:56:30 -04:00
|
|
|
static void
|
2013-10-13 06:00:12 -04:00
|
|
|
gc_mark_object(pic_state *pic, struct pic_object *obj)
|
2013-10-13 02:14:15 -04:00
|
|
|
{
|
2013-10-13 06:00:12 -04:00
|
|
|
union header *p;
|
2013-10-13 02:14:15 -04:00
|
|
|
|
2013-10-13 06:00:12 -04:00
|
|
|
p = (union header *)obj - 1;
|
|
|
|
p->s.mark = PIC_GC_MARK;
|
2013-10-13 02:14:15 -04:00
|
|
|
|
2013-10-13 06:00:12 -04:00
|
|
|
switch (obj->tt) {
|
|
|
|
case PIC_TT_PAIR: {
|
|
|
|
gc_mark(pic, ((struct pic_pair *)obj)->car);
|
|
|
|
gc_mark(pic, ((struct pic_pair *)obj)->cdr);
|
|
|
|
break;
|
2013-10-13 02:14:15 -04:00
|
|
|
}
|
2013-10-13 06:00:12 -04:00
|
|
|
case PIC_TT_SYMBOL: {
|
|
|
|
break;
|
2013-10-13 02:14:15 -04:00
|
|
|
}
|
2013-10-13 06:00:12 -04:00
|
|
|
case PIC_TT_PROC: {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
pic_raise(pic, "logic flaw");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gc_mark(pic_state *pic, pic_value v)
|
|
|
|
{
|
|
|
|
struct pic_object *obj;
|
|
|
|
|
|
|
|
if (v.type != PIC_VTYPE_HEAP)
|
|
|
|
return;
|
|
|
|
obj = pic_object_ptr(v);
|
|
|
|
|
|
|
|
gc_mark_object(pic, obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gc_mark_phase(pic_state *pic)
|
|
|
|
{
|
|
|
|
pic_value *stack;
|
|
|
|
struct pic_env *env;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* stack */
|
|
|
|
for (stack = pic->stbase; stack != pic->sp; ++stack) {
|
|
|
|
gc_mark(pic, *stack);
|
|
|
|
}
|
|
|
|
gc_mark(pic, *stack);
|
|
|
|
|
|
|
|
/* arena */
|
|
|
|
for (i = 0; i < pic->arena_idx; ++i) {
|
|
|
|
gc_mark_object(pic, pic->arena[i]);
|
2013-10-13 02:14:15 -04:00
|
|
|
}
|
2013-10-13 06:00:12 -04:00
|
|
|
|
|
|
|
/* global env */
|
|
|
|
env = pic->global_env;
|
|
|
|
do {
|
|
|
|
gc_mark(pic, env->assoc);
|
|
|
|
} while ((env = env->parent) != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
is_marked(union header *p)
|
|
|
|
{
|
|
|
|
return p->s.mark == PIC_GC_MARK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gc_unmark(union header *p)
|
|
|
|
{
|
|
|
|
p->s.mark = PIC_GC_UNMARK;
|
|
|
|
}
|
|
|
|
|
2013-10-13 06:00:39 -04:00
|
|
|
static void
|
|
|
|
gc_finalize_object(pic_state *pic, struct pic_object *obj)
|
|
|
|
{
|
2013-10-14 05:22:14 -04:00
|
|
|
#if GC_DEBUG
|
|
|
|
printf("finalizing object type %d\n", obj->tt);
|
|
|
|
#endif
|
|
|
|
|
2013-10-13 06:00:39 -04:00
|
|
|
switch (obj->tt) {
|
|
|
|
case PIC_TT_SYMBOL: {
|
|
|
|
char *name;
|
|
|
|
name = ((struct pic_symbol *)obj)->name;
|
2013-10-14 04:04:59 -04:00
|
|
|
pic_free(pic, name);
|
2013-10-13 06:00:39 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PIC_TT_PAIR: {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PIC_TT_PROC: {
|
|
|
|
struct pic_proc *proc;
|
|
|
|
|
|
|
|
proc = (struct pic_proc *)obj;
|
|
|
|
|
|
|
|
/* free irep */
|
2013-10-14 04:04:59 -04:00
|
|
|
pic_free(pic, proc->u.irep->code);
|
|
|
|
pic_free(pic, proc->u.irep);
|
2013-10-14 04:32:24 -04:00
|
|
|
break;
|
2013-10-13 06:00:39 -04:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
pic_raise(pic, "logic flaw");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-13 06:00:12 -04:00
|
|
|
static void
|
|
|
|
gc_sweep_phase(pic_state *pic)
|
|
|
|
{
|
2013-10-14 04:34:04 -04:00
|
|
|
union header *base, *bp, *p;
|
2013-10-13 06:00:12 -04:00
|
|
|
|
2013-10-14 05:22:14 -04:00
|
|
|
#if GC_DEBUG
|
|
|
|
puts("sweep started");
|
|
|
|
#endif
|
|
|
|
|
2013-10-14 04:34:04 -04:00
|
|
|
base = pic->heap->base;
|
|
|
|
for (p = base->s.ptr; p != base; p = p->s.ptr) {
|
2013-10-14 05:22:14 -04:00
|
|
|
|
|
|
|
#if GC_DEBUG
|
|
|
|
puts("sweeping block");
|
|
|
|
#endif
|
|
|
|
|
2013-10-14 19:58:23 -04:00
|
|
|
retry:
|
|
|
|
|
2013-10-13 06:00:12 -04:00
|
|
|
for (bp = p + p->s.size; bp != p->s.ptr; bp += bp->s.size) {
|
2013-10-14 05:22:14 -04:00
|
|
|
|
|
|
|
#if GC_DEBUG
|
|
|
|
printf(" bp = %p\n p = %p\n p->s.ptr = %p\n endp = %p\n",bp, p, p->s.ptr, pic->heap->endp);
|
|
|
|
#endif
|
|
|
|
|
2013-10-14 05:21:56 -04:00
|
|
|
if (p >= p->s.ptr && bp == pic->heap->endp)
|
|
|
|
break;
|
2013-10-13 06:00:12 -04:00
|
|
|
if (is_marked(bp)) {
|
2013-10-14 05:22:14 -04:00
|
|
|
|
|
|
|
#if GC_DEBUG
|
|
|
|
printf("marked:\t\t\t");
|
|
|
|
pic_debug(pic, pic_obj_value((struct pic_object *)(bp + 1)));
|
|
|
|
printf("\n");
|
|
|
|
#endif
|
|
|
|
|
2013-10-13 06:00:12 -04:00
|
|
|
gc_unmark(bp);
|
|
|
|
continue;
|
|
|
|
}
|
2013-10-14 05:22:14 -04:00
|
|
|
|
|
|
|
#if GC_DEBUG
|
|
|
|
puts("unmarked");
|
|
|
|
#endif
|
|
|
|
|
2013-10-13 06:00:12 -04:00
|
|
|
/* free! */
|
2013-10-13 06:00:39 -04:00
|
|
|
gc_finalize_object(pic, (struct pic_object *)(bp + 1));
|
2013-10-13 06:00:12 -04:00
|
|
|
if (bp + bp->s.size == p->s.ptr) {
|
|
|
|
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 += bp->s.size;
|
|
|
|
p->s.ptr = bp->s.ptr;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
p->s.ptr = bp;
|
2013-10-14 19:58:23 -04:00
|
|
|
/* retry with next p */
|
|
|
|
p = p->s.ptr;
|
2013-10-13 06:00:12 -04:00
|
|
|
}
|
2013-10-14 19:58:23 -04:00
|
|
|
|
|
|
|
goto retry;
|
2013-10-13 06:00:12 -04:00
|
|
|
}
|
2013-10-13 02:14:15 -04:00
|
|
|
}
|
2013-10-13 06:00:12 -04:00
|
|
|
}
|
|
|
|
|
2013-10-14 04:07:09 -04:00
|
|
|
void
|
2013-10-13 06:00:12 -04:00
|
|
|
pic_gc_run(pic_state *pic)
|
|
|
|
{
|
2013-10-14 05:22:14 -04:00
|
|
|
#if GC_DEBUG
|
2013-10-14 04:07:09 -04:00
|
|
|
puts("gc run!");
|
2013-10-14 05:22:14 -04:00
|
|
|
#endif
|
2013-10-13 06:00:12 -04:00
|
|
|
gc_mark_phase(pic);
|
|
|
|
gc_sweep_phase(pic);
|
2013-10-10 03:44:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
struct pic_object *
|
2013-10-13 03:55:07 -04:00
|
|
|
pic_obj_alloc(pic_state *pic, size_t size, enum pic_tt tt)
|
2013-10-10 03:44:51 -04:00
|
|
|
{
|
|
|
|
struct pic_object *obj;
|
|
|
|
|
2013-10-13 06:00:12 -04:00
|
|
|
obj = (struct pic_object *)gc_alloc(pic, size);
|
|
|
|
if (obj == NULL) {
|
|
|
|
pic_gc_run(pic);
|
|
|
|
obj = (struct pic_object *)gc_alloc(pic, size);
|
|
|
|
if (obj == NULL)
|
2013-10-14 04:07:09 -04:00
|
|
|
pic_raise(pic, "GC memory exhausted");
|
2013-10-13 06:00:12 -04:00
|
|
|
}
|
2013-10-10 03:44:51 -04:00
|
|
|
obj->tt = tt;
|
|
|
|
|
2013-10-14 05:22:14 -04:00
|
|
|
#if GC_DEBUG
|
|
|
|
printf("* alloced object type %d\n", tt);
|
|
|
|
#endif
|
|
|
|
|
2013-10-13 04:25:36 -04:00
|
|
|
gc_protect(pic, obj);
|
2013-10-10 03:44:51 -04:00
|
|
|
return obj;
|
|
|
|
}
|