aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/kpl.h2
-rw-r--r--include/kpl/gc.h22
-rw-r--r--include/kpl/object.h21
-rw-r--r--source/env.c3
-rw-r--r--source/gc.c43
-rw-r--r--test/gc.c6
6 files changed, 64 insertions, 33 deletions
diff --git a/include/kpl.h b/include/kpl.h
index fdf8bbb..593091b 100644
--- a/include/kpl.h
+++ b/include/kpl.h
@@ -40,6 +40,8 @@
#endif
// KPL forward declarations
+typedef struct _kpl_object_gc kpl_object_gc;
+
typedef struct _kpl_object_task kpl_object_task;
typedef struct _kpl_thread kpl_thread;
diff --git a/include/kpl/gc.h b/include/kpl/gc.h
index 6ed4fc4..bd99a9a 100644
--- a/include/kpl/gc.h
+++ b/include/kpl/gc.h
@@ -24,17 +24,18 @@ inline void kpl_gc_init(kpl_gc *gc) {
/*
* Create a new gc node and a new kpl_object with object_size
* Add the node to the gc_head
- * Returns the new kpl_gc_object
+ * Returns the new kpl_object, which has a pointer to the kpl_gc_object
* Only owner thread calls this
*/
-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);
/*
- * Remove node from gc_head
- * Free gc node and the object it wraps
- * Only owner thread call this during sweep or free
+ * Delete an object on gc_head before sweeping
+ * *object is the object the gc is tracking NOT a kpl_object_gc
+ * Many objects can be removed from the gc before sweep and is done though this
+ * *object is not void, force casting to prevent memory errors
*/
-void kpl_gc_delete(kpl_thread *thread, kpl_object_gc *object_gc);
+void kpl_gc_delete(kpl_thread *thread, kpl_object *object);
/*
* Mark a gc object
@@ -64,11 +65,4 @@ void kpl_gc_sweep(kpl_thread *thread);
/*
* Run delete on all objects on gc_head
*/
-inline void kpl_gc_free(kpl_gc *gc, kpl_thread *thread) {
- kpl_object_gc *head = gc->gc_head;
- while (head) {
- kpl_object_gc *tmp = head;
- head = head->gc_next;
- kpl_gc_delete(thread, tmp);
- }
-}
+void kpl_gc_free(kpl_thread *thread);
diff --git a/include/kpl/object.h b/include/kpl/object.h
index e6e2340..cdea84e 100644
--- a/include/kpl/object.h
+++ b/include/kpl/object.h
@@ -13,11 +13,16 @@ typedef enum : uint16_t {
// for atomic linked list queue
// size in bytes, index on the kpl_threads array
// Flags should only be set by owner thread
-#define KPL_OBJECT_HEADER(STRUCT_NAME) \
+#define KPL_OBJECT_GC_HEADER(STRUCT_NAME) \
struct STRUCT_NAME *_Atomic queue_next; \
int32_t object_size; \
uint16_t thread_id, object_flags;
+// Each object can access the object_gc it is a child of
+#define KPL_OBJECT_HEADER(STRUCT_NAME) \
+ KPL_OBJECT_GC_HEADER(STRUCT_NAME); \
+ kpl_object_gc *object_gc_parent;
+
typedef struct _kpl_object {
KPL_OBJECT_HEADER(_kpl_object);
} kpl_object; // all kpl objects allocated by the kpl memory allocator extend this header
@@ -62,7 +67,7 @@ void *kpl_object_queue_dequeue(kpl_object_queue *object_queue);
* Any thread can mark
*/
typedef struct _kpl_object_gc {
- KPL_OBJECT_HEADER(_kpl_object_gc);
+ KPL_OBJECT_GC_HEADER(_kpl_object_gc);
struct _kpl_object_gc *gc_prev, *gc_next;
_Atomic bool gc_mark __attribute((aligned(8)));
kpl_class class;
@@ -82,15 +87,21 @@ typedef struct _kpl_object_error {
typedef kpl_object_error *kpl_task_fn(kpl_object_task *task, kpl_thread *thread);
/*
+ * Max variables per task, used a bit field to check if the variable can be garbage collected
+ */
+#define KPL_TASK_VARIABLES_MAX 56
+
+/*
* Coroutine state object
- * Can have up to 32 arguments/locals
+ * Can have up to KPL_TASK_VARIABLES_MAX arguments/locals
* The variables_gc_mask is a bitmask, the set bit index represents the variable at the index is a kpl_object_gc
*/
typedef struct _kpl_object_task {
KPL_OBJECT_HEADER(_kpl_object_task);
- _Atomic bool join_ready __attribute__((aligned(4))); // shares cache line with variables_gc_mask
- uint32_t variables_gc_mask, io_flags; // set once then only read
+ _Atomic bool join_ready; // shares cache line with variables_gc_mask
+ uint64_t variables_gc_mask : KPL_TASK_VARIABLES_MAX; // set once then only read
int32_t io_res; // io_res and io_flags are set by io_uring complete
+ uint32_t io_flags;
kpl_object_task *join;
kpl_task_fn *task_fn;
kpl_result result;
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);
+ }
+}
diff --git a/test/gc.c b/test/gc.c
index 023d942..2af29d3 100644
--- a/test/gc.c
+++ b/test/gc.c
@@ -10,11 +10,11 @@ KPL_TEST(basic_mark_and_sweep) {
env.threads = threads;
kpl_memory_allocator_init(&thread.memory_allocator);
kpl_gc_init(&thread.gc);
- kpl_object_gc *objs[TEST_OBJECT_COUNT] = {};
+ kpl_object *objs[TEST_OBJECT_COUNT] = {};
for (int32_t obj_index = 1; obj_index <= TEST_OBJECT_COUNT; obj_index++) {
objs[obj_index - 1] = kpl_gc_new(&thread, NULL, sizeof(kpl_object_error));
if (obj_index % 2 == 0)
- kpl_gc_mark(objs[obj_index - 1]);
+ kpl_gc_mark(objs[obj_index - 1]->object_gc_parent);
}
int32_t counter = 0;
kpl_object_gc *gc_head = thread.gc.gc_head;
@@ -32,6 +32,6 @@ KPL_TEST(basic_mark_and_sweep) {
gc_head = gc_head->gc_next;
}
KPL_ASSERT(counter == TEST_OBJECT_COUNT / 2, "should have removed half of the objects");
- kpl_gc_free(&thread.gc, &thread);
+ kpl_gc_free(&thread);
kpl_memory_allocator_free(&thread.memory_allocator);
}