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"
|
|
|
|
|
|
|
|
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-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 *
|
|
|
|
pic_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;
|
|
|
|
|
|
|
|
return (void *)(p + 1);
|
|
|
|
}
|
|
|
|
|
2013-10-13 03:56:30 -04:00
|
|
|
static void
|
|
|
|
pic_gc_free(pic_state *pic, void *ptr)
|
2013-10-13 02:14:15 -04:00
|
|
|
{
|
2013-10-13 03:02:08 -04:00
|
|
|
union header *bp, *p;
|
2013-10-13 02:14:15 -04:00
|
|
|
|
2013-10-13 03:02:08 -04:00
|
|
|
bp = (union header *)ptr - 1;
|
2013-10-13 02:14:15 -04:00
|
|
|
|
2013-10-13 03:02:08 -04:00
|
|
|
for (p = pic->heap->freep; !(p < bp && bp < p->s.ptr); p = p->s.ptr) {
|
|
|
|
if (p >= p->s.ptr && (bp > p || bp < p->s.ptr))
|
2013-10-13 02:14:15 -04:00
|
|
|
break;
|
|
|
|
}
|
2013-10-13 03:02:08 -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;
|
2013-10-13 02:14:15 -04:00
|
|
|
}
|
|
|
|
else {
|
2013-10-13 03:02:08 -04:00
|
|
|
bp->s.ptr = p->s.ptr;
|
2013-10-13 02:14:15 -04:00
|
|
|
}
|
2013-10-13 03:02:08 -04:00
|
|
|
if (p + p->s.size == bp) {
|
|
|
|
p->s.size += bp->s.size;
|
|
|
|
p->s.ptr = bp->s.ptr;
|
2013-10-13 02:14:15 -04:00
|
|
|
} else {
|
2013-10-13 03:02:08 -04:00
|
|
|
p->s.ptr = bp;
|
2013-10-13 02:14:15 -04:00
|
|
|
}
|
|
|
|
pic->heap->freep = p;
|
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;
|
|
|
|
|
|
|
|
obj = (struct pic_object *)malloc(size);
|
|
|
|
obj->tt = tt;
|
|
|
|
|
2013-10-13 04:25:36 -04:00
|
|
|
gc_protect(pic, obj);
|
2013-10-10 03:44:51 -04:00
|
|
|
return obj;
|
|
|
|
}
|