2016-06-19 07:53:46 -04:00
|
|
|
/**
|
|
|
|
* See Copyright Notice in picrin.h
|
|
|
|
*/
|
|
|
|
|
2014-08-25 00:38:09 -04:00
|
|
|
#include "picrin.h"
|
2017-05-05 23:53:20 -04:00
|
|
|
#include "value.h"
|
2017-03-28 10:09:40 -04:00
|
|
|
#include "object.h"
|
|
|
|
#include "state.h"
|
2016-06-19 07:53:46 -04:00
|
|
|
|
2017-04-12 00:35:50 -04:00
|
|
|
#define PAGE_UNITS ((PIC_HEAP_PAGE_SIZE - offsetof(struct heap_page, u)) / sizeof(union header))
|
2016-06-19 13:36:41 -04:00
|
|
|
|
2017-04-12 00:35:50 -04:00
|
|
|
#define alignof(type) offsetof(struct { char c; type m; }, m)
|
|
|
|
#define roundup(n,unit) (((n) + (unit) - 1) / (unit) * (unit))
|
2016-06-19 07:53:46 -04:00
|
|
|
|
|
|
|
struct object {
|
|
|
|
union {
|
|
|
|
struct basic basic;
|
2017-04-04 01:54:58 -04:00
|
|
|
struct symbol sym;
|
2016-06-19 07:53:46 -04:00
|
|
|
struct string str;
|
|
|
|
struct blob blob;
|
|
|
|
struct pair pair;
|
|
|
|
struct vector vec;
|
|
|
|
struct dict dict;
|
2017-04-29 11:23:38 -04:00
|
|
|
struct attr attr;
|
2016-06-19 07:53:46 -04:00
|
|
|
struct data data;
|
|
|
|
struct record rec;
|
|
|
|
struct proc proc;
|
2017-04-09 02:49:04 -04:00
|
|
|
struct frame frame;
|
2017-03-30 10:29:08 -04:00
|
|
|
struct irep irep;
|
2016-06-19 07:53:46 -04:00
|
|
|
} u;
|
|
|
|
};
|
|
|
|
|
2017-04-12 00:35:50 -04:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2016-06-19 13:36:41 -04:00
|
|
|
struct heap {
|
2016-06-19 08:22:37 -04:00
|
|
|
union header base, *freep;
|
2016-06-19 07:53:46 -04:00
|
|
|
struct heap_page *pages;
|
2017-04-29 11:23:38 -04:00
|
|
|
struct attr *attrs; /* weak map chain */
|
2016-06-19 07:53:46 -04:00
|
|
|
};
|
|
|
|
|
2017-04-15 23:58:18 -04:00
|
|
|
#define unitsof(type) ((type2size(type) + sizeof(union header) - 1) / sizeof(union header))
|
2016-06-19 13:36:41 -04:00
|
|
|
|
|
|
|
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;
|
2017-04-29 11:23:38 -04:00
|
|
|
heap->attrs = NULL;
|
2016-06-19 13:36:41 -04:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2016-06-19 10:28:31 -04:00
|
|
|
|
2016-06-19 07:53:46 -04:00
|
|
|
#if PIC_USE_LIBC
|
|
|
|
void *
|
|
|
|
pic_default_allocf(void *PIC_UNUSED(userdata), void *ptr, size_t size)
|
|
|
|
{
|
|
|
|
if (size != 0) {
|
|
|
|
return realloc(ptr, size);
|
|
|
|
}
|
|
|
|
free(ptr);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void *
|
|
|
|
pic_malloc(pic_state *pic, size_t size)
|
|
|
|
{
|
|
|
|
void *ptr;
|
|
|
|
|
2017-05-05 13:11:13 -04:00
|
|
|
retry:
|
2016-06-19 07:53:46 -04:00
|
|
|
ptr = pic->allocf(pic->userdata, NULL, size);
|
|
|
|
if (ptr == NULL && size > 0) {
|
2017-05-05 13:11:13 -04:00
|
|
|
pic->panicf(pic, "out of memory", 0, NULL);
|
|
|
|
goto retry;
|
2016-06-19 07:53:46 -04:00
|
|
|
}
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
pic_realloc(pic_state *pic, void *ptr, size_t size)
|
|
|
|
{
|
2017-05-05 13:11:13 -04:00
|
|
|
retry:
|
2016-06-19 07:53:46 -04:00
|
|
|
ptr = pic->allocf(pic->userdata, ptr, size);
|
|
|
|
if (ptr == NULL && size > 0) {
|
2017-05-05 13:11:13 -04:00
|
|
|
pic->panicf(pic, "out of memory", 0, NULL);
|
|
|
|
goto retry;
|
2016-06-19 07:53:46 -04:00
|
|
|
}
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
pic_calloc(pic_state *pic, size_t count, size_t size)
|
|
|
|
{
|
|
|
|
void *ptr;
|
|
|
|
|
|
|
|
size *= count;
|
2017-05-05 13:11:13 -04:00
|
|
|
retry:
|
2016-06-19 07:53:46 -04:00
|
|
|
ptr = pic->allocf(pic->userdata, NULL, size);
|
|
|
|
if (ptr == NULL && size > 0) {
|
2017-05-05 13:11:13 -04:00
|
|
|
pic->panicf(pic, "out of memory", 0, NULL);
|
|
|
|
goto retry;
|
2016-06-19 07:53:46 -04:00
|
|
|
}
|
|
|
|
memset(ptr, 0, size);
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
pic_free(pic_state *pic, void *ptr)
|
|
|
|
{
|
|
|
|
pic->allocf(pic->userdata, ptr, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gc_protect(pic_state *pic, struct object *obj)
|
|
|
|
{
|
2017-04-15 16:20:55 -04:00
|
|
|
if (pic->ai >= pic->arena_size) {
|
2016-06-19 07:53:46 -04:00
|
|
|
pic->arena_size = pic->arena_size * 2 + 1;
|
|
|
|
pic->arena = pic_realloc(pic, pic->arena, sizeof(struct object *) * pic->arena_size);
|
|
|
|
}
|
2017-04-15 16:20:55 -04:00
|
|
|
pic->arena[pic->ai++] = obj;
|
2016-06-19 07:53:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pic_value
|
|
|
|
pic_protect(pic_state *pic, pic_value v)
|
|
|
|
{
|
2017-05-05 23:53:20 -04:00
|
|
|
if (! pic_obj_p(pic, v))
|
2016-06-19 07:53:46 -04:00
|
|
|
return v;
|
|
|
|
|
2017-05-05 23:53:20 -04:00
|
|
|
gc_protect(pic, pic_ptr(pic, v));
|
2016-06-19 07:53:46 -04:00
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
pic_enter(pic_state *pic)
|
|
|
|
{
|
2017-04-15 16:20:55 -04:00
|
|
|
return pic->ai;
|
2016-06-19 07:53:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
pic_leave(pic_state *pic, size_t state)
|
|
|
|
{
|
2017-04-15 16:20:55 -04:00
|
|
|
pic->ai = state;
|
2016-06-19 07:53:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
pic_alloca(pic_state *pic, size_t n)
|
|
|
|
{
|
2017-03-30 04:42:02 -04:00
|
|
|
static const pic_data_type t = { "pic_alloca", pic_free };
|
2016-06-19 07:53:46 -04:00
|
|
|
|
2017-04-01 05:57:24 -04:00
|
|
|
return pic_data(pic, pic_data_value(pic, pic_malloc(pic, n), &t));
|
2016-06-19 07:53:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* MARK */
|
|
|
|
|
2017-04-15 05:59:41 -04:00
|
|
|
PIC_STATIC_INLINE bool
|
2017-04-19 01:00:02 -04:00
|
|
|
is_alive(struct object *obj)
|
2016-06-19 13:36:41 -04:00
|
|
|
{
|
2017-04-09 06:14:02 -04:00
|
|
|
return obj->u.basic.tt & GC_MARK;
|
2016-06-19 13:36:41 -04:00
|
|
|
}
|
|
|
|
|
2017-04-15 05:59:41 -04:00
|
|
|
PIC_STATIC_INLINE void
|
|
|
|
mark(struct object *obj)
|
2016-06-19 13:36:41 -04:00
|
|
|
{
|
2017-04-09 06:14:02 -04:00
|
|
|
obj->u.basic.tt |= GC_MARK;
|
2016-06-19 13:36:41 -04:00
|
|
|
}
|
2016-06-19 07:53:46 -04:00
|
|
|
static void gc_mark_object(pic_state *, struct object *);
|
|
|
|
|
|
|
|
static void
|
|
|
|
gc_mark(pic_state *pic, pic_value v)
|
|
|
|
{
|
2017-05-05 23:53:20 -04:00
|
|
|
if (! pic_obj_p(pic, v))
|
2016-06-19 07:53:46 -04:00
|
|
|
return;
|
|
|
|
|
2017-05-05 23:53:20 -04:00
|
|
|
gc_mark_object(pic, pic_ptr(pic, v));
|
2016-06-19 07:53:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gc_mark_object(pic_state *pic, struct object *obj)
|
|
|
|
{
|
|
|
|
loop:
|
|
|
|
|
2017-04-19 01:00:02 -04:00
|
|
|
if (is_alive(obj))
|
2016-06-19 07:53:46 -04:00
|
|
|
return;
|
|
|
|
|
2017-04-15 05:59:41 -04:00
|
|
|
mark(obj);
|
2016-06-19 07:53:46 -04:00
|
|
|
|
|
|
|
#define LOOP(o) obj = (struct object *)(o); goto loop
|
|
|
|
|
2017-05-05 23:53:20 -04:00
|
|
|
switch (obj_type(obj)) {
|
2016-06-19 07:53:46 -04:00
|
|
|
case PIC_TYPE_PAIR: {
|
|
|
|
gc_mark(pic, obj->u.pair.car);
|
2017-05-05 23:53:20 -04:00
|
|
|
if (pic_obj_p(pic, obj->u.pair.cdr)) {
|
|
|
|
LOOP(pic_ptr(pic, obj->u.pair.cdr));
|
2016-06-19 07:53:46 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2017-04-09 02:49:04 -04:00
|
|
|
case PIC_TYPE_FRAME: {
|
2016-06-19 07:53:46 -04:00
|
|
|
int i;
|
|
|
|
|
2017-04-09 02:49:04 -04:00
|
|
|
for (i = 0; i < obj->u.frame.regc; ++i) {
|
|
|
|
gc_mark(pic, obj->u.frame.regs[i]);
|
2016-06-19 07:53:46 -04:00
|
|
|
}
|
2017-04-09 02:49:04 -04:00
|
|
|
if (obj->u.frame.up) {
|
|
|
|
LOOP(obj->u.frame.up);
|
2016-06-19 07:53:46 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2017-03-30 10:29:08 -04:00
|
|
|
case PIC_TYPE_PROC_FUNC: {
|
2017-04-14 10:40:07 -04:00
|
|
|
if (obj->u.proc.env) {
|
|
|
|
LOOP(obj->u.proc.env);
|
2016-06-19 07:53:46 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2017-03-30 10:29:08 -04:00
|
|
|
case PIC_TYPE_PROC_IREP: {
|
2017-04-14 10:40:07 -04:00
|
|
|
if (obj->u.proc.env) {
|
|
|
|
gc_mark_object(pic, (struct object *)obj->u.proc.env);
|
2017-03-30 10:29:08 -04:00
|
|
|
}
|
2017-04-09 06:42:03 -04:00
|
|
|
LOOP(obj->u.proc.u.irep);
|
2017-03-30 10:29:08 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PIC_TYPE_IREP: {
|
|
|
|
size_t i;
|
2017-04-14 10:40:07 -04:00
|
|
|
for (i = 0; i < obj->u.irep.objc; ++i) {
|
|
|
|
gc_mark(pic, obj->u.irep.obj[i]);
|
2017-03-30 10:29:08 -04:00
|
|
|
}
|
2017-04-14 10:40:07 -04:00
|
|
|
for (i = 0; i < obj->u.irep.irepc; ++i) {
|
2017-03-30 10:29:08 -04:00
|
|
|
gc_mark_object(pic, (struct object *)obj->u.irep.irep[i]);
|
2016-06-19 07:53:46 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PIC_TYPE_STRING: {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PIC_TYPE_VECTOR: {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < obj->u.vec.len; ++i) {
|
|
|
|
gc_mark(pic, obj->u.vec.data[i]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PIC_TYPE_BLOB: {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PIC_TYPE_DATA: {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PIC_TYPE_DICT: {
|
|
|
|
pic_value key, val;
|
|
|
|
int it = 0;
|
|
|
|
|
2017-03-28 19:32:28 -04:00
|
|
|
while (pic_dict_next(pic, obj_value(pic, &obj->u.dict), &it, &key, &val)) {
|
2016-06-19 07:53:46 -04:00
|
|
|
gc_mark(pic, key);
|
|
|
|
gc_mark(pic, val);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PIC_TYPE_RECORD: {
|
2017-04-09 04:34:56 -04:00
|
|
|
gc_mark(pic, obj->u.rec.datum);
|
|
|
|
LOOP(obj->u.rec.type);
|
2016-06-19 07:53:46 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PIC_TYPE_SYMBOL: {
|
2017-04-04 01:54:58 -04:00
|
|
|
LOOP(obj->u.sym.str);
|
2016-06-19 07:53:46 -04:00
|
|
|
break;
|
|
|
|
}
|
2017-04-29 11:23:38 -04:00
|
|
|
case PIC_TYPE_ATTR: {
|
|
|
|
struct attr *attr = (struct attr *)obj;
|
2016-06-19 07:53:46 -04:00
|
|
|
|
2017-04-29 11:23:38 -04:00
|
|
|
attr->prev = pic->heap->attrs;
|
|
|
|
pic->heap->attrs = attr;
|
2016-06-19 07:53:46 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
PIC_UNREACHABLE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gc_mark_phase(pic_state *pic)
|
|
|
|
{
|
2017-04-14 10:40:07 -04:00
|
|
|
struct context *cxt;
|
2016-06-19 07:53:46 -04:00
|
|
|
size_t j;
|
|
|
|
|
2017-04-29 11:23:38 -04:00
|
|
|
assert(pic->heap->attrs == NULL);
|
2016-06-19 07:53:46 -04:00
|
|
|
|
2017-04-14 10:40:07 -04:00
|
|
|
/* 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);
|
2017-04-25 13:30:17 -04:00
|
|
|
gc_mark(pic, cxt->conts);
|
2016-06-19 07:53:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* arena */
|
2017-04-15 16:20:55 -04:00
|
|
|
for (j = 0; j < pic->ai; ++j) {
|
2016-06-19 07:53:46 -04:00
|
|
|
gc_mark_object(pic, (struct object *)pic->arena[j]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* global variables */
|
|
|
|
gc_mark(pic, pic->globals);
|
|
|
|
|
2017-03-31 01:39:01 -04:00
|
|
|
/* dynamic environment */
|
|
|
|
gc_mark(pic, pic->dyn_env);
|
|
|
|
|
2017-04-14 10:40:07 -04:00
|
|
|
/* top continuation */
|
|
|
|
gc_mark(pic, pic->halt);
|
|
|
|
|
2016-06-19 07:53:46 -04:00
|
|
|
/* features */
|
|
|
|
gc_mark(pic, pic->features);
|
|
|
|
|
|
|
|
/* weak maps */
|
|
|
|
do {
|
|
|
|
struct object *key;
|
|
|
|
pic_value val;
|
|
|
|
int it;
|
2017-04-29 11:23:38 -04:00
|
|
|
khash_t(attr) *h;
|
|
|
|
struct attr *attr;
|
2016-06-19 07:53:46 -04:00
|
|
|
|
|
|
|
j = 0;
|
2017-04-29 11:23:38 -04:00
|
|
|
attr = pic->heap->attrs;
|
2016-06-19 07:53:46 -04:00
|
|
|
|
2017-04-29 11:23:38 -04:00
|
|
|
while (attr != NULL) {
|
|
|
|
h = &attr->hash;
|
2016-06-19 07:53:46 -04:00
|
|
|
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);
|
2017-04-19 01:00:02 -04:00
|
|
|
if (is_alive(key)) {
|
2017-05-05 23:53:20 -04:00
|
|
|
if (pic_obj_p(pic, val) && ! is_alive(pic_ptr(pic, val))) {
|
2016-06-19 07:53:46 -04:00
|
|
|
gc_mark(pic, val);
|
|
|
|
++j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-04-29 11:23:38 -04:00
|
|
|
attr = attr->prev;
|
2016-06-19 07:53:46 -04:00
|
|
|
}
|
|
|
|
} while (j > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* SWEEP */
|
|
|
|
|
|
|
|
static void
|
|
|
|
gc_finalize_object(pic_state *pic, struct object *obj)
|
|
|
|
{
|
2017-05-05 23:53:20 -04:00
|
|
|
switch (obj_type(obj)) {
|
2016-06-19 07:53:46 -04:00
|
|
|
case PIC_TYPE_VECTOR: {
|
|
|
|
pic_free(pic, obj->u.vec.data);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PIC_TYPE_BLOB: {
|
|
|
|
pic_free(pic, obj->u.blob.data);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PIC_TYPE_STRING: {
|
|
|
|
pic_rope_decref(pic, obj->u.str.rope);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PIC_TYPE_DATA: {
|
|
|
|
if (obj->u.data.type->dtor) {
|
|
|
|
obj->u.data.type->dtor(pic, obj->u.data.data);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PIC_TYPE_DICT: {
|
|
|
|
kh_destroy(dict, &obj->u.dict.hash);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PIC_TYPE_SYMBOL: {
|
|
|
|
/* TODO: remove this symbol's entry from pic->syms immediately */
|
|
|
|
break;
|
|
|
|
}
|
2017-04-29 11:23:38 -04:00
|
|
|
case PIC_TYPE_ATTR: {
|
|
|
|
kh_destroy(attr, &obj->u.attr.hash);
|
2016-06-19 07:53:46 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PIC_TYPE_IREP: {
|
2017-03-30 10:29:08 -04:00
|
|
|
struct irep *irep = &obj->u.irep;
|
2017-04-14 10:40:07 -04:00
|
|
|
if ((irep->flags & IREP_CODE_STATIC) == 0) {
|
2017-04-14 13:13:39 -04:00
|
|
|
pic_free(pic, (code_t *) irep->code);
|
2017-04-14 10:40:07 -04:00
|
|
|
}
|
|
|
|
pic_free(pic, irep->obj);
|
2017-03-30 10:29:08 -04:00
|
|
|
pic_free(pic, irep->irep);
|
2016-06-19 07:53:46 -04:00
|
|
|
break;
|
|
|
|
}
|
2017-04-12 00:09:21 -04:00
|
|
|
case PIC_TYPE_FRAME: {
|
2017-04-14 10:40:07 -04:00
|
|
|
pic_free(pic, obj->u.frame.regs);
|
2017-04-12 00:09:21 -04:00
|
|
|
break;
|
|
|
|
}
|
2016-06-19 07:53:46 -04:00
|
|
|
|
|
|
|
case PIC_TYPE_PAIR:
|
|
|
|
case PIC_TYPE_RECORD:
|
2017-03-30 10:29:08 -04:00
|
|
|
case PIC_TYPE_PROC_FUNC:
|
|
|
|
case PIC_TYPE_PROC_IREP:
|
2016-06-19 07:53:46 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
PIC_UNREACHABLE();
|
|
|
|
}
|
|
|
|
}
|
2014-08-25 00:38:09 -04:00
|
|
|
|
2017-04-12 00:18:06 -04:00
|
|
|
static size_t
|
|
|
|
type2size(int type)
|
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
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);
|
2017-04-29 11:23:38 -04:00
|
|
|
case PIC_TYPE_ATTR: return sizeof(struct attr);
|
2017-04-12 00:18:06 -04:00
|
|
|
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)
|
2016-06-19 13:36:41 -04:00
|
|
|
{
|
|
|
|
union header *p, *prevp;
|
2017-04-12 00:18:06 -04:00
|
|
|
struct object *obj;
|
2016-06-19 13:36:41 -04:00
|
|
|
size_t nunits;
|
|
|
|
|
2017-04-12 00:35:50 -04:00
|
|
|
nunits = unitsof(type);
|
2016-06-19 13:36:41 -04:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2017-04-12 00:55:51 -04:00
|
|
|
obj = (struct object *) p;
|
2017-04-12 00:18:06 -04:00
|
|
|
obj->u.basic.tt = type;
|
|
|
|
return obj;
|
2016-06-19 13:36:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-04-12 00:23:32 -04:00
|
|
|
free_chunk(pic_state *pic, union header *bp)
|
2016-06-19 13:36:41 -04:00
|
|
|
{
|
2017-04-12 00:23:32 -04:00
|
|
|
union header *p;
|
2016-06-19 13:36:41 -04:00
|
|
|
|
2017-04-12 00:23:32 -04:00
|
|
|
assert(bp != NULL);
|
2016-06-19 13:36:41 -04:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2017-04-12 00:35:50 -04:00
|
|
|
bp = page->u.basep;
|
2016-06-19 13:36:41 -04:00
|
|
|
bp->s.size = 0; /* bp is never used for allocation */
|
2017-04-12 00:23:32 -04:00
|
|
|
free_chunk(pic, bp);
|
2016-06-19 13:36:41 -04:00
|
|
|
|
2017-04-12 00:35:50 -04:00
|
|
|
np = page->u.basep + 1;
|
2016-06-19 13:36:41 -04:00
|
|
|
np->s.size = PAGE_UNITS - 1;
|
2017-04-12 00:23:32 -04:00
|
|
|
free_chunk(pic, np);
|
2016-06-19 13:36:41 -04:00
|
|
|
|
|
|
|
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;
|
2017-04-12 00:55:51 -04:00
|
|
|
size_t alive = 0, nunits;
|
2016-06-19 13:36:41 -04:00
|
|
|
|
2017-04-12 00:35:50 -04:00
|
|
|
for (bp = page->u.basep; ; bp = bp->s.ptr) {
|
2016-06-19 13:36:41 -04:00
|
|
|
p = bp + (bp->s.size ? bp->s.size : 1); /* first bp's size is 0, so force advnce */
|
2017-04-12 00:55:51 -04:00
|
|
|
while (p != bp->s.ptr) {
|
2017-04-12 00:35:50 -04:00
|
|
|
if (p < page->u.basep || page->u.basep + PAGE_UNITS <= p) {
|
2016-06-19 13:36:41 -04:00
|
|
|
goto escape;
|
|
|
|
}
|
2017-04-12 00:55:51 -04:00
|
|
|
obj = (struct object *) p;
|
2017-05-05 23:53:20 -04:00
|
|
|
nunits = unitsof(obj_type(obj));
|
2017-04-09 06:14:02 -04:00
|
|
|
if (obj->u.basic.tt & GC_MARK) {
|
|
|
|
obj->u.basic.tt &= ~GC_MARK;
|
2017-04-12 00:55:51 -04:00
|
|
|
alive += nunits;
|
2016-06-19 13:36:41 -04:00
|
|
|
} else {
|
2017-04-12 00:35:50 -04:00
|
|
|
gc_finalize_object(pic, obj);
|
2016-06-19 13:36:41 -04:00
|
|
|
if (head == NULL) {
|
|
|
|
head = p;
|
|
|
|
}
|
|
|
|
if (tail != NULL) {
|
|
|
|
tail->s.ptr = p;
|
|
|
|
}
|
|
|
|
tail = p;
|
2017-04-12 00:55:51 -04:00
|
|
|
tail->s.size = nunits;
|
2016-06-19 13:36:41 -04:00
|
|
|
tail->s.ptr = NULL; /* We can safely reuse ptr field of dead object */
|
|
|
|
}
|
2017-04-12 00:55:51 -04:00
|
|
|
p += nunits;
|
2016-06-19 13:36:41 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
escape:
|
|
|
|
|
|
|
|
/* free! */
|
|
|
|
while (head != NULL) {
|
|
|
|
p = head;
|
|
|
|
head = head->s.ptr;
|
2017-04-12 00:23:32 -04:00
|
|
|
free_chunk(pic, p);
|
2016-06-19 13:36:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return alive;
|
|
|
|
}
|
|
|
|
|
2016-06-19 08:22:37 -04:00
|
|
|
static void
|
|
|
|
gc_sweep_phase(pic_state *pic)
|
|
|
|
{
|
|
|
|
struct heap_page *page;
|
|
|
|
int it;
|
2017-04-29 11:23:38 -04:00
|
|
|
khash_t(attr) *h;
|
2016-06-19 08:22:37 -04:00
|
|
|
khash_t(oblist) *s = &pic->oblist;
|
2017-04-04 01:54:58 -04:00
|
|
|
struct symbol *sym;
|
2016-06-19 08:22:37 -04:00
|
|
|
struct object *obj;
|
|
|
|
size_t total = 0, inuse = 0;
|
|
|
|
|
|
|
|
/* weak maps */
|
2017-04-29 11:23:38 -04:00
|
|
|
while (pic->heap->attrs != NULL) {
|
|
|
|
h = &pic->heap->attrs->hash;
|
2016-06-19 08:22:37 -04:00
|
|
|
for (it = kh_begin(h); it != kh_end(h); ++it) {
|
|
|
|
if (! kh_exist(h, it))
|
|
|
|
continue;
|
|
|
|
obj = kh_key(h, it);
|
2017-04-19 01:00:02 -04:00
|
|
|
if (! is_alive(obj)) {
|
2017-04-29 11:23:38 -04:00
|
|
|
kh_del(attr, h, it);
|
2016-06-19 08:22:37 -04:00
|
|
|
}
|
|
|
|
}
|
2017-04-29 11:23:38 -04:00
|
|
|
pic->heap->attrs = pic->heap->attrs->prev;
|
2016-06-19 08:22:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* symbol table */
|
|
|
|
for (it = kh_begin(s); it != kh_end(s); ++it) {
|
|
|
|
if (! kh_exist(s, it))
|
|
|
|
continue;
|
|
|
|
sym = kh_val(s, it);
|
2017-04-19 01:00:02 -04:00
|
|
|
if (sym && ! is_alive((struct object *)sym)) {
|
2016-06-19 08:22:37 -04:00
|
|
|
kh_del(oblist, s, it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
page = pic->heap->pages;
|
|
|
|
while (page) {
|
|
|
|
inuse += gc_sweep_page(pic, page);
|
|
|
|
total += PAGE_UNITS;
|
|
|
|
page = page->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PIC_PAGE_REQUEST_THRESHOLD(total) <= inuse) {
|
|
|
|
heap_morecore(pic);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-19 07:53:46 -04:00
|
|
|
void
|
|
|
|
pic_gc(pic_state *pic)
|
|
|
|
{
|
|
|
|
if (! pic->gc_enable) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
gc_mark_phase(pic);
|
|
|
|
gc_sweep_phase(pic);
|
|
|
|
}
|
|
|
|
|
2016-06-19 08:22:37 -04:00
|
|
|
struct object *
|
2017-04-12 00:18:06 -04:00
|
|
|
pic_obj_alloc_unsafe(pic_state *pic, int type)
|
2016-06-19 08:22:37 -04:00
|
|
|
{
|
|
|
|
struct object *obj;
|
|
|
|
|
2017-04-19 01:00:02 -04:00
|
|
|
if (pic->heap->pages == NULL) {
|
|
|
|
heap_morecore(pic);
|
|
|
|
}
|
|
|
|
|
2016-06-19 08:22:37 -04:00
|
|
|
#if GC_STRESS
|
|
|
|
pic_gc(pic);
|
|
|
|
#endif
|
|
|
|
|
2017-04-12 00:18:06 -04:00
|
|
|
obj = obj_alloc(pic, type);
|
2016-06-19 08:22:37 -04:00
|
|
|
if (obj == NULL) {
|
|
|
|
pic_gc(pic);
|
2017-05-05 13:11:13 -04:00
|
|
|
retry:
|
2017-04-12 00:18:06 -04:00
|
|
|
obj = obj_alloc(pic, type);
|
2017-05-05 13:11:13 -04:00
|
|
|
if (obj == NULL) {
|
|
|
|
pic->panicf(pic, "out of memory", 0, NULL);
|
|
|
|
goto retry;
|
|
|
|
}
|
2016-06-19 08:22:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2016-06-19 07:53:46 -04:00
|
|
|
struct object *
|
2017-04-12 00:18:06 -04:00
|
|
|
pic_obj_alloc(pic_state *pic, int type)
|
2016-06-19 07:53:46 -04:00
|
|
|
{
|
|
|
|
struct object *obj;
|
|
|
|
|
2017-04-12 00:18:06 -04:00
|
|
|
obj = pic_obj_alloc_unsafe(pic, type);
|
2016-06-19 07:53:46 -04:00
|
|
|
|
|
|
|
gc_protect(pic, obj);
|
|
|
|
return obj;
|
|
|
|
}
|