#include "kpl.h" extern inline void kpl_gc_init(kpl_gc *gc); kpl_object_gc *kpl_gc_new(kpl_thread *thread, kpl_interface *interface, int32_t object_size) { kpl_object *obj = kpl_memory_allocator_allocate(&thread->memory_allocator, object_size, thread->thread_id); if (!obj) return NULL; obj->object_flags = KPL_OBJECT_FLAGS_EMPTY; kpl_object_gc *obj_gc = kpl_memory_allocator_allocate(&thread->memory_allocator, sizeof(kpl_object_gc), thread->thread_id); if (!obj_gc) { kpl_memory_allocator_return(&thread->memory_allocator, obj); return NULL; } obj_gc->gc_prev = obj_gc->gc_next = NULL; __atomic_clear(&obj_gc->gc_mark, __ATOMIC_RELAXED); obj_gc->class.interface = interface; obj_gc->class.value.ptr = obj; obj_gc->gc_next = thread->gc.gc_head; if (thread->gc.gc_head) thread->gc.gc_head->gc_prev = obj_gc; thread->gc.gc_head = obj_gc; thread->gc.gc_cycle_head_additions++; return obj_gc; } void kpl_gc_delete(kpl_thread *thread, kpl_object_gc *object_gc) { if (object_gc == thread->gc.gc_mark_head) { thread->gc.gc_mark_head = thread->gc.gc_mark_head->gc_next; if (thread->gc.gc_mark_head) thread->gc.gc_mark_head->gc_prev = NULL; } kpl_memory_allocator_return(&thread->memory_allocator, object_gc->class.value.ptr); if (object_gc->gc_prev) object_gc->gc_prev->gc_next = object_gc->gc_next ? object_gc->gc_next : NULL; if (object_gc->gc_next) object_gc->gc_next->gc_prev = object_gc->gc_prev ? object_gc->gc_prev : NULL; kpl_memory_allocator_return(&thread->memory_allocator, object_gc); } extern inline void kpl_gc_mark(kpl_object_gc *object_gc); extern inline void kpl_gc_prepare_sweep(kpl_gc *gc); void kpl_gc_sweep(kpl_thread *thread) { if (!thread->gc.gc_mark_head) return; kpl_object_gc *mark_head = thread->gc.gc_mark_head, *mark_tail = mark_head; while (mark_head) { kpl_object_gc *tmp = mark_head; mark_head = mark_head->gc_next; if (tmp->gc_mark) { __atomic_clear(&tmp->gc_mark, __ATOMIC_RELAXED); mark_tail = tmp; } else kpl_gc_delete(thread, tmp); } if (!thread->gc.gc_mark_head) return; mark_tail->gc_next = thread->gc.gc_head; if (thread->gc.gc_head) thread->gc.gc_head->gc_prev = mark_tail; thread->gc.gc_head = thread->gc.gc_mark_head; thread->gc.gc_mark_head = NULL; } extern inline void kpl_gc_free(kpl_gc *gc, kpl_thread *thread);