diff options
| author | nasr <nsrddyn@gmail.com> | 2026-04-13 15:33:05 +0200 |
|---|---|---|
| committer | nasr <nsrddyn@gmail.com> | 2026-04-13 15:36:24 +0200 |
| commit | 9d09d66a273f68fae7efb71504bf40c664b91983 (patch) | |
| tree | 41a46c52a01338bf22d5f3ebdf0bb27dc3d33cc1 /source | |
feature(main): init
feature(main): init
feature(main): init
feature(main): init
Diffstat (limited to 'source')
| -rwxr-xr-x | source/base/base.h | 81 | ||||
| -rwxr-xr-x | source/base/base_arena.c | 138 | ||||
| -rwxr-xr-x | source/base/base_arena.h | 26 | ||||
| -rw-r--r-- | source/base/base_error.h | 47 | ||||
| -rw-r--r-- | source/base/base_hash.c | 12 | ||||
| -rwxr-xr-x | source/base/base_include.h | 34 | ||||
| -rw-r--r-- | source/base/base_io.h | 60 | ||||
| -rw-r--r-- | source/base/base_mem.h | 26 | ||||
| -rw-r--r-- | source/base/base_os.h | 67 | ||||
| -rwxr-xr-x | source/base/base_stack.c | 187 | ||||
| -rwxr-xr-x | source/base/base_stack.h | 22 | ||||
| -rw-r--r-- | source/base/base_string.h | 53 | ||||
| -rw-r--r-- | source/base/base_test.h | 46 | ||||
| -rw-r--r-- | source/base/bash_hash.h | 15 | ||||
| -rw-r--r-- | source/tb_db/tb_ml.c | 9 |
15 files changed, 823 insertions, 0 deletions
diff --git a/source/base/base.h b/source/base/base.h new file mode 100755 index 0000000..b9e317a --- /dev/null +++ b/source/base/base.h | |||
| @@ -0,0 +1,81 @@ | |||
| 1 | #ifndef BASE_H | ||
| 2 | #define BASE_H | ||
| 3 | |||
| 4 | /* assert an expression and output the file and the line */ | ||
| 5 | |||
| 6 | #define internal static | ||
| 7 | #define global_variable static | ||
| 8 | #define local_persist static | ||
| 9 | |||
| 10 | #define ERR_OK 0 | ||
| 11 | #define ERR_IO 1 | ||
| 12 | #define ERR_PARSE 2 | ||
| 13 | #define ERR_PERM 3 | ||
| 14 | #define ERR_INVALID 4 | ||
| 15 | |||
| 16 | #define KiB(n) (((u64)(n)) << 10) | ||
| 17 | #define MiB(n) (((u64)(n)) << 20) | ||
| 18 | #define GiB(n) (((u64)(n)) << 30) | ||
| 19 | |||
| 20 | #define unused(x) (void)(x) | ||
| 21 | |||
| 22 | #define PATH_MAX_LEN 128 | ||
| 23 | #define BUFF_SMALL 128 | ||
| 24 | #define BUFF_DEFAULT 256 | ||
| 25 | #define BUFF_LARGE 512 | ||
| 26 | |||
| 27 | #define NIL 0 | ||
| 28 | |||
| 29 | #define DEPRECATED __attribute__((__deprecated__)) | ||
| 30 | |||
| 31 | #if defined(__arm__) || defined(__aarch64__) | ||
| 32 | #define breakpoint __asm__ volatile("brk #0"); | ||
| 33 | #define temp_breakpoint __asm__ volatile("udf #0"); | ||
| 34 | #elif defined(__i386__) || defined(__x86_64__) | ||
| 35 | #define breakpoint __asm__ volatile("int3"); | ||
| 36 | #endif | ||
| 37 | |||
| 38 | #define MemCpy(dest, src, len) memcpy((dest), (src), (len)) | ||
| 39 | #define MemSet(dest, len) memset((dest), (0), (len)) | ||
| 40 | |||
| 41 | #if COMPILER_MSVC || (COMPILER_CLANG && OS_WINDOWS) | ||
| 42 | #pragma section(".rdata$", read) | ||
| 43 | #define read_only __declspec(allocate(".rdata$")) | ||
| 44 | #elif (COMPILER_CLANG && OS_LINUX) | ||
| 45 | #define read_only __attribute__((section(".rodata"))) | ||
| 46 | #else | ||
| 47 | #define read_only | ||
| 48 | #endif | ||
| 49 | |||
| 50 | typedef uint64_t u64; | ||
| 51 | typedef uint32_t u32; | ||
| 52 | typedef uint16_t u16; | ||
| 53 | typedef uint8_t u8; | ||
| 54 | |||
| 55 | typedef int8_t s8; | ||
| 56 | typedef int16_t s16; | ||
| 57 | typedef int32_t s32; | ||
| 58 | typedef int64_t s64; | ||
| 59 | |||
| 60 | typedef float f32; | ||
| 61 | typedef double f64; | ||
| 62 | |||
| 63 | typedef s32 b32; | ||
| 64 | typedef s16 b16; | ||
| 65 | typedef s8 b8; | ||
| 66 | |||
| 67 | typedef uintptr_t umm; | ||
| 68 | typedef intptr_t smm; | ||
| 69 | |||
| 70 | #define TRUE (1 == 1) | ||
| 71 | #define FALSE (1 != 1) | ||
| 72 | |||
| 73 | #define RED "\x1b[31m" | ||
| 74 | #define GREEN "\x1b[32m" | ||
| 75 | #define RESET "\x1b[0m" | ||
| 76 | #define BLUE "\x1b[34m" | ||
| 77 | #define YELLOW "\x1b[33m" | ||
| 78 | |||
| 79 | #define LEN(s) (sizeof(s) - 1) | ||
| 80 | |||
| 81 | #endif | ||
diff --git a/source/base/base_arena.c b/source/base/base_arena.c new file mode 100755 index 0000000..7e9c44b --- /dev/null +++ b/source/base/base_arena.c | |||
| @@ -0,0 +1,138 @@ | |||
| 1 | internal mem_arena * | ||
| 2 | arena_create(u64 capacity) | ||
| 3 | { | ||
| 4 | mem_arena *arena = (mem_arena *)mmap( | ||
| 5 | /* kernel decides where to throw the arena */ | ||
| 6 | NULL, | ||
| 7 | capacity + sizeof(mem_arena), | ||
| 8 | PROT_READ | PROT_WRITE, | ||
| 9 | MAP_SHARED | MAP_ANONYMOUS, | ||
| 10 | -1, | ||
| 11 | 0); | ||
| 12 | |||
| 13 | if (arena == MAP_FAILED) | ||
| 14 | { | ||
| 15 | return NULL; | ||
| 16 | } | ||
| 17 | |||
| 18 | arena->capacity = capacity; | ||
| 19 | arena->base_position = (u8 *)arena + sizeof(mem_arena); | ||
| 20 | arena->current_position = 0; | ||
| 21 | arena->previous_position = 0; | ||
| 22 | |||
| 23 | return arena; | ||
| 24 | } | ||
| 25 | |||
| 26 | internal void | ||
| 27 | arena_destroy(mem_arena *arena) | ||
| 28 | { | ||
| 29 | if (!arena) | ||
| 30 | { | ||
| 31 | return; | ||
| 32 | } | ||
| 33 | munmap(arena, arena->capacity + sizeof(mem_arena)); | ||
| 34 | } | ||
| 35 | internal void * | ||
| 36 | arena_alloc(mem_arena *arena, u64 size, b32 zero) | ||
| 37 | { | ||
| 38 | if (!arena) | ||
| 39 | { | ||
| 40 | return NULL; | ||
| 41 | } | ||
| 42 | u64 aligned = Align(arena->current_position, ARENA_ALIGN); | ||
| 43 | u64 new_pos = aligned + size; | ||
| 44 | if (new_pos > arena->capacity) | ||
| 45 | { | ||
| 46 | return NULL; | ||
| 47 | } | ||
| 48 | |||
| 49 | void *out = arena->base_position + aligned; | ||
| 50 | |||
| 51 | arena->previous_position = arena->current_position; | ||
| 52 | arena->current_position = aligned + size; | ||
| 53 | |||
| 54 | if (zero) MemSet(out, size); | ||
| 55 | |||
| 56 | return out; | ||
| 57 | } | ||
| 58 | |||
| 59 | internal void | ||
| 60 | arena_pop(mem_arena *arena, u64 size) | ||
| 61 | { | ||
| 62 | size = MIN(size, arena->current_position); | ||
| 63 | arena->current_position -= size; | ||
| 64 | } | ||
| 65 | |||
| 66 | internal void | ||
| 67 | arena_pop_to(mem_arena *arena, u64 pos) | ||
| 68 | { | ||
| 69 | u64 size = pos < arena->current_position ? arena->current_position - pos : 0; | ||
| 70 | arena_pop(arena, size); | ||
| 71 | } | ||
| 72 | |||
| 73 | internal void | ||
| 74 | arena_clear(mem_arena *arena) | ||
| 75 | { | ||
| 76 | arena->current_position = 0; | ||
| 77 | } | ||
| 78 | |||
| 79 | internal mem_arena * | ||
| 80 | arena_resize_align(mem_arena *arena, void *old_memory, u64 new_size, u64 old_size, umm alignment) | ||
| 81 | { | ||
| 82 | u8 *old_mem = (u8 *)old_memory; | ||
| 83 | |||
| 84 | if (!is_pow(alignment)) | ||
| 85 | { | ||
| 86 | Align(arena->current_position, alignment); | ||
| 87 | } | ||
| 88 | |||
| 89 | if (old_memory == NULL || old_size == 0) | ||
| 90 | { | ||
| 91 | return (mem_arena *)arena_alloc(arena, new_size, 0); | ||
| 92 | } | ||
| 93 | else if ((old_mem >= arena->base_position && old_mem < arena->base_position + arena->capacity)) | ||
| 94 | { | ||
| 95 | if ((arena->base_position + arena->previous_position) == old_memory) | ||
| 96 | { | ||
| 97 | arena->current_position = arena->previous_position + new_size; | ||
| 98 | if (new_size > old_size) | ||
| 99 | { | ||
| 100 | MemSet(&arena->current_position, new_size - old_size); | ||
| 101 | } | ||
| 102 | return (mem_arena *)old_memory; | ||
| 103 | } | ||
| 104 | else | ||
| 105 | { | ||
| 106 | void *new_memory = arena_alloc(arena, new_size, 0); | ||
| 107 | umm copy_size = old_size < new_size ? old_size : new_size; | ||
| 108 | memmove(new_memory, old_mem, copy_size); | ||
| 109 | } | ||
| 110 | } | ||
| 111 | else | ||
| 112 | { | ||
| 113 | verify(0); | ||
| 114 | } | ||
| 115 | return NULL; | ||
| 116 | } | ||
| 117 | |||
| 118 | internal mem_arena * | ||
| 119 | arena_resize(mem_arena *arena, void *old_memory, u64 new_size, u64 old_size) | ||
| 120 | { | ||
| 121 | return arena_resize_align(arena, old_memory, new_size, old_size, ARENA_ALIGN); | ||
| 122 | } | ||
| 123 | |||
| 124 | internal temp_arena | ||
| 125 | temp_arena_begin(mem_arena *arena) | ||
| 126 | { | ||
| 127 | temp_arena t; | ||
| 128 | t.arena = arena; | ||
| 129 | t.start_position = arena->current_position; | ||
| 130 | |||
| 131 | return t; | ||
| 132 | } | ||
| 133 | |||
| 134 | internal void | ||
| 135 | temp_arena_end(temp_arena temp) | ||
| 136 | { | ||
| 137 | temp.arena->current_position = temp.start_position; | ||
| 138 | } | ||
diff --git a/source/base/base_arena.h b/source/base/base_arena.h new file mode 100755 index 0000000..2818ae4 --- /dev/null +++ b/source/base/base_arena.h | |||
| @@ -0,0 +1,26 @@ | |||
| 1 | #ifndef BASE_ARENA_H | ||
| 2 | #define BASE_ARENA_H | ||
| 3 | |||
| 4 | #define Align(pointer, alignment) align((u64)(pointer), (umm)(alignment)) | ||
| 5 | #define PushStruct(arena, type) (type *)arena_alloc((arena), sizeof(type), 0) | ||
| 6 | #define PushStructZero(arena, type) (type *)arena_alloc((arena), sizeof(type), 1) | ||
| 7 | #define PushArray(arena, type, len) (type *)arena_alloc((arena), sizeof(type) * (len), 0) | ||
| 8 | #define PushArrayZero(arena, type, len) (type *)arena_alloc((arena), sizeof(type) * (len), 1) | ||
| 9 | |||
| 10 | typedef struct mem_arena mem_arena; | ||
| 11 | struct mem_arena | ||
| 12 | { | ||
| 13 | u64 current_position; | ||
| 14 | u64 previous_position; | ||
| 15 | u64 capacity; | ||
| 16 | u8 *base_position; | ||
| 17 | }; | ||
| 18 | |||
| 19 | typedef struct temp_arena temp_arena; | ||
| 20 | struct temp_arena | ||
| 21 | { | ||
| 22 | mem_arena *arena; | ||
| 23 | u64 start_position; | ||
| 24 | }; | ||
| 25 | |||
| 26 | #endif /* BASE_ARENA_H */ | ||
diff --git a/source/base/base_error.h b/source/base/base_error.h new file mode 100644 index 0000000..b2bb4c0 --- /dev/null +++ b/source/base/base_error.h | |||
| @@ -0,0 +1,47 @@ | |||
| 1 | /* base library internal logging system */ | ||
| 2 | #ifndef BASE_ERROR_H | ||
| 3 | #define BASE_ERROR_H | ||
| 4 | |||
| 5 | #define error_at(msg) \ | ||
| 6 | do \ | ||
| 7 | { \ | ||
| 8 | os_write(STDERR_FD, RED "[ERROR] ", LEN(RED "[ERROR] ")); \ | ||
| 9 | write_string(STDERR_FD, __FILE__); \ | ||
| 10 | write_string(STDERR_FD, ":"); \ | ||
| 11 | write_int(__LINE__); \ | ||
| 12 | write_string(STDERR_FD, " in "); \ | ||
| 13 | write_string(STDERR_FD, __func__); \ | ||
| 14 | write_string(STDERR_FD, ": "); \ | ||
| 15 | write_string(STDERR_FD, (msg)); \ | ||
| 16 | os_write(STDERR_FD, RESET "\n", LEN(RESET "\n")); \ | ||
| 17 | } while (0) | ||
| 18 | |||
| 19 | #define fatal(msg) \ | ||
| 20 | do \ | ||
| 21 | { \ | ||
| 22 | error_at(msg); \ | ||
| 23 | _exit(1); \ | ||
| 24 | } while (0) | ||
| 25 | |||
| 26 | #define assert_msg(expr, msg) \ | ||
| 27 | do \ | ||
| 28 | { \ | ||
| 29 | if (!(expr)) \ | ||
| 30 | { \ | ||
| 31 | fatal(msg); \ | ||
| 32 | } \ | ||
| 33 | } while (0) | ||
| 34 | |||
| 35 | #define warn(msg) \ | ||
| 36 | do \ | ||
| 37 | { \ | ||
| 38 | os_write(STDERR_FD, YELLOW "[WARN] ", LEN(YELLOW "[WARN] ")); \ | ||
| 39 | write_string(STDERR_FD, __FILE__); \ | ||
| 40 | write_string(STDERR_FD, ":"); \ | ||
| 41 | write_int(__LINE__); \ | ||
| 42 | write_string(STDERR_FD, ": "); \ | ||
| 43 | write_string(STDERR_FD, (msg)); \ | ||
| 44 | os_write(STDERR_FD, RESET "\n", LEN(RESET "\n")); \ | ||
| 45 | } while (0) | ||
| 46 | |||
| 47 | #endif /* BASE_ERROR_H */ | ||
diff --git a/source/base/base_hash.c b/source/base/base_hash.c new file mode 100644 index 0000000..1964441 --- /dev/null +++ b/source/base/base_hash.c | |||
| @@ -0,0 +1,12 @@ | |||
| 1 | |||
| 2 | internal u64 | ||
| 3 | generate_hash() | ||
| 4 | { | ||
| 5 | |||
| 6 | |||
| 7 | |||
| 8 | } | ||
| 9 | |||
| 10 | internal hash_map | ||
| 11 | make_hash_map | ||
| 12 | |||
diff --git a/source/base/base_include.h b/source/base/base_include.h new file mode 100755 index 0000000..c8eec41 --- /dev/null +++ b/source/base/base_include.h | |||
| @@ -0,0 +1,34 @@ | |||
| 1 | #ifndef BASE_INCLUDE_H | ||
| 2 | #define BASE_INCLUDE_H | ||
| 3 | |||
| 4 | #include <dirent.h> | ||
| 5 | #include <sys/mman.h> | ||
| 6 | #include <sys/stat.h> | ||
| 7 | #include <sys/syscall.h> | ||
| 8 | #include <fcntl.h> | ||
| 9 | #include <stdint.h> | ||
| 10 | #include <stddef.h> | ||
| 11 | #include <string.h> | ||
| 12 | #include <unistd.h> | ||
| 13 | |||
| 14 | #include "base.h" | ||
| 15 | #include "base_mem.h" | ||
| 16 | #include "base_arena.h" | ||
| 17 | #include "base_stack.h" | ||
| 18 | #include "base_string.h" | ||
| 19 | |||
| 20 | #include "base_io.h" | ||
| 21 | #include "base_error.h" | ||
| 22 | #include "base_test.h" | ||
| 23 | |||
| 24 | #ifdef BASE_UNITY | ||
| 25 | |||
| 26 | #include "base_arena.c" | ||
| 27 | #include "base_stack.c" | ||
| 28 | |||
| 29 | #endif | ||
| 30 | |||
| 31 | #include "base_os.h" | ||
| 32 | |||
| 33 | |||
| 34 | #endif | ||
diff --git a/source/base/base_io.h b/source/base/base_io.h new file mode 100644 index 0000000..ac55737 --- /dev/null +++ b/source/base/base_io.h | |||
| @@ -0,0 +1,60 @@ | |||
| 1 | #ifndef BASE_IO_H | ||
| 2 | #define BASE_IO_H | ||
| 3 | |||
| 4 | #define STDIN_FD 0 | ||
| 5 | #define STDOUT_FD 1 | ||
| 6 | #define STDERR_FD 2 | ||
| 7 | |||
| 8 | internal s64 | ||
| 9 | os_write(s32 fd, void const *buf, u64 count) | ||
| 10 | { | ||
| 11 | return syscall(SYS_write, fd, buf, count); | ||
| 12 | } | ||
| 13 | |||
| 14 | internal s64 | ||
| 15 | os_read(s32 fd, void *buf, u64 count) | ||
| 16 | { | ||
| 17 | return syscall(SYS_read, fd, buf, count); | ||
| 18 | } | ||
| 19 | |||
| 20 | internal void | ||
| 21 | print_s8(string8 s) | ||
| 22 | { | ||
| 23 | os_write(STDOUT_FILENO, s.data, s.size); | ||
| 24 | } | ||
| 25 | |||
| 26 | internal void | ||
| 27 | print(const char *str) | ||
| 28 | { | ||
| 29 | s32 len = 0; | ||
| 30 | while (str[len]) len++; | ||
| 31 | os_write(STDOUT_FILENO, str, len); | ||
| 32 | |||
| 33 | } | ||
| 34 | |||
| 35 | internal void | ||
| 36 | write_int(s32 num) | ||
| 37 | { | ||
| 38 | |||
| 39 | if (num < 0) | ||
| 40 | { | ||
| 41 | write(STDERR_FILENO, "-", 1); | ||
| 42 | num = -num; | ||
| 43 | } | ||
| 44 | if (num >= 10) | ||
| 45 | write_int(num / 10); | ||
| 46 | char digit = '0' + (num % 10); | ||
| 47 | |||
| 48 | write(STDERR_FILENO, &digit, 1); | ||
| 49 | } | ||
| 50 | |||
| 51 | internal void | ||
| 52 | write_string(s32 fd, const char *str) | ||
| 53 | { | ||
| 54 | s32 len = 0; | ||
| 55 | while (str[len]) len++; | ||
| 56 | os_write(fd, str, len); | ||
| 57 | } | ||
| 58 | |||
| 59 | |||
| 60 | #endif /* BASE_IO_H */ | ||
diff --git a/source/base/base_mem.h b/source/base/base_mem.h new file mode 100644 index 0000000..2778fce --- /dev/null +++ b/source/base/base_mem.h | |||
| @@ -0,0 +1,26 @@ | |||
| 1 | #ifndef BASE_MEM_H | ||
| 2 | #define BASE_MEM_H | ||
| 3 | |||
| 4 | #define ARENA_ALIGN (2 * sizeof(void *)) | ||
| 5 | #define MIN(a, b) (((a) < (b)) ? (a) : (b)) | ||
| 6 | #define MAX(a, b) (((a) > (b)) ? (a) : (b)) | ||
| 7 | |||
| 8 | internal inline b8 | ||
| 9 | is_pow(umm x) | ||
| 10 | { | ||
| 11 | return (x & (x - 1)) == 0; | ||
| 12 | } | ||
| 13 | |||
| 14 | internal inline u64 | ||
| 15 | align(u64 pointer, umm alignment) | ||
| 16 | { | ||
| 17 | if ((alignment & (alignment - 1)) == 0) | ||
| 18 | { | ||
| 19 | return pointer; | ||
| 20 | } | ||
| 21 | |||
| 22 | return (pointer + alignment - 1) & ~(alignment - 1); | ||
| 23 | } | ||
| 24 | |||
| 25 | |||
| 26 | #endif | ||
diff --git a/source/base/base_os.h b/source/base/base_os.h new file mode 100644 index 0000000..388f665 --- /dev/null +++ b/source/base/base_os.h | |||
| @@ -0,0 +1,67 @@ | |||
| 1 | #ifndef BASE_OS_H | ||
| 2 | #define BASE_OS_H | ||
| 3 | |||
| 4 | internal string8 | ||
| 5 | load_file(mem_arena *arena, const char *path) | ||
| 6 | { | ||
| 7 | string8 result = {0}; | ||
| 8 | struct stat sbuf = {0}; | ||
| 9 | |||
| 10 | s32 file = open(path, O_RDONLY); | ||
| 11 | if(file == -1) | ||
| 12 | { | ||
| 13 | warn("fialed to open file. path could be invalid"); | ||
| 14 | return (string8){0}; | ||
| 15 | } | ||
| 16 | |||
| 17 | if(fstat(file, &sbuf) == -1) | ||
| 18 | { | ||
| 19 | warn("error: fstat failed"); | ||
| 20 | close(file); | ||
| 21 | return (string8){0}; | ||
| 22 | } | ||
| 23 | |||
| 24 | |||
| 25 | result = PushString(arena, sbuf.st_size); | ||
| 26 | |||
| 27 | result.size = (u64)sbuf.st_size; | ||
| 28 | if(result.size != 0) | ||
| 29 | { | ||
| 30 | result.data = (u8 *)mmap(0, result.size, PROT_READ, MAP_PRIVATE, file, 0); | ||
| 31 | } | ||
| 32 | |||
| 33 | close(file); | ||
| 34 | return result; | ||
| 35 | } | ||
| 36 | |||
| 37 | internal string8 | ||
| 38 | write_file(const char *path, string8 data) | ||
| 39 | { | ||
| 40 | |||
| 41 | string8 result = {0}; | ||
| 42 | s32 file = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644); | ||
| 43 | if(file == -1) | ||
| 44 | { | ||
| 45 | warn("failed to open file for writing. path could be invalid"); | ||
| 46 | return (string8){0}; | ||
| 47 | } | ||
| 48 | |||
| 49 | u64 written = 0; | ||
| 50 | while(written < data.size) | ||
| 51 | { | ||
| 52 | s64 err = write(file, data.data + written, data.size - written); | ||
| 53 | if(err == -1) | ||
| 54 | { | ||
| 55 | warn("write syscall failed"); | ||
| 56 | close(file); | ||
| 57 | return (string8){0}; | ||
| 58 | } | ||
| 59 | written += err; | ||
| 60 | } | ||
| 61 | |||
| 62 | close(file); | ||
| 63 | result = data; | ||
| 64 | return result; | ||
| 65 | } | ||
| 66 | |||
| 67 | #endif /* BASE_OS_H */ | ||
diff --git a/source/base/base_stack.c b/source/base/base_stack.c new file mode 100755 index 0000000..9c1218a --- /dev/null +++ b/source/base/base_stack.c | |||
| @@ -0,0 +1,187 @@ | |||
| 1 | internal mem_stack * | ||
| 2 | stack_create(u64 capacity) | ||
| 3 | { | ||
| 4 | mem_stack *stack = (mem_stack *)mmap( | ||
| 5 | 0, | ||
| 6 | capacity + sizeof(mem_stack), | ||
| 7 | PROT_READ | PROT_WRITE, | ||
| 8 | MAP_SHARED | MAP_ANONYMOUS, | ||
| 9 | -1, | ||
| 10 | 0); | ||
| 11 | |||
| 12 | if (stack == MAP_FAILED) | ||
| 13 | { | ||
| 14 | return NULL; | ||
| 15 | } | ||
| 16 | |||
| 17 | stack->capacity = capacity; | ||
| 18 | stack->base_position = (u8 *)stack + sizeof(mem_stack); | ||
| 19 | stack->current_offset = 0; | ||
| 20 | |||
| 21 | return stack; | ||
| 22 | } | ||
| 23 | |||
| 24 | internal u8 | ||
| 25 | calculate_padding(u64 pointer, u8 alignment, u64 header_size) | ||
| 26 | { | ||
| 27 | u8 modulo, padding; | ||
| 28 | |||
| 29 | if (!is_pow(alignment)) | ||
| 30 | { | ||
| 31 | return 0; | ||
| 32 | } | ||
| 33 | |||
| 34 | modulo = pointer & (u8)(alignment - 1); | ||
| 35 | |||
| 36 | padding = 0; | ||
| 37 | |||
| 38 | if (0 == modulo) | ||
| 39 | { | ||
| 40 | padding = alignment - modulo; | ||
| 41 | } | ||
| 42 | |||
| 43 | if (padding < header_size) | ||
| 44 | { | ||
| 45 | header_size -= padding; | ||
| 46 | |||
| 47 | if ((header_size & (alignment - 1)) != 0) | ||
| 48 | { | ||
| 49 | padding += alignment * (1 + (header_size / alignment)); | ||
| 50 | } | ||
| 51 | else | ||
| 52 | { | ||
| 53 | padding += alignment * (header_size / alignment); | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | return padding; | ||
| 58 | } | ||
| 59 | |||
| 60 | internal mem_stack * | ||
| 61 | stack_push_align(mem_stack *stack, u64 size, u8 alignment) | ||
| 62 | { | ||
| 63 | u8 padding = 0; | ||
| 64 | |||
| 65 | if (!is_pow(alignment)) | ||
| 66 | { | ||
| 67 | return (0); | ||
| 68 | } | ||
| 69 | |||
| 70 | if (alignment > 128) | ||
| 71 | { | ||
| 72 | alignment = 128; | ||
| 73 | } | ||
| 74 | |||
| 75 | u64 current_address = (u64)stack->base_position + stack->current_offset; | ||
| 76 | padding = calculate_padding(current_address, alignment, sizeof(mem_stack_header)); | ||
| 77 | |||
| 78 | if (stack->current_offset + padding + size > stack->capacity) | ||
| 79 | { | ||
| 80 | return 0; | ||
| 81 | } | ||
| 82 | |||
| 83 | stack->current_offset += padding; | ||
| 84 | |||
| 85 | u64 next_address = current_address + (u64)padding; | ||
| 86 | mem_stack_header *header = (mem_stack_header *)(next_address - sizeof(mem_stack_header)); | ||
| 87 | header->padding = padding; | ||
| 88 | |||
| 89 | stack->current_offset += size; | ||
| 90 | |||
| 91 | return MemSet((void *)next_address, size); | ||
| 92 | } | ||
| 93 | internal void * | ||
| 94 | stack_push(mem_stack *stack, u64 size) | ||
| 95 | { | ||
| 96 | return stack_push_align(stack, size, ARENA_ALIGN); | ||
| 97 | } | ||
| 98 | |||
| 99 | internal void | ||
| 100 | stack_pop(mem_stack *stack, void *pointer) | ||
| 101 | { | ||
| 102 | if (pointer != NULL) | ||
| 103 | { | ||
| 104 | u64 start, end, current_address; | ||
| 105 | mem_stack_header *header; | ||
| 106 | u64 prev_offset; | ||
| 107 | |||
| 108 | start = (u64)stack->base_position; | ||
| 109 | end = start + (u64)stack->capacity; | ||
| 110 | current_address = (u64)pointer; | ||
| 111 | |||
| 112 | if (!(start <= current_address && current_address < end)) | ||
| 113 | { | ||
| 114 | if (0 && "Out of bounds memory address passed to stack allocator (free)") | ||
| 115 | { | ||
| 116 | return; | ||
| 117 | } | ||
| 118 | return; | ||
| 119 | } | ||
| 120 | |||
| 121 | if (current_address >= start + (u64)stack->base_position) | ||
| 122 | { | ||
| 123 | return; | ||
| 124 | } | ||
| 125 | |||
| 126 | header = (mem_stack_header *)(current_address - sizeof(mem_stack_header)); | ||
| 127 | prev_offset = (size_t)(current_address - (u64)header->padding - start); | ||
| 128 | stack->current_offset = prev_offset; | ||
| 129 | } | ||
| 130 | } | ||
| 131 | |||
| 132 | internal mem_stack * | ||
| 133 | stack_resize_align(mem_stack *stack, void *pointer, u64 old_size, u64 new_size, u8 alignment) | ||
| 134 | { | ||
| 135 | if (pointer == NULL) | ||
| 136 | { | ||
| 137 | return stack_push_align(stack, new_size, alignment); | ||
| 138 | } | ||
| 139 | else if (new_size == 0) | ||
| 140 | { | ||
| 141 | stack_pop(stack, pointer); | ||
| 142 | return NULL; | ||
| 143 | } | ||
| 144 | |||
| 145 | u64 start, end, current_address; | ||
| 146 | u64 min_size = old_size < new_size ? old_size : new_size; | ||
| 147 | void *new_pointer; | ||
| 148 | |||
| 149 | start = (u64)stack->base_position; | ||
| 150 | end = start + (u64)stack->capacity; | ||
| 151 | current_address = (u64)pointer; | ||
| 152 | if (!(start <= current_address && current_address < end)) | ||
| 153 | { | ||
| 154 | return NULL; | ||
| 155 | } | ||
| 156 | |||
| 157 | if (current_address >= start + (u64)stack->current_offset) | ||
| 158 | { | ||
| 159 | return NULL; | ||
| 160 | } | ||
| 161 | |||
| 162 | if (old_size == new_size) | ||
| 163 | { | ||
| 164 | return pointer; | ||
| 165 | } | ||
| 166 | |||
| 167 | new_pointer = stack_push_align(stack, new_size, alignment); | ||
| 168 | memmove(new_pointer, pointer, min_size); | ||
| 169 | return new_pointer; | ||
| 170 | } | ||
| 171 | |||
| 172 | internal void | ||
| 173 | stack_pop_all(mem_stack *stack) | ||
| 174 | { | ||
| 175 | stack->current_offset = 0; | ||
| 176 | } | ||
| 177 | |||
| 178 | internal void | ||
| 179 | stack_destroy(mem_stack *stack) | ||
| 180 | { | ||
| 181 | if (!stack) | ||
| 182 | { | ||
| 183 | return; | ||
| 184 | } | ||
| 185 | |||
| 186 | munmap(stack, stack->capacity + sizeof(mem_stack)); | ||
| 187 | } | ||
diff --git a/source/base/base_stack.h b/source/base/base_stack.h new file mode 100755 index 0000000..54d61d3 --- /dev/null +++ b/source/base/base_stack.h | |||
| @@ -0,0 +1,22 @@ | |||
| 1 | #ifndef STACK_H | ||
| 2 | #define STACK_H | ||
| 3 | |||
| 4 | typedef struct mem_stack_header mem_stack_header; | ||
| 5 | struct mem_stack_header | ||
| 6 | { | ||
| 7 | u8 padding; | ||
| 8 | u8 previous_offset; | ||
| 9 | }; | ||
| 10 | |||
| 11 | |||
| 12 | typedef struct mem_stack mem_stack; | ||
| 13 | struct mem_stack | ||
| 14 | { | ||
| 15 | mem_stack_header *header; | ||
| 16 | |||
| 17 | u64 current_offset; | ||
| 18 | u64 capacity; | ||
| 19 | u8 *base_position; | ||
| 20 | }; | ||
| 21 | |||
| 22 | #endif | ||
diff --git a/source/base/base_string.h b/source/base/base_string.h new file mode 100644 index 0000000..29ccf1e --- /dev/null +++ b/source/base/base_string.h | |||
| @@ -0,0 +1,53 @@ | |||
| 1 | #ifndef BASE_STRING_H | ||
| 2 | #define BASE_STRING_H | ||
| 3 | |||
| 4 | #define PushString(arena, count) (string8){ .data = (PushArrayZero(arena, u8, (count))), .size = (count) } | ||
| 5 | #define StringCast(data, size) (string8){(u8 *)(data), (u64)(size) } | ||
| 6 | #define StringPCast(data, size) (string8 *){(u8 *)(data), (u64)(size) } | ||
| 7 | |||
| 8 | #define StringFmt "%.*s" | ||
| 9 | #define ULongFmt "%lu" | ||
| 10 | #define ULLongFmt "%llu" | ||
| 11 | |||
| 12 | typedef struct string8 string8; | ||
| 13 | struct string8 | ||
| 14 | { | ||
| 15 | u8 *data; | ||
| 16 | u64 size; | ||
| 17 | }; | ||
| 18 | |||
| 19 | internal b8 | ||
| 20 | string8_cmp(string8 a, string8 b) | ||
| 21 | { | ||
| 22 | if (a.size != b.size) return 0; | ||
| 23 | return (b8)(memcmp(a.data, b.data, a.size) == 0); | ||
| 24 | } | ||
| 25 | |||
| 26 | internal u64 | ||
| 27 | string8_to_u64(u8 *buf, umm len) | ||
| 28 | { | ||
| 29 | u64 value = 0; | ||
| 30 | for (umm i = 0; i < len; ++i) | ||
| 31 | { | ||
| 32 | u8 c = buf[i]; | ||
| 33 | if (c < '0' || c > '9') break; | ||
| 34 | value = value * 10 + (c - '0'); | ||
| 35 | } | ||
| 36 | return value; | ||
| 37 | } | ||
| 38 | |||
| 39 | internal void | ||
| 40 | string8_append_char(string8 *buf, u8 c) | ||
| 41 | { | ||
| 42 | buf->data[buf->size] = c; | ||
| 43 | buf->size += 1; | ||
| 44 | } | ||
| 45 | |||
| 46 | read_only global_variable | ||
| 47 | string8 nil_string = | ||
| 48 | { | ||
| 49 | .data = NULL, | ||
| 50 | .size = 0, | ||
| 51 | }; | ||
| 52 | |||
| 53 | #endif /* BASE_STRING_H */ | ||
diff --git a/source/base/base_test.h b/source/base/base_test.h new file mode 100644 index 0000000..fd47abc --- /dev/null +++ b/source/base/base_test.h | |||
| @@ -0,0 +1,46 @@ | |||
| 1 | // TODO(nasr): metaprogram that takes an expected output and generates a test for that specified | ||
| 2 | // function | ||
| 3 | /* base library testing framework */ | ||
| 4 | #ifndef BASE_TEST_H | ||
| 5 | #define BASE_TEST_H | ||
| 6 | |||
| 7 | // helper macro | ||
| 8 | #define show \ | ||
| 9 | do \ | ||
| 10 | { \ | ||
| 11 | write(STDOUT_FILENO, __FILE__, sizeof(__FILE__) - 1); \ | ||
| 12 | write(STDOUT_FILENO, ":", 1); \ | ||
| 13 | write(STDOUT_FILENO, __func__, sizeof(__func__) - 1); \ | ||
| 14 | write(STDOUT_FILENO, ":", 1); \ | ||
| 15 | write_int(__LINE__); \ | ||
| 16 | write(STDOUT_FILENO, "\n", 1); \ | ||
| 17 | } while (0) | ||
| 18 | |||
| 19 | #define test(expr) \ | ||
| 20 | { \ | ||
| 21 | if ((expr) != 0) \ | ||
| 22 | { \ | ||
| 23 | write(STDERR_FILENO, "[FAILED] ", LEN("[FAILED] ")); \ | ||
| 24 | show; \ | ||
| 25 | _exit(1); \ | ||
| 26 | } \ | ||
| 27 | } | ||
| 28 | |||
| 29 | #define verify(expr) \ | ||
| 30 | { \ | ||
| 31 | if ((expr) != 0) \ | ||
| 32 | { \ | ||
| 33 | write(STDERR_FILENO, RED "[ERROR] ", LEN(RED "[ERROR] ")); \ | ||
| 34 | show; \ | ||
| 35 | write(STDERR_FILENO, RESET, LEN(RESET)); \ | ||
| 36 | _exit(1); \ | ||
| 37 | } \ | ||
| 38 | else \ | ||
| 39 | { \ | ||
| 40 | write(STDERR_FILENO, GREEN "[SUCCESS] ", LEN(GREEN "[SUCCESS] ")); \ | ||
| 41 | show; \ | ||
| 42 | write(STDERR_FILENO, RESET, LEN(RESET)); \ | ||
| 43 | } \ | ||
| 44 | } | ||
| 45 | |||
| 46 | #endif /* BASE_TEST_H */ | ||
diff --git a/source/base/bash_hash.h b/source/base/bash_hash.h new file mode 100644 index 0000000..2c286a2 --- /dev/null +++ b/source/base/bash_hash.h | |||
| @@ -0,0 +1,15 @@ | |||
| 1 | ##ifndef HEADER_H | ||
| 2 | #define HEADER_H | ||
| 3 | |||
| 4 | typedef struct hash_map hash_map; | ||
| 5 | typedef struct hash hash; | ||
| 6 | |||
| 7 | |||
| 8 | struct hash_map | ||
| 9 | { | ||
| 10 | |||
| 11 | |||
| 12 | }; | ||
| 13 | |||
| 14 | |||
| 15 | #endif /* HEADER_H */ | ||
diff --git a/source/tb_db/tb_ml.c b/source/tb_db/tb_ml.c new file mode 100644 index 0000000..a86303d --- /dev/null +++ b/source/tb_db/tb_ml.c | |||
| @@ -0,0 +1,9 @@ | |||
| 1 | #define BASE_UNITY | ||
| 2 | #include "../base/base_include.h" | ||
| 3 | |||
| 4 | int main(int c, char **v) | ||
| 5 | { | ||
| 6 | |||
| 7 | |||
| 8 | return 0; | ||
| 9 | } | ||
