From 689719e0a1f139e28e57a4df334d5fde354b3379 Mon Sep 17 00:00:00 2001 From: nodist Date: Tue, 20 Oct 2026 14:11:31 -0400 Subject: config parse cli arguments --- include/kpl.h | 6 ++ include/kpl/allocate.h | 35 +++++++++++ include/kpl/class.h | 24 +------- include/kpl/env.h | 43 ++++++------- include/kpl/io.h | 4 ++ include/kpl/memory.h | 2 +- include/kpl/object.h | 38 +++++++++++- include/kpl/tree.h | 1 + include/kpl/value.h | 51 ++++++++++++++++ include/kpl_test.h | 2 - source/allocate.c | 6 ++ source/config.c | 162 +++++++++++++++++++++++++++++++++++++++++++++++++ source/env.c | 3 - source/io.c | 2 + source/kpl.c | 9 ++- source/memory.c | 4 +- test/config.c | 18 ++++++ test/io.c | 2 + 18 files changed, 356 insertions(+), 56 deletions(-) create mode 100644 include/kpl/allocate.h create mode 100644 include/kpl/value.h create mode 100644 source/allocate.c create mode 100644 source/config.c create mode 100644 test/config.c diff --git a/include/kpl.h b/include/kpl.h index 7c5e209..838478b 100644 --- a/include/kpl.h +++ b/include/kpl.h @@ -6,6 +6,10 @@ #define KPL_VERSION "0.0.0" +#define KPL_FILE_EXTENSION ".kpl" + +#define KPL_WEBSITE "https://kpl.dev" + // System includes #define _GNU_SOURCE #include @@ -43,9 +47,11 @@ 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/memory.h" #include "kpl/io.h" #include "kpl/env.h" +#include "kpl/allocate.h" diff --git a/include/kpl/allocate.h b/include/kpl/allocate.h new file mode 100644 index 0000000..ee24d72 --- /dev/null +++ b/include/kpl/allocate.h @@ -0,0 +1,35 @@ +/* + * KPL object allocators helpers + */ + +#pragma once + +/* + * All allocations must extend the KPL_OBJECT_HEADER + * All objects are allocated through this + */ +inline void *kpl_object_allocate(kpl_thread *thread, int32_t size) { + kpl_object *obj = kpl_memory_allocator_allocate(&thread->memory_allocator, size, thread->thread_id); + if (!obj) + return NULL; + obj->object_flags = KPL_OBJECT_FLAGS_EMPTY; + __atomic_clear(&obj->gc_mark, __ATOMIC_RELAXED); + obj->gc_prev = obj->gc_next = NULL; + obj->gc_next = thread->gc_head; + if (thread->gc_head) + thread->gc_head->gc_prev = obj; + thread->gc_head = obj; + return obj; +} + +/* + * All objects are returned by this + */ +inline void kpl_object_return(kpl_env *env, void *object) { + kpl_object *obj = object; + if (obj->gc_prev) + obj->gc_prev->gc_next = obj->gc_next; + if (obj->gc_next) + obj->gc_next->gc_prev = obj->gc_prev; + kpl_memory_allocator_return(&env->threads[obj->thread_id].memory_allocator, object); +} diff --git a/include/kpl/class.h b/include/kpl/class.h index f7f9678..5957a98 100644 --- a/include/kpl/class.h +++ b/include/kpl/class.h @@ -4,33 +4,11 @@ #pragma once -typedef uint8_t kpl_utf8[4]; - -typedef union { - uint8_t u8; - int8_t i8; - uint16_t u16; - int16_t i16; - uint32_t u32; - int32_t i32; - uint64_t u64; - int64_t i64; - kpl_utf8 utf8; - float f32; - double f64; - void *ptr; -} kpl_value; - -typedef struct { - kpl_value value; - int64_t tag; -} kpl_result; - typedef struct { // TODO KPL INTERFACE } kpl_interface; typedef struct { - kpl_value value; + kpl_value64 value; kpl_interface *interface; } kpl_class; diff --git a/include/kpl/env.h b/include/kpl/env.h index 7806969..1e3d3d8 100644 --- a/include/kpl/env.h +++ b/include/kpl/env.h @@ -6,27 +6,33 @@ typedef enum : int32_t { KPL_THREAD_DETERIMINE_COUNT = -1, - KPL_THREAD_MAIN_ID = 0 + 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 { - int32_t thread_id; // self id + uint16_t thread_id; // self id kpl_memory_allocator memory_allocator; + kpl_object *gc_head, *gc_mark_head; // sweep if not marked or not in io pending kpl_io_uring io_uring; // TODO jit allocator _Atomic ssize_t priority; // running + the total number of tasks in threads queue, lower is higher priority - kpl_object_queue task_queue; + kpl_object_queue task_queue, error_queue; // traverse for gc marking pthread_t pthread; // not used for main thread sem_t sem; // wait when no task queue and io queue is empty } kpl_thread; -/* - * All allocations must extend the KPL_OBJECT_HEADER - * All objects are allocated through this - */ -inline void *kpl_thread_object_allocate(kpl_thread *thread, int32_t size) { - return kpl_memory_allocator_allocate(&thread->memory_allocator, size, thread->thread_id); -} +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_RUN_MODULE = 1 << 0, + KPL_ENV_FLAG_RUN_EVAL = 1 << 1, + KPL_ENV_FLAG_RUN_REPL = 1 << 2 +} kpl_env_flags; /* * Life cycle: @@ -39,21 +45,16 @@ inline void *kpl_thread_object_allocate(kpl_thread *thread, int32_t size) { * kpl_env_free &env */ typedef struct _kpl_env { + uint32_t flags; // see kpl_env_flags for values 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 + const char *kpl_entry; // file or string to be ran, not used for repl, pointer used to get kpl args namespace _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; // *namespace_tree lookups are thread safe, *module_tree needs mutex - pthread_mutex_t module_tree_mutex; + // TODO namespace tree + // TODO module tree } kpl_env; // kpl state, no globals, must be zero initialized -/* - * All objects are returned by this - */ -inline void kpl_env_object_return(kpl_env *env, void *object) { - kpl_memory_allocator_return(&env->threads[((kpl_object*) object)->thread_id].memory_allocator, object); -} - /* * Must match the type of c main return * if code != KPL_EXIT_CODE_SUCCESS then return code @@ -66,12 +67,12 @@ typedef enum : int32_t { /* * Set up initial values before initialization - * For defaults and/or directly setting without arguments, pass 0 to argc then fields can be updated directly + * For defaults and/or directly setting without arguments, pass -1 to argc then fields can be updated directly */ kpl_exit_code kpl_env_config(kpl_env *env, int32_t argc, char *argv[]); /* - * Initialize + * 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); diff --git a/include/kpl/io.h b/include/kpl/io.h index f353da2..f5904f4 100644 --- a/include/kpl/io.h +++ b/include/kpl/io.h @@ -63,6 +63,10 @@ int32_t kpl_io_uring_submit(kpl_io_uring *io_uring); 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); diff --git a/include/kpl/memory.h b/include/kpl/memory.h index b61d861..b69867a 100644 --- a/include/kpl/memory.h +++ b/include/kpl/memory.h @@ -54,7 +54,7 @@ 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, int32_t thread_id); +void *kpl_memory_allocator_allocate(kpl_memory_allocator *memory_allocator, int32_t size, uint16_t thread_id); /* * Only call this though kpl_env_object_return diff --git a/include/kpl/object.h b/include/kpl/object.h index 1f3ec04..1053348 100644 --- a/include/kpl/object.h +++ b/include/kpl/object.h @@ -4,11 +4,21 @@ #pragma once +typedef enum : uint8_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; + // for atomic linked list queue // size in bytes, index on the kpl_threads array +// Flags should only be set by owner thread #define KPL_OBJECT_HEADER(STRUCT_NAME) \ struct STRUCT_NAME *_Atomic queue_next; \ - int32_t object_size, thread_id + int32_t object_size; \ + uint16_t thread_id; \ + uint8_t object_flags; \ + _Atomic bool gc_mark; \ + struct STRUCT_NAME *gc_prev, *gc_next typedef struct _kpl_object { KPL_OBJECT_HEADER(_kpl_object); @@ -48,7 +58,18 @@ inline void kpl_object_queue_enqueue(kpl_object_queue *object_queue, void *objec */ void *kpl_object_queue_dequeue(kpl_object_queue *object_queue); -typedef void kpl_task_fn(kpl_object_task *task, kpl_env *env); +/* + * Class data is read only, cannot modify or race conditions can occur + * The error stack can be modified across threads + */ +typedef struct _kpl_object_error { + KPL_OBJECT_HEADER(_kpl_object_error); + kpl_object_queue error_stack; + kpl_class error_file, error_data; // Can be a reference to C code or KPL code + int32_t error_line; +} kpl_object_error; + +typedef kpl_object_error *kpl_task_fn(kpl_object_task *task, kpl_env *env); /* * Coroutine state object @@ -63,3 +84,16 @@ typedef struct _kpl_object_task { kpl_result result; kpl_class variables[]; // a task has no distinction between an argument or a local, both live here } kpl_object_task; + +/* + * KPL Object Mutex is a queue of tasks on an object + * Adding: Increment counter, then add task to task queue + * Removing: Remove from task queue, run task, then decrement counter + * Requires spin check when removing task due to space between counter adding and task adding + * If the counter value before was zero take control of the mutex + * The thread that controls the mutex runs each task until counter is zero + */ +typedef struct { + _Atomic int32_t lock_counter; + kpl_object_queue task_queue; +} kpl_object_mutex; diff --git a/include/kpl/tree.h b/include/kpl/tree.h index f3503d2..de9492d 100644 --- a/include/kpl/tree.h +++ b/include/kpl/tree.h @@ -6,6 +6,7 @@ // tree_duplicates is a linked stack #define KPL_TREE_HEADER(STRUCT_NAME) \ + KPL_OBJECT_HEADER(STRUCT_NAME); \ struct STRUCT_NAME *tree_left, *tree_right, *tree_duplicates; \ int32_t tree_height diff --git a/include/kpl/value.h b/include/kpl/value.h new file mode 100644 index 0000000..1e05c00 --- /dev/null +++ b/include/kpl/value.h @@ -0,0 +1,51 @@ +/* + * KPL values based on size + */ + +#pragma once + +#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; + +typedef struct { + kpl_value64 value; + int64_t tag; +} kpl_result; diff --git a/include/kpl_test.h b/include/kpl_test.h index 6a423eb..fbad075 100644 --- a/include/kpl_test.h +++ b/include/kpl_test.h @@ -40,8 +40,6 @@ void _kpl_test_add(kpl_test *_test); // do not call outside KPL_TEST macro #define KPL_TEST(NAME) _KPL_TEST_FN_DEFINITION(NAME); \ static void __attribute__((constructor)) kpl_test_constructor_ ## NAME(void) { \ static kpl_test _test = { \ - .tree_left = NULL, .tree_right = NULL, .tree_duplicates = NULL, \ - .tree_height = KPL_TREE_TREE_HEIGHT_INIT, \ .error_line_number = KPL_TEST_ERROR_LINE_NUMBER_INIT, \ .list_next = NULL, \ .source_file = __FILE__, .name = #NAME, .error_message = NULL, \ diff --git a/source/allocate.c b/source/allocate.c new file mode 100644 index 0000000..e242562 --- /dev/null +++ b/source/allocate.c @@ -0,0 +1,6 @@ + +#include "kpl.h" + +extern inline void *kpl_object_allocate(kpl_thread *thread, int32_t size); + +extern inline void kpl_object_return(kpl_env *env, void *object); diff --git a/source/config.c b/source/config.c new file mode 100644 index 0000000..f1aabf8 --- /dev/null +++ b/source/config.c @@ -0,0 +1,162 @@ + +#include "kpl.h" + +typedef kpl_exit_code kpl_config_option_parse_fn(kpl_env *env, int32_t arg_index, char *argv[]); + +typedef struct { + int32_t flags; + char match, *help_option_argument, *help_message; + 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_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_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_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 index 4e4f463..050f95b 100644 --- a/source/env.c +++ b/source/env.c @@ -1,6 +1,3 @@ #include "kpl.h" -extern inline void *kpl_thread_object_allocate(kpl_thread *thread, int32_t size); - -extern inline void kpl_env_object_return(kpl_env *env, void *object); diff --git a/source/io.c b/source/io.c index 0cd8480..8f3db55 100644 --- a/source/io.c +++ b/source/io.c @@ -52,6 +52,7 @@ kpl_object_task *kpl_io_uring_get_completed(kpl_io_uring *io_uring) { 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; return task; } @@ -77,6 +78,7 @@ struct io_uring_sqe *kpl_io_uring_op(kpl_io_uring *io_uring, kpl_object_task *ta __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; diff --git a/source/kpl.c b/source/kpl.c index 55ca994..d970eb3 100644 --- a/source/kpl.c +++ b/source/kpl.c @@ -5,7 +5,12 @@ #include "kpl.h" #ifdef KPL_MAIN -int32_t main(void) { - return 0; +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; + // TODO + return exit_code; } #endif diff --git a/source/memory.c b/source/memory.c index a9bcc30..a9c3321 100644 --- a/source/memory.c +++ b/source/memory.c @@ -73,7 +73,7 @@ static void *kpl_memory_allocator_slab_bump(kpl_memory_allocator_slab *slab, int } static kpl_object *kpl_memory_allocator_slab_allocate(kpl_memory_allocator_slab_group *slab_group, - int32_t thread_id) { + uint16_t thread_id) { kpl_memory_allocator_slab *slab = slab_group->slab_tail; int32_t size = 1 << slab_group->slab_size_pow2; if (!slab || slab->buffer_used + size > slab->buffer_size) { @@ -93,7 +93,7 @@ static kpl_object *kpl_memory_allocator_slab_allocate(kpl_memory_allocator_slab_ return object; } -void *kpl_memory_allocator_allocate(kpl_memory_allocator *memory_allocator, int32_t size, int32_t thread_id) { +void *kpl_memory_allocator_allocate(kpl_memory_allocator *memory_allocator, int32_t size, uint16_t thread_id) { if (size < 1 << KPL_MEMORY_ALLOCATOR_MIN_ROUND_SIZE_POW2) return NULL; int32_t size_pow2 = kpl_memory_allocator_get_size_pow2(size); diff --git a/test/config.c b/test/config.c new file mode 100644 index 0000000..eb5d0ca --- /dev/null +++ b/test/config.c @@ -0,0 +1,18 @@ + +#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/io.c b/test/io.c index 8a269fa..3dba03e 100644 --- a/test/io.c +++ b/test/io.c @@ -11,8 +11,10 @@ KPL_TEST(statx) { 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(&io_uring) == 1, "should submit one io operation"); KPL_ASSERT(task == kpl_io_uring_get_completed(&io_uring), "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); -- cgit v1.2.3