From 154bf6f53529e88dfa03d6ff5034b575f92cdbb5 Mon Sep 17 00:00:00 2001 From: nasr Date: Tue, 14 Apr 2026 23:11:49 +0200 Subject: feature(setup): base implementation --- .gitignore | 12 +++ build.sh | 11 +++ source/base/base.h | 64 ++++++++++++++ source/base/base_arena.c | 138 +++++++++++++++++++++++++++++ source/base/base_arena.h | 31 +++++++ source/base/base_error.h | 43 ++++++++++ source/base/base_include.h | 36 ++++++++ source/base/base_io.h | 60 +++++++++++++ source/base/base_mem.h | 26 ++++++ source/base/base_os.h | 68 +++++++++++++++ source/base/base_rand.h | 4 + source/base/base_stack.h | 210 +++++++++++++++++++++++++++++++++++++++++++++ source/base/base_string.c | 30 +++++++ source/base/base_string.h | 78 +++++++++++++++++ source/base/base_test.h | 46 ++++++++++ source/base/bash_hash.h | 24 ++++++ 16 files changed, 881 insertions(+) create mode 100644 .gitignore mode change 100644 => 100755 build.sh create mode 100755 source/base/base.h create mode 100755 source/base/base_arena.c create mode 100755 source/base/base_arena.h create mode 100644 source/base/base_error.h create mode 100755 source/base/base_include.h create mode 100644 source/base/base_io.h create mode 100644 source/base/base_mem.h create mode 100644 source/base/base_os.h create mode 100755 source/base/base_stack.h create mode 100644 source/base/base_string.h create mode 100644 source/base/base_test.h create mode 100644 source/base/bash_hash.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..05a7b85 --- /dev/null +++ b/.gitignore @@ -0,0 +1,12 @@ +idea/ +idea +idea/* +.idea +/build +/notes.txt +/test +/tags +/helper.sh +/tests +/tags +/.cache/clangd/index diff --git a/build.sh b/build.sh old mode 100644 new mode 100755 index e69de29..29b4092 --- a/build.sh +++ b/build.sh @@ -0,0 +1,11 @@ +#!/bin/sh + +CC=cc +SRC=./source/scb/scb.c +OUT=./build/scb + +CFLAGS="-Wall -Wextra -Werror -Wpedantic -O0 -g -Wno-unused-function" + +mkdir -p ./build + +$CC $CFLAGS "$SRC" -o "$OUT" diff --git a/source/base/base.h b/source/base/base.h new file mode 100755 index 0000000..3fe9dca --- /dev/null +++ b/source/base/base.h @@ -0,0 +1,64 @@ +#ifndef BASE_H +#define BASE_H + +/* assert an expression and output the file and the line */ +#define internal static +#define global_variable static +#define local_persist static + +#define unused(x) (void)(x) +#define NIL 0 + +#define DEPRECATED __attribute__((__deprecated__)) + +#if defined(__arm__) || defined(__aarch64__) +#define breakpoint __asm__ volatile("brk #0"); +#define temp_breakpoint __asm__ volatile("udf #0"); +#elif defined(__i386__) || defined(__x86_64__) +#define breakpoint __asm__ volatile("int3"); +#endif + +#define MemCpy(dest, src, len) memcpy((dest), (src), (len)) +#define MemSet(dest, len) memset((dest), (0), (len)) + +#if COMPILER_MSVC || (COMPILER_CLANG && OS_WINDOWS) +#pragma section(".rdata$", read) +#define read_only __declspec(allocate(".rdata$")) +#elif (COMPILER_CLANG && OS_LINUX) +#define read_only __attribute__((section(".rodata"))) +#else +#define read_only +#endif + +typedef uint64_t u64; +typedef uint32_t u32; +typedef uint16_t u16; +typedef uint8_t u8; + +typedef int8_t s8; +typedef int16_t s16; +typedef int32_t s32; +typedef int64_t s64; + +typedef float f32; +typedef double f64; + +typedef s32 b32; +typedef s16 b16; +typedef s8 b8; + +typedef uintptr_t umm; +typedef intptr_t smm; + +#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 Len(s) (sizeof(s) - 1) + +#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 @@ +internal mem_arena * +arena_create(u64 capacity) +{ + mem_arena *arena = (mem_arena *)mmap( + /* kernel decides where to throw the arena */ + NULL, + capacity + sizeof(mem_arena), + PROT_READ | PROT_WRITE, + MAP_SHARED | MAP_ANONYMOUS, + -1, + 0); + + if (arena == MAP_FAILED) + { + return NULL; + } + + arena->capacity = capacity; + arena->base_position = (u8 *)arena + sizeof(mem_arena); + arena->current_position = 0; + arena->previous_position = 0; + + return arena; +} + +internal void +arena_destroy(mem_arena *arena) +{ + if (!arena) + { + return; + } + munmap(arena, arena->capacity + sizeof(mem_arena)); +} +internal void * +arena_alloc(mem_arena *arena, u64 size, b32 zero) +{ + if (!arena) + { + return NULL; + } + u64 aligned = Align(arena->current_position, ARENA_ALIGN); + u64 new_pos = aligned + size; + if (new_pos > arena->capacity) + { + return NULL; + } + + void *out = arena->base_position + aligned; + + arena->previous_position = arena->current_position; + arena->current_position = aligned + size; + + if (zero) MemSet(out, size); + + return out; +} + +internal void +arena_pop(mem_arena *arena, u64 size) +{ + size = MIN(size, arena->current_position); + arena->current_position -= size; +} + +internal void +arena_pop_to(mem_arena *arena, u64 pos) +{ + u64 size = pos < arena->current_position ? arena->current_position - pos : 0; + arena_pop(arena, size); +} + +internal void +arena_clear(mem_arena *arena) +{ + arena->current_position = 0; +} + +internal mem_arena * +arena_resize_align(mem_arena *arena, void *old_memory, u64 new_size, u64 old_size, umm alignment) +{ + u8 *old_mem = (u8 *)old_memory; + + if (!is_pow(alignment)) + { + Align(arena->current_position, alignment); + } + + if (old_memory == NULL || old_size == 0) + { + return (mem_arena *)arena_alloc(arena, new_size, 0); + } + else if ((old_mem >= arena->base_position && old_mem < arena->base_position + arena->capacity)) + { + if ((arena->base_position + arena->previous_position) == old_memory) + { + arena->current_position = arena->previous_position + new_size; + if (new_size > old_size) + { + MemSet(&arena->current_position, new_size - old_size); + } + return (mem_arena *)old_memory; + } + else + { + void *new_memory = arena_alloc(arena, new_size, 0); + umm copy_size = old_size < new_size ? old_size : new_size; + memmove(new_memory, old_mem, copy_size); + } + } + else + { + verify(0); + } + return NULL; +} + +internal mem_arena * +arena_resize(mem_arena *arena, void *old_memory, u64 new_size, u64 old_size) +{ + return arena_resize_align(arena, old_memory, new_size, old_size, ARENA_ALIGN); +} + +internal temp_arena +temp_arena_begin(mem_arena *arena) +{ + temp_arena t; + t.arena = arena; + t.start_position = arena->current_position; + + return t; +} + +internal void +temp_arena_end(temp_arena temp) +{ + temp.arena->current_position = temp.start_position; +} diff --git a/source/base/base_arena.h b/source/base/base_arena.h new file mode 100755 index 0000000..dc6dd73 --- /dev/null +++ b/source/base/base_arena.h @@ -0,0 +1,31 @@ +#ifndef BASE_ARENA_H +#define BASE_ARENA_H + +#define Align(pointer, alignment) align((u64)(pointer), (umm)(alignment)) +#define PushStruct(arena, type) (type *)arena_alloc((arena), sizeof(type), 0) +#define PushStructZero(arena, type) (type *)arena_alloc((arena), sizeof(type), 1) +#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 +{ + u64 current_position; + u64 previous_position; + u64 capacity; + u8 *base_position; +}; + +typedef struct temp_arena temp_arena; +struct temp_arena +{ + mem_arena *arena; + u64 start_position; +}; + +#endif /* BASE_ARENA_H */ diff --git a/source/base/base_error.h b/source/base/base_error.h new file mode 100644 index 0000000..4bf84fc --- /dev/null +++ b/source/base/base_error.h @@ -0,0 +1,43 @@ +/* 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")); \ + } while (0) + +#define _fatal(msg) \ + do { \ + error_at(msg); \ + _exit(1); \ + } while (0) + +#define assert_msg(expr, msg) \ + do \ + { \ + if (!(expr)) _fatal(msg); \ + } 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")); \ + } while (0) + +#endif /* BASE_ERROR_H */ diff --git a/source/base/base_include.h b/source/base/base_include.h new file mode 100755 index 0000000..54844d4 --- /dev/null +++ b/source/base/base_include.h @@ -0,0 +1,36 @@ +#ifndef BASE_INCLUDE_H +#define BASE_INCLUDE_H + +#include +#include +#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_string.c" + + +#ifdef BASE_MATH_IMPLEMENTATION +#include +//- needs math.h +#include "base_rand.h" +#endif +#include "base_io.h" +#include "base_error.h" +#include "base_test.h" +#ifdef BASE_UNITY +#include "base_arena.c" +#endif +#include "base_os.h" +#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 @@ +#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_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 @@ +#ifndef BASE_MEM_H +#define BASE_MEM_H + +#define ARENA_ALIGN (2 * sizeof(void *)) +#define MIN(a, b) (((a) < (b)) ? (a) : (b)) +#define MAX(a, b) (((a) > (b)) ? (a) : (b)) + +internal inline b8 +is_pow(umm x) +{ + return (x & (x - 1)) == 0; +} + +internal inline u64 +align(u64 pointer, umm alignment) +{ + if ((alignment & (alignment - 1)) == 0) + { + return pointer; + } + + return (pointer + alignment - 1) & ~(alignment - 1); +} + + +#endif diff --git a/source/base/base_os.h b/source/base/base_os.h new file mode 100644 index 0000000..4f84d93 --- /dev/null +++ b/source/base/base_os.h @@ -0,0 +1,68 @@ +#ifndef BASE_OS_H +#define BASE_OS_H + +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.size = (u64)sbuf.st_size; + if(result.size != 0) + { + result.data = (u8 *)mmap(0, result.size, PROT_READ, MAP_PRIVATE, file, 0); + } + + close(file); + return result; +} + +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 */ diff --git a/source/base/base_rand.h b/source/base/base_rand.h index e69de29..faa00e8 100644 --- a/source/base/base_rand.h +++ b/source/base/base_rand.h @@ -0,0 +1,4 @@ +#ifndef BASE_RAND_H +#define BASE_RAND_H + +#endif /* BASE_RAND_H */ diff --git a/source/base/base_stack.h b/source/base/base_stack.h new file mode 100755 index 0000000..43a7230 --- /dev/null +++ b/source/base/base_stack.h @@ -0,0 +1,210 @@ +#ifndef STACK_H +#define STACK_H + +typedef struct mem_stack_header mem_stack_header; +struct mem_stack_header +{ + u8 padding; + u8 previous_offset; +}; + + +typedef struct mem_stack mem_stack; +struct mem_stack +{ + mem_stack_header *header; + + u64 current_offset; + u64 capacity; + 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 index e69de29..986fde5 100644 --- a/source/base/base_string.c +++ 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 new file mode 100644 index 0000000..c4b9f94 --- /dev/null +++ b/source/base/base_string.h @@ -0,0 +1,78 @@ +#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 StringFmt "%.*s" +#define ULongFmt "%lu" +#define ULLongFmt "%llu" + +typedef struct string8 string8; +struct string8 +{ + u8 *data; + 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) +{ + if (a.size != b.size) return 0; + return (b8)(memcmp(a.data, b.data, a.size) == 0); +} + +internal void +string8_appendc(string8 *buf, u8 c) +{ + buf->data[buf->size] = c; + buf->size += 1; +} + +#endif /* BASE_STRING_H */ diff --git a/source/base/base_test.h b/source/base/base_test.h new file mode 100644 index 0000000..072bec3 --- /dev/null +++ b/source/base/base_test.h @@ -0,0 +1,46 @@ +// 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 new file mode 100644 index 0000000..758ef72 --- /dev/null +++ b/source/base/bash_hash.h @@ -0,0 +1,24 @@ +#ifndef BASH_HASH_H +#define BASH_HASH_H + +typedef struct hash_map hash_map; +typedef struct hash hash; + +struct map +{ + string8 + u64 capacity +}; + + +struct index +{ + string8 key; + string8 value; +}; + + + +internal void + +#endif /* BASH_HASH_H */ -- cgit v1.3