1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-05-13 01:00:21 +02:00

Convert semi-space collector to new API

This commit is contained in:
Andy Wingo 2022-03-28 22:13:52 +02:00
parent 06a213d1ed
commit 81037fd6d2

161
semi.h
View file

@ -1,3 +1,4 @@
#include <malloc.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
@ -6,16 +7,31 @@
#include "precise-roots.h" #include "precise-roots.h"
struct context { struct semi_space {
uintptr_t hp; uintptr_t hp;
uintptr_t limit; uintptr_t limit;
uintptr_t heap_base; uintptr_t base;
size_t heap_size; size_t size;
struct handle *roots;
void *mem;
size_t mem_size;
long count; long count;
}; };
struct heap {
struct semi_space semi_space;
};
// One mutator per space, can just store the heap in the mutator.
struct mutator {
struct heap heap;
struct handle *roots;
};
static inline struct heap* mutator_heap(struct mutator *mut) {
return &mut->heap;
}
static inline struct semi_space* heap_semi_space(struct heap *heap) {
return &heap->semi_space;
}
static inline struct semi_space* mutator_semi_space(struct mutator *mut) {
return heap_semi_space(mutator_heap(mut));
}
static const uintptr_t ALIGNMENT = 8; static const uintptr_t ALIGNMENT = 8;
@ -29,24 +45,24 @@ static inline void clear_memory(uintptr_t addr, size_t size) {
memset((char*)addr, 0, size); memset((char*)addr, 0, size);
} }
static void collect(struct context *cx) NEVER_INLINE; static void collect(struct mutator *mut) NEVER_INLINE;
static void collect_for_alloc(struct context *cx, size_t bytes) NEVER_INLINE; static void collect_for_alloc(struct mutator *mut, size_t bytes) NEVER_INLINE;
static void visit(void **loc, void *visit_data); static void visit(void **loc, void *visit_data);
static void flip(struct context *cx) { static void flip(struct semi_space *space) {
uintptr_t split = cx->heap_base + (cx->heap_size >> 1); uintptr_t split = space->base + (space->size >> 1);
if (cx->hp <= split) { if (space->hp <= split) {
cx->hp = split; space->hp = split;
cx->limit = cx->heap_base + cx->heap_size; space->limit = space->base + space->size;
} else { } else {
cx->hp = cx->heap_base; space->hp = space->base;
cx->limit = split; space->limit = split;
} }
cx->count++; space->count++;
} }
static void* copy(struct context *cx, uintptr_t kind, void *obj) { static void* copy(struct semi_space *space, uintptr_t kind, void *obj) {
size_t size; size_t size;
switch (kind) { switch (kind) {
#define COMPUTE_SIZE(name, Name, NAME) \ #define COMPUTE_SIZE(name, Name, NAME) \
@ -58,20 +74,20 @@ static void* copy(struct context *cx, uintptr_t kind, void *obj) {
default: default:
abort (); abort ();
} }
void *new_obj = (void*)cx->hp; void *new_obj = (void*)space->hp;
memcpy(new_obj, obj, size); memcpy(new_obj, obj, size);
*(uintptr_t*) obj = cx->hp; *(uintptr_t*) obj = space->hp;
cx->hp += align_up (size, ALIGNMENT); space->hp += align_up (size, ALIGNMENT);
return new_obj; return new_obj;
} }
static uintptr_t scan(struct context *cx, uintptr_t grey) { static uintptr_t scan(struct semi_space *space, uintptr_t grey) {
void *obj = (void*)grey; void *obj = (void*)grey;
uintptr_t kind = *(uintptr_t*) obj; uintptr_t kind = *(uintptr_t*) obj;
switch (kind) { switch (kind) {
#define SCAN_OBJECT(name, Name, NAME) \ #define SCAN_OBJECT(name, Name, NAME) \
case ALLOC_KIND_##NAME: \ case ALLOC_KIND_##NAME: \
visit_##name##_fields((Name*)obj, visit, cx); \ visit_##name##_fields((Name*)obj, visit, space); \
return grey + align_up(name##_size((Name*)obj), ALIGNMENT); return grey + align_up(name##_size((Name*)obj), ALIGNMENT);
FOR_EACH_HEAP_OBJECT_KIND(SCAN_OBJECT) FOR_EACH_HEAP_OBJECT_KIND(SCAN_OBJECT)
#undef SCAN_OBJECT #undef SCAN_OBJECT
@ -80,55 +96,58 @@ static uintptr_t scan(struct context *cx, uintptr_t grey) {
} }
} }
static void* forward(struct context *cx, void *obj) { static void* forward(struct semi_space *space, void *obj) {
uintptr_t header_word = *(uintptr_t*)obj; uintptr_t header_word = *(uintptr_t*)obj;
switch (header_word) { switch (header_word) {
#define CASE_ALLOC_KIND(name, Name, NAME) \ #define CASE_ALLOC_KIND(name, Name, NAME) \
case ALLOC_KIND_##NAME: case ALLOC_KIND_##NAME:
FOR_EACH_HEAP_OBJECT_KIND(CASE_ALLOC_KIND) FOR_EACH_HEAP_OBJECT_KIND(CASE_ALLOC_KIND)
#undef CASE_ALLOC_KIND #undef CASE_ALLOC_KIND
return copy(cx, header_word, obj); return copy(space, header_word, obj);
default: default:
return (void*)header_word; return (void*)header_word;
} }
} }
static void visit(void **loc, void *visit_data) { static void visit(void **loc, void *visit_data) {
struct context *cx = visit_data; struct semi_space *space = visit_data;
void *obj = *loc; void *obj = *loc;
if (obj != NULL) if (obj != NULL)
*loc = forward(cx, obj); *loc = forward(space, obj);
} }
static void collect(struct context *cx) { static void collect(struct mutator *mut) {
// fprintf(stderr, "start collect #%ld:\n", cx->count); struct semi_space *space = mutator_semi_space(mut);
flip(cx); // fprintf(stderr, "start collect #%ld:\n", space->count);
uintptr_t grey = cx->hp; flip(space);
for (struct handle *h = cx->roots; h; h = h->next) uintptr_t grey = space->hp;
visit(&h->v, cx); for (struct handle *h = mut->roots; h; h = h->next)
// fprintf(stderr, "pushed %zd bytes in roots\n", cx->hp - grey); visit(&h->v, space);
while(grey < cx->hp) // fprintf(stderr, "pushed %zd bytes in roots\n", space->hp - grey);
grey = scan(cx, grey); while(grey < space->hp)
// fprintf(stderr, "%zd bytes copied\n", (cx->heap_size>>1)-(cx->limit-cx->hp)); grey = scan(space, grey);
// fprintf(stderr, "%zd bytes copied\n", (space->size>>1)-(space->limit-space->hp));
} }
static void collect_for_alloc(struct context *cx, size_t bytes) { static void collect_for_alloc(struct mutator *mut, size_t bytes) {
collect(cx); collect(mut);
if (cx->limit - cx->hp < bytes) { struct semi_space *space = mutator_semi_space(mut);
fprintf(stderr, "ran out of space, heap size %zu\n", cx->mem_size); if (space->limit - space->hp < bytes) {
fprintf(stderr, "ran out of space, heap size %zu\n", space->size);
abort(); abort();
} }
} }
static inline void* allocate(struct context *cx, enum alloc_kind kind, static inline void* allocate(struct mutator *mut, enum alloc_kind kind,
size_t size) { size_t size) {
struct semi_space *space = mutator_semi_space(mut);
while (1) { while (1) {
uintptr_t addr = cx->hp; uintptr_t addr = space->hp;
uintptr_t new_hp = align_up (addr + size, ALIGNMENT); uintptr_t new_hp = align_up (addr + size, ALIGNMENT);
if (cx->limit < new_hp) { if (space->limit < new_hp) {
collect_for_alloc(cx, size); collect_for_alloc(mut, size);
continue; continue;
} }
cx->hp = new_hp; space->hp = new_hp;
void *ret = (void *)addr; void *ret = (void *)addr;
uintptr_t *header_word = ret; uintptr_t *header_word = ret;
*header_word = kind; *header_word = kind;
@ -138,9 +157,9 @@ static inline void* allocate(struct context *cx, enum alloc_kind kind,
return ret; return ret;
} }
} }
static inline void* allocate_pointerless(struct context *cx, static inline void* allocate_pointerless(struct mutator *mut,
enum alloc_kind kind, size_t size) { enum alloc_kind kind, size_t size) {
return allocate(cx, kind, size); return allocate(mut, kind, size);
} }
static inline void init_field(void **addr, void *val) { static inline void init_field(void **addr, void *val) {
@ -153,39 +172,43 @@ static inline void* get_field(void **addr) {
return *addr; return *addr;
} }
static struct context* initialize_gc(size_t size) { static int initialize_gc(size_t heap_size, struct heap **heap,
void *mem = mmap(NULL, size, PROT_READ|PROT_WRITE, struct mutator **mut) {
void *mem = mmap(NULL, heap_size, PROT_READ|PROT_WRITE,
MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
if (mem == MAP_FAILED) { if (mem == MAP_FAILED) {
perror("mmap failed"); perror("mmap failed");
abort(); return 0;
}
struct context *cx = mem;
cx->mem = mem;
cx->mem_size = size;
// Round up to twice ALIGNMENT so that both spaces will be aligned.
size_t overhead = align_up(sizeof(*cx), ALIGNMENT * 2);
cx->hp = cx->heap_base = ((uintptr_t) mem) + overhead;
cx->heap_size = size - overhead;
cx->count = -1;
flip(cx);
cx->roots = NULL;
return cx;
} }
static struct context* initialize_gc_for_thread(uintptr_t *stack_base, *mut = calloc(1, sizeof(struct mutator));
struct context *parent) { if (!*mut) abort();
*heap = mutator_heap(*mut);
struct semi_space *space = mutator_semi_space(*mut);
space->hp = space->base = (uintptr_t) mem;
space->size = heap_size;
space->count = -1;
flip(space);
(*mut)->roots = NULL;
return 1;
}
static struct mutator* initialize_gc_for_thread(uintptr_t *stack_base,
struct heap *heap) {
fprintf(stderr, fprintf(stderr,
"Semispace copying collector not appropriate for multithreaded use.\n"); "Semispace copying collector not appropriate for multithreaded use.\n");
exit(1); exit(1);
} }
static void finish_gc_for_thread(struct context *cx) { static void finish_gc_for_thread(struct mutator *space) {
} }
static inline void print_start_gc_stats(struct context *cx) { static inline void print_start_gc_stats(struct heap *heap) {
} }
static inline void print_end_gc_stats(struct context *cx) { static inline void print_end_gc_stats(struct heap *heap) {
printf("Completed %ld collections\n", cx->count); struct semi_space *space = heap_semi_space(heap);
printf("Heap size is %zd\n", cx->mem_size); printf("Completed %ld collections\n", space->count);
printf("Heap size is %zd\n", space->size);
} }