2014-01-17 06:58:31 -05:00
|
|
|
/**
|
|
|
|
* See Copyright Notice in picrin.h
|
|
|
|
*/
|
|
|
|
|
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"
|
2014-02-22 01:20:53 -05:00
|
|
|
#include "picrin/pair.h"
|
|
|
|
#include "picrin/string.h"
|
|
|
|
#include "picrin/vector.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"
|
2013-11-26 07:05:02 -05:00
|
|
|
#include "picrin/macro.h"
|
2013-12-07 06:58:18 -05:00
|
|
|
#include "picrin/lib.h"
|
2014-01-08 10:39:13 -05:00
|
|
|
#include "picrin/var.h"
|
2013-10-13 02:14:15 -04:00
|
|
|
|
2013-10-14 20:07:55 -04:00
|
|
|
#if GC_DEBUG
|
2013-12-02 21:45:38 -05:00
|
|
|
# include <string.h>
|
2013-10-14 20:07:55 -04:00
|
|
|
#endif
|
|
|
|
|
2014-02-22 01:53:59 -05:00
|
|
|
union header {
|
|
|
|
struct {
|
|
|
|
union header *ptr;
|
|
|
|
size_t size;
|
|
|
|
unsigned int mark : 1;
|
|
|
|
} s;
|
|
|
|
long alignment[4];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct heap_page {
|
|
|
|
union header *basep, *endp;
|
|
|
|
struct heap_page *next;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct pic_heap {
|
|
|
|
union header base, *freep;
|
|
|
|
struct heap_page *pages;
|
|
|
|
};
|
|
|
|
|
2014-02-26 23:55:19 -05:00
|
|
|
|
2014-02-22 02:54:12 -05:00
|
|
|
static void
|
2014-02-22 01:53:59 -05:00
|
|
|
heap_init(struct pic_heap *heap)
|
2013-10-13 02:14:15 -04:00
|
|
|
{
|
2013-12-02 21:45:38 -05:00
|
|
|
heap->base.s.ptr = &heap->base;
|
|
|
|
heap->base.s.size = 0; /* not 1, since it must never be used for allocation */
|
2013-11-22 09:38:29 -05:00
|
|
|
heap->base.s.mark = PIC_GC_UNMARK;
|
2013-10-13 02:14:15 -04:00
|
|
|
|
2013-12-02 21:45:38 -05:00
|
|
|
heap->freep = &heap->base;
|
|
|
|
heap->pages = NULL;
|
2013-11-22 10:12:03 -05:00
|
|
|
|
2013-11-26 23:04:12 -05:00
|
|
|
#if GC_DEBUG
|
2014-01-05 04:10:33 -05:00
|
|
|
printf("freep = %p\n", (void *)heap->freep);
|
2013-11-22 10:12:03 -05:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-02-22 01:53:59 -05:00
|
|
|
struct pic_heap *
|
2014-02-22 02:54:12 -05:00
|
|
|
pic_heap_open()
|
2014-02-22 01:53:59 -05:00
|
|
|
{
|
|
|
|
struct pic_heap *heap;
|
|
|
|
|
|
|
|
heap = (struct pic_heap *)calloc(1, sizeof(struct pic_heap));
|
|
|
|
heap_init(heap);
|
|
|
|
return heap;
|
|
|
|
}
|
|
|
|
|
2014-01-16 08:02:30 -05:00
|
|
|
void
|
2014-02-22 02:54:12 -05:00
|
|
|
pic_heap_close(struct pic_heap *heap)
|
2014-01-16 08:02:30 -05:00
|
|
|
{
|
|
|
|
struct heap_page *page;
|
|
|
|
|
|
|
|
while (heap->pages) {
|
|
|
|
page = heap->pages;
|
|
|
|
heap->pages = heap->pages->next;
|
|
|
|
free(page);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-02 21:45:38 -05:00
|
|
|
static void gc_free(pic_state *, union header *);
|
|
|
|
|
|
|
|
static void
|
|
|
|
add_heap_page(pic_state *pic)
|
|
|
|
{
|
|
|
|
union header *up, *np;
|
|
|
|
struct heap_page *page;
|
|
|
|
size_t nu;
|
|
|
|
|
2013-12-02 23:08:23 -05:00
|
|
|
#if GC_DEBUG
|
2013-12-02 21:45:38 -05:00
|
|
|
puts("adding heap page!");
|
2013-12-02 23:08:23 -05:00
|
|
|
#endif
|
2013-12-02 21:45:38 -05:00
|
|
|
|
|
|
|
nu = (PIC_HEAP_PAGE_SIZE + sizeof(union header) - 1) / sizeof(union header) + 1;
|
|
|
|
|
|
|
|
up = (union header *)pic_calloc(pic, 1 + nu + 1, sizeof(union header));
|
|
|
|
up->s.size = nu + 1;
|
|
|
|
up->s.mark = PIC_GC_UNMARK;
|
|
|
|
gc_free(pic, up);
|
|
|
|
|
|
|
|
np = up + 1;
|
|
|
|
np->s.size = nu;
|
|
|
|
np->s.ptr = up->s.ptr;
|
|
|
|
up->s.size = 1;
|
|
|
|
up->s.ptr = np;
|
|
|
|
|
|
|
|
page = (struct heap_page *)pic_alloc(pic, sizeof(struct heap_page));
|
|
|
|
page->basep = up;
|
|
|
|
page->endp = up + nu + 1;
|
|
|
|
page->next = pic->heap->pages;
|
|
|
|
|
|
|
|
pic->heap->pages = page;
|
|
|
|
}
|
|
|
|
|
2014-02-26 23:55:19 -05:00
|
|
|
static void *
|
|
|
|
alloc(void *ptr, size_t size)
|
|
|
|
{
|
|
|
|
if (size == 0) {
|
|
|
|
if (ptr) {
|
|
|
|
free(ptr);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (ptr) {
|
|
|
|
return realloc(ptr, size);
|
|
|
|
} else {
|
|
|
|
return malloc(size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-10 03:44:51 -04:00
|
|
|
void *
|
2014-03-03 10:30:11 -05:00
|
|
|
pic_alloc(pic_state *pic, size_t size)
|
2013-10-13 03:56:30 -04:00
|
|
|
{
|
|
|
|
void *ptr;
|
|
|
|
|
2014-02-26 23:55:19 -05:00
|
|
|
ptr = alloc(NULL, size);
|
2014-01-18 10:33:51 -05:00
|
|
|
if (ptr == NULL && size > 0) {
|
2013-10-20 04:41:48 -04:00
|
|
|
pic_abort(pic, "memory exhausted");
|
2013-10-13 03:56:30 -04:00
|
|
|
}
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2013-10-15 22:21:41 -04:00
|
|
|
void *
|
|
|
|
pic_realloc(pic_state *pic, void *ptr, size_t size)
|
|
|
|
{
|
2014-02-26 23:55:19 -05:00
|
|
|
ptr = alloc(ptr, size);
|
2014-01-18 10:33:51 -05:00
|
|
|
if (ptr == NULL && size > 0) {
|
2013-10-20 04:41:48 -04:00
|
|
|
pic_abort(pic, "memory exhausted");
|
2013-10-15 22:21:41 -04:00
|
|
|
}
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2013-10-23 11:32:03 -04:00
|
|
|
void *
|
2013-11-30 22:44:43 -05:00
|
|
|
pic_calloc(pic_state *pic, size_t count, size_t size)
|
2013-10-23 11:32:03 -04:00
|
|
|
{
|
|
|
|
void *ptr;
|
|
|
|
|
2014-02-26 23:55:19 -05:00
|
|
|
size *= count;
|
|
|
|
ptr = alloc(NULL, size);
|
2014-01-18 10:33:51 -05:00
|
|
|
if (ptr == NULL && size > 0) {
|
2013-10-23 11:32:03 -04:00
|
|
|
pic_abort(pic, "memory exhausted");
|
|
|
|
}
|
2014-02-26 23:55:19 -05:00
|
|
|
memset(ptr, 0, size);
|
2013-10-23 11:32:03 -04:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2013-10-13 03:56:30 -04:00
|
|
|
void
|
|
|
|
pic_free(pic_state *pic, void *ptr)
|
|
|
|
{
|
2014-01-30 04:15:59 -05:00
|
|
|
UNUSED(pic);
|
|
|
|
|
2013-10-13 03:56:30 -04:00
|
|
|
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) {
|
2013-10-20 04:41:48 -04:00
|
|
|
pic_abort(pic, "arena overflow");
|
2013-10-13 04:25:36 -04:00
|
|
|
}
|
|
|
|
pic->arena[pic->arena_idx++] = obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
pic_gc_protect(pic_state *pic, pic_value v)
|
|
|
|
{
|
|
|
|
struct pic_object *obj;
|
|
|
|
|
2013-11-04 23:37:08 -05:00
|
|
|
if (pic_vtype(v) != PIC_VTYPE_HEAP) {
|
2013-10-13 04:25:36 -04:00
|
|
|
return;
|
|
|
|
}
|
2013-10-17 09:42:20 -04:00
|
|
|
obj = pic_obj_ptr(v);
|
2013-10-13 04:25:36 -04:00
|
|
|
|
|
|
|
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-12-02 21:45:38 -05:00
|
|
|
#if GC_DEBUG
|
|
|
|
assert(size > 0);
|
|
|
|
#endif
|
|
|
|
|
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
|
|
|
|
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)
|
2013-10-13 02:14:15 -04:00
|
|
|
break;
|
2013-10-13 03:02:08 -04:00
|
|
|
if (p == freep) {
|
2013-12-02 21:45:38 -05:00
|
|
|
return NULL;
|
2013-10-13 03:02:08 -04:00
|
|
|
}
|
2013-10-13 02:14:15 -04:00
|
|
|
}
|
2013-12-02 21:45:38 -05:00
|
|
|
|
|
|
|
#if GC_DEBUG
|
|
|
|
{
|
|
|
|
unsigned char *c;
|
2014-01-19 03:54:56 -05:00
|
|
|
size_t s, i, j;
|
2013-12-02 21:45:38 -05:00
|
|
|
if (p->s.size == nunits) {
|
|
|
|
c = (unsigned char *)(p + p->s.size - nunits + 1);
|
|
|
|
s = nunits - 1;
|
|
|
|
} else {
|
|
|
|
c = (unsigned char *)(p + p->s.size - nunits);
|
|
|
|
s = nunits;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < s; ++i) {
|
|
|
|
for (j = 0; j < sizeof(union header); ++j) {
|
|
|
|
assert(c[i * sizeof(union header) + j] == 0xAA);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
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-12-02 21:45:38 -05:00
|
|
|
|
|
|
|
#if GC_DEBUG
|
|
|
|
memset(p+1, 0, sizeof(union header) * (nunits - 1));
|
|
|
|
p->s.ptr = (union header *)0xcafebabe;
|
|
|
|
#endif
|
|
|
|
|
2013-10-13 02:14:15 -04:00
|
|
|
return (void *)(p + 1);
|
|
|
|
}
|
|
|
|
|
2013-11-29 10:07:49 -05:00
|
|
|
static void
|
|
|
|
gc_free(pic_state *pic, union header *bp)
|
|
|
|
{
|
|
|
|
union header *freep, *p;
|
|
|
|
|
2013-12-02 21:45:38 -05:00
|
|
|
#if GC_DEBUG
|
|
|
|
assert(bp != NULL);
|
|
|
|
assert(bp->s.size > 1);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if GC_DEBUG
|
|
|
|
memset(bp + 1, 0xAA, (bp->s.size - 1) * sizeof(union header));
|
|
|
|
#endif
|
|
|
|
|
2013-11-29 10:07:49 -05:00
|
|
|
freep = pic->heap->freep;
|
|
|
|
for (p = 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) {
|
|
|
|
bp->s.size += p->s.ptr->s.size;
|
|
|
|
bp->s.ptr = p->s.ptr->s.ptr;
|
2013-12-02 21:45:38 -05:00
|
|
|
|
|
|
|
#if GC_DEBUG
|
|
|
|
memset(p->s.ptr, 0xAA, sizeof(union header));
|
|
|
|
#endif
|
2013-11-29 10:07:49 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
bp->s.ptr = p->s.ptr;
|
|
|
|
}
|
2013-12-02 21:45:38 -05:00
|
|
|
if (p + p->s.size == bp && p->s.size > 1) {
|
2013-11-29 10:07:49 -05:00
|
|
|
p->s.size += bp->s.size;
|
|
|
|
p->s.ptr = bp->s.ptr;
|
2013-12-02 21:45:38 -05:00
|
|
|
|
|
|
|
#if GC_DEBUG
|
|
|
|
memset(bp, 0xAA, sizeof(union header));
|
|
|
|
#endif
|
2013-11-29 10:07:49 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
p->s.ptr = bp;
|
|
|
|
}
|
|
|
|
pic->heap->freep = p;
|
|
|
|
}
|
|
|
|
|
2013-10-13 06:00:12 -04:00
|
|
|
static void gc_mark(pic_state *, pic_value);
|
2013-11-14 21:51:58 -05:00
|
|
|
static void gc_mark_object(pic_state *pic, struct pic_object *obj);
|
|
|
|
|
|
|
|
static void
|
2014-03-07 08:09:12 -05:00
|
|
|
gc_mark_block(pic_state *pic, pic_block *blk)
|
2013-11-14 21:51:58 -05:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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-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 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-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]);
|
|
|
|
}
|
2013-10-29 03:39:57 -04:00
|
|
|
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);
|
|
|
|
}
|
2014-02-25 10:39:16 -05:00
|
|
|
if (pic_proc_irep_p(proc)) {
|
2014-01-18 02:56:20 -05:00
|
|
|
gc_mark_object(pic, (struct pic_object *)proc->u.irep);
|
|
|
|
}
|
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: {
|
2014-02-10 23:21:00 -05:00
|
|
|
struct pic_error *err = (struct pic_error *)obj;
|
|
|
|
gc_mark_object(pic,(struct pic_object *)err->msg);
|
|
|
|
gc_mark(pic, err->irrs);
|
2013-11-17 03:25:26 -05:00
|
|
|
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: {
|
2014-01-30 04:14:33 -05:00
|
|
|
size_t i;
|
2013-10-29 02:51:37 -04:00
|
|
|
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;
|
2014-01-30 04:14:33 -05:00
|
|
|
size_t i;
|
|
|
|
int j;
|
2013-11-09 00:14:25 -05:00
|
|
|
|
2013-11-14 21:51:58 -05:00
|
|
|
/* block */
|
|
|
|
gc_mark_block(pic, cont->blk);
|
|
|
|
|
2013-11-09 00:14:25 -05:00
|
|
|
/* stack */
|
2013-11-17 01:58:53 -05:00
|
|
|
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 */
|
2013-11-17 01:58:53 -05:00
|
|
|
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 */
|
2014-01-30 04:14:33 -05:00
|
|
|
for (j = 0; j < cont->arena_idx; ++j) {
|
|
|
|
gc_mark_object(pic, cont->arena[j]);
|
2013-11-09 00:14:25 -05:00
|
|
|
}
|
|
|
|
|
2014-02-01 22:11:15 -05:00
|
|
|
/* result values */
|
2014-02-01 22:27:48 -05:00
|
|
|
gc_mark(pic, cont->results);
|
2013-11-09 00:14:25 -05:00
|
|
|
break;
|
|
|
|
}
|
2014-02-11 21:13:29 -05:00
|
|
|
case PIC_TT_MACRO: {
|
|
|
|
struct pic_macro *mac = (struct pic_macro *)obj;
|
2013-11-26 11:42:13 -05:00
|
|
|
|
2014-02-11 21:13:29 -05:00
|
|
|
if (mac->proc) {
|
|
|
|
gc_mark_object(pic, (struct pic_object *)mac->proc);
|
2013-11-26 11:42:13 -05:00
|
|
|
}
|
2014-02-11 21:13:29 -05:00
|
|
|
if (mac->senv) {
|
|
|
|
gc_mark_object(pic, (struct pic_object *)mac->senv);
|
2013-11-27 01:58:28 -05:00
|
|
|
}
|
2013-11-26 07:05:02 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PIC_TT_SENV: {
|
|
|
|
struct pic_senv *senv = (struct pic_senv *)obj;
|
|
|
|
|
|
|
|
if (senv->up) {
|
|
|
|
gc_mark_object(pic, (struct pic_object *)senv->up);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2013-11-27 01:04:02 -05:00
|
|
|
case PIC_TT_SC: {
|
|
|
|
struct pic_sc *sc = (struct pic_sc *)obj;
|
|
|
|
gc_mark(pic, sc->expr);
|
|
|
|
gc_mark_object(pic, (struct pic_object *)sc->senv);
|
|
|
|
break;
|
|
|
|
}
|
2013-12-07 06:58:18 -05:00
|
|
|
case PIC_TT_LIB: {
|
|
|
|
struct pic_lib *lib = (struct pic_lib *)obj;
|
2013-12-07 21:59:13 -05:00
|
|
|
gc_mark(pic, lib->name);
|
2013-12-07 06:58:18 -05:00
|
|
|
gc_mark_object(pic, (struct pic_object *)lib->senv);
|
|
|
|
break;
|
|
|
|
}
|
2014-01-08 10:39:13 -05:00
|
|
|
case PIC_TT_VAR: {
|
|
|
|
struct pic_var *var = (struct pic_var *)obj;
|
|
|
|
gc_mark(pic, var->value);
|
|
|
|
if (var->conv) {
|
|
|
|
gc_mark_object(pic, (struct pic_object *)var->conv);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2014-01-18 02:51:54 -05:00
|
|
|
case PIC_TT_IREP: {
|
2014-01-18 07:48:50 -05:00
|
|
|
struct pic_irep *irep = (struct pic_irep *)obj;
|
2014-01-30 04:14:33 -05:00
|
|
|
size_t i;
|
2014-01-18 07:48:50 -05:00
|
|
|
|
|
|
|
for (i = 0; i < irep->ilen; ++i) {
|
|
|
|
gc_mark_object(pic, (struct pic_object *)irep->irep[i]);
|
|
|
|
}
|
2014-01-18 08:32:41 -05:00
|
|
|
for (i = 0; i < irep->plen; ++i) {
|
|
|
|
gc_mark(pic, irep->pool[i]);
|
|
|
|
}
|
2014-01-18 02:51:54 -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:
|
2013-10-20 04:41:48 -04:00
|
|
|
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;
|
|
|
|
|
2013-11-04 23:37:08 -05:00
|
|
|
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;
|
2014-01-30 04:14:33 -05:00
|
|
|
size_t i;
|
|
|
|
int j;
|
2014-02-11 20:39:20 -05:00
|
|
|
xh_iter it;
|
2013-10-13 06:00:12 -04:00
|
|
|
|
2013-11-14 21:51:58 -05:00
|
|
|
/* 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) {
|
2013-10-29 03:39:57 -04:00
|
|
|
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]);
|
|
|
|
}
|
|
|
|
|
2014-02-10 23:21:00 -05:00
|
|
|
/* error object */
|
|
|
|
if (pic->err) {
|
|
|
|
gc_mark_object(pic, (struct pic_object *)pic->err);
|
|
|
|
}
|
|
|
|
|
2013-10-13 06:00:12 -04:00
|
|
|
/* arena */
|
2014-01-30 04:14:33 -05:00
|
|
|
for (j = 0; j < pic->arena_idx; ++j) {
|
|
|
|
gc_mark_object(pic, pic->arena[j]);
|
2013-10-13 02:14:15 -04:00
|
|
|
}
|
2013-10-13 06:00:12 -04:00
|
|
|
|
2014-01-18 02:06:59 -05:00
|
|
|
/* global variables */
|
2013-10-17 11:17:55 -04:00
|
|
|
for (i = 0; i < pic->glen; ++i) {
|
|
|
|
gc_mark(pic, pic->globals[i]);
|
|
|
|
}
|
|
|
|
|
2014-02-11 20:39:20 -05:00
|
|
|
/* macro objects */
|
|
|
|
for (xh_begin(pic->macros, &it); ! xh_isend(&it); xh_next(&it)) {
|
|
|
|
gc_mark_object(pic, (struct pic_object *)it.e->val);
|
|
|
|
}
|
|
|
|
|
2013-12-07 08:36:14 -05:00
|
|
|
/* library table */
|
|
|
|
gc_mark(pic, pic->lib_tbl);
|
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-12-02 21:45:38 -05:00
|
|
|
printf("* finalizing object: %s", pic_type_repr(pic_type(pic_obj_value(obj))));
|
|
|
|
// pic_debug(pic, pic_obj_value(obj));
|
2013-11-22 09:38:29 -05:00
|
|
|
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: {
|
|
|
|
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: {
|
2014-02-27 07:54:37 -05:00
|
|
|
XROPE_DECREF(((struct pic_string *)obj)->rope);
|
2013-10-20 19:48:55 -04:00
|
|
|
break;
|
|
|
|
}
|
2013-10-22 02:40:36 -04:00
|
|
|
case PIC_TT_PORT: {
|
|
|
|
break;
|
|
|
|
}
|
2013-11-17 03:25:26 -05:00
|
|
|
case PIC_TT_ERROR: {
|
|
|
|
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);
|
2013-11-14 21:51:58 -05:00
|
|
|
PIC_BLK_DECREF(pic, cont->blk);
|
2013-11-09 00:14:25 -05:00
|
|
|
break;
|
|
|
|
}
|
2013-11-26 07:05:02 -05:00
|
|
|
case PIC_TT_SENV: {
|
|
|
|
struct pic_senv *senv = (struct pic_senv *)obj;
|
2014-02-11 20:39:20 -05:00
|
|
|
xh_destroy(senv->name);
|
2013-11-26 07:05:02 -05:00
|
|
|
break;
|
|
|
|
}
|
2014-02-11 21:13:29 -05:00
|
|
|
case PIC_TT_MACRO: {
|
2013-11-26 07:05:02 -05:00
|
|
|
break;
|
|
|
|
}
|
2013-11-27 01:04:02 -05:00
|
|
|
case PIC_TT_SC: {
|
|
|
|
break;
|
|
|
|
}
|
2013-12-07 06:58:18 -05:00
|
|
|
case PIC_TT_LIB: {
|
|
|
|
struct pic_lib *lib = (struct pic_lib *)obj;
|
2014-01-30 00:33:16 -05:00
|
|
|
xh_destroy(lib->exports);
|
2013-12-07 06:58:18 -05:00
|
|
|
break;
|
|
|
|
}
|
2014-01-08 10:39:13 -05:00
|
|
|
case PIC_TT_VAR: {
|
|
|
|
break;
|
|
|
|
}
|
2014-01-18 02:51:54 -05:00
|
|
|
case PIC_TT_IREP: {
|
2014-01-18 02:53:39 -05:00
|
|
|
struct pic_irep *irep = (struct pic_irep *)obj;
|
|
|
|
pic_free(pic, irep->code);
|
2014-01-18 08:31:25 -05:00
|
|
|
pic_free(pic, irep->irep);
|
2014-01-18 08:32:41 -05:00
|
|
|
pic_free(pic, irep->pool);
|
2014-01-18 02:51:54 -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:
|
2013-10-20 04:41:48 -04:00
|
|
|
pic_abort(pic, "logic flaw");
|
2013-10-13 06:00:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-22 09:38:29 -05:00
|
|
|
static void
|
2013-12-02 21:45:38 -05:00
|
|
|
gc_sweep_page(pic_state *pic, struct heap_page *page)
|
2013-11-22 09:38:29 -05:00
|
|
|
{
|
2013-12-02 21:45:38 -05:00
|
|
|
#if GC_DEBUG
|
|
|
|
static union header *NIL = (union header *)0xdeadbeef;
|
|
|
|
#else
|
|
|
|
static union header *NIL = NULL;
|
|
|
|
#endif
|
|
|
|
union header *bp, *p, *s = NIL, *t;
|
|
|
|
|
|
|
|
#if GC_DEBUG
|
2013-11-28 22:03:48 -05:00
|
|
|
int c = 0;
|
|
|
|
#endif
|
2013-11-22 09:38:29 -05:00
|
|
|
|
2013-12-02 21:45:38 -05:00
|
|
|
for (bp = page->basep; ; bp = bp->s.ptr) {
|
2013-11-22 09:38:29 -05:00
|
|
|
for (p = bp + bp->s.size; p != bp->s.ptr; p += p->s.size) {
|
2013-12-02 21:45:38 -05:00
|
|
|
if (p == page->endp) {
|
|
|
|
goto escape;
|
2013-11-28 22:05:22 -05:00
|
|
|
}
|
2013-11-22 09:38:29 -05:00
|
|
|
if (! gc_is_marked(p)) {
|
2013-12-02 21:45:38 -05:00
|
|
|
if (s == NIL) {
|
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-12-02 21:45:38 -05:00
|
|
|
t->s.ptr = NIL; /* 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-13 02:14:15 -04:00
|
|
|
}
|
2013-12-02 21:45:38 -05:00
|
|
|
escape:
|
2013-10-15 20:22:15 -04:00
|
|
|
|
2013-11-22 09:38:29 -05:00
|
|
|
/* free! */
|
2013-12-02 21:45:38 -05:00
|
|
|
while (s != NIL) {
|
|
|
|
t = s->s.ptr;
|
2013-11-24 10:22:32 -05:00
|
|
|
gc_finalize_object(pic, (struct pic_object *)(s + 1));
|
|
|
|
gc_free(pic, s);
|
2013-12-02 21:45:38 -05:00
|
|
|
s = t;
|
|
|
|
|
|
|
|
#if GC_DEBUG
|
2013-11-28 22:03:48 -05:00
|
|
|
c++;
|
|
|
|
#endif
|
2013-11-22 09:38:29 -05:00
|
|
|
}
|
2013-11-28 22:03:48 -05:00
|
|
|
|
2013-12-02 21:45:38 -05:00
|
|
|
#if GC_DEBUG
|
|
|
|
printf("freed objects count: %d\n", c);
|
2013-11-28 22:03:48 -05:00
|
|
|
#endif
|
2013-10-13 06:00:12 -04:00
|
|
|
}
|
|
|
|
|
2013-12-02 21:45:38 -05:00
|
|
|
static void
|
|
|
|
gc_sweep_phase(pic_state *pic)
|
|
|
|
{
|
|
|
|
struct heap_page *page = pic->heap->pages;
|
|
|
|
|
|
|
|
while (page) {
|
|
|
|
gc_sweep_page(pic, page);
|
|
|
|
page = page->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-14 04:07:09 -04:00
|
|
|
void
|
2013-10-13 06:00:12 -04:00
|
|
|
pic_gc_run(pic_state *pic)
|
|
|
|
{
|
2013-12-02 21:45:38 -05:00
|
|
|
#if GC_DEBUG
|
|
|
|
struct heap_page *page;
|
|
|
|
#endif
|
|
|
|
|
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-12-02 21:45:38 -05:00
|
|
|
|
2013-10-13 06:00:12 -04:00
|
|
|
gc_mark_phase(pic);
|
|
|
|
gc_sweep_phase(pic);
|
2013-12-02 21:45:38 -05:00
|
|
|
|
|
|
|
#if GC_DEBUG
|
|
|
|
for (page = pic->heap->pages; page; page = page->next) {
|
|
|
|
union header *bp, *p;
|
|
|
|
unsigned char *c;
|
|
|
|
|
|
|
|
for (bp = page->basep; ; bp = bp->s.ptr) {
|
|
|
|
for (c = (unsigned char *)(bp+1); c != (unsigned char *)(bp + bp->s.size); ++c) {
|
|
|
|
assert(*c == 0xAA);
|
|
|
|
}
|
|
|
|
for (p = bp + bp->s.size; p != bp->s.ptr; p += p->s.size) {
|
|
|
|
if (p == page->endp) {
|
|
|
|
/* if (page->next) */
|
|
|
|
/* assert(bp->s.ptr == page->next->basep); */
|
|
|
|
/* else */
|
|
|
|
/* assert(bp->s.ptr == &pic->heap->base); */
|
|
|
|
goto escape;
|
|
|
|
}
|
|
|
|
assert(! gc_is_marked(p));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
escape:
|
|
|
|
((void)0);
|
|
|
|
}
|
|
|
|
|
|
|
|
puts("not error on heap found! gc successfully finished");
|
|
|
|
#endif
|
2013-10-10 03:44:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
struct pic_object *
|
2013-11-15 05:30:53 -05:00
|
|
|
pic_obj_alloc_unsafe(pic_state *pic, size_t size, enum pic_tt tt)
|
2013-10-10 03:44:51 -04:00
|
|
|
{
|
|
|
|
struct pic_object *obj;
|
|
|
|
|
2013-12-02 21:45:38 -05:00
|
|
|
#if GC_DEBUG
|
|
|
|
printf("*allocating: %s\n", pic_type_repr(tt));
|
|
|
|
#endif
|
|
|
|
|
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);
|
2013-12-02 21:45:38 -05:00
|
|
|
if (obj == NULL) {
|
|
|
|
add_heap_page(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
|
|
|
}
|
2013-10-10 03:44:51 -04:00
|
|
|
obj->tt = tt;
|
|
|
|
|
2013-11-15 05:30:53 -05:00
|
|
|
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);
|
|
|
|
|
2013-10-13 04:25:36 -04:00
|
|
|
gc_protect(pic, obj);
|
2013-10-10 03:44:51 -04:00
|
|
|
return obj;
|
|
|
|
}
|