diff options
Diffstat (limited to 'include')
| -rw-r--r-- | include/README | 20 | ||||
| -rw-r--r-- | include/kpl.h | 73 | ||||
| -rw-r--r-- | include/kpl/array.h | 31 | ||||
| -rw-r--r-- | include/kpl/ast.h | 193 | ||||
| -rw-r--r-- | include/kpl/class.h | 38 | ||||
| -rw-r--r-- | include/kpl/color.h | 120 | ||||
| -rw-r--r-- | include/kpl/env.h | 136 | ||||
| -rw-r--r-- | include/kpl/error.h | 31 | ||||
| -rw-r--r-- | include/kpl/gc.h | 69 | ||||
| -rw-r--r-- | include/kpl/identifier.h | 58 | ||||
| -rw-r--r-- | include/kpl/io.h | 101 | ||||
| -rw-r--r-- | include/kpl/memory.h | 75 | ||||
| -rw-r--r-- | include/kpl/object.h | 185 | ||||
| -rw-r--r-- | include/kpl/operator.h | 13 | ||||
| -rw-r--r-- | include/kpl/storage.h | 49 | ||||
| -rw-r--r-- | include/kpl/string.h | 29 | ||||
| -rw-r--r-- | include/kpl/task.h | 39 | ||||
| -rw-r--r-- | include/kpl/tree.h | 61 | ||||
| -rw-r--r-- | include/kpl/type.h | 179 | ||||
| -rw-r--r-- | include/kpl/value.h | 83 | ||||
| -rw-r--r-- | include/kpl_test.h | 69 |
21 files changed, 0 insertions, 1652 deletions
diff --git a/include/README b/include/README deleted file mode 100644 index 4147aed..0000000 --- a/include/README +++ /dev/null @@ -1,20 +0,0 @@ -Header Files - ---- - -Includes: - -Only include one of these files in the source - -kpl.h # For the main kpl application -kpl_test.h # For Testing - ---- - -Compiler Flags - -KPL_COLOR_OFF # Turn off internal pretty printing, does not effect source file pretty printing - -KPL_VALGRIND # Add valgrind memory checking to the custom allocators - ---- diff --git a/include/kpl.h b/include/kpl.h deleted file mode 100644 index ede4fdd..0000000 --- a/include/kpl.h +++ /dev/null @@ -1,73 +0,0 @@ -/* - * This is the only header included in any c source file - */ - -#pragma once - -#define KPL_VERSION "0.0.0" - -#define KPL_FILE_EXTENSION ".kpl" - -#define KPL_WEBSITE "https://kpl.dev" - -// System includes -#define _GNU_SOURCE -#include <stdlib.h> -#include <stdio.h> -#include <stdint.h> -#include <stdbool.h> -#include <stddef.h> -#include <string.h> -#include <ctype.h> -#include <errno.h> -#include <limits.h> -#include <sched.h> -#include <pthread.h> -#include <semaphore.h> -#include <fcntl.h> -#include <unistd.h> -#include <sys/syscall.h> -#include <sys/stat.h> -#include <sys/ioctl.h> -#include <sys/mman.h> -#include <sys/uio.h> -#include <sys/param.h> -#include <linux/fs.h> -#include <linux/io_uring.h> -#ifdef KPL_VALGRIND -#include <valgrind/valgrind.h> -#include <valgrind/memcheck.h> -#endif - -// 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; - -typedef struct _kpl_env kpl_env; - -// KPL includes -#include "kpl/color.h" -#include "kpl/value.h" -#include "kpl/class.h" -#include "kpl/object.h" -#include "kpl/tree.h" -#include "kpl/identifier.h" -#include "kpl/type.h" -#include "kpl/storage.h" -#include "kpl/ast.h" -#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/type.h" -#include "kpl/operator.h" -#include "kpl/ast.h" -#include "kpl/env.h" diff --git a/include/kpl/array.h b/include/kpl/array.h deleted file mode 100644 index de3b0e8..0000000 --- a/include/kpl/array.h +++ /dev/null @@ -1,31 +0,0 @@ -/* - * 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/ast.h b/include/kpl/ast.h deleted file mode 100644 index 427ce1b..0000000 --- a/include/kpl/ast.h +++ /dev/null @@ -1,193 +0,0 @@ -/* - * KPL AST - * An AST is the definition of code and data - * AST nodes cannot be shared across threads - */ - -#pragma once - -/* - * Must fit within 4 bits, 8 kinds total, 0 invalid - */ -typedef enum : uint8_t { - KPL_AST_KIND_LIST = 1, - KPL_AST_KIND_FUNCTION = 2, - KPL_AST_KIND_COROUTINE = 3, - KPL_AST_KIND_DATA = 4, - KPL_AST_KIND_IDENTIFIER = 5, - KPL_AST_KIND_TYPE = 6, - KPL_AST_KIND_OP = 7, - KPL_AST_KIND_ACTION = 8 -} kpl_ast_kind; - -// Ast nodes can be placed in a linked list -#define KPL_AST_HEADER(STRUCT_NAME) \ - KPL_OBJECT_ALLOCATOR_HEADER(STRUCT_NAME); \ - struct STRUCT_NAME *ast_prev, *ast_next - -/* - * All ast nodes inherit this, use meta to cast to node type - */ -typedef struct _kpl_ast_base { - KPL_AST_HEADER(_kpl_ast_base); -} kpl_ast_base; - -/* - * Use object_flags field because the ast does not use the garbage collector - */ -typedef struct { - kpl_ast_kind kind : 4; - uint16_t info : 12; // meaning of this changes based on kind -} kpl_ast_meta; - -#define KPL_AST_META_KIND_BIT_WIDTH 4 - -#define KPL_AST_META_KIND_BIT_MASK 15 - -/* - * Load metadata stored in the header object_flags - */ -inline kpl_ast_meta kpl_ast_meta_get(void *ast) { - kpl_ast_base *ast_base = ast; - uint16_t object_flags = ast_base->object_flags; - kpl_ast_meta ast_meta = {}; - ast_meta.kind = object_flags & KPL_AST_META_KIND_BIT_MASK; - ast_meta.info = object_flags >> KPL_AST_META_KIND_BIT_WIDTH; - return ast_meta; -} - -/* - * Store metadata in the header object_flags - */ -inline void kpl_ast_meta_set(void *ast, kpl_ast_meta ast_meta) { - kpl_ast_base *ast_base = ast; - ast_base->object_flags = 0; - ast_base->object_flags = ast_meta.info << KPL_AST_META_KIND_BIT_WIDTH; - ast_base->object_flags |= ast_meta.kind; -} - -/* - * An identifier without its specification (var, arg, ref) in a lists scope - */ -typedef struct _kpl_ast_scope { - KPL_TREE_HEADER(_kpl_ast_scope); - uint32_t id; // unique id - kpl_type_base *type; - kpl_class class; - kpl_identifier identifier; -} kpl_ast_scope; - -/* - * Stored on ast_meta.info - */ -typedef enum : uint8_t { - KPL_AST_LIST_KIND_BLOCK = 1, - KPL_AST_LIST_KIND_COLLECTION = 2, - KPL_AST_LIST_KIND_MODIFIERS = 3 -} kpl_ast_list_kind; - -typedef struct _kpl_ast_function kpl_ast_function; - -/* - * A "list" of statements - * Only block lists have their own scope - */ -typedef struct _kpl_ast_list { - KPL_AST_HEADER(_kpl_ast_list); - struct _kpl_ast_list *parent_list; - kpl_ast_function *function_parent; // owns this list - kpl_ast_base *list_head, *list_tail; - kpl_tree scope_tree; -} kpl_ast_list; - -typedef struct _kpl_ast_coroutine kpl_ast_coroutine; - -/* - * Synchronous code - */ -typedef struct _kpl_ast_function { - KPL_AST_HEADER(_kpl_ast_function); - kpl_ast_coroutine *coroutine_parent; // owns this function - kpl_ast_list *statements; -} kpl_ast_function; - -/* - * A "list" of functions - * Coroutine is synchronous if function_head == function_tail - * Synchronous coroutines can be called asynchronously, but not vice versa - */ -typedef struct _kpl_ast_coroutine { - KPL_AST_HEADER(_kpl_ast_coroutine); - kpl_ast_coroutine *parent_coroutine; - kpl_type_coroutine *type; - kpl_ast_function *function_head, *function_tail; -} kpl_ast_coroutine; - -/* - * Value or collection - */ -typedef struct _kpl_ast_data { - KPL_AST_HEADER(_kpl_ast_data); - kpl_type_base *type; - kpl_class class; -} kpl_ast_data; - -/* - * Stored on ast_meta.info - */ -typedef enum : uint16_t { // 12 bit limit - KPL_AST_IDENTIFIER_KIND_FLAG_EMPTY = 0, - KPL_AST_IDENTIFIER_KIND_VARIABLE = 1 << 0, - KPL_AST_IDENTIFIER_KIND_ARGUMENT = 1 << 1, - KPL_AST_IDENTIFIER_KIND_REFERENCE = 1 << 2, - KPL_AST_IDENTIFIER_FLAG_NOT_NULL = 1 << 3 // Flow typing nullable type has been checked -} kpl_ast_identifier_kind_flags; - -/* - * Represents a variable, argument or ref - */ -typedef struct _kpl_ast_identifier { - KPL_AST_HEADER(_kpl_ast_identifier); - kpl_ast_scope *scope; -} kpl_ast_identifier; - -/* - * Represents a type - * Types are left associative operators, they cast data and define arguments - * Cannot define variables - */ -typedef struct _kpl_ast_type { - KPL_AST_HEADER(_kpl_ast_type); - kpl_type_base *type; - kpl_ast_base *ast_right; // the ast node this type is casting/defining -} kpl_ast_type; - -/* - * Represents an operator - * All operators return a Result - * The operator name is stored on ast_meta.info - */ -typedef struct _kpl_ast_operator { - KPL_AST_HEADER(_kpl_ast_operator); - kpl_type_result *type; - kpl_ast_base *ast_children[2]; // left 0, right 1 -} kpl_ast_operator; - -/* - * Stored on ast_meta.info - */ -typedef enum : uint8_t { - KPL_AST_ACTION_KIND_IF = 1, - KPL_AST_ACTION_KIND_LOOP = 2, - KPL_AST_ACTION_KIND_MATCH = 3 -} kpl_ast_action_kind; - -/* - * Action, if, loop, match - * All actions return a Result - */ -typedef struct _kpl_ast_action { - KPL_AST_HEADER(_kpl_ast_action); - kpl_type_result *type; - kpl_ast_list *condition_list, *body_list; -} kpl_ast_action; diff --git a/include/kpl/class.h b/include/kpl/class.h deleted file mode 100644 index 8cb05da..0000000 --- a/include/kpl/class.h +++ /dev/null @@ -1,38 +0,0 @@ -/* - * The kpl class a 64bit value with a table of functions - */ - -#pragma once - -/* - * How to traverse the object and mark kpl_object_gc - */ -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); - -/* - * `copy operator - */ -typedef kpl_value64 kpl_interface_copy_fn(kpl_thread *thread_from, kpl_thread *thread_to, kpl_value64 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_copy_fn *copy_fn; -} kpl_interface; - -typedef struct { - kpl_value64 value; - const kpl_interface *interface; -} kpl_class; diff --git a/include/kpl/color.h b/include/kpl/color.h deleted file mode 100644 index dc7fe20..0000000 --- a/include/kpl/color.h +++ /dev/null @@ -1,120 +0,0 @@ -/* - * Colors for pretty printing - */ - -#pragma once - -#define KPL_COLOR_CONCAT(COLOR) KPL_COLOR_ ## COLOR - -#ifndef KPL_COLOR_OFF - -#define KPL_COLOR(A) "\e[" KPL_COLOR_CONCAT(A) "m" -#define KPL_COLOR2(A, B) "\e[" KPL_COLOR_CONCAT(A) ";" KPL_COLOR_CONCAT(B) "m" -#define KPL_COLOR3(A, B, C) "\e[" KPL_COLOR_CONCAT(A) ";" KPL_COLOR_CONCAT(B) ";" KPL_COLOR_CONCAT(C) "m" - -#define KPL_COLOR_RESET "0" -#define KPL_COLOR_BOLD "1" -#define KPL_COLOR_FAINT "2" -#define KPL_COLOR_ITALIC "3" -#define KPL_COLOR_UNDERLINE "4" -#define KPL_COLOR_BLINK "5" -#define KPL_COLOR_REVERSE "7" -#define KPL_COLOR_HIDDEN "8" -#define KPL_COLOR_RESET_BOLD "21" -#define KPL_COLOR_RESET_FAINT "22" -#define KPL_COLOR_RESET_ITALIC "23" -#define KPL_COLOR_RESET_UNDERLINE "24" -#define KPL_COLOR_RESET_BLINK "25" -#define KPL_COLOR_RESET_REVERSE "27" -#define KPL_COLOR_RESET_HIDDEN "28" -#define KPL_COLOR_BLACK "30" -#define KPL_COLOR_RED "31" -#define KPL_COLOR_GREEN "32" -#define KPL_COLOR_YELLOW "33" -#define KPL_COLOR_BLUE "34" -#define KPL_COLOR_MAGENTA "35" -#define KPL_COLOR_CYAN "36" -#define KPL_COLOR_GREY "37" -#define KPL_COLOR_DEFAULT "39" -#define KPL_COLOR_BG_BLACK "30" -#define KPL_COLOR_BG_RED "41" -#define KPL_COLOR_BG_GREEN "42" -#define KPL_COLOR_BG_YELLOW "43" -#define KPL_COLOR_BG_BLUE "44" -#define KPL_COLOR_BG_MAGENTA "45" -#define KPL_COLOR_BG_CYAN "46" -#define KPL_COLOR_BG_GREY "47" -#define KPL_COLOR_DARK_GREY "90" -#define KPL_COLOR_LIGHT_RED "91" -#define KPL_COLOR_LIGHT_GREEN "92" -#define KPL_COLOR_LIGHT_YELLOW "93" -#define KPL_COLOR_LIGHT_BLUE "94" -#define KPL_COLOR_LIGHT_MAGENTA "95" -#define KPL_COLOR_LIGHT_CYAN "96" -#define KPL_COLOR_WHITE "97" -#define KPL_COLOR_BG_DARK_GREY "100" -#define KPL_COLOR_BG_LIGHT_RED "101" -#define KPL_COLOR_BG_LIGHT_GREEN "102" -#define KPL_COLOR_BG_LIGHT_YELLOW "103" -#define KPL_COLOR_BG_LIGHT_BLUE "104" -#define KPL_COLOR_BG_LIGHT_MAGENTA "105" -#define KPL_COLOR_BG_LIGHT_CYAN "106" -#define KPL_COLOR_BG_WHITE "107" - -#else - -#define KPL_COLOR(A) KPL_COLOR_CONCAT(A) -#define KPL_COLOR2(A, B) KPL_COLOR_CONCAT(A) KPL_COLOR_CONCAT(B) -#define KPL_COLOR3(A, B, C) KPL_COLOR_CONCAT(A) KPL_COLOR_CONCAT(B) KPL_COLOR_CONCAT(C) - - -#define KPL_COLOR_RESET "" -#define KPL_COLOR_BOLD "" -#define KPL_COLOR_FAINT "" -#define KPL_COLOR_ITALIC "" -#define KPL_COLOR_UNDERLINE "" -#define KPL_COLOR_BLINK "" -#define KPL_COLOR_REVERSE "" -#define KPL_COLOR_HIDDEN "" -#define KPL_COLOR_RESET_BOLD "" -#define KPL_COLOR_RESET_FAINT "" -#define KPL_COLOR_RESET_ITALIC "" -#define KPL_COLOR_RESET_UNDERLINE "" -#define KPL_COLOR_RESET_BLINK "" -#define KPL_COLOR_RESET_REVERSE "" -#define KPL_COLOR_RESET_HIDDEN "" -#define KPL_COLOR_BLACK "" -#define KPL_COLOR_RED "" -#define KPL_COLOR_GREEN "" -#define KPL_COLOR_YELLOW "" -#define KPL_COLOR_BLUE "" -#define KPL_COLOR_MAGENTA "" -#define KPL_COLOR_CYAN "" -#define KPL_COLOR_GREY "" -#define KPL_COLOR_DEFAULT "" -#define KPL_COLOR_BG_BLACK "" -#define KPL_COLOR_BG_RED "" -#define KPL_COLOR_BG_GREEN "" -#define KPL_COLOR_BG_YELLOW "" -#define KPL_COLOR_BG_BLUE "" -#define KPL_COLOR_BG_MAGENTA "" -#define KPL_COLOR_BG_CYAN "" -#define KPL_COLOR_BG_GREY "" -#define KPL_COLOR_DARK_GREY "" -#define KPL_COLOR_LIGHT_RED "" -#define KPL_COLOR_LIGHT_GREEN "" -#define KPL_COLOR_LIGHT_YELLOW "" -#define KPL_COLOR_LIGHT_BLUE "" -#define KPL_COLOR_LIGHT_MAGENTA "" -#define KPL_COLOR_LIGHT_CYAN "" -#define KPL_COLOR_WHITE "" -#define KPL_COLOR_BG_DARK_GREY "" -#define KPL_COLOR_BG_LIGHT_RED "" -#define KPL_COLOR_BG_LIGHT_GREEN "" -#define KPL_COLOR_BG_LIGHT_YELLOW "" -#define KPL_COLOR_BG_LIGHT_BLUE "" -#define KPL_COLOR_BG_LIGHT_MAGENTA "" -#define KPL_COLOR_BG_LIGHT_CYAN "" -#define KPL_COLOR_BG_WHITE "" - -#endif diff --git a/include/kpl/env.h b/include/kpl/env.h deleted file mode 100644 index c4237fa..0000000 --- a/include/kpl/env.h +++ /dev/null @@ -1,136 +0,0 @@ -/* - * KPL environment objects - */ - -#pragma once - -typedef enum : int32_t { - KPL_THREAD_DETERIMINE_COUNT = -1, - KPL_THREAD_MAIN_ID = 0, - KPL_THEAD_THREAD_COUNT_MIN = 1, - KPL_THEAD_THREAD_COUNT_MAX = UINT16_MAX -} kpl_thread_enum; - -typedef struct _kpl_thread { - uint16_t thread_id; // self id - kpl_env *env; // access env - kpl_memory_allocator memory_allocator; - kpl_gc gc; - kpl_io_uring io_uring; - // TODO jit allocator - _Atomic ssize_t priority; // io+running the total number of tasks in threads queue, lower is higher priority - kpl_object_queue task_queue, error_queue; // traverse for gc marking - pthread_t pthread; // not used for main thread - sem_t sem; // wait when task queue and io queue is empty -} kpl_thread; - -typedef enum : int32_t { - KPL_ENV_CONFIG_NO_ARGS = -1 -} kpl_env_enum; - -typedef enum : uint32_t { - KPL_ENV_FLAG_EMPTY = 0, - KPL_ENV_FLAG_STOP = 1 << 0, // any thread can set, all threads check - KPL_ENV_FLAG_HAS_ERROR = 1 << 1, - KPL_ENV_FLAG_RUN_MODULE = 1 << 2, - KPL_ENV_FLAG_RUN_EVAL = 1 << 3, - KPL_ENV_FLAG_RUN_REPL = 1 << 4, - KPL_ENV_FLAG_RUN_FUNCTION = 1 << 5 -} kpl_env_flags; - -/* - * Life cycle: - * kpl_env env = {}; - * kpl_env_config &env, argc, argv - * kpl_env_init &env - * add to the namespace tree - * kpl_env_run &env - * kpl_env_free &env - */ -typedef struct _kpl_env { - _Atomic uint32_t flags; // see kpl_env_flags for values, any thread can modify or check needs atomic - int32_t io_uring_queue_size_pow2; // the power of two to calculate the entries in the io_uring queue - int32_t thread_count; // -1 updates this value to max possible, 0 invalid, >= 1 for specific number - union { - const char *kpl_c_string_entry; // file or string, not used for repl, pointer used in kpl args namespace - kpl_task_fn *kpl_fn_entry; // a task function, for a task with no variables, placed on main thread - }; // only one entry point can be defined - _Atomic ssize_t total_priority; // the sum of the threads priority fields, application stops on zero - kpl_thread *threads; // 0 is main thread, >= 1 are spawned pthreads - kpl_tree namespace_tree, module_tree; // Trees of kpl_storage - pthread_mutex_t module_tree_mutex; - // TODO jit -} kpl_env; // kpl state, no globals, must be zero initialized - -/* - * Set up initial values before initialization - * For defaults and/or directly setting without arguments, pass -1 to argc then fields can be updated directly - * If not passing args, one of the RUN flags must be set - */ -kpl_exit_code kpl_env_config(kpl_env *env, int32_t argc, char *argv[]); - -/* - * Initialize, read flags to create initial entry task - * env kpl_env_config must be called before this - */ -kpl_exit_code kpl_env_init(kpl_env *env); - -/* - * Increase priority by one - */ -inline void kpl_env_priority_increment(kpl_thread *thread) { - thread->priority++; - thread->env->total_priority++; -} - -/* - * Increase priority by amount - */ -inline void kpl_env_priority_add(kpl_thread *thread, ssize_t amount) { - thread->priority += amount; - thread->env->total_priority += amount; -} - -/* - * Decrease priority by one - */ -inline void kpl_env_priority_decrement(kpl_thread *thread) { - thread->priority--; - thread->env->total_priority--; -} - -typedef enum : uint8_t { - KPL_ENV_QUEUE_MODE_ASYNC = 0, - KPL_ENV_QUEUE_MODE_SYNC = 1 -} kpl_env_queue_mode; - -/* - * queue_mode ASYNC queues thread with lowest priority - * queue_mode SYNC queues thread on threads[task->thread_id] - */ -void kpl_env_queue_task(kpl_env *env, kpl_object_task *task, kpl_env_queue_mode queue_mode); - -/* - * Run once, not re-enetrant - * TODO RUN TASK AND IO ORDERING - * Cleans Up each thread async - */ -kpl_exit_code kpl_env_run(kpl_env *env); - -/* - * Stops KPL, can be called on any task in KPL - * Does not return just signals to all threads to stop, the return status will be from kpl_env_run - */ -inline void kpl_env_stop(kpl_env *env) { - env->flags |= KPL_ENV_FLAG_STOP; -} - -/* - * Run garbage collector - */ -void kpl_env_gc(kpl_env *env); - -/* - * Clean up - */ -void kpl_env_free(kpl_env *env); diff --git a/include/kpl/error.h b/include/kpl/error.h deleted file mode 100644 index 407363c..0000000 --- a/include/kpl/error.h +++ /dev/null @@ -1,31 +0,0 @@ -/* - * 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 deleted file mode 100644 index 5240c40..0000000 --- a/include/kpl/gc.h +++ /dev/null @@ -1,69 +0,0 @@ -/* - * KPL Garbage Collector - */ - -#pragma once - -/* - * Per thread gc, additions and removals are done by the owner thread - * Marking can be done by any thread - */ -typedef struct { - _Atomic size_t gc_cycle_head_additions; // updated by owner thread, checked by any thread - kpl_object_gc *gc_head, *gc_mark_head; // only can be accessed by owner thread -} kpl_gc; - -/* - * Init - */ -inline void kpl_gc_init(kpl_gc *gc) { - __atomic_store_n(&gc->gc_cycle_head_additions, 0, __ATOMIC_RELAXED); - gc->gc_head = gc->gc_mark_head = NULL; -} - -/* - * Create a new gc node and a new kpl_object with object_size - * Add the node to the gc_head - * 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, 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); - -/* - * Mark a gc object - * Tells gc not to delete object - */ -inline void kpl_gc_mark(kpl_object_gc *object_gc) { - __atomic_test_and_set(&object_gc->gc_mark, __ATOMIC_SEQ_CST); -} - -/* - * Move gc_head to gc_mark head - * Need task to traverse task and error queue to mark objects all call kpl_interface_gc_mark_object_fn - * Has to be called separately from kpl_gc_sweep, other tasks will be running while gc runs - */ -inline void kpl_gc_prepare_sweep(kpl_gc *gc) { - gc->gc_mark_head = gc->gc_head; - gc->gc_head = NULL; - gc->gc_cycle_head_additions = 0; -} - -/* - * Run sweep on unmarked objects on gc_mark_head - * Clear mark on marked objects and return them to gc_head - */ -void kpl_gc_sweep(kpl_thread *thread); - -/* - * Run delete on all objects on gc_head - */ -void kpl_gc_free(kpl_thread *thread); diff --git a/include/kpl/identifier.h b/include/kpl/identifier.h deleted file mode 100644 index 9bb42b0..0000000 --- a/include/kpl/identifier.h +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Thread safe identifiers, at the cost of space remove the need for hash/tree lookups - * Keep 50 [A-Za-z0-9_] Characters in 40 bytes - */ - -#pragma once - -#define KPL_IDENTIFIER_PARTS 5 - -#define KPL_IDENTIFIER_BITS_PER_PART 64 - -#define KPL_IDENTIFIER_BITS_PER_VALUE 6 - -#define KPL_IDENTIFIER_VALUES_PER_PART (KPL_IDENTIFIER_BITS_PER_PART / KPL_IDENTIFIER_BITS_PER_VALUE) - -#define KPL_IDENTIFIER_MAX_LENGTH (KPL_IDENTIFIER_VALUES_PER_PART * KPL_IDENTIFIER_PARTS) - -#define KPL_IDENTIFIER_C_STRING_SIZE (KPL_IDENTIFIER_MAX_LENGTH + sizeof(char)) - -/* - * Each character takes 6 bits from bit 0 to bit 59 - * The upper 4 bits represent the number of characters in the part - */ -typedef struct { - uint64_t parts[KPL_IDENTIFIER_PARTS]; -} kpl_identifier; - -/* - * Converts a c string into an identifier - * Checks if the c_string_length <= KPL_IDENTIFIER_MAX_LENGTH - */ -bool kpl_identifier_encode(kpl_identifier *identifier, const char *c_string, size_t c_string_length); - -/* - * Converts an identifier into a c string - * Does not clear the c_string buffer - * Does not check the c_string buffer size assumes the buffer is at least KPL_IDENTIFIER_C_STRING_SIZE - */ -void kpl_identifier_decode(kpl_identifier *identifier, char c_string[KPL_IDENTIFIER_C_STRING_SIZE]); - -/* - * Compares identifiers like strings - * 0 - equal, negative - left < right, positive - left > right - */ -int32_t kpl_identifier_compare(const kpl_identifier *identifier_left, const kpl_identifier *identifier_right); - -/* - * Check if two identifiers are equal - * Faster then comparing when equality check is needed - */ -inline bool kpl_identifier_equal(const kpl_identifier *identifier_a, const kpl_identifier *identifier_b) { - for (int32_t part_index = 0; part_index < KPL_IDENTIFIER_PARTS; part_index++) - if (!identifier_a->parts[part_index] && !identifier_b->parts[part_index]) - break; - else if (identifier_a->parts[part_index] != identifier_b->parts[part_index]) - return false; - return true; -} diff --git a/include/kpl/io.h b/include/kpl/io.h deleted file mode 100644 index 80fadd8..0000000 --- a/include/kpl/io.h +++ /dev/null @@ -1,101 +0,0 @@ -/* - * IO uring - */ - -#pragma once - -typedef enum : int32_t { - KPL_IO_URING_QUEUE_SIZE_POW2_MIN = 12, - KPL_IO_URING_QUEUE_SIZE_POW2_DEFAULT = 15, - KPL_IO_URING_QUEUE_SIZE_POW2_MAX = 20 -} kpl_io_uring_enum; - -/* - * Per thread io_uring container, do not use across threads - * Operations are added to the submit queue by a functions specific to that operation - * Once one or more operations are added, call kpl_io_uring_submit to send the operations to the kernel - * Only one completed operation can be read at a time by kpl_io_uring_get_completed - * Uses defaults that require kernel version of 6.6 or greater - */ -typedef struct { - int32_t ring_fd, ring_queue_entries; // fd for submit, number of queue slots - uint32_t added_to_queue, submitted_to_kernel; //added to submission queue, added with io uring enter - uint32_t sq_cq_size, sqes_size; // using single mmap for kernel submission and completion - uint32_t *sring_head, *sring_tail, *sring_mask; - uint32_t *cring_head, *cring_tail, *cring_mask; - void *sring_ptr, *cring_ptr; - struct io_uring_sqe *sqes; - struct io_uring_cqe *cqes; - struct io_uring_params params; -} kpl_io_uring; // must be zero initialized - -/* - * Called by owner thread - */ -int32_t kpl_io_uring_init(kpl_io_uring *io_uring, int32_t queue_size_pow2); - -/* - * Be sure no operations are in queue before calling - * Called by owner thread - */ -inline void kpl_io_uring_free(kpl_io_uring *io_uring) { - munmap(io_uring->sring_ptr, io_uring->sq_cq_size); - munmap(io_uring->sqes, io_uring->sqes_size); -} - -/* - * 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_object_task - * *thread is needed to decrease priority - */ -kpl_object_task *kpl_io_uring_get_completed(kpl_thread *thread); - -/* - * Submits all sqes (added_to_queue has total) in one io uring eneter - * This does not wait - * *thread is need to increase priority by the number of operations submitted - */ -int32_t kpl_io_uring_submit(kpl_thread *thread); - -/* - * Only call this though one of the functions below to add an operation to the queue - */ -struct io_uring_sqe *kpl_io_uring_op(kpl_io_uring *io_uring, kpl_object_task *task, int32_t op, int32_t fd, - const void *addr, uint32_t len, uint64_t offset); - -/* - * Add operations to io_uring, can add more then one before submit - */ - -inline bool kpl_io_uring_openat(kpl_io_uring *io_uring, kpl_object_task *task, int32_t dir_fd, - const char *path, int32_t flags, mode_t mode) { - struct io_uring_sqe *sqe = kpl_io_uring_op(io_uring, task, IORING_OP_OPENAT, dir_fd, path, mode, 0); - if (sqe) - sqe->open_flags = (uint32_t) flags; - return sqe; -} - -inline bool kpl_io_uring_close(kpl_io_uring *io_uring, kpl_object_task *task, int32_t fd) { - return kpl_io_uring_op(io_uring, task, IORING_OP_CLOSE, fd, NULL, 0, 0); -} - -inline bool kpl_io_uring_read(kpl_io_uring *io_uring, kpl_object_task *task, int32_t fd, void *buffer, - uint32_t byte_size, uint64_t offset) { - return kpl_io_uring_op(io_uring, task, IORING_OP_READ, fd, buffer, byte_size, offset); -} - -inline bool kpl_io_uring_write(kpl_io_uring *uring, kpl_object_task *task, int32_t fd, void *buffer, - uint32_t byte_size, uint64_t offset) { - return kpl_io_uring_op(uring, task, IORING_OP_WRITE, fd, buffer, byte_size, offset); -} - -inline bool kpl_io_uring_statx(kpl_io_uring *io_uring, kpl_object_task *task, int32_t dir_fd, - const char *path, int32_t flags, uint32_t mask, struct statx *statx_object) { - struct io_uring_sqe *sqe = kpl_io_uring_op(io_uring, task, IORING_OP_STATX, dir_fd, path, mask, - (uint64_t) statx_object); - if (sqe) - sqe->statx_flags = (uint32_t) flags; - return sqe; -} diff --git a/include/kpl/memory.h b/include/kpl/memory.h deleted file mode 100644 index 5056483..0000000 --- a/include/kpl/memory.h +++ /dev/null @@ -1,75 +0,0 @@ -/* - * KPL memory allocator - */ - -typedef enum : int32_t { - KPL_MEMORY_ALLOCATOR_MIN_SIZE_POW2 = 5, - KPL_MEMORY_ALLOCATOR_MIN_ROUND_SIZE_POW2 = KPL_MEMORY_ALLOCATOR_MIN_SIZE_POW2 - 1, - KPL_MEMORY_ALLOCATOR_MAX_SIZE_POW2 = 22, - KPL_MEMORY_ALLOCATOR_SLAB_COUNT = KPL_MEMORY_ALLOCATOR_MAX_SIZE_POW2 + 1 -} kpl_memory_allocator_enum; - -/* - * Each slab is a linked list allocated with calloc and free - * *buffer is allocated with mmap - */ -typedef struct _kpl_memory_allocator_slab { - uint32_t buffer_size, buffer_used; - struct _kpl_memory_allocator_slab *slab_next; - uint8_t *buffer; -} kpl_memory_allocator_slab; - -/* - * Each group only allocates for a single power of two - */ -typedef struct { - int32_t slab_size_pow2; - kpl_memory_allocator_slab *slab_head, *slab_tail; - kpl_object_queue object_pool; -} kpl_memory_allocator_slab_group; - -/* - * The allocator allocates object rounded to the next power of two - * We first check if the pool has objects, then bump allocate off the slab *buffer - * Each slab is only responsible for one power of two, all objects in buffer and pool are of the same size - * 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; - -/* - * Called by owner thread - */ -void kpl_memory_allocator_init(kpl_memory_allocator *memory_allocator); - -/* - * Called by owner thread - */ -void kpl_memory_allocator_free(kpl_memory_allocator *memory_allocator); - -/* - * Only call this though kpl_thread_object_allocate - * All allocations must extend the KPL_OBJECT_HEADER - */ -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_ALLOCATOR_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 deleted file mode 100644 index 5566b00..0000000 --- a/include/kpl/object.h +++ /dev/null @@ -1,185 +0,0 @@ -/* - * KPL specific objects - */ - -#pragma once - -typedef enum : uint16_t { - KPL_OBJECT_FLAGS_EMPTY = 0, - KPL_OBJECT_FLAGS_IO_PENDING = 1 << 0, // When io pending tasks will not be reachable by GC - KPL_OBJECT_FLAGS_GC_PERSIST = 1 << 1 // Object is never swept by GC until program end -} kpl_object_flags; - -// 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_ALLOCATOR_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_ALLOCATOR_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; - -/* - * Atomic linked list queue any thread can add, only owner thread can remove - */ -typedef struct { - kpl_object *_Atomic head, *tail; // head is modified with atomic exchange - struct { - kpl_object *_Atomic queue_next; - } dummy; // head and tail are never null will always point to this -} kpl_object_queue; - -/* - * Only owner thread can call this - */ -inline void kpl_object_queue_init(kpl_object_queue *object_queue) { - __atomic_store_n(&object_queue->dummy.queue_next, NULL, __ATOMIC_RELAXED); - __atomic_store_n(&object_queue->head, &object_queue->dummy, __ATOMIC_RELAXED); - object_queue->tail = (kpl_object*) &object_queue->dummy; -} - -/* - * Always succeeds, any thread can call this - */ -inline void kpl_object_queue_enqueue(kpl_object_queue *object_queue, void *object) { - kpl_object *obj = object; - __atomic_store_n(&obj->queue_next, NULL, __ATOMIC_RELAXED); - kpl_object *head = __atomic_exchange_n(&object_queue->head, obj, __ATOMIC_SEQ_CST); - __atomic_store_n(&head->queue_next, obj, __ATOMIC_RELEASE); -} - -/* - * Null for not found, only owner thread can call this - */ -void *kpl_object_queue_dequeue(kpl_object_queue *object_queue); - -/* - * All runtime objects are wrapped in the object_gc - * Only owner thread can use gc_prev and gc_next - * Any thread can mark - */ -typedef struct _kpl_object_gc { - KPL_OBJECT_ALLOCATOR_HEADER(_kpl_object_gc); - struct _kpl_object_gc *gc_prev, *gc_next; - _Atomic bool gc_mark __attribute((aligned(8))); - 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 - * 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_class error_file, error_data; // Can be a reference to C code or KPL code - int32_t error_line; -} kpl_object_error; - -typedef void kpl_task_fn(kpl_thread *thread, kpl_object_task *object_task); - -/* - * Max variables per task, a bit mask used to check if the variable can be garbage collected - */ -#define KPL_TASK_VARIABLES_MAX 56 - -/* - * Coroutine state object - * 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; // 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_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 -} kpl_object_task; diff --git a/include/kpl/operator.h b/include/kpl/operator.h deleted file mode 100644 index 7912ff7..0000000 --- a/include/kpl/operator.h +++ /dev/null @@ -1,13 +0,0 @@ -/* - * KPL Operator names - * AST and IR use same enum - */ - -#pragma once - -/* - * Limited to 12 bits for AST, 0 is invalid - */ -typedef enum : uint16_t { - KPL_OPERATOR_NAME_INVALID, -} kpl_operator_name; diff --git a/include/kpl/storage.h b/include/kpl/storage.h deleted file mode 100644 index 5b0e13a..0000000 --- a/include/kpl/storage.h +++ /dev/null @@ -1,49 +0,0 @@ -/* - * KPL Object storage - */ - -#pragma once - -/* - * Inner tree storage - * Keys will always be kpl_identifier - */ -typedef struct _kpl_storgae_object { - KPL_TREE_HEADER(_kpl_storgae_object); - kpl_type_base *type; - union { - kpl_class class; - kpl_function function; - }; - kpl_identifier identifier; -} kpl_stoage_object; - -// c_string_ptr used for tree search comparisons -#define KPL_STORAGE_HEADER(STRUCT_NAME) \ - KPL_TREE_HEADER(STRUCT_NAME); \ - bool import_ready; \ - kpl_tree object_tree; \ - const char *c_string_ptr - -/* - * Uses const c string keys - * The namespace is c_string_ptr - */ -typedef struct _kpl_storage_namespace { - KPL_STORAGE_HEADER(_kpl_storage_namespace); -} kpl_stoage_namespace; - -/* - * Alias used for string comparison functions - */ -typedef kpl_stoage_namespace kpl_stoage_search; - -/* - * Modules make a copy of absolute file path - * The c_string_ptr is pointed to this file path copy string - * This tree can only be accessed through a mutex - */ -typedef struct _kpl_storage_module { - KPL_STORAGE_HEADER(_kpl_storage_module); - char c_string_array[]; // null term, c_string_ptr points to this -} kpl_storage_module; diff --git a/include/kpl/string.h b/include/kpl/string.h deleted file mode 100644 index 8c88825..0000000 --- a/include/kpl/string.h +++ /dev/null @@ -1,29 +0,0 @@ -/* - * 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 deleted file mode 100644 index ce183da..0000000 --- a/include/kpl/task.h +++ /dev/null @@ -1,39 +0,0 @@ -/* - * 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); -} - -/* - * An error has occurred in the task - */ -void kpl_object_task_error(kpl_thread *thread, kpl_object_error *object_error); diff --git a/include/kpl/tree.h b/include/kpl/tree.h deleted file mode 100644 index f3503d2..0000000 --- a/include/kpl/tree.h +++ /dev/null @@ -1,61 +0,0 @@ -/* - * KPL tree - */ - -#pragma once - -// tree_duplicates is a linked stack -#define KPL_TREE_HEADER(STRUCT_NAME) \ - struct STRUCT_NAME *tree_left, *tree_right, *tree_duplicates; \ - int32_t tree_height - -/* - * Recursive AVL saves 8 bytes per object (no need for parent pointer) and keeps the insertion and finding simple - */ -typedef struct _kpl_tree_node { - KPL_TREE_HEADER(_kpl_tree_node); -} kpl_tree_node; - -typedef enum : int32_t { - KPL_TREE_TREE_HEIGHT_INIT = 1 -} kpl_tree_enum; - -/* - * -1 search left, 1 search right, 0 found - */ -typedef int32_t kpl_tree_node_compare_fn(const void *node_a, const void *node_b); - -typedef void kpl_tree_node_free_fn(void *node); - -/* - * KPL needs trees with insertion and find operations, deletion is not needed, all nodes are freed on tree free - */ -typedef struct { - kpl_tree_node *root_node; - kpl_tree_node_compare_fn *node_compare_fn; - kpl_tree_node_free_fn *node_free_fn; -} kpl_tree; - -#define KPL_TREE_INIT_STATIC(COMPARE_FN, FREE_FN) \ - (kpl_tree) { .root_node = NULL, .node_compare_fn = COMPARE_FN, .node_free_fn = FREE_FN } - -inline void kpl_tree_init(kpl_tree *tree, kpl_tree_node_compare_fn node_compare_fn, - kpl_tree_node_free_fn node_free_fn) { - tree->root_node = NULL; - tree->node_compare_fn = node_compare_fn; - tree->node_free_fn = node_free_fn; -} - -void kpl_tree_free(kpl_tree *tree); - -/* - * *find is stack allocated - * NULL for not found - */ -void *kpl_tree_find(kpl_tree *tree, const void *find_node); - -/* - * *insert is heap allocated - * All inserts succeed, duplicates are added to *tree_duplicates - */ -void kpl_tree_insert(kpl_tree *tree, void *insert_node); diff --git a/include/kpl/type.h b/include/kpl/type.h deleted file mode 100644 index 91c3bd4..0000000 --- a/include/kpl/type.h +++ /dev/null @@ -1,179 +0,0 @@ -/* - * KPL Types - * A Type is a declaration of code and data - * Types can be shared across threads - */ - -#pragma once - -typedef enum : uint8_t { - KPL_TYPE_NAME_INVALID, - // VALUES - KPL_TYPE_NAME_BOOLEAN, - KPL_TYPE_NAME_INTEGER, - KPL_TYPE_NAME_U8, - KPL_TYPE_NAME_U16, - KPL_TYPE_NAME_U32, - KPL_TYPE_NAME_U64, - KPL_TYPE_NAME_I8, - KPL_TYPE_NAME_I16, - KPL_TYPE_NAME_I32, - KPL_TYPE_NAME_I64, - KPL_TYPE_NAME_FLOAT, - KPL_TYPE_NAME_F32, - KPL_TYPE_NAME_F64, - KPL_TYPE_NAME_CHAR, - // CONTAINERS - KPL_TYPE_NAME_STRING, - KPL_TYPE_NAME_ARRAY, - KPL_TYPE_NAME_TUPLE, - KPL_TYPE_NAME_TABLE, - KPL_TYPE_NAME_MAP, - KPL_TYPE_NAME_SET, - KPL_TYPE_NAME_UNION, - KPL_TYPE_NAME_ANY, - KPL_TYPE_NAME_NATIVE, - KPL_TYPE_NAME_ERROR, - // COROUTINES - KPL_TYPE_NAME_LAMBDA, - KPL_TYPE_NAME_GENERATOR, - KPL_TYPE_NAME_REGEXP, - // STORAGE - KPL_TYPE_NAME_MODULE, - KPL_TYPE_NAME_NAMESPACE, - // DEFINITIONS - KPL_TYPE_NAME_ALIAS, - KPL_TYPE_NAME_UNIQUE, - // ENUM COUNT - KPL_TYPE_NAME_ENUM_COUNT -} kpl_type_name; - -const char *kpl_type_name_c_string(kpl_type_name type_name); - -/* - * The c struct for this type - */ -typedef enum : uint8_t { - KPL_TYPE_KIND_INVALID, - KPL_TYPE_KIND_BASE, // has no additional type information - KPL_TYPE_KIND_NATIVE, // keeps interface pointer - KPL_TYPE_KIND_ARRAY, // type wraps an unknown number of types - KPL_TYPE_KIND_TREE, // tree of type/type or identifier/type - KPL_TYPE_KIND_RESULT, // union of array and tree for tags - KPL_TYPE_KIND_COROUTINE // coroutine type, tree + array -} kpl_type_kind; - -kpl_type_kind kpl_type_name_get_kind(kpl_type_name type_name); - -typedef enum : uint16_t { - KPL_TYPE_FLAG_EMPTY = 0, - KPL_TYPE_FLAG_NULLABLE = 1 << 0, - KPL_TYPE_FLAG_TREE_TYPE = 1 << 1, - KPL_TYPE_FLAG_TREE_IDENTIFIER = 1 << 2 -} kpl_type_flags; - -// type_kind is for struct polymorphism -// types are in the gc cannot use object_flags for meta -#define KPL_TYPE_HEADER(STRUCT_NAME) \ - KPL_OBJECT_HEADER(STRUCT_NAME); \ - kpl_type_name type_name; \ - kpl_type_kind type_kind; \ - uint16_t type_flags - -/* - * Type has no additional information - */ -typedef struct _kpl_type_base { - KPL_TYPE_HEADER(_kpl_type_base); -} kpl_type_base; - -/* - * Keeps interface pointer for its type comparison - */ -typedef struct _kpl_type_native { - KPL_TYPE_HEADER(_kpl_type_native); - kpl_interface *interface; -} kpl_type_native; - -/* - * Type wraps an unknown number of types - * Array does not grow or shrink - * Minimum array size is 1 - */ -typedef struct _kpl_type_array { - KPL_TYPE_HEADER(_kpl_type_array); - uint16_t array_size; - kpl_type_base *array[]; -} kpl_type_array; - -/* - * Tree based on matching types - */ -typedef struct _kpl_type_tree_item_type { - KPL_TREE_HEADER(_kpl_type_tree_item_type); - uint16_t tag; // unique id - kpl_type_base *type; -} kpl_type_tree_item_type; - -/* - * Tree based on matching identifiers - */ -typedef struct _kpl_type_tree_item_identifier { - KPL_TREE_HEADER(_kpl_type_tree_item_identifier); - uint16_t tag; // unique id for the item - kpl_type_base *type; - kpl_identifier identifier; -} kpl_type_tree_item_identifier; - -/* - * Tree of types - * Tag counter provides a unique tag to each new inserted item - */ -typedef struct _kpl_type_tree { - KPL_TYPE_HEADER(_kpl_type_tree); - uint16_t tag_counter; - kpl_tree tag_tree; -} kpl_type_tree; - -/* - * Reserved ids for result types - * For result_types tag_counter has to be initialized to 3 - * New tags cannot use these ids - */ -typedef enum : uint8_t { - KPL_TYPE_RESULT_ID_DEFAULT = 0, // .default can be any type - KPL_TYPE_RESULT_ID_ERROR = 1, // .error must be error type - KPL_TYPE_RESULT_ID_DONE = 2 // .done must be void -} kpL_type_result_ids; - -/* - * Most of the time we can use default_error and map all .default and .error tags to this array - * .done does not need a type it is always void - * If more tags are added move .default, .error (.done if applicable) to the tree and add additional tags to tree - */ -typedef union { - kpl_type_base *default_error[2]; // used if tag counter == 3 - kpl_tree tree; // tag counter > 3 -} kpl_type_result_union; - -/* - * KPL result type - */ -typedef struct _kpl_type_result { - KPL_TYPE_HEADER(_kpl_type_result); - uint16_t tag_counter; - kpl_type_result_union result; -} kpl_type_result; - -/* - * Coroutine Type - * Arguments size is known at creation and does not grow - * Arguments size can be zero - * Can be cased to kpl_type_result when working with results - */ -typedef struct _kpl_type_coroutine { - KPL_TYPE_HEADER(_kpl_type_coroutine); - uint16_t tag_counter, arguments_size; // unique id generator - kpl_type_result_union result; - kpl_type_base *arguments[]; -} kpl_type_coroutine; diff --git a/include/kpl/value.h b/include/kpl/value.h deleted file mode 100644 index d6120ee..0000000 --- a/include/kpl/value.h +++ /dev/null @@ -1,83 +0,0 @@ -/* - * KPL value definitions - */ - -#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 - -typedef union { - _KPL_VALUE8; -} kpl_value8; - -#define _KPL_VALUE16 \ - uint16_t u16; \ - int16_t i16 - -typedef union { - _KPL_VALUE8; - _KPL_VALUE16; -} kpl_value16; - -typedef uint8_t kpl_utf8[4]; - -#define _KPL_VALUE32 \ - kpl_utf8 utf8; \ - uint32_t u32; \ - int32_t i32; \ - float f32 - -typedef union { - _KPL_VALUE8; - _KPL_VALUE16; - _KPL_VALUE32; -} kpl_value32; - -typedef union { - _KPL_VALUE8; - _KPL_VALUE16; - _KPL_VALUE32; - uint64_t u64; - int64_t i64; - double f64; - 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; - -/* - * Must match the type of c main return - * if code != KPL_EXIT_CODE_SUCCESS then return code - */ -typedef enum : int32_t { - 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/include/kpl_test.h b/include/kpl_test.h deleted file mode 100644 index fbad075..0000000 --- a/include/kpl_test.h +++ /dev/null @@ -1,69 +0,0 @@ -/* - * This is the only header included in any c test file - */ - -#pragma once - -#include "kpl.h" - -typedef enum : int32_t { - KPL_TEST_ERROR_LINE_NUMBER_INIT = -1, -} kpl_test_enum; - -typedef struct _kpl_test kpl_test; - -typedef void kpl_test_fn(__attribute__((unused)) kpl_test *_test); - -/* - * The kpl_test object is used to build a static singly linked list and a static tree - * The list is for running all tests and moving failed tests to an error linked list - * The tree is used for running tests by name - */ -typedef struct _kpl_test { - KPL_TREE_HEADER(_kpl_test); - int32_t error_line_number; // -1 line number for no error - struct _kpl_test *list_next; - const char *source_file, *name, *error_message; // static c strings only - kpl_test_fn *test_fn; -} kpl_test; // Per test object, lists and trees are globals - -void _kpl_test_add(kpl_test *_test); // do not call outside KPL_TEST macro - -#define _KPL_TEST_FN_NAME(NAME) kpl_test_ ## NAME - -#define _KPL_TEST_FN_DEFINITION(NAME) static void _KPL_TEST_FN_NAME(NAME)(__attribute__((unused)) kpl_test *_test) - -/* - * Each test is a static function added to the test list and tree by a static gnu constructor function - * The NAME becomes the function name _NAME becomes the constructor name, only one name can be used per file - */ -#define KPL_TEST(NAME) _KPL_TEST_FN_DEFINITION(NAME); \ -static void __attribute__((constructor)) kpl_test_constructor_ ## NAME(void) { \ - static kpl_test _test = { \ - .error_line_number = KPL_TEST_ERROR_LINE_NUMBER_INIT, \ - .list_next = NULL, \ - .source_file = __FILE__, .name = #NAME, .error_message = NULL, \ - .test_fn = _KPL_TEST_FN_NAME(NAME) \ - }; \ - _kpl_test_add(&_test); \ -} \ -_KPL_TEST_FN_DEFINITION(NAME) - -/* - * MESSAGE is a static c string, sets the error fields in a test once, aborting the rest of test - * Can only be called within a KPL_TEST - */ -#define KPL_FAIL(MESSAGE) do { \ - _test->error_line_number = __LINE__; \ - _test->error_message = MESSAGE; \ - return; \ -} while (0) - -/* - * CONDITION is statement evaluating to true or false, MESSAGE is a static c string, false invokes KPL_FAIL - * Can only be called within KPL_TEST - */ -#define KPL_ASSERT(CONDITION, MESSAGE) do { \ - if (!(CONDITION)) \ - KPL_FAIL(#CONDITION "; " MESSAGE); \ -} while (0) |
