diff options
| author | nodist <kevin.comas.git@gmail.com> | 2026-09-20 17:10:21 -0400 |
|---|---|---|
| committer | nodist <kevin.comas.git@gmail.com> | 2026-09-20 17:10:21 -0400 |
| commit | 260e50d627aea535abcd9d9902c1b4ae56da4c54 (patch) | |
| tree | 91a5ec00e02e67c78437040a798f2fa3140e57a0 | |
| parent | 4e19e6ab1bd24466d9dcb0d8eea54a7e3d84d576 (diff) | |
add config for io and thread
| -rw-r--r-- | include/kpl.h | 12 | ||||
| -rw-r--r-- | include/kpl/io.h | 6 | ||||
| -rw-r--r-- | include/kpl/macro.h | 6 | ||||
| -rw-r--r-- | include/kpl/memory.h | 8 | ||||
| -rw-r--r-- | include/kpl/thread.h | 2 | ||||
| -rw-r--r-- | source/config.c | 95 | ||||
| -rw-r--r-- | source/kpl.c | 14 | ||||
| -rw-r--r-- | test/config.c | 20 | ||||
| -rwxr-xr-x | vm.sh | 16 |
9 files changed, 144 insertions, 35 deletions
diff --git a/include/kpl.h b/include/kpl.h index 15933ee..c4450ca 100644 --- a/include/kpl.h +++ b/include/kpl.h @@ -6,14 +6,18 @@ #include "kpl/macro.h" #include "kpl/io.h" #include "kpl/thread.h" -#include "kpl/memory.h" + +typedef enum : uint32_t { + KPL_CONFIG_FLAG_EVAL = 1 << 0, + KPL_CONFIG_FLAG_REPL = 1 << 1 +} kpl_config_flags; typedef struct { + uint32_t flags; int32_t argc, main_module_arg_index; const char **argv, *module_extension; kpl_config_io io; kpl_config_thread thread; - kpl_config_memory memory; } kpl_config; void kpl_config_init(kpl_config *config); @@ -21,3 +25,7 @@ void kpl_config_init(kpl_config *config); bool kpl_config_valid_module_exension(kpl_config *config, const char *module_name); int32_t kpl_config_parse_args(kpl_config *config, const int32_t argc, const char *argv[]); + +typedef struct { + kpl_config config; +} kpl_env; diff --git a/include/kpl/io.h b/include/kpl/io.h index e6ea690..2b33755 100644 --- a/include/kpl/io.h +++ b/include/kpl/io.h @@ -5,6 +5,12 @@ #include "kpl/macro.h" +typedef enum : int32_t { + KPL_IO_QUEUE_POW_MIN = 4, + KPL_IO_QUEUE_POW_DEFAULT = 10, + KPL_IO_QUEUE_POW_MAX = 16 +} kpl_io_queue_pow_options; + typedef struct { int32_t queue_pow; } kpl_config_io; diff --git a/include/kpl/macro.h b/include/kpl/macro.h index 63699d0..cdcaa1f 100644 --- a/include/kpl/macro.h +++ b/include/kpl/macro.h @@ -6,4 +6,8 @@ #include "color.h" #include "type.h" -#define KPL_PRINT_TODO(MESSAGE) printf(KPL_COLOR2(BOLD, LIGHT_YELLOW) "TODO " KPL_COLOR(RESET) KPL_COLOR(BOLD) "%s" KPL_COLOR(RESET) ":" KPL_COLOR(LIGHT_BLUE) "%s" KPL_COLOR(RESET) ":" KPL_COLOR(LIGHT_MAGENTA) "%d" KPL_COLOR(RESET) "::" KPL_COLOR(LIGHT_YELLOW) "%s\n" KPL_COLOR(RESET), __FILE__, __FUNCTION__, __LINE__, MESSAGE) +#define _KPL_PRINT_(MESSAGE_COLOR, MESSAGE) printf(KPL_COLOR2(BOLD, MESSAGE_COLOR) "TODO " KPL_COLOR(RESET) KPL_COLOR(BOLD) "%s" KPL_COLOR(RESET) ":" KPL_COLOR(LIGHT_BLUE) "%s" KPL_COLOR(RESET) ":" KPL_COLOR(LIGHT_MAGENTA) "%d" KPL_COLOR(RESET) "::" KPL_COLOR(MESSAGE_COLOR) "%s\n" KPL_COLOR(RESET), __FILE__, __FUNCTION__, __LINE__, MESSAGE) + +#define KPL_PRINT_TODO(MESSAGE) _KPL_PRINT_(LIGHT_YELLOW, MESSAGE) + +#define KPL_PRINT_ERROR(MESSAGE) _KPL_PRINT_(LIGHT_RED, MESSAGE) diff --git a/include/kpl/memory.h b/include/kpl/memory.h deleted file mode 100644 index 9b9a843..0000000 --- a/include/kpl/memory.h +++ /dev/null @@ -1,8 +0,0 @@ - -#pragma once - -// Kpl memory pools and garbage collection - -typedef struct { - -} kpl_config_memory; diff --git a/include/kpl/thread.h b/include/kpl/thread.h index 99db80f..7304340 100644 --- a/include/kpl/thread.h +++ b/include/kpl/thread.h @@ -6,5 +6,5 @@ #include "kpl/macro.h" typedef struct { - int32_t thread_count; + int32_t count; } kpl_config_thread; diff --git a/source/config.c b/source/config.c index 7fa97d0..461197d 100644 --- a/source/config.c +++ b/source/config.c @@ -4,9 +4,13 @@ static kpl_config default_config = { .argc = -1, .main_module_arg_index = -1, .argv = NULL, .module_extension = ".kpl", - .io = { .queue_pow = 12 }, - .thread = { .thread_count = -1 }, - .memory = {} + .flags = 0, + .io = { + .queue_pow = KPL_IO_QUEUE_POW_DEFAULT + }, + .thread = { + .count = -1 + } }; void kpl_config_init(kpl_config *config) { @@ -20,19 +24,70 @@ typedef struct { kpl_arg_match_fn *arg_match_fn; } kpl_arg_match; -static int32_t kpl_arg_match_help(const char *match_str, const int32_t match_str_index, kpl_config *config); static int32_t kpl_arg_match_module_extension(const char *match_str, const int32_t match_str_index, kpl_config *config) { config->module_extension = match_str + match_str_index; return KPL_EXIT_CODE_SUCCESS; } +static int32_t kpl_arg_match_eval(const char *match_str, const int32_t match_str_index, kpl_config *config) { + (void) match_str; + (void) match_str_index; + config->flags |= KPL_CONFIG_FLAG_EVAL; + return KPL_EXIT_CODE_SUCCESS; +} + +static int32_t kpl_arg_match_repl(const char *match_str, const int32_t match_str_index, kpl_config *config) { + (void) match_str; + (void) match_str_index; + config->flags |= KPL_CONFIG_FLAG_REPL; + return KPL_EXIT_CODE_SUCCESS; +} + +static int32_t kpl_arg_match_io_queue_pow(const char *match_str, const int32_t match_str_index, kpl_config *config) { + int32_t queue_pow = atoi(match_str + match_str_index); + if (queue_pow < KPL_IO_QUEUE_POW_MIN || queue_pow > KPL_IO_QUEUE_POW_MAX) + return KPL_EXIT_CODE_ERROR; + config->io.queue_pow = queue_pow; + return KPL_EXIT_CODE_SUCCESS; +} + +static int32_t kpl_arg_match_thread_count(const char *match_str, const int32_t match_str_index, kpl_config *config) { + int32_t thread_count = atoi(match_str + match_str_index); + if (!thread_count) + return KPL_EXIT_CODE_ERROR; + config->thread.count = thread_count; + return KPL_EXIT_CODE_SUCCESS; +} + +static int32_t kpl_arg_match_help(const char *match_str, const int32_t match_str_index, kpl_config *config); + static kpl_arg_match arg_match_array[] = { { .match_str = "--module-extension=", - .help_message="[WORD], Change the module extension for imported files (default .kpl)", + .help_message="[WORD], Change the module extension for imports (default .kpl)", .arg_match_fn = kpl_arg_match_module_extension }, + { + .match_str = "--eval", + .help_message = " [STRING...], Evaluate next argument(s) as modules", + .arg_match_fn = kpl_arg_match_eval + }, + { + .match_str = "--io-queue-pow=", + .help_message = "[NUMBER], Power of two for calculating the size of the io_uring queue", + .arg_match_fn = kpl_arg_match_io_queue_pow + }, + { + .match_str = "--thread-count=", + .help_message = "[NUMBER], Number of running threads, minimum is 1", + .arg_match_fn = kpl_arg_match_thread_count + }, + { + .match_str = "--repl", + .help_message = ", Start (R)ead, (E)valuate, (P)rint, (L)oop", + .arg_match_fn = kpl_arg_match_repl + }, { .match_str = "--help", .help_message = ", Show help", .arg_match_fn = kpl_arg_match_help }, { .match_str = NULL, .help_message = NULL, .arg_match_fn = NULL } }; @@ -40,8 +95,7 @@ static kpl_arg_match arg_match_array[] = { static int32_t kpl_arg_match_help(const char *match_str, const int32_t match_str_index, kpl_config *config) { (void) match_str; (void) match_str_index; - (void) config; - printf("\nUsage " KPL_COLOR(BOLD) "%s" KPL_COLOR(RESET) KPL_COLOR(WHITE) " [OPTIONS] [FILE]" KPL_COLOR(RESET) + printf("\nUsage " KPL_COLOR(BOLD) "%s" KPL_COLOR(RESET) KPL_COLOR(WHITE) " [OPTIONS] [MODULE]" KPL_COLOR(RESET) KPL_COLOR(BOLD) "%s\n\n" KPL_COLOR(RESET), config->argv[0], config->module_extension); printf("Where " KPL_COLOR(WHITE) "[OPTIONS]" KPL_COLOR(RESET) " are:\n"); const kpl_arg_match *match_array = arg_match_array; @@ -73,8 +127,12 @@ bool kpl_config_valid_module_exension(kpl_config *config, const char *module_nam int32_t kpl_config_parse_args(kpl_config *config, const int32_t argc, const char *argv[]) { config->argc = argc; config->argv = argv; + if (argc == 1) { + printf(KPL_COLOR(LIGHT_RED) "\nNo Module Specified\n" KPL_COLOR(RESET)); + return kpl_arg_match_help(NULL, 0, config); + } int32_t arg_index = 1; - while (arg_index < argc) { + while (arg_index < argc && !config->flags) { int32_t match_str_index = 0; const char *match_str_arg = argv[arg_index]; const kpl_arg_match *match_array = arg_match_array; @@ -83,8 +141,12 @@ int32_t kpl_config_parse_args(kpl_config *config, const int32_t argc, const char match_str_index++; if (kpl_valid_match_str(match_array->match_str, match_str_arg, match_str_index)) { int32_t exit_status = match_array->arg_match_fn(match_str_arg, match_str_index, config); - if (exit_status != KPL_EXIT_CODE_SUCCESS) + if (exit_status != KPL_EXIT_CODE_SUCCESS) { + if (exit_status == KPL_EXIT_CODE_ERROR) + printf(KPL_COLOR(LIGHT_RED) "\nInvalid Option: " KPL_COLOR(BOLD) "%s\n" KPL_COLOR(RESET), + match_str_arg); return exit_status; + } break; } match_str_index = 0; @@ -94,14 +156,23 @@ int32_t kpl_config_parse_args(kpl_config *config, const int32_t argc, const char break; arg_index++; } - if (arg_index == argc) + config->main_module_arg_index = arg_index; + if (config->flags) { + if ((config->flags & KPL_CONFIG_FLAG_EVAL) && argc - arg_index < 1) { + printf(KPL_COLOR(LIGHT_RED) "\nInvalid EVAL\n" KPL_COLOR(RESET)); + return kpl_arg_match_help(NULL, 0, config); + } + if ((config->flags & KPL_CONFIG_FLAG_REPL) && argc != arg_index) { + printf(KPL_COLOR(LIGHT_RED) "\nInvalid REPL\n" KPL_COLOR(RESET)); + return kpl_arg_match_help(NULL, 0, config); + } return KPL_EXIT_CODE_SUCCESS; + } if (!kpl_config_valid_module_exension(config, argv[arg_index])) { printf(KPL_COLOR(LIGHT_RED) "\nInvalid " KPL_COLOR(RESET) - KPL_COLOR(WHITE) "[OPTION]/[FILE]: " KPL_COLOR(RESET) + KPL_COLOR(WHITE) "[OPTION]/[MODULE]: " KPL_COLOR(RESET) KPL_COLOR2(BOLD, LIGHT_RED) "%s\n" KPL_COLOR(RESET), argv[arg_index]); return kpl_arg_match_help(NULL, 0, config); } - config->main_module_arg_index = arg_index; return KPL_EXIT_CODE_SUCCESS; } diff --git a/source/kpl.c b/source/kpl.c index aacbd5c..cc98b4b 100644 --- a/source/kpl.c +++ b/source/kpl.c @@ -4,11 +4,17 @@ #ifdef KPL_MAIN int32_t main(const int32_t argc, const char *argv[]) { int32_t exit_status = KPL_EXIT_CODE_SUCCESS; - kpl_config config; - kpl_config_init(&config); - if ((exit_status = kpl_config_parse_args(&config, argc, argv))) + kpl_env env; + kpl_config_init(&env.config); + if ((exit_status = kpl_config_parse_args(&env.config, argc, argv))) return exit_status; - KPL_PRINT_TODO("MAIN PROGRAM"); + KPL_PRINT_TODO("INIT KPL"); + if (env.config.flags & KPL_CONFIG_FLAG_EVAL) + KPL_PRINT_TODO("RUN EVAL"); + else if (env.config.flags & KPL_CONFIG_FLAG_REPL) + KPL_PRINT_TODO("RUN REPL"); + else + KPL_PRINT_TODO("RUN MODULE"); return exit_status; } #endif diff --git a/test/config.c b/test/config.c index 8a1dc64..a8fd1ad 100644 --- a/test/config.c +++ b/test/config.c @@ -19,3 +19,23 @@ KPL_TEST(config_change_module_extension) { KPL_ASSERT(code == KPL_EXIT_CODE_SUCCESS, "Invalid exit code"); KPL_ASSERT(strcmp(config.module_extension, ".l") == 0, "Invalid new module extension"); } + +KPL_TEST(config_io_queue_pow) { + kpl_config config; + const int32_t argc = 3; + const char *argv[] = { "./kpl", "--io-queue-pow=14", "./file.kpl" }; + kpl_config_init(&config); + int32_t code = kpl_config_parse_args(&config, argc, argv); + KPL_ASSERT(code == KPL_EXIT_CODE_SUCCESS, "Invalid exit code"); + KPL_ASSERT(config.io.queue_pow == 14, "Invalid io queue power of two"); +} + +KPL_TEST(config_thread_count) { + kpl_config config; + const int32_t argc = 3; + const char *argv[] = { "./kpl", "--thread-count=1", "./file.kpl" }; + kpl_config_init(&config); + int32_t code = kpl_config_parse_args(&config, argc, argv); + KPL_ASSERT(code == KPL_EXIT_CODE_SUCCESS, "Invalid exit code"); + KPL_ASSERT(config.thread.count == 1, "Invalid thread count"); +} @@ -12,6 +12,8 @@ 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 -a" +CLEAN_COMMAND="make clean; find -type f -regex '^.*\.[ch]' -exec rm -v {} \;" X64="X64" A64="A64" NAMES=("$X64" "$A64") @@ -221,7 +223,7 @@ vm_sync() { vm_test_cmd() { local MACHINE=$1 - vm_cmd $MACHINE "make kpl_test && arch && ./kpl_test -a" + vm_cmd $MACHINE $TEST_COMMAND } vm_test() { @@ -285,7 +287,7 @@ vm_clean_cmd() { echo "Invalid Machine Specified" return 1 fi - vm_cmd $MACHINE "make clean; find -type f -regex '^.*\.[ch]' -exec rm -v {} \;" + vm_cmd $MACHINE $CLEAN_COMMAND } vm_clean() { @@ -320,7 +322,7 @@ vm_destroy() { echo -e "Removed $DIR directory" } -declare -A COMMANDS=( +declare -A SCRIPT_COMMANDS=( ["-init"]="vm_init" ["-setup"]="vm_setup" ["-cli"]="vm_cli" @@ -336,11 +338,11 @@ declare -A COMMANDS=( ) vm_help() { - local LENGTH="${#COMMANDS[@]}" + local LENGTH="${#SCRIPT_COMMANDS[@]}" local I=0 echo "See TESTING" echo -n "Usage $SCRIPT [" - for CMD in "${!COMMANDS[@]}" + for CMD in "${!SCRIPT_COMMANDS[@]}" do echo -n "$CMD" if (( I + 1 < LENGTH )) @@ -355,11 +357,11 @@ vm_help() { } vm_main() { - for CMD in "${!COMMANDS[@]}" + for CMD in "${!SCRIPT_COMMANDS[@]}" do if [[ "$OPTION" == "$CMD" ]] then - "${COMMANDS[$CMD]}" + "${SCRIPT_COMMANDS[$CMD]}" return fi done |
