picrin/src/gc.c

515 lines
9.7 KiB
C
Raw Normal View History

#include <stdlib.h>
#include "picrin.h"
#include "picrin/gc.h"
2013-10-13 06:00:39 -04:00
#include "picrin/irep.h"
2013-10-15 00:21:40 -04:00
#include "picrin/proc.h"
2013-10-22 02:40:36 -04:00
#include "picrin/port.h"
2013-11-04 22:38:23 -05:00
#include "picrin/blob.h"
2013-11-09 00:14:25 -05:00
#include "picrin/cont.h"
2013-11-17 03:25:26 -05:00
#include "picrin/error.h"
#if GC_DEBUG
# include <stdio.h>
#endif
void
2013-11-22 10:19:31 -05:00
init_heap(struct pic_heap *heap)
{
2013-11-22 10:12:03 -05:00
heap->base.s.ptr = heap->freep = &heap->base;
2013-11-22 09:38:29 -05:00
heap->base.s.size = 0; /* not 1, since it must never be fused into other headers */
heap->base.s.mark = PIC_GC_UNMARK;
2013-10-17 07:55:33 -04:00
#if GC_DEBUG
2013-11-22 09:38:29 -05:00
printf("freep = %p\n", heap->freep);
2013-10-17 07:55:33 -04:00
#endif
}
2013-11-22 10:12:03 -05:00
static void gc_free(pic_state *, union header *);
static void *
add_heap_page(pic_state *pic)
{
int nu;
union header *p, *ep;
#if DEBUG
puts("heap page added");
#endif
nu = (PIC_HEAP_PAGE_SIZE + sizeof(union header) - 1) / sizeof(union header) + 1;
p = (union header *)calloc(nu, sizeof(union header));
p->s.size = nu - 1;
p->s.mark = PIC_GC_UNMARK;
gc_free(pic, p);
ep = p + p->s.size; /* end of page */
ep->s.size = 0;
ep->s.mark = PIC_GC_UNMARK;
ep->s.ptr = p->s.ptr;
p->s.ptr = ep;
return pic->heap->freep;
}
void *
pic_alloc(pic_state *pic, size_t size)
{
void *ptr;
ptr = malloc(size);
if (ptr == NULL) {
pic_abort(pic, "memory exhausted");
}
return ptr;
}
2013-10-15 22:21:41 -04:00
void *
pic_realloc(pic_state *pic, void *ptr, size_t size)
{
ptr = realloc(ptr, size);
if (ptr == NULL) {
pic_abort(pic, "memory exhausted");
2013-10-15 22:21:41 -04:00
}
return ptr;
}
2013-10-23 11:32:03 -04:00
void *
pic_calloc(pic_state *pic, unsigned count, size_t size)
{
void *ptr;
ptr = calloc(count ,size);
if (ptr == NULL) {
pic_abort(pic, "memory exhausted");
}
return ptr;
}
void
pic_free(pic_state *pic, void *ptr)
{
free(ptr);
}
static void
gc_protect(pic_state *pic, struct pic_object *obj)
{
if (pic->arena_idx >= PIC_ARENA_SIZE) {
pic_abort(pic, "arena overflow");
}
pic->arena[pic->arena_idx++] = obj;
}
void
pic_gc_protect(pic_state *pic, pic_value v)
{
struct pic_object *obj;
if (pic_vtype(v) != PIC_VTYPE_HEAP) {
return;
}
2013-10-17 09:42:20 -04:00
obj = pic_obj_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;
}
static void *
2013-10-13 06:00:12 -04:00
gc_alloc(pic_state *pic, size_t size)
{
2013-10-13 03:02:08 -04:00
union header *freep, *p, *prevp;
size_t nunits;
2013-10-13 03:02:08 -04:00
nunits = (size + sizeof(union header) - 1) / sizeof(union header) + 1;
2013-11-22 09:38:29 -05:00
prevp = freep = pic->heap->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)
break;
2013-10-13 03:02:08 -04:00
if (p == freep) {
if ((p = add_heap_page(pic)) == NULL)
return NULL;
2013-10-13 03:02:08 -04:00
}
}
2013-10-13 03:02:08 -04:00
if (p->s.size == nunits) {
prevp->s.ptr = p->s.ptr;
}
else {
2013-10-13 03:02:08 -04:00
p->s.size -= nunits;
p += p->s.size;
p->s.size = nunits;
}
pic->heap->freep = prevp;
2013-10-13 06:00:12 -04:00
p->s.mark = PIC_GC_UNMARK;
return (void *)(p + 1);
}
2013-10-13 06:00:12 -04:00
static void gc_mark(pic_state *, pic_value);
static void gc_mark_object(pic_state *pic, struct pic_object *obj);
static void
gc_mark_block(pic_state *pic, struct pic_block *blk)
{
while (blk) {
if (blk->in)
gc_mark_object(pic, (struct pic_object *)blk->in);
if (blk->out)
gc_mark_object(pic, (struct pic_object *)blk->out);
blk = blk->prev;
}
}
2013-10-13 06:00:12 -04:00
2013-11-15 02:11:54 -05:00
static bool
2013-11-15 05:32:01 -05:00
gc_is_marked(union header *p)
2013-11-15 02:11:54 -05:00
{
return p->s.mark == PIC_GC_MARK;
}
static void
gc_unmark(union header *p)
{
p->s.mark = PIC_GC_UNMARK;
}
static void
2013-10-13 06:00:12 -04:00
gc_mark_object(pic_state *pic, struct pic_object *obj)
{
2013-10-13 06:00:12 -04:00
union header *p;
2013-11-15 05:32:01 -05:00
p = ((union header *)obj) - 1;
2013-11-15 02:11:54 -05:00
2013-11-15 05:32:01 -05:00
if (gc_is_marked(p))
2013-11-15 02:11:54 -05:00
return;
2013-10-13 06:00:12 -04:00
p->s.mark = PIC_GC_MARK;
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-23 13:02:07 -04:00
case PIC_TT_ENV: {
struct pic_env *env = (struct pic_env *)obj;
int i;
2013-11-04 16:07:36 -05:00
for (i = 0; i < env->valuec; ++i) {
2013-10-23 13:02:07 -04:00
gc_mark(pic, env->values[i]);
}
if (env->up) {
gc_mark_object(pic, (struct pic_object *)env->up);
}
2013-10-23 13:02:07 -04:00
break;
}
2013-10-13 06:00:12 -04:00
case PIC_TT_PROC: {
2013-10-23 13:02:07 -04:00
struct pic_proc *proc = (struct pic_proc *)obj;
if (proc->env) {
gc_mark_object(pic, (struct pic_object *)proc->env);
}
2013-10-13 06:00:12 -04:00
break;
}
2013-10-20 19:48:55 -04:00
case PIC_TT_PORT: {
break;
}
2013-11-17 03:25:26 -05:00
case PIC_TT_ERROR: {
gc_mark(pic, ((struct pic_error *)obj)->irrs);
break;
}
2013-10-20 19:48:55 -04:00
case PIC_TT_STRING: {
2013-10-29 02:51:37 -04:00
break;
}
case PIC_TT_VECTOR: {
int i;
for (i = 0; i < ((struct pic_vector *)obj)->len; ++i) {
gc_mark(pic, ((struct pic_vector *)obj)->data[i]);
}
break;
2013-10-20 19:48:55 -04:00
}
2013-11-04 22:38:23 -05:00
case PIC_TT_BLOB: {
break;
}
2013-11-09 00:14:25 -05:00
case PIC_TT_CONT: {
struct pic_cont *cont = (struct pic_cont *)obj;
pic_value *stack;
pic_callinfo *ci;
int i;
/* block */
gc_mark_block(pic, cont->blk);
2013-11-09 00:14:25 -05:00
/* stack */
for (stack = cont->st_ptr; stack != cont->st_ptr + cont->sp_offset; ++stack) {
2013-11-09 00:14:25 -05:00
gc_mark(pic, *stack);
}
/* callinfo */
for (ci = cont->ci_ptr + cont->ci_offset; ci != cont->ci_ptr; --ci) {
2013-11-09 00:14:25 -05:00
if (ci->env) {
gc_mark_object(pic, (struct pic_object *)ci->env);
}
}
2013-11-17 02:02:58 -05:00
/* exception handlers */
for (i = 0; i < cont->ridx; ++i) {
gc_mark_object(pic, (struct pic_object *)cont->rescue[i]);
}
2013-11-09 00:14:25 -05:00
/* arena */
for (i = 0; i < cont->arena_idx; ++i) {
gc_mark_object(pic, cont->arena[i]);
}
gc_mark(pic, cont->result);
break;
}
2013-10-20 19:48:55 -04:00
case PIC_TT_NIL:
case PIC_TT_BOOL:
case PIC_TT_FLOAT:
2013-10-27 11:21:24 -04:00
case PIC_TT_INT:
2013-10-28 13:11:31 -04:00
case PIC_TT_SYMBOL:
2013-11-04 21:37:18 -05:00
case PIC_TT_CHAR:
2013-10-22 03:02:20 -04:00
case PIC_TT_EOF:
2013-10-20 19:48:55 -04:00
case PIC_TT_UNDEF:
pic_abort(pic, "logic flaw");
2013-10-13 06:00:12 -04:00
}
}
static void
gc_mark(pic_state *pic, pic_value v)
{
struct pic_object *obj;
if (pic_vtype(v) != PIC_VTYPE_HEAP)
2013-10-13 06:00:12 -04:00
return;
2013-10-17 09:42:20 -04:00
obj = pic_obj_ptr(v);
2013-10-13 06:00:12 -04:00
gc_mark_object(pic, obj);
}
static void
gc_mark_phase(pic_state *pic)
{
pic_value *stack;
2013-10-28 21:16:56 -04:00
pic_callinfo *ci;
2013-10-13 06:00:12 -04:00
int i;
/* block */
gc_mark_block(pic, pic->blk);
2013-10-13 06:00:12 -04:00
/* stack */
for (stack = pic->stbase; stack != pic->sp; ++stack) {
gc_mark(pic, *stack);
}
2013-10-28 21:16:56 -04:00
/* callinfo */
for (ci = pic->ci; ci != pic->cibase; --ci) {
if (ci->env) {
gc_mark_object(pic, (struct pic_object *)ci->env);
}
2013-10-28 21:16:56 -04:00
}
2013-11-17 02:02:58 -05:00
/* exception handlers */
for (i = 0; i < pic->ridx; ++i) {
gc_mark_object(pic, (struct pic_object *)pic->rescue[i]);
}
2013-10-13 06:00:12 -04:00
/* arena */
for (i = 0; i < pic->arena_idx; ++i) {
gc_mark_object(pic, pic->arena[i]);
}
2013-10-13 06:00:12 -04:00
2013-10-17 11:17:55 -04:00
/* globals */
for (i = 0; i < pic->glen; ++i) {
gc_mark(pic, pic->globals[i]);
}
2013-11-01 05:55:44 -04:00
/* macros */
for (i = 0; i < pic->mlen; ++i) {
gc_mark_object(pic, (struct pic_object *)pic->macros[i]);
}
2013-10-20 20:29:56 -04:00
/* pool */
for (i = 0; i < pic->plen; ++i) {
gc_mark(pic, pic->pool[i]);
}
2013-10-13 06:00:12 -04:00
}
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
2013-11-22 09:38:29 -05:00
printf("* finalizing object:");
pic_debug(pic, pic_obj_value(obj));
puts("");
2013-10-14 05:22:14 -04:00
#endif
2013-10-13 06:00:39 -04:00
switch (obj->tt) {
case PIC_TT_PAIR: {
break;
}
2013-10-23 13:02:07 -04:00
case PIC_TT_ENV: {
pic_free(pic, ((struct pic_env *)obj)->values);
break;
}
2013-10-13 06:00:39 -04:00
case PIC_TT_PROC: {
2013-10-14 04:32:24 -04:00
break;
2013-10-13 06:00:39 -04:00
}
2013-10-29 02:51:37 -04:00
case PIC_TT_VECTOR: {
pic_free(pic, ((struct pic_vector *)obj)->data);
break;
}
2013-11-04 22:38:23 -05:00
case PIC_TT_BLOB: {
pic_free(pic, ((struct pic_blob *)obj)->data);
break;
}
2013-10-20 19:48:55 -04:00
case PIC_TT_STRING: {
pic_free(pic, (void*)((struct pic_string *)obj)->str);
break;
}
2013-10-22 02:40:36 -04:00
case PIC_TT_PORT: {
struct pic_port *port = (struct pic_port *)obj;
2013-10-22 02:44:03 -04:00
if (port->status == PIC_PORT_OPEN) {
2013-10-22 02:40:36 -04:00
fclose(port->file);
}
break;
}
2013-11-17 03:25:26 -05:00
case PIC_TT_ERROR: {
pic_free(pic, ((struct pic_error *)obj)->msg);
break;
}
2013-11-09 00:14:25 -05:00
case PIC_TT_CONT: {
struct pic_cont *cont = (struct pic_cont *)obj;
pic_free(pic, cont->stk_ptr);
2013-11-10 20:14:42 -05:00
pic_free(pic, cont->st_ptr);
pic_free(pic, cont->ci_ptr);
2013-11-17 02:02:58 -05:00
pic_free(pic, cont->rescue);
PIC_BLK_DECREF(pic, cont->blk);
2013-11-09 00:14:25 -05:00
break;
}
2013-10-20 19:48:55 -04:00
case PIC_TT_NIL:
case PIC_TT_BOOL:
case PIC_TT_FLOAT:
2013-10-27 11:21:24 -04:00
case PIC_TT_INT:
2013-10-28 13:11:31 -04:00
case PIC_TT_SYMBOL:
2013-11-04 21:37:18 -05:00
case PIC_TT_CHAR:
2013-10-22 03:02:20 -04:00
case PIC_TT_EOF:
2013-10-20 19:48:55 -04:00
case PIC_TT_UNDEF:
pic_abort(pic, "logic flaw");
2013-10-13 06:00:39 -04:00
}
}
2013-10-13 06:00:12 -04:00
static void
2013-11-22 09:38:29 -05:00
gc_free(pic_state *pic, union header *p)
2013-10-13 06:00:12 -04:00
{
2013-11-22 09:38:29 -05:00
union header *freep, *bp;
2013-10-14 19:58:23 -04:00
2013-11-22 09:38:29 -05:00
freep = pic->heap->freep;
for (bp = freep; ! (p > bp && p < bp->s.ptr); bp = bp->s.ptr) {
if (bp >= bp->s.ptr && (p > bp || p < bp->s.ptr)) {
break;
}
}
if (p + p->s.size == bp->s.ptr) {
p->s.size += bp->s.ptr->s.size;
p->s.ptr = bp->s.ptr->s.ptr;
}
else {
p->s.ptr = bp->s.ptr;
}
if (bp + bp->s.size == p) {
bp->s.size += p->s.size;
bp->s.ptr = p->s.ptr;
}
else {
bp->s.ptr = p;
}
pic->heap->freep = bp;
}
2013-10-14 05:22:14 -04:00
2013-11-22 09:38:29 -05:00
static void
gc_sweep_phase(pic_state *pic)
{
2013-11-24 10:22:32 -05:00
union header *basep, *bp, *p, *s = NULL, *t;
2013-11-22 09:38:29 -05:00
basep = &pic->heap->base;
for (bp = basep->s.ptr; bp != basep; bp = bp->s.ptr) {
if (bp->s.size == 0) /* end of page */
continue;
for (p = bp + bp->s.size; p != bp->s.ptr; p += p->s.size) {
if (! gc_is_marked(p)) {
2013-11-24 10:22:32 -05:00
if (s == NULL) {
2013-11-24 21:10:57 -05:00
s = p;
2013-11-24 10:22:32 -05:00
}
else {
t->s.ptr = p;
}
2013-11-24 21:10:57 -05:00
t = p;
2013-11-24 10:22:32 -05:00
t->s.ptr = NULL; /* For dead objects we can safely reuse ptr field */
2013-10-13 06:00:12 -04:00
}
2013-11-22 09:38:29 -05:00
gc_unmark(p);
2013-10-13 06:00:12 -04:00
}
}
2013-10-15 20:22:15 -04:00
2013-11-22 09:38:29 -05:00
/* free! */
2013-11-24 10:22:32 -05:00
while (s) {
p = s->s.ptr;
gc_finalize_object(pic, (struct pic_object *)(s + 1));
gc_free(pic, s);
s = p;
2013-11-22 09:38:29 -05: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-30 03:43:15 -04:00
#if 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);
}
struct pic_object *
pic_obj_alloc_unsafe(pic_state *pic, size_t size, enum pic_tt tt)
{
struct pic_object *obj;
2013-10-29 03:40:25 -04:00
#if GC_STRESS
pic_gc_run(pic);
#endif
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)
pic_abort(pic, "GC memory exhausted");
2013-10-13 06:00:12 -04:00
}
obj->tt = tt;
return obj;
}
struct pic_object *
pic_obj_alloc(pic_state *pic, size_t size, enum pic_tt tt)
{
struct pic_object *obj;
obj = pic_obj_alloc_unsafe(pic, size, tt);
gc_protect(pic, obj);
return obj;
}