From 078e21a1feb811f9ef7797ce3ee5d2e8ffcccfce Mon Sep 17 00:00:00 2001 From: nasr Date: Fri, 17 Apr 2026 17:49:10 +0200 Subject: feature(main): during my work on other projects I improved the base library a bit. this is a drag and drop of that in the project. the next steps exit out of implementing lineair regression and attempting to calcualte what the value would be of a key in the btree... Signed-off-by: nasr feature(main): during my work on other projects I improved the base library a bit. this is a drag and drop of that in the project. the next steps exit out of implementing lineair regression and attempting to calcualte what the value would be of a key in the btree... Signed-off-by: nasr --- README.md | 4 + source/base/base.h | 37 +++------ source/base/base_arena.c | 3 +- source/base/base_arena.h | 5 ++ source/base/base_error.h | 89 ++++++++++++--------- source/base/base_hash.c | 12 --- source/base/base_include.h | 22 +++--- source/base/base_io.h | 60 -------------- source/base/base_os.h | 96 +++++++++++++++++++--- source/base/base_platform.h | 6 ++ source/base/base_rand.h | 28 +++++++ source/base/base_stack.c | 187 ------------------------------------------- source/base/base_stack.h | 188 ++++++++++++++++++++++++++++++++++++++++++++ source/base/base_string.c | 30 +++++++ source/base/base_string.h | 79 +++++++++++++------ source/base/base_test.h | 46 ----------- source/base/bash_hash.h | 19 +++-- source/tb_db/tb_ml.c | 9 --- source/tb_ml/tb_ml.c | 20 +++++ 19 files changed, 516 insertions(+), 424 deletions(-) delete mode 100644 source/base/base_hash.c delete mode 100644 source/base/base_io.h create mode 100644 source/base/base_platform.h create mode 100644 source/base/base_rand.h delete mode 100755 source/base/base_stack.c create mode 100644 source/base/base_string.c delete mode 100644 source/base/base_test.h delete mode 100644 source/tb_db/tb_ml.c create mode 100644 source/tb_ml/tb_ml.c diff --git a/README.md b/README.md index 7297d23..b059617 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,7 @@ TB_ML Attempt at writing a machine learning library in c using tb_db for the input data. + +pseudo random number generation +https://www.youtube.com/watch?v=OlSYfj8VZi0 +https://www.pcg-random.org/ diff --git a/source/base/base.h b/source/base/base.h index b9e317a..3fe9dca 100755 --- a/source/base/base.h +++ b/source/base/base.h @@ -2,28 +2,11 @@ #define BASE_H /* assert an expression and output the file and the line */ - -#define internal static +#define internal static #define global_variable static -#define local_persist static - -#define ERR_OK 0 -#define ERR_IO 1 -#define ERR_PARSE 2 -#define ERR_PERM 3 -#define ERR_INVALID 4 - -#define KiB(n) (((u64)(n)) << 10) -#define MiB(n) (((u64)(n)) << 20) -#define GiB(n) (((u64)(n)) << 30) +#define local_persist static #define unused(x) (void)(x) - -#define PATH_MAX_LEN 128 -#define BUFF_SMALL 128 -#define BUFF_DEFAULT 256 -#define BUFF_LARGE 512 - #define NIL 0 #define DEPRECATED __attribute__((__deprecated__)) @@ -67,15 +50,15 @@ typedef s8 b8; typedef uintptr_t umm; typedef intptr_t smm; -#define TRUE (1 == 1) -#define FALSE (1 != 1) +#define True (1 == 1) +#define False (1 != 1) -#define RED "\x1b[31m" -#define GREEN "\x1b[32m" -#define RESET "\x1b[0m" -#define BLUE "\x1b[34m" -#define YELLOW "\x1b[33m" +#define Red "\x1b[31m" +#define Green "\x1b[32m" +#define Reset "\x1b[0m" +#define Blue "\x1b[34m" +#define Yellow "\x1b[33m" -#define LEN(s) (sizeof(s) - 1) +#define Len(s) (sizeof(s) - 1) #endif diff --git a/source/base/base_arena.c b/source/base/base_arena.c index 7e9c44b..8fc8c13 100755 --- a/source/base/base_arena.c +++ b/source/base/base_arena.c @@ -110,8 +110,9 @@ arena_resize_align(mem_arena *arena, void *old_memory, u64 new_size, u64 old_siz } else { - verify(0); + // warn(0); } + return NULL; } diff --git a/source/base/base_arena.h b/source/base/base_arena.h index 2818ae4..dc6dd73 100755 --- a/source/base/base_arena.h +++ b/source/base/base_arena.h @@ -7,6 +7,11 @@ #define PushArray(arena, type, len) (type *)arena_alloc((arena), sizeof(type) * (len), 0) #define PushArrayZero(arena, type, len) (type *)arena_alloc((arena), sizeof(type) * (len), 1) +#define KiB(n) (((u64)(n)) << 10) +#define MiB(n) (((u64)(n)) << 20) +#define GiB(n) (((u64)(n)) << 30) + + typedef struct mem_arena mem_arena; struct mem_arena { diff --git a/source/base/base_error.h b/source/base/base_error.h index b2bb4c0..e9f52b7 100644 --- a/source/base/base_error.h +++ b/source/base/base_error.h @@ -1,47 +1,66 @@ -/* base library internal logging system */ +/* base library internal error checking system */ #ifndef BASE_ERROR_H #define BASE_ERROR_H -#define error_at(msg) \ - do \ - { \ - os_write(STDERR_FD, RED "[ERROR] ", LEN(RED "[ERROR] ")); \ - write_string(STDERR_FD, __FILE__); \ - write_string(STDERR_FD, ":"); \ - write_int(__LINE__); \ - write_string(STDERR_FD, " in "); \ - write_string(STDERR_FD, __func__); \ - write_string(STDERR_FD, ": "); \ - write_string(STDERR_FD, (msg)); \ - os_write(STDERR_FD, RESET "\n", LEN(RESET "\n")); \ +#define error_at(msg) \ + do { \ + write_string(STDERR_FD, Red "[ERROR] " __FILE__ ":"); \ + write_int(STDERR_FD, __LINE__); \ + write_string(STDERR_FD, ":" __func__ ": " Reset); \ + write_string(STDERR_FD, (msg)); \ + write_string(STDERR_FD, "\n"); \ } while (0) -#define fatal(msg) \ - do \ - { \ - error_at(msg); \ - _exit(1); \ +#define warn(msg) \ + do { \ + write_string(STDERR_FD, Yellow "[WARN] " __FILE__ ":"); \ + write_int(STDERR_FD, __LINE__); \ + write_string(STDERR_FD, ":" __func__ ": " Reset); \ + write_string(STDERR_FD, (msg)); \ + write_string(STDERR_FD, "\n"); \ } while (0) -#define assert_msg(expr, msg) \ - do \ - { \ - if (!(expr)) \ - { \ - fatal(msg); \ - } \ +#define assert_msg(expr, msg) \ + do { \ + if (!(expr)) { \ + write_string(STDERR_FD, Red "[ERROR] " __FILE__ ":"); \ + write_int(STDERR_FD, __LINE__); \ + write_string(STDERR_FD, ":" __func__ ": " Reset); \ + write_string(STDERR_FD, (msg)); \ + write_string(STDERR_FD, "\n"); \ + _exit(1); \ + } \ } while (0) -#define warn(msg) \ - do \ - { \ - os_write(STDERR_FD, YELLOW "[WARN] ", LEN(YELLOW "[WARN] ")); \ - write_string(STDERR_FD, __FILE__); \ - write_string(STDERR_FD, ":"); \ - write_int(__LINE__); \ - write_string(STDERR_FD, ": "); \ - write_string(STDERR_FD, (msg)); \ - os_write(STDERR_FD, RESET "\n", LEN(RESET "\n")); \ +#define show \ + do { \ + write_string(STDOUT_FD, __FILE__ ":"); \ + write_int(STDOUT_FD, __LINE__); \ + write_string(STDOUT_FD, ":" __func__ "\n"); \ + } while (0) + +#define test(expr) \ + do { \ + if ((expr) != 0) { \ + write_string(STDERR_FD, "[FAILED] " __FILE__ ":"); \ + write_int(STDERR_FD, __LINE__); \ + write_string(STDERR_FD, ":" __func__ "\n"); \ + _exit(1); \ + } \ + } while (0) + +#define verify(expr) \ + do { \ + if ((expr) != 0) { \ + write_string(STDERR_FD, Red "[ERROR] " __FILE__ ":"); \ + write_int(STDERR_FD, __LINE__); \ + write_string(STDERR_FD, ":" __func__ "\n" Reset); \ + _exit(1); \ + } else { \ + write_string(STDERR_FD, Green "[OK] " __FILE__ ":"); \ + write_int(STDERR_FD, __LINE__); \ + write_string(STDERR_FD, ":" __func__ "\n" Reset); \ + } \ } while (0) #endif /* BASE_ERROR_H */ diff --git a/source/base/base_hash.c b/source/base/base_hash.c deleted file mode 100644 index 1964441..0000000 --- a/source/base/base_hash.c +++ /dev/null @@ -1,12 +0,0 @@ - -internal u64 -generate_hash() -{ - - - -} - -internal hash_map -make_hash_map - diff --git a/source/base/base_include.h b/source/base/base_include.h index c8eec41..a376f29 100755 --- a/source/base/base_include.h +++ b/source/base/base_include.h @@ -9,26 +9,30 @@ #include #include #include +#include #include +#include +#include #include "base.h" #include "base_mem.h" + #include "base_arena.h" #include "base_stack.h" -#include "base_string.h" - -#include "base_io.h" -#include "base_error.h" -#include "base_test.h" - -#ifdef BASE_UNITY +#ifdef BASE_IMPLEMENTATION #include "base_arena.c" -#include "base_stack.c" - #endif + +#include "base_string.h" +#include "base_string.c" + #include "base_os.h" +#include "base_error.h" + +#include "base_rand.h" + #endif diff --git a/source/base/base_io.h b/source/base/base_io.h deleted file mode 100644 index ac55737..0000000 --- a/source/base/base_io.h +++ /dev/null @@ -1,60 +0,0 @@ -#ifndef BASE_IO_H -#define BASE_IO_H - -#define STDIN_FD 0 -#define STDOUT_FD 1 -#define STDERR_FD 2 - -internal s64 -os_write(s32 fd, void const *buf, u64 count) -{ - return syscall(SYS_write, fd, buf, count); -} - -internal s64 -os_read(s32 fd, void *buf, u64 count) -{ - return syscall(SYS_read, fd, buf, count); -} - -internal void -print_s8(string8 s) -{ - os_write(STDOUT_FILENO, s.data, s.size); -} - -internal void -print(const char *str) -{ - s32 len = 0; - while (str[len]) len++; - os_write(STDOUT_FILENO, str, len); - -} - -internal void -write_int(s32 num) -{ - - if (num < 0) - { - write(STDERR_FILENO, "-", 1); - num = -num; - } - if (num >= 10) - write_int(num / 10); - char digit = '0' + (num % 10); - - write(STDERR_FILENO, &digit, 1); -} - -internal void -write_string(s32 fd, const char *str) -{ - s32 len = 0; - while (str[len]) len++; - os_write(fd, str, len); -} - - -#endif /* BASE_IO_H */ diff --git a/source/base/base_os.h b/source/base/base_os.h index 388f665..82aa70d 100644 --- a/source/base/base_os.h +++ b/source/base/base_os.h @@ -1,28 +1,35 @@ #ifndef BASE_OS_H #define BASE_OS_H +#define STDIN_FD 0 +#define STDOUT_FD 1 +#define STDERR_FD 2 + +#endif /* BASE_OS_H */ + +#ifdef BASE_IMPLEMENTATION + internal string8 load_file(mem_arena *arena, const char *path) { string8 result = {0}; struct stat sbuf = {0}; + // TODO(nasr): abstract this to a platform layer s32 file = open(path, O_RDONLY); if(file == -1) { - warn("fialed to open file. path could be invalid"); return (string8){0}; } if(fstat(file, &sbuf) == -1) { - warn("error: fstat failed"); close(file); return (string8){0}; } - result = PushString(arena, sbuf.st_size); + result = PushString8(arena, sbuf.st_size); result.size = (u64)sbuf.st_size; if(result.size != 0) @@ -37,31 +44,102 @@ load_file(mem_arena *arena, const char *path) internal string8 write_file(const char *path, string8 data) { - string8 result = {0}; s32 file = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644); if(file == -1) { - warn("failed to open file for writing. path could be invalid"); return (string8){0}; } - + u64 written = 0; while(written < data.size) { s64 err = write(file, data.data + written, data.size - written); if(err == -1) { - warn("write syscall failed"); close(file); return (string8){0}; } written += err; } - + close(file); result = data; return result; } -#endif /* BASE_OS_H */ +#define os_write(buf, count) _os_write(STDIN_FD, buf, count) +#define os_read(buf, count) _os_read(STDIN_FD, buf, count) + +internal s64 +_os_write(s32 fd, void const *buf, u64 count) +{ + return syscall(SYS_write, fd, buf, count); +} + +internal s64 +_os_read(s32 fd, void *buf, u64 count) +{ + return syscall(SYS_read, fd, buf, count); +} + +internal void +log_s8(string8 s) +{ + os_write(s.data, s.size); +} + +#if 1 +internal void +_log(const char *str) +{ +#ifdef BASE_LOGGING + s32 len = 0; + while (str[len]) len++; + os_write(str, len); +#else + unused(str); +#endif + +} +#endif + +internal void +write_string(const char *str) +{ + s32 len = 0; + while (str[len]) len++; + os_write(str, len); +} + +internal void +write_int(s32 num) +{ + + if (num < 0) + { + write(STDERR_FILENO, "-", 1); + num = -num; + } + if (num >= 10) write_int(num / 10); + + char digit = '0' + (num % 10); + + write(STDERR_FILENO, &digit, 1); +} + +internal inline void +sleep_ms(long ms) +{ + struct timespec ts; + ts.tv_sec = ms / 1000; + ts.tv_nsec = (ms % 1000) * 1000000L; + + while (nanosleep(&ts, &ts)) + { + NULL; + } +} + + +#endif diff --git a/source/base/base_platform.h b/source/base/base_platform.h new file mode 100644 index 0000000..b34a7d0 --- /dev/null +++ b/source/base/base_platform.h @@ -0,0 +1,6 @@ +#ifndef BASE_PLATFORM_H +#define BASE_PLATFORM_H + +#ifdef BASE_PLATFORM_IMPLEMENTATION +#endif /* BASE_PLATFORM_IMPLEMENTATION */ +#endif /* BASE_PLATFORM_H */ diff --git a/source/base/base_rand.h b/source/base/base_rand.h new file mode 100644 index 0000000..bfdab0f --- /dev/null +++ b/source/base/base_rand.h @@ -0,0 +1,28 @@ +#ifndef BASE_RAND_H +#define BASE_RAND_H + +// source: https://rosettacode.org/wiki/Pseudo-random_numbers/Splitmix64 + +#define RAND_CONSTANT 6364136223846793005 +#define GEN_RAND() _generate_random_u64(RAND_CONSTANT) + +#endif /* BASE_RAND_H */ + +#ifdef BASE_RAND_IMPLEMENTATION +internal u64 +generate_random_u64(u64 constant) +{ + time_t current_time = time(0); + constant = current_time ^ constant; + + constant += 0x9e3779b97f4a7c15; + + u64 z = constant; + z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9; + z = (z ^ (z >> 27)) * 0x94d049bb133111eb; + z = z ^ (z >> 31); + + return z; +} + +#endif diff --git a/source/base/base_stack.c b/source/base/base_stack.c deleted file mode 100755 index 9c1218a..0000000 --- a/source/base/base_stack.c +++ /dev/null @@ -1,187 +0,0 @@ -internal mem_stack * -stack_create(u64 capacity) -{ - mem_stack *stack = (mem_stack *)mmap( - 0, - capacity + sizeof(mem_stack), - PROT_READ | PROT_WRITE, - MAP_SHARED | MAP_ANONYMOUS, - -1, - 0); - - if (stack == MAP_FAILED) - { - return NULL; - } - - stack->capacity = capacity; - stack->base_position = (u8 *)stack + sizeof(mem_stack); - stack->current_offset = 0; - - return stack; -} - -internal u8 -calculate_padding(u64 pointer, u8 alignment, u64 header_size) -{ - u8 modulo, padding; - - if (!is_pow(alignment)) - { - return 0; - } - - modulo = pointer & (u8)(alignment - 1); - - padding = 0; - - if (0 == modulo) - { - padding = alignment - modulo; - } - - if (padding < header_size) - { - header_size -= padding; - - if ((header_size & (alignment - 1)) != 0) - { - padding += alignment * (1 + (header_size / alignment)); - } - else - { - padding += alignment * (header_size / alignment); - } - } - - return padding; -} - -internal mem_stack * -stack_push_align(mem_stack *stack, u64 size, u8 alignment) -{ - u8 padding = 0; - - if (!is_pow(alignment)) - { - return (0); - } - - if (alignment > 128) - { - alignment = 128; - } - - u64 current_address = (u64)stack->base_position + stack->current_offset; - padding = calculate_padding(current_address, alignment, sizeof(mem_stack_header)); - - if (stack->current_offset + padding + size > stack->capacity) - { - return 0; - } - - stack->current_offset += padding; - - u64 next_address = current_address + (u64)padding; - mem_stack_header *header = (mem_stack_header *)(next_address - sizeof(mem_stack_header)); - header->padding = padding; - - stack->current_offset += size; - - return MemSet((void *)next_address, size); -} -internal void * -stack_push(mem_stack *stack, u64 size) -{ - return stack_push_align(stack, size, ARENA_ALIGN); -} - -internal void -stack_pop(mem_stack *stack, void *pointer) -{ - if (pointer != NULL) - { - u64 start, end, current_address; - mem_stack_header *header; - u64 prev_offset; - - start = (u64)stack->base_position; - end = start + (u64)stack->capacity; - current_address = (u64)pointer; - - if (!(start <= current_address && current_address < end)) - { - if (0 && "Out of bounds memory address passed to stack allocator (free)") - { - return; - } - return; - } - - if (current_address >= start + (u64)stack->base_position) - { - return; - } - - header = (mem_stack_header *)(current_address - sizeof(mem_stack_header)); - prev_offset = (size_t)(current_address - (u64)header->padding - start); - stack->current_offset = prev_offset; - } -} - -internal mem_stack * -stack_resize_align(mem_stack *stack, void *pointer, u64 old_size, u64 new_size, u8 alignment) -{ - if (pointer == NULL) - { - return stack_push_align(stack, new_size, alignment); - } - else if (new_size == 0) - { - stack_pop(stack, pointer); - return NULL; - } - - u64 start, end, current_address; - u64 min_size = old_size < new_size ? old_size : new_size; - void *new_pointer; - - start = (u64)stack->base_position; - end = start + (u64)stack->capacity; - current_address = (u64)pointer; - if (!(start <= current_address && current_address < end)) - { - return NULL; - } - - if (current_address >= start + (u64)stack->current_offset) - { - return NULL; - } - - if (old_size == new_size) - { - return pointer; - } - - new_pointer = stack_push_align(stack, new_size, alignment); - memmove(new_pointer, pointer, min_size); - return new_pointer; -} - -internal void -stack_pop_all(mem_stack *stack) -{ - stack->current_offset = 0; -} - -internal void -stack_destroy(mem_stack *stack) -{ - if (!stack) - { - return; - } - - munmap(stack, stack->capacity + sizeof(mem_stack)); -} diff --git a/source/base/base_stack.h b/source/base/base_stack.h index 54d61d3..43a7230 100755 --- a/source/base/base_stack.h +++ b/source/base/base_stack.h @@ -19,4 +19,192 @@ struct mem_stack u8 *base_position; }; +internal mem_stack * +stack_create(u64 capacity) +{ + mem_stack *stack = (mem_stack *)mmap( + 0, + capacity + sizeof(mem_stack), + PROT_READ | PROT_WRITE, + MAP_SHARED | MAP_ANONYMOUS, + -1, + 0); + + if (stack == MAP_FAILED) + { + return NULL; + } + + stack->capacity = capacity; + stack->base_position = (u8 *)stack + sizeof(mem_stack); + stack->current_offset = 0; + + return stack; +} + +internal u8 +calculate_padding(u64 pointer, u8 alignment, u64 header_size) +{ + u8 modulo, padding; + + if (!is_pow(alignment)) + { + return 0; + } + + modulo = pointer & (u8)(alignment - 1); + + padding = 0; + + if (0 == modulo) + { + padding = alignment - modulo; + } + + if (padding < header_size) + { + header_size -= padding; + + if ((header_size & (alignment - 1)) != 0) + { + padding += alignment * (1 + (header_size / alignment)); + } + else + { + padding += alignment * (header_size / alignment); + } + } + + return padding; +} + +internal mem_stack * +stack_push_align(mem_stack *stack, u64 size, u8 alignment) +{ + u8 padding = 0; + + if (!is_pow(alignment)) + { + return (0); + } + + if (alignment > 128) + { + alignment = 128; + } + + u64 current_address = (u64)stack->base_position + stack->current_offset; + padding = calculate_padding(current_address, alignment, sizeof(mem_stack_header)); + + if (stack->current_offset + padding + size > stack->capacity) + { + return 0; + } + + stack->current_offset += padding; + + u64 next_address = current_address + (u64)padding; + mem_stack_header *header = (mem_stack_header *)(next_address - sizeof(mem_stack_header)); + header->padding = padding; + + stack->current_offset += size; + + return MemSet((void *)next_address, size); +} +internal void * +stack_push(mem_stack *stack, u64 size) +{ + return stack_push_align(stack, size, ARENA_ALIGN); +} + +internal void +stack_pop(mem_stack *stack, void *pointer) +{ + if (pointer != NULL) + { + u64 start, end, current_address; + mem_stack_header *header; + u64 prev_offset; + + start = (u64)stack->base_position; + end = start + (u64)stack->capacity; + current_address = (u64)pointer; + + if (!(start <= current_address && current_address < end)) + { + if (0 && "Out of bounds memory address passed to stack allocator (free)") + { + return; + } + return; + } + + if (current_address >= start + (u64)stack->base_position) + { + return; + } + + header = (mem_stack_header *)(current_address - sizeof(mem_stack_header)); + prev_offset = (size_t)(current_address - (u64)header->padding - start); + stack->current_offset = prev_offset; + } +} + +internal mem_stack * +stack_resize_align(mem_stack *stack, void *pointer, u64 old_size, u64 new_size, u8 alignment) +{ + if (pointer == NULL) + { + return stack_push_align(stack, new_size, alignment); + } + else if (new_size == 0) + { + stack_pop(stack, pointer); + return NULL; + } + + u64 start, end, current_address; + u64 min_size = old_size < new_size ? old_size : new_size; + void *new_pointer; + + start = (u64)stack->base_position; + end = start + (u64)stack->capacity; + current_address = (u64)pointer; + if (!(start <= current_address && current_address < end)) + { + return NULL; + } + + if (current_address >= start + (u64)stack->current_offset) + { + return NULL; + } + + if (old_size == new_size) + { + return pointer; + } + + new_pointer = stack_push_align(stack, new_size, alignment); + memmove(new_pointer, pointer, min_size); + return new_pointer; +} + +internal void +stack_pop_all(mem_stack *stack) +{ + stack->current_offset = 0; +} + +internal void +stack_destroy(mem_stack *stack) +{ + if (!stack) + { + return; + } + + munmap(stack, stack->capacity + sizeof(mem_stack)); +} + #endif diff --git a/source/base/base_string.c b/source/base/base_string.c new file mode 100644 index 0000000..986fde5 --- /dev/null +++ b/source/base/base_string.c @@ -0,0 +1,30 @@ +internal b32 +is_alpha(u8 point) +{ + return ((point >= 'a' && point <= 'z') || (point >= 'A' && point <= 'Z') || (point == '_')); +} + +internal b32 +is_digit(u8 point) +{ + return (point >= '0' && point <= '9'); +} + +internal b32 +is_alpha_num(u8 point) +{ + return (is_alpha(point) || is_digit(point)); +} + +internal b32 is_whitespace(u8 point) +{ + return (point == '\n' || point == '\r' || point == ' ' || point == '\t'); +} + +internal b32 +is_slash(u8 point) +{ + return (point == '/' || point == '\\'); +} + + diff --git a/source/base/base_string.h b/source/base/base_string.h index 29ccf1e..eb51e65 100644 --- a/source/base/base_string.h +++ b/source/base/base_string.h @@ -1,9 +1,15 @@ #ifndef BASE_STRING_H #define BASE_STRING_H -#define PushString(arena, count) (string8){ .data = (PushArrayZero(arena, u8, (count))), .size = (count) } -#define StringCast(data, size) (string8){(u8 *)(data), (u64)(size) } -#define StringPCast(data, size) (string8 *){(u8 *)(data), (u64)(size) } +#define PushString8(arena, count) (string8){ .data = (PushArrayZero(arena, u8, (count))), .size = (count) } +#define PushString16(arena, count) (string16){ .data = (PushArrayZero(arena, u16, (count))), .size = (count) } +#define PushString32(arena, count) (string32){ .data = (PushArrayZero(arena, u32, (count))), .size = (count) } + +#define String8(data, size) (string8){(u8 *)(data), (u64)(size) } +#define String16(data, size) (string16){(u16 *)(data), (u64)(size) } +#define String32(data, size) (string32){(u32 *)(data), (u64)(size) } + + #define StringFmt "%.*s" #define ULongFmt "%lu" @@ -16,6 +22,51 @@ struct string8 u64 size; }; +typedef struct string16 string16; +struct string16 +{ + u16 *data; + u64 size; +}; + +typedef struct string32 string32; +struct string32 +{ + u32 *data; + u64 size; +}; + +//- string linked list implementation +typedef struct string8_node string8_node; +struct string8_node +{ + string8 *next; + string8 string; +}; + +typedef struct string8_list string8_list; +struct string8_list +{ + string8 *first; + string8 *last; + u64 count; +}; + +typedef struct string16_list string16_list; +struct string16_list +{ + string16 *next; + string16 string; +}; + +typedef struct string32_list string32_list; +struct string32_list +{ + string32 *first; + string32 *last; + u64 count; +}; + internal b8 string8_cmp(string8 a, string8 b) { @@ -23,31 +74,11 @@ string8_cmp(string8 a, string8 b) return (b8)(memcmp(a.data, b.data, a.size) == 0); } -internal u64 -string8_to_u64(u8 *buf, umm len) -{ - u64 value = 0; - for (umm i = 0; i < len; ++i) - { - u8 c = buf[i]; - if (c < '0' || c > '9') break; - value = value * 10 + (c - '0'); - } - return value; -} - internal void -string8_append_char(string8 *buf, u8 c) +string8_appendc(string8 *buf, u8 c) { buf->data[buf->size] = c; buf->size += 1; } -read_only global_variable -string8 nil_string = -{ - .data = NULL, - .size = 0, -}; - #endif /* BASE_STRING_H */ diff --git a/source/base/base_test.h b/source/base/base_test.h deleted file mode 100644 index fd47abc..0000000 --- a/source/base/base_test.h +++ /dev/null @@ -1,46 +0,0 @@ -// TODO(nasr): metaprogram that takes an expected output and generates a test for that specified -// function -/* base library testing framework */ -#ifndef BASE_TEST_H -#define BASE_TEST_H - -// helper macro -#define show \ - do \ - { \ - write(STDOUT_FILENO, __FILE__, sizeof(__FILE__) - 1); \ - write(STDOUT_FILENO, ":", 1); \ - write(STDOUT_FILENO, __func__, sizeof(__func__) - 1); \ - write(STDOUT_FILENO, ":", 1); \ - write_int(__LINE__); \ - write(STDOUT_FILENO, "\n", 1); \ - } while (0) - -#define test(expr) \ - { \ - if ((expr) != 0) \ - { \ - write(STDERR_FILENO, "[FAILED] ", LEN("[FAILED] ")); \ - show; \ - _exit(1); \ - } \ - } - -#define verify(expr) \ - { \ - if ((expr) != 0) \ - { \ - write(STDERR_FILENO, RED "[ERROR] ", LEN(RED "[ERROR] ")); \ - show; \ - write(STDERR_FILENO, RESET, LEN(RESET)); \ - _exit(1); \ - } \ - else \ - { \ - write(STDERR_FILENO, GREEN "[SUCCESS] ", LEN(GREEN "[SUCCESS] ")); \ - show; \ - write(STDERR_FILENO, RESET, LEN(RESET)); \ - } \ - } - -#endif /* BASE_TEST_H */ diff --git a/source/base/bash_hash.h b/source/base/bash_hash.h index 2c286a2..758ef72 100644 --- a/source/base/bash_hash.h +++ b/source/base/bash_hash.h @@ -1,15 +1,24 @@ -##ifndef HEADER_H -#define HEADER_H +#ifndef BASH_HASH_H +#define BASH_HASH_H typedef struct hash_map hash_map; typedef struct hash hash; - -struct hash_map +struct map { + string8 + u64 capacity +}; +struct index +{ + string8 key; + string8 value; }; -#endif /* HEADER_H */ + +internal void + +#endif /* BASH_HASH_H */ diff --git a/source/tb_db/tb_ml.c b/source/tb_db/tb_ml.c deleted file mode 100644 index a86303d..0000000 --- a/source/tb_db/tb_ml.c +++ /dev/null @@ -1,9 +0,0 @@ -#define BASE_UNITY -#include "../base/base_include.h" - -int main(int c, char **v) -{ - - - return 0; -} diff --git a/source/tb_ml/tb_ml.c b/source/tb_ml/tb_ml.c new file mode 100644 index 0000000..cf9625a --- /dev/null +++ b/source/tb_ml/tb_ml.c @@ -0,0 +1,20 @@ +#define BASE_UNITY +#include "../base/base_include.h" + +#if 0 + +#include "../third_party/btree_impl.h" +#endif + + + +//- dataset is an implementation of the btree + +int main(int c, char **v) +{ + unused(c); + unused(v); + + + return 0; +} -- cgit v1.3