diff options
| author | nodist <kevin.comas.git@gmail.com> | 2026-10-03 08:50:07 -0400 |
|---|---|---|
| committer | nodist <kevin.comas.git@gmail.com> | 2026-10-03 08:50:07 -0400 |
| commit | de65adbc1b2d1b3efdbe6a023bb7435f84b2f3a7 (patch) | |
| tree | 2851060d2cf464146d0fb67a50dc52f710076e05 /source | |
| parent | 2e908a7be31558cc98a9f0e97556e49ee5748c16 (diff) | |
redesign
Diffstat (limited to 'source')
| -rw-r--r-- | source/README | 17 | ||||
| -rw-r--r-- | source/config.c | 242 | ||||
| -rw-r--r-- | source/kpl.c | 20 | ||||
| -rw-r--r-- | source/queue.c | 91 |
4 files changed, 0 insertions, 370 deletions
diff --git a/source/README b/source/README deleted file mode 100644 index 0ec82e2..0000000 --- a/source/README +++ /dev/null @@ -1,17 +0,0 @@ -Source - ---- - -Includes: - -#include "kpl.h" - -This is the only header for any C file in the source folder - ---- - -Complier Flags: - -KPL_MAIN # For standalone exec, off for testing or including in other code - ---- diff --git a/source/config.c b/source/config.c deleted file mode 100644 index 31de7d4..0000000 --- a/source/config.c +++ /dev/null @@ -1,242 +0,0 @@ - -#include "kpl.h" - -static const kpl_config default_config = { - .argc = -1, .main_module_arg_index = -1, .version = KPL_VERSION, - .argv = NULL, .module_extension = ".kpl", - .flags = 0, - .io = { - .queue_pow2 = KPL_CONFIG_IO_QUEUE_POW2_DEFAULT - }, - .thread = { - .count = KPL_CONFIG_THREAD_COUNT_AFFINITY, - .ring_pool_entries_pow2 = { - [0] = 0, [1] = 0, [2] = 0, [3] = 0, [4] = 0, [5] = 13, - [6] = 13, [7] = 13, [8] = 13, [9] = 13, [10] = 13, [11] = 13, - [12] = 13, [13] = 13, [14] = 13, [15] = 13, [16] = 13, [17] = 13, - [18] = 13, [19] = 13, [20] = 13, [21] = 13, [22] = 13, [23] = 13, - [24] = 13, [25] = 13, [26] = 13, [27] = 13 - } - }, - .code = { - .queue_pow2 = KPL_CONFIG_CODE_QUEUE_POW2_DEFAULT - } -}; - -void kpl_config_init(kpl_config *config) { - memcpy(config, &default_config, sizeof(kpl_config)); -} - -typedef int32_t kpl_arg_match_fn(const char *match_str, const int32_t match_str_index, kpl_config *config); - -typedef struct { - const char *match_str, *help_message; - kpl_arg_match_fn *arg_match_fn; -} kpl_arg_match; - - -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_io_queue_pow2(const char *match_str, const int32_t match_str_index, kpl_config *config) { - int32_t queue_pow2 = atoi(match_str + match_str_index); - if (queue_pow2 < KPL_CONFIG_IO_QUEUE_POW2_MIN || queue_pow2 > KPL_CONFIG_IO_QUEUE_POW2_MAX) - return KPL_EXIT_CODE_ERROR; - config->io.queue_pow2 = queue_pow2; - 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 <= 0 || thread_count > KPL_CONFIG_THREAD_COUNT_MAX) - return KPL_EXIT_CODE_ERROR; - config->thread.count = thread_count; - return KPL_EXIT_CODE_SUCCESS; -} - -static int32_t kpl_arg_match_memory_pool_pow2(const char *match_str, const int32_t match_str_index, kpl_config *config) { - char *endptr; - int32_t object_size_pow2 = strtol(match_str + match_str_index, &endptr, 10); - if (object_size_pow2 < KPL_CONFIG_THREAD_RING_POOL_MIN_OBJECT_SIZE_POW2 || - object_size_pow2 > KPL_CONFIG_THREAD_RING_POOL_MAX_OBJECT_SIZE_POW2) - return KPL_EXIT_CODE_ERROR; - while (*endptr && !isdigit(*endptr)) - endptr++; - int32_t entry_size_pow2 = strtol(endptr, NULL, 10); - if (entry_size_pow2 < KPL_CONFIG_THREAD_RING_POOL_MIN_RING_SIZE_POW2 || - entry_size_pow2 > KPL_CONFIG_THREAD_RING_POOL_MAX_RING_SIZE_POW2) - return KPL_EXIT_CODE_ERROR; - config->thread.ring_pool_entries_pow2[object_size_pow2] = entry_size_pow2; - return KPL_EXIT_CODE_SUCCESS; -} - -static int32_t kpl_arg_match_code_queue_pow2(const char *match_str, const int32_t match_str_index, kpl_config *config) { - int32_t queue_pow2 = atoi(match_str + match_str_index); - if (queue_pow2 < KPL_CONFIG_CODE_QUEUE_POW2_MIN || queue_pow2 > KPL_CONFIG_CODE_QUEUE_POW2_MAX) - return KPL_EXIT_CODE_ERROR; - config->code.queue_pow2 = queue_pow2; - 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_version(const char *match_str, const int32_t match_str_index, kpl_config *config) { - (void) match_str_index; - printf("\nKevin's Programming Language (" KPL_COLOR(LIGHT_BLUE) "KPL" KPL_COLOR(RESET) - "), Version: " KPL_COLOR(LIGHT_GREEN) "%s\n" KPL_COLOR(RESET), - config->version); - if (match_str) - putchar('\n'); - return KPL_EXIT_CODE_HELP; -} - -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 imports (default .kpl)", - .arg_match_fn = kpl_arg_match_module_extension - }, - { - .match_str = "--io-queue-pow2=", - .help_message = "[NUMBER]; Power of 2 for calculating the size of the io_uring queue", - .arg_match_fn = kpl_arg_match_io_queue_pow2 - }, - { - .match_str = "--thread-count=", - .help_message = "[NUMBER]; Number of running threads, minimum is 1", - .arg_match_fn = kpl_arg_match_thread_count - }, - { - .match_str = "--memory-pool-pow2=", - .help_message = "[NUMBER1],[NUMBER2]; Set the memory pool [NUMBER1] to have 2 ** [NUMBER2] entires", - .arg_match_fn = kpl_arg_match_memory_pool_pow2 - }, - { - .match_str = "--code-queue-pow2=", - .help_message = "[NUMBER]; Power of 2 for calculating the size of the code queue", - .arg_match_fn = kpl_arg_match_code_queue_pow2 - }, - { - .match_str = "--eval", - .help_message = " [STRING...]; Evaluate next argument(s) as modules", - .arg_match_fn = kpl_arg_match_eval - }, - { - .match_str = "--repl", - .help_message = "; Start (R)ead, (E)valuate, (P)rint, (L)oop", - .arg_match_fn = kpl_arg_match_repl - }, - { - .match_str = "--version", - .help_message = "; Show version", - .arg_match_fn = kpl_arg_match_version - }, - { - .match_str = "--help", - .help_message = "; Show help", - .arg_match_fn = kpl_arg_match_help - }, - { .match_str = NULL, .help_message = NULL, .arg_match_fn = NULL } -}; - -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; - kpl_arg_match_version(NULL, 0, config); - 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; - while (match_array->match_str) { - printf(KPL_COLOR(LIGHT_YELLOW) "\t%s" KPL_COLOR(RESET) "%s\n", - match_array->match_str, match_array->help_message); - match_array++; - } - putchar('\n'); - return KPL_EXIT_CODE_HELP; -} - -static bool kpl_valid_match_str_index(const char *match_str_array, const char *match_str_arg, int32_t match_str_index) { - return match_str_array[match_str_index] && - match_str_array[match_str_index] == match_str_arg[match_str_index]; -} - -static bool kpl_valid_match_str(const char *match_str_array, const char *match_str_arg, int32_t match_str_index) { - return !match_str_array[match_str_index] && (!match_str_arg[match_str_index] || - (match_str_arg[match_str_index] && match_str_array[match_str_index - 1] == '=')); -} - -bool kpl_config_valid_module_exension(kpl_config *config, const char *module_name) { - size_t name_length = strlen(module_name), module_extension_length = strlen(config->module_extension); - return name_length > module_extension_length && - strcmp(module_name + name_length - module_extension_length, config->module_extension) == 0; -} - -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 && !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; - while (match_array->match_str) { - while (kpl_valid_match_str_index(match_array->match_str, match_str_arg, match_str_index)) - 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_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; - match_array++; - } - if (!match_array->match_str) - break; - arg_index++; - } - 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]/[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); - } - return KPL_EXIT_CODE_SUCCESS; -} diff --git a/source/kpl.c b/source/kpl.c deleted file mode 100644 index cc98b4b..0000000 --- a/source/kpl.c +++ /dev/null @@ -1,20 +0,0 @@ - -#include "kpl.h" - -#ifdef KPL_MAIN -int32_t main(const int32_t argc, const char *argv[]) { - int32_t exit_status = KPL_EXIT_CODE_SUCCESS; - 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("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/source/queue.c b/source/queue.c deleted file mode 100644 index 350c4e2..0000000 --- a/source/queue.c +++ /dev/null @@ -1,91 +0,0 @@ - -#include "kpl.h" - -extern inline void kpl_task_queue_init(kpl_task_queue *task_queue); - -extern inline void kpl_task_queue_add(kpl_task_queue *task_queue, kpl_task *task); - -kpl_task *kpl_task_queue_next(kpl_task_queue *task_queue) { - kpl_task *task = NULL; - for (;;) { - kpl_task *tail = task_queue->tail, *next = __atomic_load_n(&tail->next, __ATOMIC_ACQUIRE); - if (tail == (kpl_task*) &task_queue->dummy) { - if (!next) - break; - tail = task_queue->tail = next; - next = __atomic_load_n(&tail->next, __ATOMIC_ACQUIRE); - } - if (!next) { - kpl_task *head = __atomic_load_n(&task_queue->head, __ATOMIC_ACQUIRE); - if (tail != head) - continue; - kpl_task_queue_add(task_queue, (kpl_task*) &task_queue->dummy); - next = __atomic_load_n(&tail->next, __ATOMIC_ACQUIRE); - } - if (next) { - task_queue->tail = next; - task = tail; - break; - } - } - if (task) - __atomic_store_n(&task->next, NULL, __ATOMIC_RELAXED); - return task; -} - -bool kpl_ring_queue_init(kpl_ring_queue *ring_queue, int8_t size_pow2) { - if (size_pow2 < KPL_RING_QUEUE_MIN_POW2 || size_pow2 > KPL_RING_QUEUE_MAX_POW2) - return false; - size_t size = 1L << size_pow2; - ring_queue->mmap_size = KPL_PAGE_SIZE_ROUND(sizeof(kpl_ring_queue_item) * size); - ring_queue->items = mmap(NULL, ring_queue->mmap_size, - PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); - ring_queue->items_mask = size - 1; - __atomic_store_n(&ring_queue->head_index, 0, __ATOMIC_RELAXED); - __atomic_store_n(&ring_queue->tail_index, 0, __ATOMIC_RELAXED); - for (size_t index = 0; index < size; index++) - __atomic_store_n(&ring_queue->items[index].index, index, __ATOMIC_RELAXED); - return true; -} - -extern inline void kpl_ring_queue_free(kpl_ring_queue *ring_queue); - -bool kpl_ring_queue_add(kpl_ring_queue *ring_queue, void *data) { - for (;;) { - ssize_t tail_index = __atomic_load_n(&ring_queue->tail_index, __ATOMIC_RELAXED); - kpl_ring_queue_item *item = &ring_queue->items[tail_index & ring_queue->items_mask]; - ssize_t item_index = __atomic_load_n(&item->index, __ATOMIC_ACQUIRE), diff = item_index - tail_index; - if (diff > 0) - continue; - if (diff < 0) - break; - if (!__atomic_compare_exchange_n(&ring_queue->tail_index, &tail_index, tail_index + 1, true, - __ATOMIC_RELAXED, __ATOMIC_RELAXED)) - continue; - item->data = data; - __atomic_store_n(&item->index, tail_index + 1, __ATOMIC_RELEASE); - return true; - } - return false; -} - -void *kpl_ring_queue_next(kpl_ring_queue *ring_queue) { - for (;;) { - ssize_t head_index = __atomic_load_n(&ring_queue->head_index, __ATOMIC_RELAXED); - kpl_ring_queue_item *item = &ring_queue->items[head_index & ring_queue->items_mask]; - ssize_t item_index = __atomic_load_n(&item->index, __ATOMIC_ACQUIRE), - diff = item_index - (head_index + 1); - if (diff > 0) - continue; - if (diff < 0) - break; - if (!__atomic_compare_exchange_n(&ring_queue->head_index, &head_index, head_index + 1, true, - __ATOMIC_RELAXED, __ATOMIC_RELAXED)) - continue; - void *data = item->data; - item->data = NULL; - __atomic_store_n(&item->index, head_index + ring_queue->items_mask + 1, __ATOMIC_RELEASE); - return data; - } - return NULL; -} |
