From 7b9204a8680539bc4795a14f3d9a34e47981d0c3 Mon Sep 17 00:00:00 2001 From: nodist Date: Thu, 5 Nov 2026 15:22:02 -0500 Subject: more objects and special slab for gc --- include/kpl.h | 6 ++++ include/kpl/array.h | 31 ++++++++++++++++++ include/kpl/class.h | 14 ++++++++- include/kpl/error.h | 31 ++++++++++++++++++ include/kpl/gc.h | 3 +- include/kpl/io.h | 2 +- include/kpl/memory.h | 13 ++++++++ include/kpl/object.h | 88 ++++++++++++++++++++++++++++++++++++++++++++++++---- include/kpl/string.h | 29 +++++++++++++++++ include/kpl/task.h | 34 ++++++++++++++++++++ include/kpl/value.h | 26 +++++++++++++--- source/array.c | 21 +++++++++++++ source/env.c | 5 +-- source/error.c | 28 +++++++++++++++++ source/gc.c | 13 +++++--- source/io.c | 4 +++ source/memory.c | 72 ++++++++++++++++++++++++++++-------------- source/string.c | 24 ++++++++++++++ source/task.c | 44 ++++++++++++++++++++++++++ test/gc.c | 5 +-- test/string.c | 14 +++++++++ 21 files changed, 458 insertions(+), 49 deletions(-) create mode 100644 include/kpl/array.h create mode 100644 include/kpl/error.h create mode 100644 include/kpl/string.h create mode 100644 include/kpl/task.h create mode 100644 source/array.c create mode 100644 source/error.c create mode 100644 source/string.c create mode 100644 source/task.c create mode 100644 test/string.c diff --git a/include/kpl.h b/include/kpl.h index 593091b..467196e 100644 --- a/include/kpl.h +++ b/include/kpl.h @@ -42,6 +42,8 @@ // KPL forward declarations typedef struct _kpl_object_gc kpl_object_gc; +typedef struct _kpl_object_native kpl_object_native; + typedef struct _kpl_object_task kpl_object_task; typedef struct _kpl_thread kpl_thread; @@ -61,4 +63,8 @@ typedef struct _kpl_env kpl_env; #include "kpl/memory.h" #include "kpl/io.h" #include "kpl/gc.h" +#include "kpl/string.h" +#include "kpl/array.h" +#include "kpl/error.h" +#include "kpl/task.h" #include "kpl/env.h" diff --git a/include/kpl/array.h b/include/kpl/array.h new file mode 100644 index 0000000..de3b0e8 --- /dev/null +++ b/include/kpl/array.h @@ -0,0 +1,31 @@ +/* + * KPL array methods + */ + +#pragma once + +/* + * Create new array + */ +kpl_object_array *kpl_object_array_create(kpl_thread *thread, kpl_value_bit_width value_bit_width, + int32_t value_size); + +/* + * Return array to gc before sweep + */ +inline void kpl_object_array_destroy(kpl_thread *thread, kpl_object_array *object_array) { + kpl_gc_delete(thread, (kpl_object*) object_array); +} + +typedef enum : int32_t { + KPL_ARRAY_RESULT_TAG_SUCCESS = 0, + KPL_ARRAY_RESULT_TAG_RESIZE_FAIL = 1, + KPL_ARRAY_RESULT_TAG_ARRAY_EMPTY = 2, + KPL_ARRAY_RESULT_TAG_INDEX_INVALID = 3 +} kpl_array_result_tag; + +// TODO PUSH + +// TODO POP + +// TODO INDEX diff --git a/include/kpl/class.h b/include/kpl/class.h index 0842db2..d1c836e 100644 --- a/include/kpl/class.h +++ b/include/kpl/class.h @@ -9,12 +9,24 @@ */ typedef void kpl_interface_gc_mark_object_fn(void *object); +/* + * A free for objects allocated outside the kpl allocator, wrapped in a kpl_object_native + */ +typedef void kpl_interface_native_free_fn(kpl_object_native *object_native); + +/* + * `log operator + */ +typedef void kpl_interface_log_fn(kpl_thread *thread, void *object); + typedef struct { // TODO kpl_interface_gc_mark_object_fn *gc_mark_object_fn; + kpl_interface_native_free_fn *native_free_fn; + kpl_interface_log_fn *log_fn; } kpl_interface; typedef struct { kpl_value64 value; - kpl_interface *interface; + const kpl_interface *interface; } kpl_class; diff --git a/include/kpl/error.h b/include/kpl/error.h new file mode 100644 index 0000000..407363c --- /dev/null +++ b/include/kpl/error.h @@ -0,0 +1,31 @@ +/* + * KPL Error object methods + */ + +#pragma once + +/* + * Create a new error object + */ +kpl_object_error *kpl_object_error_create(kpl_thread *thread, kpl_class error_file, kpl_class error_data, + int32_t error_line); + +/* + * Delete object in gc before sweep + */ +inline void kpl_object_error_destroy(kpl_thread *thread, kpl_object_error *object_error) { + kpl_gc_delete(thread, (kpl_object*) object_error); +} + +/* + * Add to error stack + * Uses queue_next as stack, stack is a linked list, can only be added to, no removing + * The bottom of the error stack is the base error and then it grows from the top down + * Thread safe uses atomic exchange to update queue_next + */ +inline void kpl_object_error_stack(kpl_object_error *object_error_base, + kpl_object_error *object_error_stack) { + kpl_object_error *error_next = __atomic_exchange_n(&object_error_base->queue_next, object_error_stack, + __ATOMIC_SEQ_CST); + __atomic_store_n(&object_error_stack->queue_next, error_next, __ATOMIC_RELAXED); +} diff --git a/include/kpl/gc.h b/include/kpl/gc.h index bd99a9a..5240c40 100644 --- a/include/kpl/gc.h +++ b/include/kpl/gc.h @@ -27,13 +27,14 @@ inline void kpl_gc_init(kpl_gc *gc) { * Returns the new kpl_object, which has a pointer to the kpl_gc_object * Only owner thread calls this */ -void *kpl_gc_new(kpl_thread *thread, kpl_interface *interface, int32_t object_size); +void *kpl_gc_new(kpl_thread *thread, const kpl_interface *interface, int32_t object_size); /* * 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 + * The object and its gc parent must belong to the thread */ void kpl_gc_delete(kpl_thread *thread, kpl_object *object); diff --git a/include/kpl/io.h b/include/kpl/io.h index 30503ad..80fadd8 100644 --- a/include/kpl/io.h +++ b/include/kpl/io.h @@ -47,7 +47,7 @@ inline void kpl_io_uring_free(kpl_io_uring *io_uring) { * Reads one from cqes, will call io uring enter if none are complete and submitted_to_kernel is not zero * This will wait if io uring enter is called * NULL for none completed - * If an error occurred, the error details will be on the kpl_task_object + * If an error occurred, the error details will be on the kpl_object_task * *thread is needed to decrease priority */ kpl_object_task *kpl_io_uring_get_completed(kpl_thread *thread); diff --git a/include/kpl/memory.h b/include/kpl/memory.h index b69867a..3bb4871 100644 --- a/include/kpl/memory.h +++ b/include/kpl/memory.h @@ -35,8 +35,10 @@ typedef struct { * The memory is allocated from buffer and returned to the pool * The slabs and buffers are freed and returned to the system at once on allocator free * The object pools are from each mapped buffer, they do not need to be freed + * The kpl_object_gc get their own allocator+pool because they do not align to a power of two and a lot are needed */ typedef struct { + kpl_memory_allocator_slab_group slab_gc; // all kpl_object_gc are allocated through here kpl_memory_allocator_slab_group slabs[KPL_MEMORY_ALLOCATOR_SLAB_COUNT]; } kpl_memory_allocator; @@ -56,7 +58,18 @@ void kpl_memory_allocator_free(kpl_memory_allocator *memory_allocator); */ void *kpl_memory_allocator_allocate(kpl_memory_allocator *memory_allocator, int32_t size, uint16_t thread_id); +/* + * Only called though gc_new + * All allocations must extend KPL_OBJECT_GC_HEADER + */ +void *kpl_memory_allocator_allocate_gc(kpl_memory_allocator *memory_allocator, uint16_t thread_id); + /* * Only call this though kpl_env_object_return */ void kpl_memory_allocator_return(kpl_memory_allocator *memory_allocator, void *object); + +/* + * Only called though gc delete + */ +void kpl_memory_allocator_return_gc(kpl_memory_allocator *memory_allocator, void *object); diff --git a/include/kpl/object.h b/include/kpl/object.h index cdea84e..933b459 100644 --- a/include/kpl/object.h +++ b/include/kpl/object.h @@ -23,9 +23,13 @@ typedef enum : uint16_t { KPL_OBJECT_GC_HEADER(STRUCT_NAME); \ kpl_object_gc *object_gc_parent; +/* + * All kpl objects allocated by the kpl memory allocator extend this header + * The kpl_object_gc extends is partially, so it can still be used with the memory allocator + */ typedef struct _kpl_object { KPL_OBJECT_HEADER(_kpl_object); -} kpl_object; // all kpl objects allocated by the kpl memory allocator extend this header +} kpl_object; /* * Atomic linked list queue any thread can add, only owner thread can remove @@ -73,21 +77,93 @@ typedef struct _kpl_object_gc { kpl_class class; } kpl_object_gc; +/* + * UTF-8 Strings + * Strings must be null termed + */ +typedef struct _kpl_object_string { + KPL_OBJECT_HEADER(_kpl_object_string); + int32_t bytes_length, bytes_size; // length does not include null term, size includes null term + uint8_t bytes[]; +} kpl_object_string; + +#define KPL_ARRAY_MAX_VALUE_SIZE ((1 << 30) - 1) + +// for array polymorphism +// values_length and values_size refers to the number of values, to get in bytes multiply by VALUE_BIT_WIDTH +#define KPL_OBJECT_ARRAY_HEADER(STRUCT_NAME) \ + KPL_OBJECT_HEADER(STRUCT_NAME); \ + kpl_value_bit_width value_bit_width : 3; \ + uint32_t values_length : 29, values_size : 29 + +/* + * KPL arrays + * Use this object as the base for all arrays + * Array methods will cast to different value sizes + */ +typedef struct _kpl_object_array { + KPL_OBJECT_ARRAY_HEADER(_kpl_object_array); +} kpl_object_array; + +// One container for each internal value bit width +#define _KPL_OBJECT_ARRAY_DEFINE(VALUE_BIT_WIDTH) \ +typedef struct _kpl_object_array ## VALUE_BIT_WIDTH { \ + KPL_OBJECT_ARRAY_HEADER(_kpl_object_array ## VALUE_BIT_WIDTH); \ + kpl_value ## VALUE_BIT_WIDTH values[]; \ +} kpl_object_array ## VALUE_BIT_WIDTH; + +/* + * kpl_object_array8 + */ +_KPL_OBJECT_ARRAY_DEFINE(8); +/* + * kpl_object_array16 + */ +_KPL_OBJECT_ARRAY_DEFINE(16); +/* + * kpl_object_array32 + */ +_KPL_OBJECT_ARRAY_DEFINE(32); +/* + * kpl_object_array64 + */ +_KPL_OBJECT_ARRAY_DEFINE(64); + +#define KPL_TUPLE_ITEMS_MAX 64 + +/* + * Tuple, array of different types + * Can hold up TO KPL_TUPLE_ITEMS_MAX items, if (items_gc_mask & 1UL << index) then items[index] is a kpl_object_gc + */ +typedef struct _kpl_object_tuple { + KPL_OBJECT_HEADER(_kpl_object_tuple); + uint64_t items_gc_mask; + kpl_value64 items[]; +} kpl_object_tuple; + +/* + * Wraps C objects + * The c object is freed with the kpl_interface_native_free_fn + */ +typedef struct _kpl_object_native { + KPL_OBJECT_HEADER(_kpl_object_native); + void *c_object; +} kpl_object_native; + /* * Class data is read only, cannot modify or race conditions can occur - * The error stack can be modified across threads + * queue_next from the object header is used to create an error stack */ typedef struct _kpl_object_error { KPL_OBJECT_HEADER(_kpl_object_error); - kpl_object_queue error_stack; kpl_class error_file, error_data; // Can be a reference to C code or KPL code int32_t error_line; } kpl_object_error; -typedef kpl_object_error *kpl_task_fn(kpl_object_task *task, kpl_thread *thread); +typedef kpl_object_error *kpl_task_fn(kpl_thread *thread, kpl_object_task *task); /* - * Max variables per task, used a bit field to check if the variable can be garbage collected + * Max variables per task, a bit mask used to check if the variable can be garbage collected */ #define KPL_TASK_VARIABLES_MAX 56 @@ -102,7 +178,7 @@ typedef struct _kpl_object_task { 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_object_task *join_task; kpl_task_fn *task_fn; kpl_result result; kpl_value64 variables[]; // a task has no distinction between an argument or a local, both live here diff --git a/include/kpl/string.h b/include/kpl/string.h new file mode 100644 index 0000000..8c88825 --- /dev/null +++ b/include/kpl/string.h @@ -0,0 +1,29 @@ +/* + * KPL UTF-8 string and character methods + */ + +#pragma once + +/* + * Create new string + */ +kpl_object_string *kpl_object_string_create(kpl_thread *thread, int32_t bytes_size); + +/* + * Create a string from a c string + * Copies the c string + */ +inline kpl_object_string *kpl_object_string_from_c_string(kpl_thread *thread, const char *c_string) { + int32_t bytes_size = strlen(c_string); + kpl_object_string *object_string = kpl_object_string_create(thread, bytes_size); + object_string->bytes_length = bytes_size; + memcpy(object_string->bytes, c_string, bytes_size); + return object_string; +} + +/* + * Return string to gc before sweep + */ +inline void kpl_object_string_destroy(kpl_thread *thread, kpl_object_string *object_string) { + kpl_gc_delete(thread, (kpl_object*) object_string); +} diff --git a/include/kpl/task.h b/include/kpl/task.h new file mode 100644 index 0000000..bfdbb4a --- /dev/null +++ b/include/kpl/task.h @@ -0,0 +1,34 @@ +/* + * KPL task methods + */ + +#pragma once + +/* + * Create a new task + */ +kpl_object_task *kpl_object_task_create(kpl_thread *thread, int32_t variables_size, uint64_t variables_gc_mask); + +/* + * Delete object in gc before sweep + */ +inline void kpl_object_task_destroy(kpl_thread *thread, kpl_object_task *object_task) { + kpl_gc_delete(thread, (kpl_object*) object_task); +} + +/* + * Queue join task + * Done needs to be called twice, once by joining the task and once the task is done + * The joining or completing can be called in any order + * Once both done functions are called the join task is queued + */ +void kpl_object_task_done(kpl_thread *thread, kpl_object_task *object_task); + +/* + * Set the task to queue next after kpl_task_complete finishes + */ +inline void kpl_object_task_join(kpl_thread *thread, kpl_object_task *restrict object_task_complete, + kpl_object_task *restrict object_task_join) { + object_task_complete->join_task = object_task_join; + kpl_object_task_done(thread, object_task_complete); +} diff --git a/include/kpl/value.h b/include/kpl/value.h index e0bf0c7..d6120ee 100644 --- a/include/kpl/value.h +++ b/include/kpl/value.h @@ -4,6 +4,16 @@ #pragma once +/* + * Enum values need to fit within in 3 bits + */ +typedef enum : uint8_t { + KPL_VALUE_BIT_WIDTH8 = 1, + KPL_VALUE_BIT_WIDTH16 = 2, + KPL_VALUE_BIT_WIDTH32 = 3, + KPL_VALUE_BIT_WIDTH64 = 4 +} kpl_value_bit_width; + #define _KPL_VALUE8 \ uint8_t u8; \ int8_t i8 @@ -45,11 +55,15 @@ typedef union { void *ptr; } kpl_value64; +#define KPL_VALUE64_EMPTY ((kpl_value64) { .ptr = NULL }) + typedef struct { kpl_value64 value; int64_t tag; } kpl_result; +#define KPL_RESULT_EMPTY ((kpl_result) { .value = KPL_VALUE64_EMPTY, .tag = 0 }) + typedef struct { void *async, *sync; } kpl_function; @@ -59,9 +73,11 @@ typedef struct { * if code != KPL_EXIT_CODE_SUCCESS then return code */ typedef enum : int32_t { - KPL_EXIT_CODE_SUCCESS = 0, - KPL_EXIT_CODE_ERROR = 1, - KPL_EXIT_CODE_HELP = 2, - KPL_EXIT_CODE_IO_ERROR = 3, - KPL_EXIT_CODE_THREAD_ERROR = 4 + KPL_EXIT_CODE_SUCCESS, + KPL_EXIT_CODE_ERROR, + KPL_EXIT_CODE_HELP, + KPL_EXIT_CODE_MEMORY_ERROR, + KPL_EXIT_CODE_IO_ERROR, + KPL_EXIT_CODE_THREAD_ERROR, + KPL_EXIT_CODE_GC_ERROR } kpl_exit_code; diff --git a/source/array.c b/source/array.c new file mode 100644 index 0000000..4366ec5 --- /dev/null +++ b/source/array.c @@ -0,0 +1,21 @@ + +#include "kpl.h" + +// TODO ARRAY INTERFACE + +kpl_object_array *kpl_object_array_create(kpl_thread *thread, kpl_value_bit_width value_bit_width, + int32_t value_size) { + if (value_size < 1 || value_size > KPL_ARRAY_MAX_VALUE_SIZE) + return NULL; + kpl_object_array *object_array = NULL; + switch (value_bit_width) { + // TODO + default: + return NULL; + } + (void) thread; + // TODO + return object_array; +} + +extern inline void kpl_object_array_destroy(kpl_thread *thread, kpl_object_array *object_array); diff --git a/source/env.c b/source/env.c index 65e1bff..6629e7e 100644 --- a/source/env.c +++ b/source/env.c @@ -108,8 +108,8 @@ void *kpl_env_run_loop(void *arg) { if (!task) 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); + kpl_object_error *error = task->task_fn(thread, task); + kpl_object_task_destroy(thread, task); if (!error) continue; kpl_object_queue_enqueue(&thread->error_queue, error); @@ -145,6 +145,7 @@ extern inline void kpl_env_stop(kpl_env *env); void kpl_env_gc(kpl_env *env) { (void) env; + // TODO } void kpl_env_free(kpl_env *env) { diff --git a/source/error.c b/source/error.c new file mode 100644 index 0000000..1ea9297 --- /dev/null +++ b/source/error.c @@ -0,0 +1,28 @@ + +#include "kpl.h" + +static void kpl_object_error_log(kpl_thread *thread, void *object) { + kpl_object_error *object_error = object; + (void) object_error; + (void) thread; +} + +static const kpl_interface object_error_interface = { + .gc_mark_object_fn = NULL, + .native_free_fn = NULL, + .log_fn = kpl_object_error_log +}; + +kpl_object_error *kpl_object_error_create(kpl_thread *thread, kpl_class error_file, kpl_class error_data, + int32_t error_line) { + kpl_object_error *object_error = kpl_gc_new(thread, &object_error_interface, sizeof(kpl_object_error)); + object_error->error_file = error_file; + object_error->error_data = error_data; + object_error->error_line = error_line; + return object_error; +} + +extern inline void kpl_object_error_destroy(kpl_thread *thread, kpl_object_error *object_error); + +extern inline void kpl_object_error_stack(kpl_object_error *object_error_base, + kpl_object_error *object_error_stack); diff --git a/source/gc.c b/source/gc.c index e12c949..83b8eb1 100644 --- a/source/gc.c +++ b/source/gc.c @@ -3,13 +3,12 @@ extern inline void kpl_gc_init(kpl_gc *gc); -void *kpl_gc_new(kpl_thread *thread, kpl_interface *interface, int32_t object_size) { +void *kpl_gc_new(kpl_thread *thread, const 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); + kpl_object_gc *obj_gc = kpl_memory_allocator_allocate_gc(&thread->memory_allocator, thread->thread_id); if (!obj_gc) { kpl_memory_allocator_return(&thread->memory_allocator, obj); return NULL; @@ -30,12 +29,16 @@ void *kpl_gc_new(kpl_thread *thread, kpl_interface *interface, int32_t object_si 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->thread_id != thread->thread_id || obj->thread_id != thread->thread_id) + exit(KPL_EXIT_CODE_GC_ERROR); + if (object_gc->class.interface && object_gc->class.interface->native_free_fn) + object_gc->class.interface->native_free_fn((kpl_object_native*) obj); + kpl_memory_allocator_return(&thread->memory_allocator, obj); 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->env->threads[object_gc->thread_id].memory_allocator, object_gc); + kpl_memory_allocator_return_gc(&thread->memory_allocator, object_gc); } void kpl_gc_delete(kpl_thread *thread, kpl_object *object) { diff --git a/source/io.c b/source/io.c index 0a377e0..c8f2157 100644 --- a/source/io.c +++ b/source/io.c @@ -17,7 +17,11 @@ int32_t kpl_io_uring_init(kpl_io_uring *io_uring, int32_t queue_size_pow2) { io_uring->sqes_size = io_uring->params.sq_entries * sizeof(struct io_uring_sqe); void *sring_ptr, *cring_ptr; sring_ptr = cring_ptr = kpl_io_uring_mmap(io_uring->sq_cq_size, io_uring->ring_fd, IORING_OFF_SQ_RING); + if (!sring_ptr) + exit(KPL_EXIT_CODE_IO_ERROR); io_uring->sqes = kpl_io_uring_mmap(io_uring->sqes_size, io_uring->ring_fd, IORING_OFF_SQES); + if (!io_uring->sqes) + exit(KPL_EXIT_CODE_IO_ERROR); io_uring->sring_head = sring_ptr + io_uring->params.sq_off.head; io_uring->sring_tail = sring_ptr + io_uring->params.sq_off.tail; io_uring->sring_mask = sring_ptr + io_uring->params.sq_off.ring_mask; diff --git a/source/memory.c b/source/memory.c index d8b2585..2775f99 100644 --- a/source/memory.c +++ b/source/memory.c @@ -1,17 +1,25 @@ #include "kpl.h" +static void kpl_memory_allocator_init_slab(kpl_memory_allocator_slab_group *slab_group , int32_t slab_pow2) { + slab_group->slab_size_pow2 = slab_pow2; + slab_group->slab_head = slab_group->slab_tail = NULL; + kpl_object_queue_init(&slab_group->object_pool); +} + void kpl_memory_allocator_init(kpl_memory_allocator *memory_allocator) { + kpl_memory_allocator_init_slab(&memory_allocator->slab_gc, -1); for (int32_t slab_pow2 = KPL_MEMORY_ALLOCATOR_MIN_SIZE_POW2; - slab_pow2 <= KPL_MEMORY_ALLOCATOR_MAX_SIZE_POW2; slab_pow2++) { - kpl_memory_allocator_slab_group *slab_group = &memory_allocator->slabs[slab_pow2]; - slab_group->slab_size_pow2 = slab_pow2; - slab_group->slab_head = slab_group->slab_tail = NULL; - kpl_object_queue_init(&slab_group->object_pool); - } + slab_pow2 <= KPL_MEMORY_ALLOCATOR_MAX_SIZE_POW2; slab_pow2++) + kpl_memory_allocator_init_slab(&memory_allocator->slabs[slab_pow2], slab_pow2); } static void kpl_memory_allocator_slab_free(kpl_memory_allocator_slab_group *slab_group) { +#ifdef KPL_VALGRIND + kpl_object *object = NULL; + while ((object = kpl_object_queue_dequeue(&slab_group->object_pool))) + VALGRIND_FREELIKE_BLOCK(object, 0); +#endif kpl_memory_allocator_slab *slab_head = slab_group->slab_head; while (slab_head) { kpl_memory_allocator_slab *slab_tmp = slab_head; @@ -22,22 +30,18 @@ static void kpl_memory_allocator_slab_free(kpl_memory_allocator_slab_group *slab } void kpl_memory_allocator_free(kpl_memory_allocator *memory_allocator) { + kpl_memory_allocator_slab_free(&memory_allocator->slab_gc); for (int32_t slab_pow2 = KPL_MEMORY_ALLOCATOR_MIN_SIZE_POW2; - slab_pow2 < KPL_MEMORY_ALLOCATOR_SLAB_COUNT; slab_pow2++) { - kpl_memory_allocator_slab_group *slab_group = &memory_allocator->slabs[slab_pow2]; -#ifdef KPL_VALGRIND - kpl_object *object = NULL; - while ((object = kpl_object_queue_dequeue(&slab_group->object_pool))) - VALGRIND_FREELIKE_BLOCK(object, 0); -#endif - kpl_memory_allocator_slab_free(slab_group); - } + slab_pow2 < KPL_MEMORY_ALLOCATOR_SLAB_COUNT; slab_pow2++) + kpl_memory_allocator_slab_free(&memory_allocator->slabs[slab_pow2]); } static int32_t kpl_memory_allocator_get_size_pow2(int32_t size) { return size > 1 ? 32 - __builtin_clz(size - 1) : 0; } +#define KPL_MEMORY_ALLOCATOR_GC_PAGES 7 + static const int32_t pages_per_pow2[] = { [5] = 5, [6] = 10, @@ -59,10 +63,12 @@ static const int32_t pages_per_pow2[] = { [22] = 1024 }; -static kpl_memory_allocator_slab *kpl_memory_allocator_slab_init(int32_t size_pow2) { +static kpl_memory_allocator_slab *kpl_memory_allocator_slab_init(int32_t page_count) { kpl_memory_allocator_slab *slab = calloc(1, sizeof(kpl_memory_allocator_slab)); - slab->buffer_size = getpagesize() * pages_per_pow2[size_pow2]; + slab->buffer_size = getpagesize() * page_count; slab->buffer = mmap(NULL, slab->buffer_size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -1, 0); + if (!slab->buffer) + exit(KPL_EXIT_CODE_MEMORY_ERROR); return slab; } @@ -73,11 +79,10 @@ static void *kpl_memory_allocator_slab_bump(kpl_memory_allocator_slab *slab, int } static kpl_object *kpl_memory_allocator_slab_allocate(kpl_memory_allocator_slab_group *slab_group, - uint16_t thread_id) { + int32_t size, int32_t page_count, uint16_t thread_id) { kpl_memory_allocator_slab *slab = slab_group->slab_tail; - int32_t size = 1 << slab_group->slab_size_pow2; if (!slab || slab->buffer_used + size > slab->buffer_size) { - slab = kpl_memory_allocator_slab_init(slab_group->slab_size_pow2); + slab = kpl_memory_allocator_slab_init(page_count); if (!slab_group->slab_head) slab_group->slab_head = slab_group->slab_tail = slab; else @@ -94,20 +99,39 @@ static kpl_object *kpl_memory_allocator_slab_allocate(kpl_memory_allocator_slab_ } void *kpl_memory_allocator_allocate(kpl_memory_allocator *memory_allocator, int32_t size, uint16_t thread_id) { - if (size < 1 << KPL_MEMORY_ALLOCATOR_MIN_ROUND_SIZE_POW2) - return NULL; + if (size < 1 << KPL_MEMORY_ALLOCATOR_MIN_ROUND_SIZE_POW2 || size > 1 << KPL_MEMORY_ALLOCATOR_MAX_SIZE_POW2) + exit(KPL_EXIT_CODE_MEMORY_ERROR); int32_t size_pow2 = kpl_memory_allocator_get_size_pow2(size); if (size_pow2 < KPL_MEMORY_ALLOCATOR_MIN_SIZE_POW2) size_pow2 = KPL_MEMORY_ALLOCATOR_MIN_SIZE_POW2; kpl_memory_allocator_slab_group *slab_group = &memory_allocator->slabs[size_pow2]; kpl_object *object = kpl_object_queue_dequeue(&slab_group->object_pool); - if (!object) - object = kpl_memory_allocator_slab_allocate(slab_group, thread_id); + if (!object) { + int32_t size = 1 << slab_group->slab_size_pow2; + int32_t page_count = pages_per_pow2[slab_group->slab_size_pow2]; + object = kpl_memory_allocator_slab_allocate(slab_group, size, page_count, thread_id); + } object->object_flags = KPL_OBJECT_FLAGS_EMPTY; return object; } +void *kpl_memory_allocator_allocate_gc(kpl_memory_allocator *memory_allocator, uint16_t thread_id) { + kpl_memory_allocator_slab_group *slab_gc = &memory_allocator->slab_gc; + kpl_object_gc *object_gc = kpl_object_queue_dequeue(&slab_gc->object_pool); + if (!object_gc) { + int32_t size = sizeof(kpl_object_gc); + int32_t page_count = KPL_MEMORY_ALLOCATOR_GC_PAGES; + object_gc = (kpl_object_gc*) kpl_memory_allocator_slab_allocate(slab_gc, size, page_count, thread_id); + } + object_gc->object_flags = KPL_OBJECT_FLAGS_EMPTY; + return object_gc; +} + void kpl_memory_allocator_return(kpl_memory_allocator *memory_allocator, void *object) { int32_t size_pow2 = kpl_memory_allocator_get_size_pow2(((kpl_object*) object)->object_size); kpl_object_queue_enqueue(&memory_allocator->slabs[size_pow2].object_pool, object); } + +void kpl_memory_allocator_return_gc(kpl_memory_allocator *memory_allocator, void *object) { + kpl_object_queue_enqueue(&memory_allocator->slab_gc.object_pool, object); +} diff --git a/source/string.c b/source/string.c new file mode 100644 index 0000000..e2d19ed --- /dev/null +++ b/source/string.c @@ -0,0 +1,24 @@ + +#include "kpl.h" + +static const kpl_interface object_string_interface = { + .gc_mark_object_fn = NULL, + .native_free_fn = NULL, + .log_fn = NULL +}; + +kpl_object_string *kpl_object_string_create(kpl_thread *thread, int32_t bytes_size) { + if (bytes_size < 0) + return NULL; + bytes_size++; + kpl_object_string *object_string = kpl_gc_new(thread, &object_string_interface, + sizeof(kpl_object_string) + sizeof(uint8_t) * bytes_size); + object_string->bytes_length = 0; + object_string->bytes_size = object_string->object_size - sizeof(kpl_object_string); + memset(object_string->bytes, '\0', object_string->bytes_size); + return object_string; +} + +extern inline kpl_object_string *kpl_object_string_from_c_string(kpl_thread *thread, const char *c_string); + +extern inline void kpl_object_string_destroy(kpl_thread *thread, kpl_object_string *object_string); diff --git a/source/task.c b/source/task.c new file mode 100644 index 0000000..add3363 --- /dev/null +++ b/source/task.c @@ -0,0 +1,44 @@ + +#include "kpl.h" + +static void kpl_interface_object_task_gc_mark_object(void *object) { + (void) object; + // TODO GC MARK +} + +static const kpl_interface object_task_interface = { + .gc_mark_object_fn = kpl_interface_object_task_gc_mark_object, + .native_free_fn = NULL, + .log_fn = NULL +}; + +kpl_object_task *kpl_object_task_create(kpl_thread *thread, int32_t variables_size, uint64_t variables_gc_mask) { + if (variables_size < 0 || variables_size > KPL_TASK_VARIABLES_MAX || + variables_gc_mask > 1LU << KPL_TASK_VARIABLES_MAX) + return NULL; + kpl_object_task *object_task = kpl_gc_new(thread, &object_task_interface, + sizeof(kpl_object_task) + sizeof(kpl_value64) * variables_size); + __atomic_clear(&object_task->join_ready, __ATOMIC_RELAXED); + object_task->variables_gc_mask = variables_gc_mask; + object_task->io_res = 0; + object_task->io_flags = 0; + object_task->join_task = NULL; + object_task->result = KPL_RESULT_EMPTY; + for (int32_t variable_index = 0; variable_index < variables_size; variable_index++) + object_task->variables[variable_index] = KPL_VALUE64_EMPTY; + return object_task; +} + +extern inline void kpl_object_task_destroy(kpl_thread *thread, kpl_object_task *object_task); + +void kpl_object_task_done(kpl_thread *thread, kpl_object_task *object_task) { + if (!__atomic_test_and_set(&object_task->join_ready, __ATOMIC_SEQ_CST) || !object_task->join_task) + return; + kpl_object_task *join_task = object_task->join_task; + object_task->join_task = NULL; + __atomic_clear(&object_task->join_ready, __ATOMIC_RELAXED); + kpl_env_queue_task(thread->env, join_task, KPL_ENV_QUEUE_MODE_SYNC); +} + +extern inline void kpl_object_task_join(kpl_thread *thread, kpl_object_task *restrict object_task_complete, + kpl_object_task *restrict object_task_join); diff --git a/test/gc.c b/test/gc.c index 2af29d3..6834ad2 100644 --- a/test/gc.c +++ b/test/gc.c @@ -4,10 +4,7 @@ #define TEST_OBJECT_COUNT 10 KPL_TEST(basic_mark_and_sweep) { - kpl_env env = {}; - kpl_thread thread = { .env = &env }; - kpl_thread *threads = { &thread }; - env.threads = threads; + kpl_thread thread = {}; kpl_memory_allocator_init(&thread.memory_allocator); kpl_gc_init(&thread.gc); kpl_object *objs[TEST_OBJECT_COUNT] = {}; diff --git a/test/string.c b/test/string.c new file mode 100644 index 0000000..8a6742c --- /dev/null +++ b/test/string.c @@ -0,0 +1,14 @@ + +#include "kpl_test.h" + +KPL_TEST(string_init) { + kpl_thread thread = {}; + const char *c_string = "test string\n"; + kpl_memory_allocator_init(&thread.memory_allocator); + kpl_gc_init(&thread.gc); + kpl_object_string *string = kpl_object_string_from_c_string(&thread, c_string); + KPL_ASSERT(string->bytes_length == (int32_t) strlen(c_string), "string should have same length as c string"); + KPL_ASSERT(strcmp(c_string, (char*) string->bytes) == 0, "new string should equal c string"); + kpl_gc_free(&thread); + kpl_memory_allocator_free(&thread.memory_allocator); +} -- cgit v1.2.3