From c8893930ae511365a0221e75c11df1cc49cebfc3 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 19 Jun 2016 21:22:37 +0900 Subject: [PATCH] remove more duplicate functions --- extlib/benz/gc.c | 111 ++++++++++++++++++++++++++++- extlib/benz/gc/bitmap.c | 103 +------------------------- extlib/benz/gc/markandsweep.c | 104 --------------------------- extlib/benz/include/picrin/setup.h | 1 - 4 files changed, 112 insertions(+), 207 deletions(-) diff --git a/extlib/benz/gc.c b/extlib/benz/gc.c index 7a2a0e2b..c06fe7a6 100644 --- a/extlib/benz/gc.c +++ b/extlib/benz/gc.c @@ -35,7 +35,9 @@ struct object { }; struct heap { - union header base, *freep; /* unused if PIC_USE_BITMAP */ +#if !PIC_USE_BITMAPGC + union header base, *freep; +#endif struct heap_page *pages; struct weak *weaks; /* weak map chain */ }; @@ -448,6 +450,85 @@ gc_finalize_object(pic_state *pic, struct object *obj) # include "./gc/markandsweep.c" #endif +struct heap * +pic_heap_open(pic_state *pic) +{ + struct heap *heap; + + heap = pic_malloc(pic, sizeof(struct heap)); + +#if !PIC_USE_BITMAPGC + 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; +#endif + + heap->pages = NULL; + heap->weaks = NULL; + + 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); +} + +static void +gc_sweep_phase(pic_state *pic) +{ + struct heap_page *page; + int it; + khash_t(weak) *h; + khash_t(oblist) *s = &pic->oblist; + symbol *sym; + struct object *obj; + size_t total = 0, inuse = 0; + + /* weak maps */ + while (pic->heap->weaks != NULL) { + h = &pic->heap->weaks->hash; + for (it = kh_begin(h); it != kh_end(h); ++it) { + if (! kh_exist(h, it)) + continue; + obj = kh_key(h, it); + if (! is_marked(pic, obj)) { + kh_del(weak, h, it); + } + } + pic->heap->weaks = pic->heap->weaks->prev; + } + + /* symbol table */ + for (it = kh_begin(s); it != kh_end(s); ++it) { + if (! kh_exist(s, it)) + continue; + sym = kh_val(s, it); + if (sym && ! is_marked(pic, (struct object *)sym)) { + 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); + } +} + void pic_gc(pic_state *pic) { @@ -461,6 +542,34 @@ pic_gc(pic_state *pic) gc_sweep_phase(pic); } +struct object * +pic_obj_alloc_unsafe(pic_state *pic, size_t size, int type) +{ + struct object *obj; + +#if GC_STRESS + pic_gc(pic); +#endif + + obj = (struct object *)heap_alloc(pic, size); + if (obj == NULL) { + pic_gc(pic); + obj = (struct object *)heap_alloc(pic, size); + if (obj == NULL) { + heap_morecore(pic); + obj = (struct object *)heap_alloc(pic, size); + if (obj == NULL) + pic_panic(pic, "GC memory exhausted"); + } + } +#if !PIC_USE_BITMAPGC + obj->u.basic.gc_mark = WHITE; +#endif + obj->u.basic.tt = type; + + return obj; +} + struct object * pic_obj_alloc(pic_state *pic, size_t size, int type) { diff --git a/extlib/benz/gc/bitmap.c b/extlib/benz/gc/bitmap.c index deb1f505..ce7d39e4 100644 --- a/extlib/benz/gc/bitmap.c +++ b/extlib/benz/gc/bitmap.c @@ -18,32 +18,6 @@ struct heap_page { uint32_t index[BITMAP_SIZE / UNIT_SIZE]; }; -struct heap * -pic_heap_open(pic_state *pic) -{ - struct heap *heap; - - heap = pic_malloc(pic, sizeof(struct heap)); - - heap->pages = NULL; - heap->weaks = NULL; - - 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); -} - /* bitmap */ static union header * @@ -55,9 +29,9 @@ index2header(struct heap_page *page, size_t index) static struct heap_page * obj2page(pic_state *PIC_UNUSED(pic), union header *h) { - unsigned long int mask = ~(PIC_HEAP_PAGE_SIZE - 1); + static const unsigned long mask = ~(PIC_HEAP_PAGE_SIZE - 1); - return (struct heap_page *)(((unsigned long int) h) & mask); + return (struct heap_page *)(((unsigned long)h) & mask); } static int @@ -105,7 +79,6 @@ is_marked_at(uint32_t *bitmap, size_t index, size_t size) } return 0; - } static void * @@ -223,53 +196,6 @@ gc_sweep_page(pic_state *pic, struct heap_page *page) return inuse; } -static void -gc_sweep_phase(pic_state *pic) -{ - struct heap_page *page; - int it; - khash_t(weak) *h; - khash_t(oblist) *s = &pic->oblist; - symbol *sym; - struct object *obj; - size_t total = 0, inuse = 0; - - /* weak maps */ - while (pic->heap->weaks != NULL) { - h = &pic->heap->weaks->hash; - for (it = kh_begin(h); it != kh_end(h); ++it) { - if (! kh_exist(h, it)) - continue; - obj = kh_key(h, it); - if (! is_marked(pic, obj)) { - kh_del(weak, h, it); - } - } - pic->heap->weaks = pic->heap->weaks->prev; - } - - /* symbol table */ - for (it = kh_begin(s); it != kh_end(s); ++it) { - if (! kh_exist(s, it)) - continue; - sym = kh_val(s, it); - if (sym && ! is_marked(pic, (struct object *)sym)) { - 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); - } -} - void gc_init(pic_state *pic) { @@ -285,28 +211,3 @@ gc_init(pic_state *pic) page = page->next; } } - -struct object * -pic_obj_alloc_unsafe(pic_state *pic, size_t size, int type) -{ - struct object *obj; - -#if GC_STRESS - pic_gc(pic); -#endif - - obj = (struct object *)heap_alloc(pic, size); - if (obj == NULL) { - pic_gc(pic); - obj = (struct object *)heap_alloc(pic, size); - if (obj == NULL) { - heap_morecore(pic); - obj = (struct object *)heap_alloc(pic, size); - if (obj == NULL) - pic_panic(pic, "GC memory exhausted"); - } - } - obj->u.basic.tt = type; - - return obj; -} diff --git a/extlib/benz/gc/markandsweep.c b/extlib/benz/gc/markandsweep.c index f72e6bab..a8891446 100644 --- a/extlib/benz/gc/markandsweep.c +++ b/extlib/benz/gc/markandsweep.c @@ -18,37 +18,6 @@ struct heap_page { union header basep[1]; }; -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; - - heap->weaks = NULL; - - 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); -} - static void * heap_alloc(pic_state *pic, size_t size) { @@ -186,81 +155,8 @@ gc_sweep_page(pic_state *pic, struct heap_page *page) return alive; } -static void -gc_sweep_phase(pic_state *pic) -{ - struct heap_page *page; - int it; - khash_t(weak) *h; - khash_t(oblist) *s = &pic->oblist; - symbol *sym; - struct object *obj; - size_t total = 0, inuse = 0; - - /* weak maps */ - while (pic->heap->weaks != NULL) { - h = &pic->heap->weaks->hash; - for (it = kh_begin(h); it != kh_end(h); ++it) { - if (! kh_exist(h, it)) - continue; - obj = kh_key(h, it); - if (obj->u.basic.gc_mark == WHITE) { - kh_del(weak, h, it); - } - } - pic->heap->weaks = pic->heap->weaks->prev; - } - - /* symbol table */ - for (it = kh_begin(s); it != kh_end(s); ++it) { - if (! kh_exist(s, it)) - continue; - sym = kh_val(s, it); - if (sym && ! is_marked(pic, (struct object *)sym)) { - 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); - } -} - static void gc_init(pic_state *PIC_UNUSED(pic)) { return; } - -struct object * -pic_obj_alloc_unsafe(pic_state *pic, size_t size, int type) -{ - struct object *obj; - -#if GC_STRESS - pic_gc(pic); -#endif - - obj = (struct object *)heap_alloc(pic, size); - if (obj == NULL) { - pic_gc(pic); - obj = (struct object *)heap_alloc(pic, size); - if (obj == NULL) { - heap_morecore(pic); - obj = (struct object *)heap_alloc(pic, size); - if (obj == NULL) - pic_panic(pic, "GC memory exhausted"); - } - } - obj->u.basic.gc_mark = WHITE; - obj->u.basic.tt = type; - - return obj; -} diff --git a/extlib/benz/include/picrin/setup.h b/extlib/benz/include/picrin/setup.h index 3f339142..ad185e00 100644 --- a/extlib/benz/include/picrin/setup.h +++ b/extlib/benz/include/picrin/setup.h @@ -466,4 +466,3 @@ double PIC_CSTRING_TO_DOUBLE(const char *); # define PIC_MEMALIGN(pic, buf, alignment, size) posix_memalign(buf, alignment, size) # define PIC_USE_BITMAPGC 1 #endif -