From 3913d1778318cd0c6bfb871148d38abb33ec7fd3 Mon Sep 17 00:00:00 2001 From: nasr Date: Wed, 28 Jan 2026 13:13:40 +0100 Subject: checkpoint --- library/arena.c | 81 +++++++++++++++++++++++++++++++++++++++ library/arena.h | 69 ++++++++++++++++++++++++++++++++++ library/base.h | 42 +++++++++++++++++++++ library/clang-format | 104 +++++++++++++++++++++++++++++++++++++++++++++++++++ library/clangd | 59 +++++++++++++++++++++++++++++ 5 files changed, 355 insertions(+) create mode 100644 library/arena.c create mode 100644 library/arena.h create mode 100644 library/base.h create mode 100644 library/clang-format create mode 100644 library/clangd (limited to 'library') diff --git a/library/arena.c b/library/arena.c new file mode 100644 index 0000000..39711d5 --- /dev/null +++ b/library/arena.c @@ -0,0 +1,81 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "arena.h" + +mem_arena * +arena_create(u64 capacity) +{ + mem_arena *arena = mmap(0, capacity, PROT_READ | PROT_WRITE | PROT_EXEC, + MAP_SHARED | MAP_ANONYMOUS, -1, 0); + if (arena == MAP_FAILED) + { + assert(0); + } + + arena->capacity = capacity; + arena->pos = ARENA_BASE_POS; + + return arena; +} + +// make it a void pointer to allow implicit conversion +void +arena_destroy(mem_arena *arena) +{ + munmap(arena, arena->capacity); +} + +void * +arena_push(mem_arena *arena, u64 size, b32 non_zero) +{ + u64 pos_aligned = ALIGN_UP_POW2(arena->pos, ARENA_ALIGN); + u64 new_pos = pos_aligned + size; + + if (new_pos > arena->capacity) + { + assert(0); + return NULL; + } + + arena->pos = new_pos; + // cast to u8 to be able to do pointer arithemtic + u8 *out = (u8 *)arena + pos_aligned; + + if (!non_zero) + { + memset(out, 0, size); + } + return out; +} +void +arena_pop(mem_arena *arena, u64 size) +{ + size = MIN(size, arena->pos - ARENA_BASE_POS); + arena->pos -= size; +} + +void +arena_pop_to(mem_arena *arena, u64 pos) +{ + u64 size = pos < arena->pos ? arena->pos - pos : 0; + arena_pop(arena, size); +} + +void +arena_clear(mem_arena *arena) +{ + arena_pop_to(arena, ARENA_BASE_POS); +} + +#define PUSH_STRUCT(arena, T) (T *)arena_push((arena), sizeof(T), 0) +#define PUSH_STRUCT_NZ(arena, T) (T *)arena_push((arena), sizeof(T), 1) +#define PUSH_ARRAY(arena, T, n) (T *)arena_push((arena), sizeof(T) * (n), 0) +#define PUSH_ARRAY_NZ(arena, T, n) (T *)arena_push((arena), sizeof(T) * (n), 1) + diff --git a/library/arena.h b/library/arena.h new file mode 100644 index 0000000..330023e --- /dev/null +++ b/library/arena.h @@ -0,0 +1,69 @@ +#ifndef ARENA_H +#define ARENA_H + +#include "base.h" + +/** + * Arena Helper macro's + * */ + +#define MIN(a, b) (((a) < (b)) ? (a) : (b)) +#define MAX(a, b) (((a) > (b)) ? (a) : (b)) +#define ALIGN_UP_POW2(n, p) (((u64)(n) + ((u64)(p) - 1)) & (~((u64)(p) - 1))) + + + +/* + * Represents a disk partition with major/minor device numbers and block count. + */ + +/** + * replacing malloc/free with arena allocaters + * + * */ + +#define ARENA_BASE_POS (sizeof(mem_arena)) +// void * for the size of a pointer on the machine, 64/32bit comp +#define ARENA_ALIGN (sizeof(void *)) + + +static inline u64 KiB(u64 n) { return n << 10; } +static inline u64 MiB(u64 n) { return n << 20; } +static inline u64 GiB(u64 n) { return n << 30; } + +typedef struct mem_arena mem_arena; + + +struct mem_arena +{ + u64 capacity; + u64 pos; +} ; + +// arena prototypes +mem_arena * +arena_create(u64 capacity); +// make it a void pointer to allow implicit conversion +void +arena_destroy(mem_arena *arena); + +void * +arena_push(mem_arena *arena, u64 size, b32 non_zero); + +void +arena_pop(mem_arena *arena, u64 size); + +void +arena_pop_to(mem_arena *arena, u64 pos); + +void +arena_clear(mem_arena *arena); + + +#define PUSH_STRUCT(arena, T) (T *)arena_push((arena), sizeof(T), 0) +#define PUSH_STRUCT_NZ(arena, T) (T *)arena_push((arena), sizeof(T), 1) +#define PUSH_ARRAY(arena, T, n) (T *)arena_push((arena), sizeof(T) * (n), 0) +#define PUSH_ARRAY_NZ(arena, T, n) (T *)arena_push((arena), sizeof(T) * (n), 1) + + +#endif diff --git a/library/base.h b/library/base.h new file mode 100644 index 0000000..74b6303 --- /dev/null +++ b/library/base.h @@ -0,0 +1,42 @@ +#ifndef BASE_H +#define BASE_H + +#include +#include + +#define OK 0 +#define ERR_IO 1 +#define ERR_PARSE 2 +#define ERR_PERM 3 +#define ERR_INVALID 4 + +enum { + BUFFER_SIZE_SMALL = 128, + BUFFER_SIZE_DEFAULT = 256, + BUFFER_SIZE_LARGE = 512, + PATH_MAX_LEN = 4096 +}; + +typedef uint64_t u64; +typedef uint32_t u32; +typedef uint16_t u16; +typedef uint8_t u8; + +typedef int8_t i8; +typedef int16_t i16; +typedef int32_t i32; +typedef int64_t i64; + +typedef float f32; +typedef double f64; + +typedef i32 b32; +typedef i16 b16; +typedef u8 b8; + +#define TRUE 1 +#define FALSE 0 + + + +#endif diff --git a/library/clang-format b/library/clang-format new file mode 100644 index 0000000..3c61d5e --- /dev/null +++ b/library/clang-format @@ -0,0 +1,104 @@ +BasedOnStyle: LLVM +Language: C + +# ------------------------------------------------------------------- +# Indentation & layout +# ------------------------------------------------------------------- +IndentWidth: 2 +TabWidth: 2 +UseTab: Never +ContinuationIndentWidth: 2 + +IndentCaseLabels: true +IndentGotoLabels: true +IndentPPDirectives: None +IndentExternBlock: NoIndent + +# ------------------------------------------------------------------- +# Line breaking +# ------------------------------------------------------------------- +ColumnLimit: 0 + +AllowAllParametersOfDeclarationOnNextLine: false +AllowAllArgumentsOnNextLine: false + +AllowShortFunctionsOnASingleLine: false +AllowShortIfStatementsOnASingleLine: Never +AllowShortLoopsOnASingleLine: false +AllowShortBlocksOnASingleLine: Never +AllowShortCaseLabelsOnASingleLine: false + +AlwaysBreakAfterReturnType: All +AlwaysBreakTemplateDeclarations: No # harmless for C + +BreakBeforeBinaryOperators: None + +# ------------------------------------------------------------------- +# Braces (Allman style) +# ------------------------------------------------------------------- +BreakBeforeBraces: Allman + +BraceWrapping: + AfterCaseLabel: true + BeforeElse: true + BeforeCatch: true + SplitEmptyFunction: true + SplitEmptyRecord: true + SplitEmptyNamespace: true + +# NOTE: +# AfterControlStatement / AfterFunction / AfterStruct / AfterEnum / AfterUnion +# are IMPLIED by Allman and must NOT be redundantly specified. + +# ------------------------------------------------------------------- +# Spacing +# ------------------------------------------------------------------- +SpaceBeforeParens: ControlStatements +SpaceBeforeAssignmentOperators: true +SpaceBeforeRangeBasedForLoopColon: true # ignored in C, harmless + +SpacesInParentheses: false +SpacesInSquareBrackets: false +SpacesInAngles: false +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 1 + +PointerAlignment: Right +DerivePointerAlignment: false + +# ------------------------------------------------------------------- +# Alignment (explicitly disabled) +# ------------------------------------------------------------------- +AlignAfterOpenBracket: DontAlign +AlignOperands: false +AlignTrailingComments: false +AlignConsecutiveAssignments: false +AlignConsecutiveDeclarations: false +AlignEscapedNewlines: DontAlign + +# ------------------------------------------------------------------- +# Comments +# ------------------------------------------------------------------- +ReflowComments: false +CommentPragmas: '^ dont touch:' +KeepEmptyLinesAtTheStartOfBlocks: false +MaxEmptyLinesToKeep: 1 + +# ------------------------------------------------------------------- +# Includes +# ------------------------------------------------------------------- +SortIncludes: Never +IncludeBlocks: Preserve + +# ------------------------------------------------------------------- +# Macros & preprocessor +# ------------------------------------------------------------------- +MacroBlockBegin: '' +MacroBlockEnd: '' +SpaceAfterCStyleCast: false + +# ------------------------------------------------------------------- +# C-specific +# ------------------------------------------------------------------- +Cpp11BracedListStyle: false +DisableFormat: false diff --git a/library/clangd b/library/clangd new file mode 100644 index 0000000..624678a --- /dev/null +++ b/library/clangd @@ -0,0 +1,59 @@ +CompileFlags: + Add: + - -std=c99 + - -xc + + - -Iinclude + + - -Wall + - -Wextra + - -Wpedantic + - -Wshadow + - -Wconversion + - -Wsign-conversion + - -Wmissing-declarations + - -Wundef + - -Wpointer-arith + - -Wcast-align + - -Wcast-qual + - -Wwrite-strings + - -Wswitch-enum + - -Wformat=2 + - -Wstrict-aliasing=2 + - -Werror=implicit-function-declaration + - -Werror=implicit-int + - -Werror=incompatible-pointer-types + - -Werror=return-type + - -Wformat-security + - -Wnull-dereference + - -Wmisleading-indentation + + - -Wunused + - -Wuninitialized + - -Werror + - -Wdouble-promotion + - -Wstrict-overflow=2 + + - -D_POSIX_C_SOURCE=200809L + - "-I/include" + + Remove: + - -std=* + - -O* + - -march=* + - -mtune=* + + +Hover: + ShowAKA: true + +InlayHints: + Enabled: true + ParameterNames: true + DeducedTypes: true + +Completion: + AllScopes: true + +Index: + Background: Build -- cgit v1.3