diff options
51 files changed, 0 insertions, 3764 deletions
diff --git a/Makefile b/Makefile deleted file mode 100644 index 42d9982..0000000 --- a/Makefile +++ /dev/null @@ -1,53 +0,0 @@ - -.PHONY: kpl_valgrind kpl_test_valgrind kpl_test_memory kpl_test_thread clean rsync_list - -MAKEFLAGS := -j$(shell nproc) - -CC := gcc -OO := -g -Og -FFLAGS := -fno-omit-frame-pointer -fhardened -WFLAGS := -Wall -Wextra -Wstack-protector -CFLAGS = -std=gnu99 $(FLAGS) -I./include $(OO) $(FFLAGS) $(WFLAGS) -CCOBJ = $(CC) $(CFLAGS) -o $@ $^ - -SOURCE_OBJECTS := $(patsubst %.c, %.o, $(wildcard ./source/*.c)) -TEST_OBJECTS := $(patsubst %.c, %.o, $(wildcard ./test/*.c)) - -ifdef KPL_COLOR_OFF -FLAGS += -DKPL_COLOR_OFF -endif - -kpl: OO := -O2 -kpl: WFLAGS += -Werror -kpl: FLAGS += -DKPL_MAIN -kpl: $(SOURCE_OBJECTS) - $(CCOBJ) - -kpl_valgrind: FLAGS += -DKPL_VALGRIND -kpl_valgrind: kpl - -kpl_test: $(SOURCE_OBJECTS) $(TEST_OBJECTS) - $(CCOBJ) - -kpl_test_valgrind: FLAGS += -DKPL_VALGRIND -kpl_test_valgrind: kpl_test - -kpl_test_memory: FFLAGS += -fsanitize=address,leak,undefined -kpl_test_memory: kpl_test - -kpl_test_thread: FFLAGS += -fsanitize=thread -kpl_test_thread: kpl_test - -clean: - @rm -fv kpl - @rm -fv kpl_test - @find -type f -name "*.o" -exec rm -v {} \; - -rsync_list: - @echo "include/" - @echo "include/*/" - @echo "source/" - @echo "test/" - @echo "*.c" - @echo "*.h" - @echo "Makefile" @@ -1,51 +1 @@ KPL - ---- - -Requirements: - -Linux 6+ -GCC 14+ -make - ---- - -Building: - -make - ---- - -Running: - -./kpl [OPTIONS] ./path/to/file.kpl # Run File -./kpl [OPTIONS] # Start REPL -./kpl -h # See OPTIONS - ---- - -Testing: - -./kpl_test # Run all tests - -./kpl_test NAME1... # Run test by test name - ---- - -Cross Architecture Testing: - -See TESTING - ---- - -Valgrind - -make kpl_valgrind # Build KPL with valgrind support for KPL's custom allocator -make kpl_test_valgrind # Build tests with valgrind support for KPL's custom allocator - ---- - -Sanitizers - -make kpl_test_memory # Build tests with memory,leak and undefined sanitizers -make kpl_test_thread # Build tests with thread sanitizer diff --git a/TESTING b/TESTING deleted file mode 100644 index 987c5ba..0000000 --- a/TESTING +++ /dev/null @@ -1,81 +0,0 @@ -Testing KPL With QEMU - ---- - -Requirements: - -qemu-system-x86-64 -qemu-system-aarch64 -ed2k -wget -ssh -rsync -locate - ---- - -Note: - -X64 = X86-64 -A64 = Aarch64 - ---- - -Setup: - -./vm.sh -init # Create the enviroment -./vm.sh -cli X64 # Set up root password for X64 machine -./vm.sh -setup # Follow instructions -./vm.sh -cli A64 # Set up root password for A64 machine -./vm.sh -setup # Follow instructions - ---- - -Starting: - -./vm.sh -start # Start both X64 and A64 machines -./vm.sh -start X64 # Start X64 -./vm.sh -start A64 # Start A64 - ---- - -Syncing: - -./vm.sh -sync # Sync the codebase on both X64 and A64 machines -./vm.sh -sync X64 # Sync on X64 -./vm.sh -sync A64 # Sync on A64 - ---- - -Testing: - -./vm.sh -test # Sync and test the codebase on both X64 and A64 machines -./vm.sh -test X64 # Sync and test the codebase on X64 -./vm.sh -test A64 # Sync and test the codebase on A64 ---- - -Accessing: - -./vm.sh -ssh X64 # SSH into X86-64 -./vm.sh -ssh A64 # SSH into Aarch64 - ---- - -Stopping: - -./vm.sh -stop # Power off A64 and X64 machines -./vm.sh -kill # Force stop QEMU - ---- - -Cleaning: - -./vm.sh -clean # Remove test files on each vm machine -./vm.sh -clean X64 # Remove test files on X64 -./vm.sh -clean A64 # Remove test files on A64 - -Destroying: - -./vm.sh -destroy # Remove the qemu testing directory - ---- 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) diff --git a/source/README b/source/README deleted file mode 100644 index e277eab..0000000 --- a/source/README +++ /dev/null @@ -1,9 +0,0 @@ -Source Files - ---- - -Compiler Flags - -KPL_MAIN # Add the c main function for creating the standalone KPL executable - ---- diff --git a/source/array.c b/source/array.c deleted file mode 100644 index 4366ec5..0000000 --- a/source/array.c +++ /dev/null @@ -1,21 +0,0 @@ - -#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/ast.c b/source/ast.c deleted file mode 100644 index 491724a..0000000 --- a/source/ast.c +++ /dev/null @@ -1,6 +0,0 @@ - -#include "kpl.h" - -extern inline kpl_ast_meta kpl_ast_meta_get(void *ast); - -extern inline void kpl_ast_meta_set(void *ast, kpl_ast_meta ast_meta); diff --git a/source/config.c b/source/config.c deleted file mode 100644 index a3f895d..0000000 --- a/source/config.c +++ /dev/null @@ -1,167 +0,0 @@ - -#include "kpl.h" - -typedef kpl_exit_code kpl_config_option_parse_fn(kpl_env *env, int32_t arg_index, char *argv[]); - -/* - * Per configuration option actions - * Used to print help messages and parse arg strings and set env values - */ -typedef struct { - int32_t flags; // flags to set on env->flags - char match; // must be set, used in printing help message - char *help_option_argument, *help_message; // help_option_argument, string type if the option takes an arg - kpl_config_option_parse_fn *parse_fn; -} kpl_config_option; - -#define KPL_CONFIG_MATCH(CHAR) (CHAR - 'a') - -static kpl_exit_code kpl_config_option_parse_eval(kpl_env *env, int32_t arg_index, char *argv[]) { - env->kpl_c_string_entry = argv[arg_index]; - env->flags |= KPL_ENV_FLAG_RUN_EVAL; - return KPL_EXIT_CODE_SUCCESS; -} - -static const kpl_config_option config_options[]; - -static kpl_exit_code kpl_config_option_parse_help(kpl_env *env, int32_t arg_index, char *argv[]) { - (void) env; - (void) arg_index; - printf(KPL_COLOR(LIGHT_GREEN) "Kevin's Programming Language" KPL_COLOR(RESET) " (" KPL_COLOR(LIGHT_CYAN) - "KPL" KPL_COLOR(RESET) ") Version: " KPL_COLOR(LIGHT_MAGENTA) KPL_VERSION KPL_COLOR(RESET) - " Website: " KPL_COLOR(LIGHT_BLUE) KPL_WEBSITE KPL_COLOR(RESET) "\n"); - printf("Usage:" KPL_COLOR(BOLD) " %s " KPL_COLOR(RESET) KPL_COLOR(LIGHT_YELLOW) "OPTIONS" KPL_COLOR(RESET) - KPL_COLOR(BOLD) " [-r|-e STRING|FILE" KPL_FILE_EXTENSION KPL_COLOR(RESET) "]\n", argv[0]); - for (int32_t option_index = KPL_CONFIG_MATCH('a'); option_index < KPL_CONFIG_MATCH('z'); option_index++) { - const kpl_config_option *option = &config_options[option_index]; - if (!option->match) - continue; - printf("\t" KPL_COLOR(LIGHT_YELLOW) "-%c" KPL_COLOR(RESET), option->match); - if (option->help_option_argument) - printf(KPL_COLOR(GREEN) " [%s]" KPL_COLOR(RESET), option->help_option_argument); - printf(", %s\n", option->help_message); - } - return KPL_EXIT_CODE_HELP; -} - -static kpl_exit_code kpl_config_option_parse_io_queue(kpl_env *env, int32_t arg_index, char *argv[]) { - int32_t io_uring_queue_size_pow2 = atoi(argv[arg_index]); - if (io_uring_queue_size_pow2 < KPL_IO_URING_QUEUE_SIZE_POW2_MIN || - io_uring_queue_size_pow2 > KPL_IO_URING_QUEUE_SIZE_POW2_MAX) { - printf(KPL_COLOR2(BOLD, LIGHT_RED) "Invalid power of two for io_uring, must be between %d and %d\n" - KPL_COLOR(RESET), KPL_IO_URING_QUEUE_SIZE_POW2_MIN, KPL_IO_URING_QUEUE_SIZE_POW2_MAX); - return KPL_EXIT_CODE_ERROR; - } - env->io_uring_queue_size_pow2 = io_uring_queue_size_pow2; - return KPL_EXIT_CODE_SUCCESS; -} - -static kpl_exit_code kpl_config_option_parse_repl(kpl_env *env, int32_t arg_index, char *argv[]) { - (void) arg_index; - (void) argv; - env->flags |= KPL_ENV_FLAG_RUN_REPL; - return KPL_EXIT_CODE_SUCCESS; -} - -static kpl_exit_code kpl_config_option_parse_thread_count(kpl_env *env, int32_t arg_index, char *argv[]) { - int32_t thread_count = atoi(argv[arg_index]); - if (thread_count < KPL_THEAD_THREAD_COUNT_MIN || thread_count > KPL_THEAD_THREAD_COUNT_MAX) { - printf(KPL_COLOR2(BOLD, LIGHT_RED) "Invalid thread count, must be between %d and %d\n" KPL_COLOR(RESET), - KPL_THEAD_THREAD_COUNT_MIN, KPL_THEAD_THREAD_COUNT_MAX); - return KPL_EXIT_CODE_ERROR; - } - env->thread_count = thread_count; - return KPL_EXIT_CODE_SUCCESS; -} - -static kpl_exit_code kpl_config_option_parse_version(kpl_env *env, int32_t arg_index, char *argv[]) { - (void) env; - (void) arg_index; - (void) argv; - printf("kpl version " KPL_VERSION "\n"); - return KPL_EXIT_CODE_HELP; -} - -static const kpl_config_option config_options[] = { - [KPL_CONFIG_MATCH('a')] = {}, - [KPL_CONFIG_MATCH('e')] = { - .match = 'e', - .help_option_argument = "STRING", - .help_message = "Evaluate KPL String", - .parse_fn = kpl_config_option_parse_eval - }, - [KPL_CONFIG_MATCH('h')] = { - .match = 'h', - .help_message = "Show Help", - .parse_fn = kpl_config_option_parse_help - }, - [KPL_CONFIG_MATCH('i')] = { - .match = 'i', - .help_option_argument = "NUMBER", - .help_message = "Power Of Two For io_uring Queue Size", - .parse_fn = kpl_config_option_parse_io_queue - }, - [KPL_CONFIG_MATCH('r')] = { - .match = 'r', - .help_message = "Start KPL REPL", - .parse_fn = kpl_config_option_parse_repl - }, - [KPL_CONFIG_MATCH('t')] = { - .match = 't', - .help_option_argument = "NUMBER", - .help_message = "Number Of Threads To Use", - .parse_fn = kpl_config_option_parse_thread_count - }, - [KPL_CONFIG_MATCH('v')] = { - .match = 'v', - .help_message = "Show Version", - .parse_fn = kpl_config_option_parse_version - }, - [KPL_CONFIG_MATCH('z')] = {} -}; - -static void kpl_env_set_defaults(kpl_env *env) { - env->io_uring_queue_size_pow2 = KPL_IO_URING_QUEUE_SIZE_POW2_DEFAULT; - env->thread_count = KPL_THREAD_DETERIMINE_COUNT; -} - -kpl_exit_code kpl_env_config(kpl_env *env, int32_t argc, char *argv[]) { - kpl_env_set_defaults(env); - if (argc == KPL_ENV_CONFIG_NO_ARGS) - return KPL_EXIT_CODE_SUCCESS; - int32_t arg_index = 1, exit_code = KPL_EXIT_CODE_SUCCESS; - while (arg_index < argc) { - if (argv[arg_index][0] != '-') { - env->kpl_c_string_entry = argv[arg_index]; - return KPL_EXIT_CODE_SUCCESS; - } - if (argv[arg_index][1] && argv[arg_index][2]) { - printf(KPL_COLOR2(BOLD, LIGHT_RED) "Invalid option %s\n" KPL_COLOR(RESET), argv[arg_index]); - return KPL_EXIT_CODE_ERROR; - } - if (argv[arg_index][1] == '-') { - arg_index++; - if (arg_index >= argc) { - printf(KPL_COLOR2(BOLD, LIGHT_RED) "Invalid, no arguments after --\n" KPL_COLOR(RESET)); - return KPL_EXIT_CODE_ERROR; - } - env->kpl_c_string_entry = argv[arg_index]; - return KPL_EXIT_CODE_SUCCESS; - } - const kpl_config_option *option = &config_options[KPL_CONFIG_MATCH(argv[arg_index][1])]; - if (option->help_option_argument) { - arg_index++; - if (arg_index >= argc) { - printf(KPL_COLOR2(BOLD, LIGHT_RED) "Invalid, option -%c requires argument\n" KPL_COLOR(RESET), - option->match); - return KPL_EXIT_CODE_ERROR; - } - } - if ((exit_code = option->parse_fn(env, arg_index, argv)) != KPL_EXIT_CODE_SUCCESS) - return exit_code; - if (env->flags & (KPL_ENV_FLAG_RUN_EVAL | KPL_ENV_FLAG_RUN_REPL)) - return KPL_EXIT_CODE_SUCCESS; - arg_index++; - } - return kpl_config_option_parse_help(env, 0, argv); -} diff --git a/source/env.c b/source/env.c deleted file mode 100644 index 22b87b1..0000000 --- a/source/env.c +++ /dev/null @@ -1,184 +0,0 @@ - -#include "kpl.h" - -static uint32_t kpl_env_run_mode(kpl_env *env) { - return env->flags & (KPL_ENV_FLAG_RUN_MODULE | KPL_ENV_FLAG_RUN_EVAL | - KPL_ENV_FLAG_RUN_REPL | KPL_ENV_FLAG_RUN_FUNCTION); -} - -kpl_exit_code kpl_env_init(kpl_env *env) { - if (!env->thread_count || env->thread_count < -1) - return KPL_EXIT_CODE_ERROR; - if (env->io_uring_queue_size_pow2 < KPL_IO_URING_QUEUE_SIZE_POW2_MIN || - env->io_uring_queue_size_pow2 > KPL_IO_URING_QUEUE_SIZE_POW2_MAX) - return KPL_EXIT_CODE_ERROR; - uint32_t run_mode = kpl_env_run_mode(env); - if (run_mode != KPL_ENV_FLAG_RUN_MODULE && run_mode != KPL_ENV_FLAG_RUN_EVAL && - run_mode != KPL_ENV_FLAG_RUN_REPL && run_mode != KPL_ENV_FLAG_RUN_FUNCTION) - return KPL_EXIT_CODE_ERROR; - if (env->thread_count == -1) { - cpu_set_t cpus; - CPU_ZERO(&cpus); - if (sched_getaffinity(0, sizeof(cpu_set_t), &cpus) != 0) - return KPL_EXIT_CODE_ERROR; - env->thread_count = CPU_COUNT(&cpus); - } - __atomic_store_n(&env->total_priority, env->thread_count, __ATOMIC_RELAXED); - env->threads = calloc(env->thread_count, sizeof(kpl_thread)); - for (uint16_t thread_id = 0; thread_id < env->thread_count; thread_id++) { - env->threads[thread_id].thread_id = thread_id; - env->threads[thread_id].env = env; - } - // TODO NAMESPACE AND MODULE TREES - // TODO MODULE TREE MUTEX - return KPL_EXIT_CODE_SUCCESS; -} - -extern inline void kpl_env_priority_increment(kpl_thread *thread); - -extern inline void kpl_env_priority_add(kpl_thread *thread, ssize_t amount); - -extern inline void kpl_env_priority_decrement(kpl_thread *thread); - -static void kpl_env_thread_queue_task(kpl_thread *thread, kpl_object_task *task) { - kpl_object_queue_enqueue(&thread->task_queue, task); - kpl_env_priority_increment(thread); - sem_post(&thread->sem); -} - -void kpl_env_queue_task(kpl_env *env, kpl_object_task *task, kpl_env_queue_mode queue_mode) { - if (queue_mode == KPL_ENV_QUEUE_MODE_SYNC) - return kpl_env_thread_queue_task(&env->threads[task->thread_id], task); - uint16_t thread_id = 0; - ssize_t priority = __atomic_load_n(&env->threads[thread_id].priority, __ATOMIC_ACQUIRE); - for (int16_t next_thread_id = 1; next_thread_id < env->thread_count; next_thread_id++) { - ssize_t next_priority = __atomic_load_n(&env->threads[next_thread_id].priority, __ATOMIC_ACQUIRE); - if (next_priority < priority) { - priority = next_priority; - thread_id = next_thread_id; - } - } - kpl_env_thread_queue_task(&env->threads[thread_id], task); -} - -static void kpl_env_thread_init_eval(kpl_thread *thread) { - kpl_object_string *string = kpl_object_string_from_c_string(thread, thread->env->kpl_c_string_entry); - (void) string; - // TODO -} - -static void kpl_env_thread_init_function(kpl_thread *thread) { - kpl_object_task *object_task = kpl_object_task_create(thread, 0, 0); - object_task->task_fn = thread->env->kpl_fn_entry; - kpl_env_queue_task(thread->env, object_task, KPL_ENV_QUEUE_MODE_SYNC); - -} - -static void kpl_env_thread_init(kpl_thread *thread) { - kpl_memory_allocator_init(&thread->memory_allocator); - kpl_gc_init(&thread->gc); - kpl_io_uring_init(&thread->io_uring, thread->env->io_uring_queue_size_pow2); - __atomic_store_n(&thread->priority, 0, __ATOMIC_RELAXED); - kpl_object_queue_init(&thread->task_queue); - kpl_object_queue_init(&thread->error_queue); - sem_init(&thread->sem, 0, 0); - if (thread->thread_id != KPL_THREAD_MAIN_ID) - return; - switch (kpl_env_run_mode(thread->env)) { - case KPL_ENV_FLAG_RUN_MODULE: - // TODO - break; - case KPL_ENV_FLAG_RUN_EVAL: - kpl_env_thread_init_eval(thread); - break; - case KPL_ENV_FLAG_RUN_REPL: - // TODO - break; - case KPL_ENV_FLAG_RUN_FUNCTION: - kpl_env_thread_init_function(thread); - break; - } -} - -static kpl_object_task *kpl_thread_dequeue_task(kpl_thread *thread) { - kpl_object_task *object_task = kpl_object_queue_dequeue(&thread->task_queue); - if (object_task) - kpl_env_priority_decrement(thread); - return object_task; -} - -static void kpl_env_thread_free(kpl_thread *thread) { - kpl_object_task *tmp_task; - while ((tmp_task = kpl_io_uring_get_completed(thread))) - continue; - kpl_io_uring_free(&thread->io_uring); - kpl_gc_free(thread); - kpl_memory_allocator_free(&thread->memory_allocator); - sem_destroy(&thread->sem); -} - -#define KPL_ENV_FLAGS_STOP_THREAD_MASK (KPL_ENV_FLAG_STOP | KPL_ENV_FLAG_HAS_ERROR) - -static void kpl_env_wake_all_threads(kpl_env *env) { - for (int32_t thread_id = 0; thread_id < env->thread_count; thread_id++) - sem_post(&env->threads[thread_id].sem); -} - -void *kpl_env_run_loop(void *arg) { - kpl_thread *thread = arg; - cpu_set_t cpus; - CPU_ZERO(&cpus); - CPU_SET(thread->thread_id, &cpus); - if (pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpus)) - exit(KPL_EXIT_CODE_THREAD_ERROR); - kpl_env_thread_init(thread); - for (;;) { - if (thread->env->flags & KPL_ENV_FLAGS_STOP_THREAD_MASK) { - kpl_env_wake_all_threads(thread->env); - break; - } - // TODO RUN GC - kpl_object_task *object_task = kpl_thread_dequeue_task(thread); - if (!object_task) - object_task = kpl_io_uring_get_completed(thread); - if (object_task) { - object_task->task_fn(thread, object_task); - kpl_object_task_destroy(thread, object_task); - continue; - } - kpl_env_priority_decrement(thread); - if (!thread->env->total_priority) { - kpl_env_wake_all_threads(thread->env); - break; - } - sem_wait(&thread->sem); - if (!thread->env->total_priority || thread->env->flags & KPL_ENV_FLAGS_STOP_THREAD_MASK) - break; - kpl_env_priority_increment(thread); - } - // TODO REPORT ERRORS - kpl_env_thread_free(thread); - return NULL; -} - -kpl_exit_code kpl_env_run(kpl_env *env) { - for (int16_t thread_id = 1; thread_id < env->thread_count; thread_id++) - pthread_create(&env->threads[thread_id].pthread, NULL, kpl_env_run_loop, &env->threads[thread_id]); - kpl_env_run_loop(&env->threads[KPL_THREAD_MAIN_ID]); - for (int16_t thread_id = 1; thread_id < env->thread_count; thread_id++) - pthread_join(env->threads[thread_id].pthread, NULL); - return env->flags & KPL_ENV_FLAG_HAS_ERROR ? KPL_EXIT_CODE_ERROR : KPL_EXIT_CODE_SUCCESS; -} - -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) { - free(env->threads); - // TODO NAMESPACE AND MODULE TREES - // TODO MODULE TREE MUTEX -} diff --git a/source/error.c b/source/error.c deleted file mode 100644 index 650bcce..0000000 --- a/source/error.c +++ /dev/null @@ -1,29 +0,0 @@ - -#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, - .copy_fn = NULL -}; - -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 deleted file mode 100644 index 83b8eb1..0000000 --- a/source/gc.c +++ /dev/null @@ -1,104 +0,0 @@ - -#include "kpl.h" - -extern inline void kpl_gc_init(kpl_gc *gc); - -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_gc(&thread->memory_allocator, thread->thread_id); - if (!obj_gc) { - kpl_memory_allocator_return(&thread->memory_allocator, obj); - return NULL; - } - obj_gc->object_flags = KPL_OBJECT_FLAGS_EMPTY; - obj_gc->gc_prev = obj_gc->gc_next = NULL; - __atomic_clear(&obj_gc->gc_mark, __ATOMIC_RELAXED); - obj_gc->class.interface = interface; - obj_gc->class.value.ptr = obj; - obj->object_gc_parent = obj_gc; - obj_gc->gc_next = thread->gc.gc_head; - if (thread->gc.gc_head) - thread->gc.gc_head->gc_prev = obj_gc; - thread->gc.gc_head = obj_gc; - thread->gc.gc_cycle_head_additions++; - return obj; -} - -static void kpl_gc_return(kpl_thread *thread, kpl_object_gc *object_gc) { - kpl_object *obj = object_gc->class.value.ptr; - 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_gc(&thread->memory_allocator, object_gc); -} - -void kpl_gc_delete(kpl_thread *thread, kpl_object *object) { - kpl_object_gc *object_gc = object->object_gc_parent; - if (object_gc == thread->gc.gc_head) { - thread->gc.gc_head = thread->gc.gc_head->gc_next; - if (thread->gc.gc_head) - thread->gc.gc_head->gc_prev = NULL; - } - thread->gc.gc_cycle_head_additions--; - kpl_gc_return(thread, object_gc); -} - -static void kpl_gc_sweep_delete(kpl_thread *thread, kpl_object_gc *object_gc) { - if (object_gc == thread->gc.gc_mark_head) { - thread->gc.gc_mark_head = thread->gc.gc_mark_head->gc_next; - if (thread->gc.gc_mark_head) - thread->gc.gc_mark_head->gc_prev = NULL; - } - kpl_gc_return(thread, object_gc); -} - -extern inline void kpl_gc_mark(kpl_object_gc *object_gc); - -extern inline void kpl_gc_prepare_sweep(kpl_gc *gc); - -#define KPL_OBJECT_GC_FLAGS_NO_SWEEP_MASK (KPL_OBJECT_FLAGS_IO_PENDING | KPL_OBJECT_FLAGS_GC_PERSIST) - -static bool kpl_gc_object_is_alive(kpl_object_gc *object_gc) { - return object_gc->gc_mark || - ((kpl_object*) object_gc->class.value.ptr)->object_flags & KPL_OBJECT_GC_FLAGS_NO_SWEEP_MASK; -} - -void kpl_gc_sweep(kpl_thread *thread) { - if (!thread->gc.gc_mark_head) - return; - kpl_object_gc *mark_head = thread->gc.gc_mark_head, *mark_tail = mark_head; - while (mark_head) { - kpl_object_gc *tmp = mark_head; - mark_head = mark_head->gc_next; - if (kpl_gc_object_is_alive(tmp)) { - __atomic_clear(&tmp->gc_mark, __ATOMIC_RELAXED); - mark_tail = tmp; - } else - kpl_gc_sweep_delete(thread, tmp); - } - if (!thread->gc.gc_mark_head) - return; - mark_tail->gc_next = thread->gc.gc_head; - if (thread->gc.gc_head) - thread->gc.gc_head->gc_prev = mark_tail; - thread->gc.gc_head = thread->gc.gc_mark_head; - thread->gc.gc_mark_head = NULL; -} - -void kpl_gc_free(kpl_thread *thread) { - kpl_object_gc *head = thread->gc.gc_head; - while (head) { - kpl_object_gc *tmp = head; - head = head->gc_next; - kpl_gc_return(thread, tmp); - } -} diff --git a/source/identifier.c b/source/identifier.c deleted file mode 100644 index 176f46f..0000000 --- a/source/identifier.c +++ /dev/null @@ -1,95 +0,0 @@ - -#include "kpl.h" - -#define KPL_IDENTIFIER_NUMBER_RANGE_START 1 - -#define KPL_IDENTIFIER_UPPER_RANGE_START 12 - -#define KPL_IDENTIFIER_LOWER_RANGE_START 38 - -static uint64_t kpl_identifier_char_to_value(char c, uint64_t value_index) { - uint64_t value = 0; - if (c >= '0' && c <= '9') - value = c - '0' + KPL_IDENTIFIER_NUMBER_RANGE_START; - else if (c >= 'A' && c <= 'Z') - value = c - 'A' + KPL_IDENTIFIER_UPPER_RANGE_START; - else if (c >= 'a' && c <= 'z') - value = c - 'a' + KPL_IDENTIFIER_LOWER_RANGE_START; - return value << value_index * KPL_IDENTIFIER_BITS_PER_VALUE; -} - -#define KPL_IDENTIFIER_VALUES_TOTAL_BITS (KPL_IDENTIFIER_BITS_PER_VALUE * KPL_IDENTIFIER_VALUES_PER_PART) - -bool kpl_identifier_encode(kpl_identifier *identifier, const char *c_string, size_t c_string_length) { - if (c_string_length > KPL_IDENTIFIER_MAX_LENGTH) - return false; - for (size_t string_index = 0; string_index < c_string_length; string_index++) - if (!isalnum(c_string[string_index]) && c_string[string_index] != '_') - return false; - for (uint32_t part_index = 0; part_index < KPL_IDENTIFIER_PARTS; part_index++) - identifier->parts[part_index] = 0; - uint32_t string_index = 0, part_index = 0; - while (string_index < c_string_length) { - uint64_t value_index = 0; - while (value_index < KPL_IDENTIFIER_VALUES_PER_PART && string_index < c_string_length) { - identifier->parts[part_index] |= kpl_identifier_char_to_value(c_string[string_index], value_index); - value_index++; - string_index++; - } - identifier->parts[part_index] |= value_index << KPL_IDENTIFIER_VALUES_TOTAL_BITS; - part_index++; - } - return true; -} - -static char kpl_identifier_value_to_char(uint64_t part) { - uint8_t value = part & (1 << 5 | 1 << 4 | 1 << 3 | 1 << 2 | 1 << 1 | 1 << 0); - if (!value) - return '_'; - if (value < KPL_IDENTIFIER_UPPER_RANGE_START) - return value - KPL_IDENTIFIER_NUMBER_RANGE_START + '0'; - if (value < KPL_IDENTIFIER_LOWER_RANGE_START) - return value - KPL_IDENTIFIER_UPPER_RANGE_START + 'A'; - return value - KPL_IDENTIFIER_LOWER_RANGE_START + 'a'; -} - -void kpl_identifier_decode(kpl_identifier *identifier, char c_string[KPL_IDENTIFIER_C_STRING_SIZE]) { - uint32_t string_index = 0; - for (uint32_t part_index = 0; part_index < KPL_IDENTIFIER_PARTS; part_index++) { - uint64_t part = identifier->parts[part_index]; - uint64_t value_length = part >> KPL_IDENTIFIER_VALUES_TOTAL_BITS; - if (!value_length) - break; - while (value_length) { - c_string[string_index] = kpl_identifier_value_to_char(part); - part >>= KPL_IDENTIFIER_BITS_PER_VALUE; - value_length--; - string_index++; - } - } -} - -#define KPL_IDENTIFIER_PART_LENGTH_MASK ~(1UL << 63 | 1UL << 62 | 1UL << 61 | 1UL << 60) - -int32_t kpl_identifier_compare(const kpl_identifier *identifier_left, const kpl_identifier *identifier_right) { - for (uint32_t part_index = 0; part_index < KPL_IDENTIFIER_PARTS; part_index++) { - uint64_t left_part = identifier_left->parts[part_index], right_part = identifier_right->parts[part_index]; - if (left_part == right_part) - continue; - if (!left_part && !right_part) - break; - left_part &= KPL_IDENTIFIER_PART_LENGTH_MASK; - right_part &= KPL_IDENTIFIER_PART_LENGTH_MASK; - while (left_part && right_part) { - char left = kpl_identifier_value_to_char(left_part), right = kpl_identifier_value_to_char(right_part); - int32_t result = left - right; - if (result) - return result; - left_part >>= KPL_IDENTIFIER_BITS_PER_VALUE; - right_part >>= KPL_IDENTIFIER_BITS_PER_VALUE; - } - } - return 0; -} - -extern inline bool kpl_identifier_equal(const kpl_identifier *identifier_a, const kpl_identifier *identifier_b); diff --git a/source/io.c b/source/io.c deleted file mode 100644 index c8f2157..0000000 --- a/source/io.c +++ /dev/null @@ -1,111 +0,0 @@ - -#include "kpl.h" - -static void *kpl_io_uring_mmap(uint32_t size, int32_t fd, off_t offset) { - return mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd, offset); -} - -int32_t kpl_io_uring_init(kpl_io_uring *io_uring, int32_t queue_size_pow2) { - io_uring->params.flags |= IORING_SETUP_NO_SQARRAY; - io_uring->ring_queue_entries = 1 << queue_size_pow2; - io_uring->ring_fd = syscall(__NR_io_uring_setup, io_uring->ring_queue_entries, &io_uring->params); - if (io_uring->ring_fd < 0) - return -errno; - uint32_t ssize = io_uring->params.sq_off.array + io_uring->params.sq_entries * sizeof(uint32_t); - uint32_t csize = io_uring->params.cq_off.cqes + io_uring->params.cq_entries * sizeof(struct io_uring_cqe); - io_uring->sq_cq_size = MAX(ssize, csize); - 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; - io_uring->cring_head = cring_ptr + io_uring->params.cq_off.head; - io_uring->cring_tail = cring_ptr + io_uring->params.cq_off.tail; - io_uring->cring_mask = cring_ptr + io_uring->params.cq_off.ring_mask; - io_uring->cqes = cring_ptr + io_uring->params.cq_off.cqes; - io_uring->sring_ptr = sring_ptr; - io_uring->cring_ptr = cring_ptr; - return 0; -} - -extern inline void kpl_io_uring_free(kpl_io_uring *io_uring); - -static int32_t kpl_io_uring_enter(int32_t ring_fd, uint32_t to_submit, uint32_t to_wait, uint32_t flags) { - int32_t ret = syscall(__NR_io_uring_enter, ring_fd, to_submit, to_wait, flags, NULL, 0); - return ret < 0 ? -errno : ret; -} - -kpl_object_task *kpl_io_uring_get_completed(kpl_thread *thread) { - kpl_io_uring *io_uring = &thread->io_uring; - if (!io_uring->submitted_to_kernel) - return NULL; - uint32_t cring_head = __atomic_load_n(io_uring->cring_head, __ATOMIC_ACQUIRE); - if (cring_head == *io_uring->cring_tail) { - if (kpl_io_uring_enter(io_uring->ring_fd, 0, 1, IORING_ENTER_GETEVENTS) < 0) - exit(KPL_EXIT_CODE_IO_ERROR); - } - struct io_uring_cqe *cqe = &io_uring->cqes[cring_head & *io_uring->cring_mask]; - kpl_object_task *task = (kpl_object_task*) cqe->user_data; - task->io_res = cqe->res; - task->io_flags = cqe->flags; - cring_head++; - __atomic_store_n(io_uring->cring_head, cring_head, __ATOMIC_RELEASE); - io_uring->submitted_to_kernel--; - task->object_flags ^= KPL_OBJECT_FLAGS_IO_PENDING; - kpl_env_priority_decrement(thread); - return task; -} - -int32_t kpl_io_uring_submit(kpl_thread *thread) { - kpl_io_uring *io_uring = &thread->io_uring; - if (!io_uring->added_to_queue) - return -1; - int32_t ret = kpl_io_uring_enter(io_uring->ring_fd, io_uring->added_to_queue, 0, 0); - if (ret > 0) { - io_uring->added_to_queue -= ret; - io_uring->submitted_to_kernel += ret; - kpl_env_priority_add(thread, ret); - } - return ret; -} - -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) { - uint32_t sring_head = __atomic_load_n(io_uring->sring_head, __ATOMIC_ACQUIRE); - uint32_t sring_tail = *io_uring->sring_tail; - if (sring_tail - sring_head >= (uint32_t) io_uring->ring_queue_entries) - return NULL; - struct io_uring_sqe *sqe = &io_uring->sqes[sring_tail & *io_uring->sring_mask]; - sring_tail++; - __atomic_store_n(io_uring->sring_tail, sring_tail, __ATOMIC_RELEASE); - io_uring->added_to_queue++; - memset(sqe, 0, sizeof(struct io_uring_sqe)); - task->object_flags |= KPL_OBJECT_FLAGS_IO_PENDING; - sqe->user_data = (uint64_t) task; - sqe->opcode = op; - sqe->fd = fd; - sqe->off = offset; - sqe->addr = (uint64_t) addr; - sqe->len = len; - return sqe; -} - -extern 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); - -extern inline bool kpl_io_uring_close(kpl_io_uring *io_uring, kpl_object_task *task, int32_t fd); - -extern 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); - -extern 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); - -extern 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); diff --git a/source/kpl.c b/source/kpl.c deleted file mode 100644 index 0c50b2e..0000000 --- a/source/kpl.c +++ /dev/null @@ -1,19 +0,0 @@ -/* - * Core kpl functions and entry point - */ - -#include "kpl.h" - -#ifdef KPL_MAIN -int32_t main(int32_t argc, char *argv[]) { - int32_t exit_code = KPL_EXIT_CODE_SUCCESS; - kpl_env env = {}; - if ((exit_code = kpl_env_config(&env, argc, argv)) != KPL_EXIT_CODE_SUCCESS) - return exit_code; - if ((exit_code = kpl_env_init(&env)) != KPL_EXIT_CODE_SUCCESS) - return exit_code; - exit_code = kpl_env_run(&env); - kpl_env_free(&env); - return exit_code; -} -#endif diff --git a/source/memory.c b/source/memory.c deleted file mode 100644 index 2775f99..0000000 --- a/source/memory.c +++ /dev/null @@ -1,137 +0,0 @@ - -#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_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; - slab_head = slab_head->slab_next; - munmap(slab_tmp->buffer, slab_tmp->buffer_size); - free(slab_tmp); - } -} - -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_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, - [7] = 15, - [8] = 20, - [9] = 25, - [10] = 30, - [11] = 35, - [12] = 40, - [13] = 2, - [14] = 4, - [15] = 8, - [16] = 16, - [17] = 32, - [18] = 64, - [19] = 128, - [20] = 256, - [21] = 512, - [22] = 1024 -}; - -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() * 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; -} - -static void *kpl_memory_allocator_slab_bump(kpl_memory_allocator_slab *slab, int32_t size) { - void *object = slab->buffer + slab->buffer_used; - slab->buffer_used += size; - return object; -} - -static kpl_object *kpl_memory_allocator_slab_allocate(kpl_memory_allocator_slab_group *slab_group, - int32_t size, int32_t page_count, uint16_t thread_id) { - kpl_memory_allocator_slab *slab = slab_group->slab_tail; - if (!slab || slab->buffer_used + size > slab->buffer_size) { - slab = kpl_memory_allocator_slab_init(page_count); - if (!slab_group->slab_head) - slab_group->slab_head = slab_group->slab_tail = slab; - else - slab_group->slab_tail = slab_group->slab_tail->slab_next = slab; - } - kpl_object *object = kpl_memory_allocator_slab_bump(slab, size); -#ifdef KPL_VALGRIND - VALGRIND_MALLOCLIKE_BLOCK(object, size, 0, 0); -#endif - __atomic_store_n(&object->queue_next, NULL, __ATOMIC_RELAXED); - object->object_size = size; - object->thread_id = thread_id; - return object; -} - -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 || 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) { - 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/object.c b/source/object.c deleted file mode 100644 index d569df0..0000000 --- a/source/object.c +++ /dev/null @@ -1,34 +0,0 @@ - -#include "kpl.h" - -extern inline void kpl_object_queue_init(kpl_object_queue *object_queue); - -extern inline void kpl_object_queue_enqueue(kpl_object_queue *object_queue, void *object); - -void *kpl_object_queue_dequeue(kpl_object_queue *object_queue) { - kpl_object *object = NULL; - for (;;) { - kpl_object *tail = object_queue->tail, *queue_next = __atomic_load_n(&tail->queue_next, __ATOMIC_ACQUIRE); - if (tail == (kpl_object*) &object_queue->dummy) { - if (!queue_next) - break; - tail = object_queue->tail = queue_next; - queue_next = __atomic_load_n(&tail->queue_next, __ATOMIC_ACQUIRE); - } - if (!queue_next) { - kpl_object *head = __atomic_load_n(&object_queue->head, __ATOMIC_ACQUIRE); - if (head != tail) - continue; - kpl_object_queue_enqueue(object_queue, (kpl_object*) &object_queue->dummy); - queue_next = __atomic_load_n(&tail->queue_next, __ATOMIC_ACQUIRE); - } - if (queue_next) { - object_queue->tail = queue_next; - object = tail; - break; - } - } - if (object) - __atomic_store_n(&object->queue_next, NULL, __ATOMIC_RELAXED); - return object; -} diff --git a/source/string.c b/source/string.c deleted file mode 100644 index 10c0820..0000000 --- a/source/string.c +++ /dev/null @@ -1,25 +0,0 @@ - -#include "kpl.h" - -static const kpl_interface object_string_interface = { - .gc_mark_object_fn = NULL, - .native_free_fn = NULL, - .log_fn = NULL, - .copy_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 deleted file mode 100644 index 0766f62..0000000 --- a/source/task.c +++ /dev/null @@ -1,52 +0,0 @@ - -#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, - .copy_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); - -extern inline void kpl_object_task_error(kpl_thread *thread, kpl_object_error *object_error); - -void kpl_object_task_error(kpl_thread *thread, kpl_object_error *object_error) { - kpl_object_queue_enqueue(&thread->error_queue, object_error); - thread->env->flags |= KPL_ENV_FLAG_HAS_ERROR; -} diff --git a/source/tree.c b/source/tree.c deleted file mode 100644 index 1fbd980..0000000 --- a/source/tree.c +++ /dev/null @@ -1,105 +0,0 @@ - -#include "kpl.h" - -extern inline void kpl_tree_init(kpl_tree *tree, kpl_tree_node_compare_fn node_compare_fn, - kpl_tree_node_free_fn node_free_fn); - -static void kpl_tree_free_helper(kpl_tree_node *root_node, kpl_tree_node_free_fn node_free_fn) { - if (!root_node) - return; - while (root_node->tree_duplicates) { - kpl_tree_node *tmp_node = root_node->tree_duplicates; - root_node->tree_duplicates = root_node->tree_duplicates->tree_duplicates; - node_free_fn(tmp_node); - } - kpl_tree_free_helper(root_node->tree_left, node_free_fn); - kpl_tree_free_helper(root_node->tree_right, node_free_fn); - node_free_fn(root_node); -} - -void kpl_tree_free(kpl_tree *tree) { - kpl_tree_free_helper(tree->root_node, tree->node_free_fn); -} - -static void *kpl_tree_find_helper(kpl_tree_node *root_node, const void *find_node, - kpl_tree_node_compare_fn node_compare_fn) { - if (!root_node) - return NULL; - int32_t compare = node_compare_fn(root_node, find_node); - if (compare < 0) - return kpl_tree_find_helper(root_node->tree_left, find_node, node_compare_fn); - if (compare > 0) - return kpl_tree_find_helper(root_node->tree_right, find_node, node_compare_fn); - return root_node; -} - -void *kpl_tree_find(kpl_tree *tree, const void *find_node) { - return kpl_tree_find_helper(tree->root_node, find_node, tree->node_compare_fn); -} - -static int32_t kpl_tree_node_height_get(const kpl_tree_node *node) { - return node ? node->tree_height : 0; -} - -static void kpl_tree_node_height_update(kpl_tree_node *node) { - int32_t left_height = kpl_tree_node_height_get(node->tree_left); - int32_t right_height = kpl_tree_node_height_get(node->tree_right); - node->tree_height = MAX(left_height, right_height) + 1; -} - -static kpl_tree_node *kpl_tree_node_rotate_right(kpl_tree_node *right_node) { - kpl_tree_node *right_left = right_node->tree_left, *right_left_right = right_left->tree_right; - right_left->tree_right = right_node; - right_node->tree_left = right_left_right; - kpl_tree_node_height_update(right_node); - kpl_tree_node_height_update(right_left); - return right_left; -} - -static kpl_tree_node *kpl_tree_node_rotate_left(kpl_tree_node *left_node) { - kpl_tree_node *left_right = left_node->tree_right, *left_right_left = left_right->tree_left; - left_right->tree_left = left_node; - left_node->tree_right = left_right_left; - kpl_tree_node_height_update(left_node); - kpl_tree_node_height_update(left_right); - return left_right; -} - -static kpl_tree_node *kpl_tree_insert_helper(kpl_tree_node *restrict root_node, - kpl_tree_node *restrict insert_node, kpl_tree_node_compare_fn node_compare_fn) { - if (!root_node) - return insert_node; - int32_t compare = node_compare_fn(root_node, insert_node); - if (compare < 0) - root_node->tree_left = kpl_tree_insert_helper(root_node->tree_left, insert_node, node_compare_fn); - else if (compare > 0) - root_node->tree_right = kpl_tree_insert_helper(root_node->tree_right, insert_node, node_compare_fn); - else { - insert_node->tree_duplicates = root_node->tree_duplicates; - root_node->tree_duplicates = insert_node; - return root_node; - } - kpl_tree_node_height_update(root_node); - int32_t balance = kpl_tree_node_height_get(root_node->tree_left) - - kpl_tree_node_height_get(root_node->tree_right); - if (balance > 1 && compare < 0) - return kpl_tree_node_rotate_right(root_node); - if (balance < -1 && compare > 0) - return kpl_tree_node_rotate_left(root_node); - if (balance > 1 && compare > 0) { - root_node->tree_left = kpl_tree_node_rotate_left(root_node->tree_left); - return kpl_tree_node_rotate_right(root_node); - } - if (balance < -1 && compare < 0) { - root_node->tree_right = kpl_tree_node_rotate_right(root_node->tree_right); - return kpl_tree_node_rotate_left(root_node); - } - return root_node; -} - -void kpl_tree_insert(kpl_tree *tree, void *insert_node) { - kpl_tree_node *node = insert_node; - node->tree_left = node->tree_right = node->tree_duplicates = NULL; - node->tree_height = KPL_TREE_TREE_HEIGHT_INIT; - tree->root_node = kpl_tree_insert_helper(tree->root_node, insert_node, tree->node_compare_fn); -} diff --git a/source/type.c b/source/type.c deleted file mode 100644 index 684f8fc..0000000 --- a/source/type.c +++ /dev/null @@ -1,84 +0,0 @@ - -#include "kpl.h" - -static const char *type_name_c_string[] = { - "INVALID", - "BOOLEAN", - "INTEGER", - "U8", - "U16", - "U32", - "U64", - "I8", - "I16", - "I32", - "I64", - "FLOAT", - "F32", - "F64", - "CHAR", - "STRING", - "ARRAY", - "TUPLE", - "TABLE", - "MAP", - "SET", - "UNION", - "ANY", - "NATIVE", - "ERROR", - "LAMBDA", - "GENERATOR", - "REGEXP", - "MODULE", - "NAMESPACE", - "ALIAS", - "UNIQUE", - "ENUM_COUNT" -}; - -const char *kpl_type_name_c_string(kpl_type_name type_name) { - if (type_name <= KPL_TYPE_NAME_INVALID || type_name >= KPL_TYPE_NAME_ENUM_COUNT) - return "INVALID TYPE NAME"; - return type_name_c_string[type_name]; -} - -static const kpl_type_kind type_name_kind[] = { - [KPL_TYPE_NAME_INVALID] = KPL_TYPE_KIND_INVALID, - [KPL_TYPE_NAME_BOOLEAN] = KPL_TYPE_KIND_BASE, - [KPL_TYPE_NAME_INTEGER] = KPL_TYPE_KIND_BASE, - [KPL_TYPE_NAME_U8] = KPL_TYPE_KIND_BASE, - [KPL_TYPE_NAME_U16] = KPL_TYPE_KIND_BASE, - [KPL_TYPE_NAME_U32] = KPL_TYPE_KIND_BASE, - [KPL_TYPE_NAME_U64] = KPL_TYPE_KIND_BASE, - [KPL_TYPE_NAME_I8] = KPL_TYPE_KIND_BASE, - [KPL_TYPE_NAME_I16] = KPL_TYPE_KIND_BASE, - [KPL_TYPE_NAME_I32] = KPL_TYPE_KIND_BASE, - [KPL_TYPE_NAME_I64] = KPL_TYPE_KIND_BASE, - [KPL_TYPE_NAME_FLOAT] = KPL_TYPE_KIND_BASE, - [KPL_TYPE_NAME_F32] = KPL_TYPE_KIND_BASE, - [KPL_TYPE_NAME_F64] = KPL_TYPE_KIND_BASE, - [KPL_TYPE_NAME_CHAR] = KPL_TYPE_KIND_BASE, - [KPL_TYPE_NAME_STRING] = KPL_TYPE_KIND_BASE, - [KPL_TYPE_NAME_ARRAY] = KPL_TYPE_KIND_ARRAY, - [KPL_TYPE_NAME_TUPLE] = KPL_TYPE_KIND_ARRAY, - [KPL_TYPE_NAME_TABLE] = KPL_TYPE_KIND_TREE, - [KPL_TYPE_NAME_MAP] = KPL_TYPE_KIND_ARRAY, - [KPL_TYPE_NAME_SET] = KPL_TYPE_KIND_ARRAY, - [KPL_TYPE_NAME_UNION] = KPL_TYPE_KIND_TREE, - [KPL_TYPE_NAME_ANY] = KPL_TYPE_KIND_TREE, - [KPL_TYPE_NAME_NATIVE] = KPL_TYPE_KIND_NATIVE, - [KPL_TYPE_NAME_ERROR] = KPL_TYPE_KIND_BASE, - [KPL_TYPE_NAME_LAMBDA] = KPL_TYPE_KIND_COROUTINE, - [KPL_TYPE_NAME_GENERATOR] = KPL_TYPE_KIND_COROUTINE, - [KPL_TYPE_NAME_REGEXP] = KPL_TYPE_KIND_COROUTINE, - [KPL_TYPE_NAME_MODULE] = KPL_TYPE_KIND_BASE, - [KPL_TYPE_NAME_NAMESPACE] = KPL_TYPE_KIND_BASE, - [KPL_TYPE_NAME_ALIAS] = KPL_TYPE_KIND_ARRAY, - [KPL_TYPE_NAME_UNIQUE] = KPL_TYPE_KIND_ARRAY, - [KPL_TYPE_NAME_ENUM_COUNT] = KPL_TYPE_KIND_INVALID -}; - -kpl_type_kind kpl_type_name_get_kind(kpl_type_name type_name) { - return type_name_kind[type_name]; -} diff --git a/test/README b/test/README deleted file mode 100644 index 5a0e197..0000000 --- a/test/README +++ /dev/null @@ -1,31 +0,0 @@ -Test Files - ---- - -Testing: - -Each test is a function wrapped in a macro - -KPL_TEST(my_test) { - // code here -} - -Each test passes by default - ---- - -Testing Macros: - -The testing framework provides two macros KPL_ASSERT and KPL_FAIL - -Both macros can only be used inside a KPL_TEST function - -Asserting: - -KPL_ASSERT(condition, "Fail Message"); - ---- - -Failing: - -KPL_FAIL("The test failed"); diff --git a/test/config.c b/test/config.c deleted file mode 100644 index eb5d0ca..0000000 --- a/test/config.c +++ /dev/null @@ -1,18 +0,0 @@ - -#include "kpl_test.h" - -KPL_TEST(set_thread_count) { - kpl_env env = {}; - int32_t argc = 4; - char *argv[] = {"./kpl", "-t", "4", "file.kpl"}; - KPL_ASSERT(kpl_env_config(&env, argc, argv) == KPL_EXIT_CODE_SUCCESS, "Should return success"); - KPL_ASSERT(env.thread_count == 4, "Threads should not be -1"); -} - -KPL_TEST(set_io_uring_queue_pow2_size) { - kpl_env env = {}; - int32_t argc = 4; - char *argv[] = {"./kpl", "-i", "12", "file.kpl"}; - KPL_ASSERT(kpl_env_config(&env, argc, argv) == KPL_EXIT_CODE_SUCCESS, "Should return success"); - KPL_ASSERT(env.io_uring_queue_size_pow2 != KPL_IO_URING_QUEUE_SIZE_POW2_DEFAULT, "Should not be default"); -} diff --git a/test/gc.c b/test/gc.c deleted file mode 100644 index 6834ad2..0000000 --- a/test/gc.c +++ /dev/null @@ -1,34 +0,0 @@ - -#include "kpl_test.h" - -#define TEST_OBJECT_COUNT 10 - -KPL_TEST(basic_mark_and_sweep) { - kpl_thread thread = {}; - kpl_memory_allocator_init(&thread.memory_allocator); - kpl_gc_init(&thread.gc); - kpl_object *objs[TEST_OBJECT_COUNT] = {}; - for (int32_t obj_index = 1; obj_index <= TEST_OBJECT_COUNT; obj_index++) { - objs[obj_index - 1] = kpl_gc_new(&thread, NULL, sizeof(kpl_object_error)); - if (obj_index % 2 == 0) - kpl_gc_mark(objs[obj_index - 1]->object_gc_parent); - } - int32_t counter = 0; - kpl_object_gc *gc_head = thread.gc.gc_head; - while (gc_head) { - counter++; - gc_head = gc_head->gc_next; - } - KPL_ASSERT(counter == TEST_OBJECT_COUNT, "should have TEST_OBJECT_COUNT Objects in gc list"); - kpl_gc_prepare_sweep(&thread.gc); - kpl_gc_sweep(&thread); - gc_head = thread.gc.gc_head; - counter = 0; - while (gc_head) { - counter++; - gc_head = gc_head->gc_next; - } - KPL_ASSERT(counter == TEST_OBJECT_COUNT / 2, "should have removed half of the objects"); - kpl_gc_free(&thread); - kpl_memory_allocator_free(&thread.memory_allocator); -} diff --git a/test/identifier.c b/test/identifier.c deleted file mode 100644 index abbcdac..0000000 --- a/test/identifier.c +++ /dev/null @@ -1,24 +0,0 @@ - -#include "kpl_test.h" - -KPL_TEST(c_string_to_identifier) { - const char *string = "aA0_t4_12w0abcdefghijklmnop456789Z"; - kpl_identifier identifier; - KPL_ASSERT(kpl_identifier_encode(&identifier, string, strlen(string)), "should encode"); - char buffer[100] = {}; - const int32_t offset = 10; - kpl_identifier_decode(&identifier, buffer + offset); - KPL_ASSERT(strcmp(string, buffer + offset) == 0, "should decode to same string"); -} - -KPL_TEST(identifier_compare) { - const char *string_a = "abc123456789", *string_b = "abc123456789", *string_c = "abd123456789"; - kpl_identifier identifier_a, identifier_b, identifier_c; - KPL_ASSERT(kpl_identifier_encode(&identifier_a, string_a, strlen(string_a)), "should encode"); - KPL_ASSERT(kpl_identifier_encode(&identifier_b, string_b, strlen(string_b)), "should encode"); - KPL_ASSERT(kpl_identifier_encode(&identifier_c, string_c, strlen(string_c)), "should encode"); - KPL_ASSERT(kpl_identifier_compare(&identifier_a, &identifier_b) == 0, "should be 0"); - int32_t compare_value = strcmp(string_b, string_c); - KPL_ASSERT(kpl_identifier_compare(&identifier_b, &identifier_c) == compare_value, "should equal strcmp"); - KPL_ASSERT(kpl_identifier_equal(&identifier_a, &identifier_b), "should be equal"); -} diff --git a/test/io.c b/test/io.c deleted file mode 100644 index 50077f6..0000000 --- a/test/io.c +++ /dev/null @@ -1,57 +0,0 @@ - -#include "kpl_test.h" - -static const char *linux_version = "/proc/version"; - -KPL_TEST(statx) { - kpl_env env = {}; - kpl_thread thread = { .env = &env }; - kpl_io_uring *io_uring = &thread.io_uring; - KPL_ASSERT(kpl_io_uring_init(&thread.io_uring, KPL_IO_URING_QUEUE_SIZE_POW2_MIN) == 0, "should return 0 for success"); - struct statx statx_object = {}; - kpl_object_task *task = calloc(1, sizeof(kpl_object_task) + sizeof(kpl_class) * 1); - task->io_res = 1; - KPL_ASSERT(kpl_io_uring_statx(io_uring, task, AT_FDCWD, linux_version, 0, STATX_ALL, &statx_object), - "should add statx operation"); - KPL_ASSERT(task->object_flags & KPL_OBJECT_FLAGS_IO_PENDING , "task should have io pending flag"); - KPL_ASSERT(kpl_io_uring_submit(&thread) == 1, "should submit one io operation"); - KPL_ASSERT(task == kpl_io_uring_get_completed(&thread), "should return a task"); - KPL_ASSERT(!(task->object_flags & KPL_OBJECT_FLAGS_IO_PENDING) , "task should not have io pending flag"); - KPL_ASSERT(task->io_res == 0, "io_res should have success stax return"); - KPL_ASSERT(statx_object.stx_nlink > 0, "should have one or more hard link(s)"); - free(task); - kpl_io_uring_free(io_uring); -} - -#define TEST_TO_READ 5 - -static const char *test_linix_compare = "Linux"; - -KPL_TEST(open_read_close) { - kpl_env env = {}; - kpl_thread thread = { .env = &env }; - kpl_io_uring *io_uring = &thread.io_uring; - KPL_ASSERT(kpl_io_uring_init(io_uring, KPL_IO_URING_QUEUE_SIZE_POW2_MIN) == 0, "should return 0 for success"); - char buffer[TEST_TO_READ + 1] = {}; - kpl_object_task *task = calloc(1, sizeof(kpl_object_task) + sizeof(kpl_class) * 1); - task->io_res = 1; - KPL_ASSERT(kpl_io_uring_openat(io_uring, task, AT_FDCWD, linux_version, O_RDONLY, 0), - "should add open operation"); - KPL_ASSERT(kpl_io_uring_submit(&thread) == 1, "should submit one io operation"); - KPL_ASSERT(task == kpl_io_uring_get_completed(&thread), "should return a task"); - KPL_ASSERT(task->io_res > 0, "io_res should have open fd"); - int32_t fd = task->io_res; - task->io_res = 1; - KPL_ASSERT(kpl_io_uring_read(io_uring, task, fd, &buffer, TEST_TO_READ, 0), "should add read operation"); - KPL_ASSERT(kpl_io_uring_submit(&thread) == 1, "should submit one io operation"); - KPL_ASSERT(task == kpl_io_uring_get_completed(&thread), "should return a task"); - KPL_ASSERT(task->io_res == TEST_TO_READ, "io_res should equal the number of bytes read"); - KPL_ASSERT(strcmp(test_linix_compare, buffer) == 0, "should have Linux string in buffer"); - task->io_res = 1; - KPL_ASSERT(kpl_io_uring_close(io_uring, task, fd), "should add close operation"); - KPL_ASSERT(kpl_io_uring_submit(&thread) == 1, "should submit one io operation"); - KPL_ASSERT(task == kpl_io_uring_get_completed(&thread), "should return a task"); - KPL_ASSERT(task->io_res == 0, "io_res should have close success"); - free(task); - kpl_io_uring_free(io_uring); -} diff --git a/test/memory.c b/test/memory.c deleted file mode 100644 index 9308b2b..0000000 --- a/test/memory.c +++ /dev/null @@ -1,33 +0,0 @@ - -#include "kpl_test.h" - -KPL_TEST(allocate_and_pool) { - kpl_memory_allocator allocator = {}; - kpl_memory_allocator_init(&allocator); - for (int32_t times = 0; times < 700; times++) { - kpl_object *objects[10] = {}; - for (int32_t object = 0; object < 10; object++) { - objects[object] = kpl_memory_allocator_allocate(&allocator, 32, KPL_THREAD_MAIN_ID); - KPL_ASSERT(objects[object]->object_size == 32, "Invalid size for 32 byte object"); - } - for (int32_t object = 0; object < 10; object++) - kpl_memory_allocator_return(&allocator, objects[object]); - } - kpl_memory_allocator_slab_group *slab_group32 = &allocator.slabs[5]; - KPL_ASSERT(slab_group32->slab_head == slab_group32->slab_tail, "Only one slab should have been allocated"); - kpl_memory_allocator_free(&allocator); -} - -KPL_TEST(allocate_multiple_slabs) { - kpl_memory_allocator allocator = {}; - kpl_memory_allocator_init(&allocator); - kpl_object *large1 = kpl_memory_allocator_allocate(&allocator, 1 << 13, KPL_THREAD_MAIN_ID); - kpl_object *large2 = kpl_memory_allocator_allocate(&allocator, 1 << 13, KPL_THREAD_MAIN_ID); - KPL_ASSERT(large1->object_size == 1 << 13, "Invalid object size"); - KPL_ASSERT(large1->object_size == large2->object_size, "Objects should have the same size"); - kpl_memory_allocator_slab_group *slab_group = &allocator.slabs[13]; - KPL_ASSERT(slab_group->slab_head != slab_group->slab_tail, "More the one slab should have been allocated"); - kpl_memory_allocator_return(&allocator, large1); - kpl_memory_allocator_return(&allocator, large2); - kpl_memory_allocator_free(&allocator); -} diff --git a/test/string.c b/test/string.c deleted file mode 100644 index 8a6742c..0000000 --- a/test/string.c +++ /dev/null @@ -1,14 +0,0 @@ - -#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); -} diff --git a/test/task.c b/test/task.c deleted file mode 100644 index 8374989..0000000 --- a/test/task.c +++ /dev/null @@ -1,24 +0,0 @@ - -#include "kpl_test.h" - -static int32_t run_task_value = 0; - -#define KPL_RUN_TASK_SET_VALUE 1234567 - -void run_task_set_value(kpl_thread *thread, kpl_object_task *object_task) { - run_task_value = KPL_RUN_TASK_SET_VALUE; - kpl_object_task_done(thread, object_task); -} - -KPL_TEST(run_task) { - kpl_env env = {}; - kpl_env_config(&env, -1, NULL); - env.thread_count = 1; - env.io_uring_queue_size_pow2 = KPL_IO_URING_QUEUE_SIZE_POW2_MIN; - env.flags |= KPL_ENV_FLAG_RUN_FUNCTION; - env.kpl_fn_entry = run_task_set_value; - KPL_ASSERT(kpl_env_init(&env) == KPL_EXIT_CODE_SUCCESS, "env should init"); - KPL_ASSERT(kpl_env_run(&env) == KPL_EXIT_CODE_SUCCESS, "env should run"); - kpl_env_free(&env); - KPL_ASSERT(run_task_value == KPL_RUN_TASK_SET_VALUE, "task should have set value"); -} diff --git a/test/test.c b/test/test.c deleted file mode 100644 index 0dcf302..0000000 --- a/test/test.c +++ /dev/null @@ -1,92 +0,0 @@ - -#include "kpl_test.h" - -static int32_t kpl_test_tree_compare(const void *node_a, const void *node_b) { - const kpl_test *test_a = node_a, *test_b = node_b; - return strcmp(test_a->name, test_b->name); -} - -static kpl_tree test_tree = KPL_TREE_INIT_STATIC(kpl_test_tree_compare, NULL); - -static kpl_test *test_head = NULL, *test_tail = NULL, *error_head = NULL, *error_tail = NULL; - -void _kpl_test_add(kpl_test *_test) { - if (!test_head) - test_head = test_tail = _test; - else - test_tail = test_tail->list_next = _test; - kpl_tree_insert(&test_tree, _test); -} - -static void kpl_test_report(const kpl_test *_test) { - printf(KPL_COLOR(BOLD) "%s" KPL_COLOR(RESET) ":" KPL_COLOR(LIGHT_CYAN) "%s" KPL_COLOR(RESET), - _test->source_file, _test->name); - if (_test->error_line_number == KPL_TEST_ERROR_LINE_NUMBER_INIT) { - printf(KPL_COLOR(LIGHT_GREEN) " PASS\n" KPL_COLOR(RESET)); - return; - } - printf("::" KPL_COLOR(LIGHT_MAGENTA) "%d" KPL_COLOR(RESET) KPL_COLOR(LIGHT_RED) " %s\n" KPL_COLOR(RESET), - _test->error_line_number, _test->error_message); -} - -static kpl_exit_code kpl_test_report_error(void) { - if (!error_head) { - printf(KPL_COLOR2(BOLD, GREEN) "PASSED\n" KPL_COLOR(RESET)); - return KPL_EXIT_CODE_SUCCESS; - } - printf(KPL_COLOR2(BOLD, RED) "FAILED\n" KPL_COLOR(RESET)); - while (error_head) { - kpl_test *_test = error_head; - error_head = error_head->list_next; - kpl_test_report(_test); - } - return KPL_EXIT_CODE_ERROR; -} - -static void kpl_test_run_one(kpl_test *_test) { - _test->list_next = NULL; - _test->test_fn(_test); - kpl_test_report(_test); - if (_test->error_line_number == KPL_TEST_ERROR_LINE_NUMBER_INIT) - return; - if (!error_head) - error_head = error_tail = _test; - else - error_tail = error_tail->list_next = _test; -} - -static kpl_exit_code kpl_test_run_all(void) { - while (test_head) { - kpl_test *_test = test_head; - test_head = test_head->list_next; - kpl_test_run_one(_test); - } - return kpl_test_report_error(); -} - -int32_t main(int32_t argc, char *argv[]) { - if (argc < 2) - return kpl_test_run_all(); - kpl_test find = {}; - for (int32_t arg_index = 1; arg_index < argc; arg_index++) { - find.name = argv[arg_index]; - kpl_test *found = kpl_tree_find(&test_tree, &find); - if (!found) { - printf(KPL_COLOR2(BOLD, RED) "TEST: " KPL_COLOR(RESET) KPL_COLOR2(BOLD, WHITE) "%s " KPL_COLOR(RESET) - KPL_COLOR2(BOLD, RED) "NOT FOUND\n" KPL_COLOR(RESET), find.name); - return KPL_EXIT_CODE_ERROR; - } - kpl_test *duplicates = found->tree_duplicates; - while (duplicates) { - kpl_test_run_one(duplicates); - duplicates = duplicates->tree_duplicates; - } - kpl_test_run_one(found); - } - return kpl_test_report_error(); -} - -KPL_TEST(pass_by_default) { - // pass, uncomment below to fail - // KPL_FAIL("example fail"); -} diff --git a/test/tree.c b/test/tree.c deleted file mode 100644 index aef4061..0000000 --- a/test/tree.c +++ /dev/null @@ -1,47 +0,0 @@ - -#include "kpl_test.h" - -typedef struct _c_string_test_node { - KPL_TREE_HEADER(_c_string_test_node); - const char *c_string; -} c_string_test_node; - -static int32_t c_string_test_compare(const void *node_a, const void *node_b) { - const c_string_test_node *a = node_a, *b = node_b; - return strcmp(a->c_string, b->c_string); -} - -KPL_TEST(c_string_trees) { - const char *c_strings[] = { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", NULL }; - const int32_t c_string_counter[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }; - kpl_tree c_string_tree = {}; - kpl_tree_init(&c_string_tree, c_string_test_compare, free); - const char **c_strings_iterator = c_strings; - const int32_t *c_string_counter_iterator = c_string_counter; - while (*c_strings_iterator && *c_string_counter_iterator) { - for (int32_t inserts = 0; inserts < *c_string_counter_iterator; inserts++) { - c_string_test_node *node = calloc(1, sizeof(c_string_test_node)); - node->c_string = *c_strings_iterator; - kpl_tree_insert(&c_string_tree, node); - } - c_strings_iterator++; - c_string_counter_iterator++; - } - c_strings_iterator = c_strings; - c_string_counter_iterator = c_string_counter; - c_string_test_node find = {}, *found; - while (*c_strings_iterator && *c_string_counter_iterator) { - int32_t count = *c_string_counter_iterator; - find.c_string = *c_strings_iterator; - KPL_ASSERT(found = kpl_tree_find(&c_string_tree, &find), "c string node not found"); - while (count) { - KPL_ASSERT(strcmp(found->c_string, *c_strings_iterator) == 0, "c string does not match"); - found = found->tree_duplicates; - count--; - } - KPL_ASSERT(!found, "found pointer should be null on last duplicate"); - c_strings_iterator++; - c_string_counter_iterator++; - } - kpl_tree_free(&c_string_tree); -} @@ -1,372 +0,0 @@ -#!/bin/bash - -SCRIPT="$0" -OPTION="$1" -VM="$2" -ARG_COUNT=$# -DIR="qemu" -USER="user" -HOST="localhost" -RSYNC_LIST="rsync_list" -CORES="2" -MEMORY="2G" -DEVICE="virtio-net-pci,netdev=net0" -HDA_URL="https://cloud.debian.org/images/cloud/trixie/latest" -TEST_COMMAND="make kpl_test && arch && ./kpl_test" -CLEAN_COMMAND="make clean; find -type f -regex '^.*\.[ch]' -exec rm -v {} \;" -X64="X64" -A64="A64" -NAMES=("$X64" "$A64") -declare -A QEMU=( - [$X64]="qemu-system-x86_64" - [$A64]="qemu-system-aarch64" -) -declare -A HDA=( - [$X64]="debian-13-nocloud-amd64.qcow2" - [$A64]="debian-13-nocloud-arm64.qcow2" -) -declare -A BIOS_FD=( - [$X64]="OVMF_CODE.fd" - [$A64]="QEMU_EFI.fd" -) -declare -A SSH_PORTS=( - [$X64]="2222" - [$A64]="2223" -) -declare -A PID_FILES=( - [$X64]="$X64.pid" - [$A64]="$A64.pid" -) -declare -A MACHINES=( - [$X64]="pc" - [$A64]="virt" -) - -vm_init() { - if [[ ! -d "$DIR" ]] - then - mkdir $DIR - echo "DO NOT MODIFY CONTENTS OF THIS DIRECTORY" > "$DIR/README" - fi - make rsync_list > "$DIR/$RSYNC_LIST" - cd $DIR - if [[ ! -d "$USER" ]] - then - mkdir $USER - ssh-keygen -t ed25519 -f "$USER/$USER" -N "" - fi - for H in ${HDA[@]} - do - if [[ ! -f "$H" ]] - then - wget "$HDA_URL/$H" - fi - done -} - -vm_setup() { - local PUB=$(cat "$DIR/$USER/$USER.pub") - local HEADER="COPY AND RUN IN QEMU:" - echo $HEADER - echo " sed -i 's/GRUB_TIMEOUT=5/GRUB_TIMEOUT_STYLE=hidden\nGRUB_TIMEOUT=0/' /etc/default/grub; update-grub; sed -i 's/^%sudo\s\+ALL=(ALL:ALL)\sALL$/%sudo ALL=(ALL:ALL) NOPASSWD:ALL/' /etc/sudoers; adduser --disabled-password --gecos "" --shell /bin/bash $USER; adduser $USER sudo; su - $USER" - echo $HEADER - echo " mkdir .ssh; cd .ssh; echo \"$PUB\" > authorized_keys; chmod 600 authorized_keys; cd ~" - echo $HEADER - echo " sudo apt update; yes | sudo apt upgrade; yes | sudo apt install ssh make gcc gdb rsync; sudo poweroff" -} - -vm_machine_check() { - local MACHINE=$1 - if [[ "$MACHINE" != "$X64" && "$MACHINE" != "$A64" ]] - then - echo "Usage $SCRIPT $OPTION $X64|$A64" - return 1 - fi - return 0 -} - -vm_boot() { - local MACHINE=$1 - local PID_FILE="${PID_FILES[$MACHINE]}" - if [[ -f "$PID_FILE" ]] - then - echo "VM $MACHINE is running" - return 1 - fi - local BIOS=$(locate "${BIOS_FD[$MACHINE]}" | grep "edk2" | head -n 1) - local CMD=( - "${QEMU[$MACHINE]}" - "-M" "${MACHINES[$MACHINE]}" - "-cpu" "max" - "-m" "$MEMORY" - "-smp" "$CORES" - "-device" "$DEVICE" - "-netdev" "user,id=net0,hostfwd=tcp::${SSH_PORTS[$MACHINE]}-:22" - "-bios" "$BIOS" - "-hda" "${HDA[$MACHINE]}" - "-pidfile" "$PID_FILE" - ) - for ARG in "${@:2}" - do - CMD+=("$ARG") - done - echo "${CMD[*]}" - "${CMD[@]}" -} - -vm_dir_check() { - if [[ ! -d "$DIR" ]] - then - echo "Environment not setup, run: $SCRIPT -init" - exit 1 - fi - cd $DIR -} - -vm_cli() { - vm_dir_check - vm_machine_check $VM - if [[ $? != 0 ]] - then - return 1 - fi - vm_boot $VM "-nographic" -} - -vm_start() { - vm_dir_check - if [[ $ARG_COUNT == 2 ]] - then - vm_machine_check $VM - if [[ $? == 0 ]] - then - vm_boot $VM "-display" "none" "-daemonize" - return - else - echo "Invalid Machine Specified" - return 1 - fi - fi - for MACHINE in "${NAMES[@]}" - do - vm_boot $MACHINE "-display" "none" "-daemonize" - done -} - -vm_ssh_options() { - local MACHINE=$1 - local -n OPTIONS=$2 - OPTIONS=( - "ssh" - "-o" "UserKnownHostsFile=/dev/null" - "-o" "StrictHostKeyChecking=no" - "-p" "${SSH_PORTS[$MACHINE]}" - ) -} - -vm_cmd() { - local MACHINE=$1 - vm_machine_check $MACHINE - if [[ $? != 0 ]] - then - return 1 - fi - local SSH_OPTIONS=() - vm_ssh_options $MACHINE SSH_OPTIONS - "${SSH_OPTIONS[@]}" "$USER@$HOST" $2 -} - -vm_ssh_add() { - ssh-add "$USER/$USER" &> /dev/null -} - -vm_ssh() { - vm_dir_check - vm_ssh_add - vm_cmd $VM -} - -vm_rsync_machine() { - local MACHINE=$1 - vm_machine_check $MACHINE - if [[ $? != 0 ]] - then - echo "Invalid Machine Specified" - return 1 - fi - local SSH_OPTIONS=() - vm_ssh_options $MACHINE SSH_OPTIONS - local CMD=( - "rsync" - "-avz" - "-e" "${SSH_OPTIONS[*]}" - "--include-from=$DIR/$RSYNC_LIST" - "--exclude=*" - "$PWD/" - "$USER@$HOST:/home/$USER" - ) - vm_ssh_add - "${CMD[@]}" -} - -vm_sync() { - if [[ "$ARG_COUNT" == 2 ]] - then - vm_rsync_machine $VM - return - fi - for MACHINE in "${NAMES[@]}" - do - vm_rsync_machine $MACHINE - done -} - -vm_test_cmd() { - local MACHINE=$1 - vm_cmd $MACHINE $TEST_COMMAND -} - -vm_test() { - if [[ "$ARG_COUNT" == 2 ]] - then - vm_rsync_machine $VM - vm_test_cmd $VM - return - fi - local -A TEST_STATUS=([$X64]=0 [$A64]=0) - for MACHINE in "${NAMES[@]}" - do - vm_rsync_machine $MACHINE - vm_test_cmd $MACHINE - TEST_STATUS[$MACHINE]=$? - done - for MACHINE in "${NAMES[@]}" - do - if [[ "${TEST_STATUS[$MACHINE]}" != 0 ]] - then - echo "-----------------------" - echo " VM $MACHINE FAILED TESTS" - echo "-----------------------" - fi - done - return $(( ${TEST_STATUS[$X64]} | ${TEST_STATUS[$A64]} )) -} - -vm_stop() { - vm_dir_check - vm_ssh_add - for MACHINE in "${NAMES[@]}" - do - local PID_FILE="${PID_FILES[$MACHINE]}" - if [[ -f "$PID_FILE" ]] - then - vm_cmd $MACHINE "sudo poweroff" - echo "VM $MACHINE powering off" - fi - done -} - -vm_kill() { - vm_dir_check - for MACHINE in "${NAMES[@]}" - do - local PID_FILE="${PID_FILES[$MACHINE]}" - if [[ -f "$PID_FILE" ]] - then - kill $(cat $PID_FILE) - echo "VM $MACHINE force stop" - fi - done -} - -vm_clean_cmd() { - local MACHINE=$1 - vm_machine_check $MACHINE - if [[ $? != 0 ]] - then - echo "Invalid Machine Specified" - return 1 - fi - vm_cmd $MACHINE $CLEAN_COMMAND -} - -vm_clean() { - vm_ssh_add - if [[ "$ARG_COUNT" == 2 ]] - then - vm_clean_cmd $VM - return - fi - for MACHINE in "${NAMES[@]}" - do - vm_clean_cmd $MACHINE - done -} - -vm_destroy() { - if [[ ! -d "$DIR" ]] - then - return - fi - local CHOICE - read -n 1 -r -p "Are you sure? [y/n] " CHOICE - if [[ "$CHOICE" != "y" ]] - then - echo - return - fi - echo - vm_kill - cd .. - rm -rf $DIR - echo -e "Removed $DIR directory" -} - -declare -A SCRIPT_COMMANDS=( - ["-init"]="vm_init" - ["-setup"]="vm_setup" - ["-cli"]="vm_cli" - ["-start"]="vm_start" - ["-ssh"]="vm_ssh" - ["-sync"]="vm_sync" - ["-test"]="vm_test" - ["-clean"]="vm_clean" - ["-stop"]="vm_stop" - ["-kill"]="vm_kill" - ["-destroy"]="vm_destroy" - ["-help"]="vm_help" -) - -vm_help() { - local LENGTH="${#SCRIPT_COMMANDS[@]}" - local I=0 - echo "See TESTING" - echo -n "Usage $SCRIPT [" - for CMD in "${!SCRIPT_COMMANDS[@]}" - do - echo -n "$CMD" - if (( I + 1 < LENGTH )) - then - echo -n "|" - else - echo "]" - fi - (( I++ )) - done - return 2 -} - -vm_main() { - for CMD in "${!SCRIPT_COMMANDS[@]}" - do - if [[ "$OPTION" == "$CMD" ]] - then - "${SCRIPT_COMMANDS[$CMD]}" - return - fi - done - echo -n "Invalid [OPTION], " - vm_help -} - -vm_main |
