diff options
Diffstat (limited to 'include/kpl')
| -rw-r--r-- | include/kpl/array.h | 31 | ||||
| -rw-r--r-- | include/kpl/class.h | 14 | ||||
| -rw-r--r-- | include/kpl/error.h | 31 | ||||
| -rw-r--r-- | include/kpl/gc.h | 3 | ||||
| -rw-r--r-- | include/kpl/io.h | 2 | ||||
| -rw-r--r-- | include/kpl/memory.h | 13 | ||||
| -rw-r--r-- | include/kpl/object.h | 88 | ||||
| -rw-r--r-- | include/kpl/string.h | 29 | ||||
| -rw-r--r-- | include/kpl/task.h | 34 | ||||
| -rw-r--r-- | include/kpl/value.h | 26 |
10 files changed, 257 insertions, 14 deletions
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; @@ -57,6 +59,17 @@ 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 @@ -74,20 +78,92 @@ typedef struct _kpl_object_gc { } 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; |
