aboutsummaryrefslogtreecommitdiff
path: root/source
diff options
context:
space:
mode:
Diffstat (limited to 'source')
-rw-r--r--source/env.c3
-rw-r--r--source/gc.c43
2 files changed, 35 insertions, 11 deletions
diff --git a/source/env.c b/source/env.c
index b5d59b8..65e1bff 100644
--- a/source/env.c
+++ b/source/env.c
@@ -82,7 +82,7 @@ static void kpl_env_thread_free(kpl_thread *thread) {
while ((tmp_task = kpl_io_uring_get_completed(thread)))
continue;
kpl_io_uring_free(&thread->io_uring);
- kpl_gc_free(&thread->gc, thread);
+ kpl_gc_free(thread);
kpl_memory_allocator_free(&thread->memory_allocator);
sem_destroy(&thread->sem);
}
@@ -109,6 +109,7 @@ void *kpl_env_run_loop(void *arg) {
task = kpl_io_uring_get_completed(thread);
if (task) {
kpl_object_error *error = task->task_fn(task, thread);
+ kpl_gc_delete(thread, (kpl_object*) task);
if (!error)
continue;
kpl_object_queue_enqueue(&thread->error_queue, error);
diff --git a/source/gc.c b/source/gc.c
index 6970304..e12c949 100644
--- a/source/gc.c
+++ b/source/gc.c
@@ -3,7 +3,7 @@
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) {
+void *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;
@@ -19,20 +19,16 @@ kpl_object_gc *kpl_gc_new(kpl_thread *thread, kpl_interface *interface, int32_t
__atomic_clear(&obj_gc->gc_mark, __ATOMIC_RELAXED);
obj_gc->class.interface = interface;
obj_gc->class.value.ptr = obj;
+ obj->object_gc_parent = obj_gc;
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;
+ return obj;
}
-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;
- }
+static void kpl_gc_return(kpl_thread *thread, kpl_object_gc *object_gc) {
kpl_object *obj = object_gc->class.value.ptr;
kpl_memory_allocator_return(&thread->env->threads[obj->thread_id].memory_allocator, obj);
if (object_gc->gc_prev)
@@ -42,6 +38,26 @@ void kpl_gc_delete(kpl_thread *thread, kpl_object_gc *object_gc) {
kpl_memory_allocator_return(&thread->env->threads[object_gc->thread_id].memory_allocator, object_gc);
}
+void kpl_gc_delete(kpl_thread *thread, kpl_object *object) {
+ kpl_object_gc *object_gc = object->object_gc_parent;
+ if (object_gc == thread->gc.gc_head) {
+ thread->gc.gc_head = thread->gc.gc_head->gc_next;
+ if (thread->gc.gc_head)
+ thread->gc.gc_head->gc_prev = NULL;
+ }
+ thread->gc.gc_cycle_head_additions--;
+ kpl_gc_return(thread, object_gc);
+}
+
+static void kpl_gc_sweep_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_gc_return(thread, object_gc);
+}
+
extern inline void kpl_gc_mark(kpl_object_gc *object_gc);
extern inline void kpl_gc_prepare_sweep(kpl_gc *gc);
@@ -64,7 +80,7 @@ void kpl_gc_sweep(kpl_thread *thread) {
__atomic_clear(&tmp->gc_mark, __ATOMIC_RELAXED);
mark_tail = tmp;
} else
- kpl_gc_delete(thread, tmp);
+ kpl_gc_sweep_delete(thread, tmp);
}
if (!thread->gc.gc_mark_head)
return;
@@ -75,4 +91,11 @@ void kpl_gc_sweep(kpl_thread *thread) {
thread->gc.gc_mark_head = NULL;
}
-extern inline void kpl_gc_free(kpl_gc *gc, kpl_thread *thread);
+void kpl_gc_free(kpl_thread *thread) {
+ kpl_object_gc *head = thread->gc.gc_head;
+ while (head) {
+ kpl_object_gc *tmp = head;
+ head = head->gc_next;
+ kpl_gc_return(thread, tmp);
+ }
+}