temporarily remove bitmap gc

This commit is contained in:
Yuichi Nishiwaki 2017-04-12 02:54:03 +09:00
parent 972e9eecc1
commit 1063c45105
3 changed files with 4 additions and 237 deletions

209
lib/gc.c
View File

@ -35,8 +35,6 @@ struct object {
} u; } u;
}; };
#if !PIC_BITMAP_GC
struct heap { struct heap {
union header base, *freep; union header base, *freep;
struct heap_page *pages; struct heap_page *pages;
@ -48,26 +46,6 @@ struct heap_page {
union header basep[1]; union header basep[1];
}; };
#else
struct heap {
struct heap_page *pages;
struct weak *weaks; /* weak map chain */
};
#define UNIT_SIZE (sizeof(uint32_t) * CHAR_BIT)
#define BITMAP_SIZE (PIC_HEAP_PAGE_SIZE / sizeof(union header) / UNIT_SIZE)
struct heap_page {
struct heap_page *next;
size_t freep;
uint32_t bitmap[BITMAP_SIZE];
uint32_t shadow[BITMAP_SIZE];
union header basep[1];
};
#endif
struct heap * struct heap *
pic_heap_open(pic_state *pic) pic_heap_open(pic_state *pic)
{ {
@ -75,11 +53,9 @@ pic_heap_open(pic_state *pic)
heap = pic_malloc(pic, sizeof(struct heap)); heap = pic_malloc(pic, sizeof(struct heap));
#if !PIC_BITMAP_GC
heap->base.s.ptr = &heap->base; heap->base.s.ptr = &heap->base;
heap->base.s.size = 0; /* not 1, since it must never be used for allocation */ heap->base.s.size = 0; /* not 1, since it must never be used for allocation */
heap->freep = &heap->base; heap->freep = &heap->base;
#endif
heap->pages = NULL; heap->pages = NULL;
heap->weaks = NULL; heap->weaks = NULL;
@ -197,8 +173,6 @@ pic_alloca(pic_state *pic, size_t n)
/* MARK */ /* MARK */
#if !PIC_BITMAP_GC
static bool static bool
is_marked(pic_state *PIC_UNUSED(pic), struct object *obj) is_marked(pic_state *PIC_UNUSED(pic), struct object *obj)
{ {
@ -210,95 +184,6 @@ mark(pic_state *PIC_UNUSED(pic), struct object *obj)
{ {
obj->u.basic.tt |= GC_MARK; obj->u.basic.tt |= GC_MARK;
} }
#else
static union header *
index2header(struct heap_page *page, size_t index)
{
return page->basep + index;
}
static struct heap_page *
obj2page(pic_state *PIC_UNUSED(pic), union header *h)
{
return (struct heap_page *)(((unsigned long)h) & ~(PIC_HEAP_PAGE_SIZE - 1));
}
static int
popcount32(uint32_t bits)
{
bits = bits - (bits >> 1 & 0x55555555);
bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333);
bits = bits + ((bits >> 4) & 0x0f0f0f0f);
bits = bits * 0x01010101;
return bits >> 24;
}
static void
mark_at(struct heap_page *page, size_t index, size_t size)
{
size_t mark_size;
while (0 < size) {
if (size <= UNIT_SIZE - (index % UNIT_SIZE))
mark_size = size;
else
mark_size = UNIT_SIZE - (index % UNIT_SIZE);
page->bitmap[index / UNIT_SIZE] |= ~(-1 << mark_size) << (index % UNIT_SIZE);
size -= mark_size;
index += mark_size;
}
}
static int
is_marked_at(uint32_t *bitmap, size_t index, size_t size)
{
size_t test_size;
while (0 < size) {
if (size <= UNIT_SIZE - (index % UNIT_SIZE))
test_size = size;
else
test_size = UNIT_SIZE - (index % UNIT_SIZE);
if ((bitmap[index / UNIT_SIZE] >> (index % UNIT_SIZE)) & ~((~0) << test_size))
return 1;
size -= test_size;
index += test_size;
}
return 0;
}
static bool
is_marked(pic_state *pic, struct object *obj)
{
union header *h = ((union header *)obj) - 1;
struct heap_page *page;
size_t index;
page = obj2page(pic, h);
index = h - page->basep;
return is_marked_at(page->bitmap, index, h->s.size);
}
static void
mark(pic_state *pic, struct object *obj)
{
union header *h = ((union header *)obj) - 1;
struct heap_page *page;
size_t index;
page = obj2page(pic, h);
index = h - page->basep;
mark_at(page, index, h->s.size);
}
#endif
static void gc_mark_object(pic_state *, struct object *); static void gc_mark_object(pic_state *, struct object *);
static void static void
@ -551,8 +436,6 @@ gc_finalize_object(pic_state *pic, struct object *obj)
} }
} }
#if !PIC_BITMAP_GC
static void * static void *
heap_alloc(pic_state *pic, size_t size) heap_alloc(pic_state *pic, size_t size)
{ {
@ -678,79 +561,6 @@ gc_sweep_page(pic_state *pic, struct heap_page *page)
return alive; return alive;
} }
#else
static void *
heap_alloc(pic_state *pic, size_t size)
{
struct heap_page *page;
size_t nunits;
assert(size > 0);
nunits = (size + sizeof(union header) - 1) / sizeof(union header) + 1;
page = pic->heap->pages;
while (page) {
size_t index;
union header *h;
for (index = page->freep; index < PAGE_UNITS - nunits; ++index) {
if (! is_marked_at(page->bitmap, index, nunits)) {
mark_at(page, index, nunits);
h = index2header(page, index);
h->s.size = nunits;
page->freep = index + nunits;
return (void *)(h + 1);
}
}
page = page->next;
}
return NULL;
}
static void
heap_morecore(pic_state *pic)
{
struct heap_page *page;
assert(2 <= PAGE_UNITS);
if (PIC_MEMALIGN(pic, (void **)&page, PIC_HEAP_PAGE_SIZE, PIC_HEAP_PAGE_SIZE) != 0)
pic_panic(pic, "memory exhausted");
memset(page->bitmap, 0, sizeof(page->bitmap));
page->freep = 0;
page->next = pic->heap->pages;
pic->heap->pages = page;
}
static size_t
gc_sweep_page(pic_state *pic, struct heap_page *page)
{
size_t index, i, inuse = 0;
union header *h;
for (i = 0; i < BITMAP_SIZE; ++i) {
page->shadow[i] &= ~page->bitmap[i];
inuse += popcount32(page->bitmap[i]);
}
for (index = 0; index < PAGE_UNITS; ++index) {
if (page->shadow[index / UNIT_SIZE] & (1 << (index % UNIT_SIZE))) {
h = index2header(page, index);
index += h->s.size - 1;
gc_finalize_object(pic, (struct object *) (h + 1));
}
}
return inuse;
}
#endif
static void static void
gc_sweep_phase(pic_state *pic) gc_sweep_phase(pic_state *pic)
{ {
@ -798,23 +608,6 @@ gc_sweep_phase(pic_state *pic)
} }
} }
void
gc_init(pic_state *PIC_UNUSED(pic))
{
#if PIC_BITMAP_GC
struct heap_page *page;
page = pic->heap->pages;
while (page) {
/* clear mark bits */
memcpy(page->shadow, page->bitmap, sizeof(page->bitmap));
memset(page->bitmap, 0, sizeof(page->bitmap));
page->freep = 0;
page = page->next;
}
#endif
}
void void
pic_gc(pic_state *pic) pic_gc(pic_state *pic)
{ {
@ -822,8 +615,6 @@ pic_gc(pic_state *pic)
return; return;
} }
gc_init(pic);
gc_mark_phase(pic); gc_mark_phase(pic);
gc_sweep_phase(pic); gc_sweep_phase(pic);
} }

View File

@ -489,9 +489,3 @@ pic_dtoa(double dval, char *buf)
#else #else
# define PIC_NAN_BOXING 0 # define PIC_NAN_BOXING 0
#endif #endif
#if PIC_USE_LIBC && (defined (__unix__) || (defined (__APPLE__) && defined (__MACH__)))
# include <unistd.h>
# define PIC_MEMALIGN(pic, buf, alignment, size) posix_memalign(buf, alignment, size)
# define PIC_BITMAP_GC 1
#endif

View File

@ -11,17 +11,11 @@ extern "C" {
#include "khash.h" #include "khash.h"
#if PIC_BITMAP_GC #define OBJECT_HEADER \
# define OBJECT_HEADER \
unsigned char tt; unsigned char tt;
#else
# define OBJECT_HEADER \
unsigned char tt;
#endif
#if !PIC_BITMAP_GC #define TYPE_MASK 0x7f
# define GC_MARK 0x80 #define GC_MARK 0x80
#endif
struct object; /* defined in gc.c */ struct object; /* defined in gc.c */
@ -171,24 +165,12 @@ struct error {
if (tolen - at < e - s) pic_error(pic, "invalid range", 0); \ if (tolen - at < e - s) pic_error(pic, "invalid range", 0); \
} while (0) } while (0)
#if PIC_BITMAP_GC
PIC_STATIC_INLINE int PIC_STATIC_INLINE int
obj_type(pic_state *PIC_UNUSED(pic), void *ptr) obj_type(pic_state *PIC_UNUSED(pic), void *ptr)
{ {
return ((struct basic *)ptr)->tt; return ((struct basic *)ptr)->tt & TYPE_MASK;
} }
#else
PIC_STATIC_INLINE int
obj_type(pic_state *PIC_UNUSED(pic), void *ptr)
{
return ((struct basic *)ptr)->tt & ~GC_MARK;
}
#endif
#if !PIC_NAN_BOXING #if !PIC_NAN_BOXING
PIC_STATIC_INLINE struct object * PIC_STATIC_INLINE struct object *