From 73fe5265014f0e4954ff987969eca77ed902391f Mon Sep 17 00:00:00 2001 From: Ed_ Date: Sat, 1 Feb 2025 20:42:40 -0500 Subject: [PATCH] progress on base --- code/base.h | 2 +- code/base/command_line.h | 11 +- code/base/{arch.h => cracking_arch.h} | 0 code/base/{compiler.h => cracking_compiler.h} | 0 code/base/{os.h => cracking_os.h} | 0 code/base/cstd.h | 22 -- code/base/macros.h | 20 +- code/base/math.h | 2 +- code/base/memory.h | 221 +++++++++++++++++- code/base/namespace.h | 19 ++ code/base/platform.c | 6 + code/base/platform.h | 22 ++ code/base/strings.h | 20 +- code/base/toolchain.h | 38 +++ 14 files changed, 313 insertions(+), 70 deletions(-) rename code/base/{arch.h => cracking_arch.h} (100%) rename code/base/{compiler.h => cracking_compiler.h} (100%) rename code/base/{os.h => cracking_os.h} (100%) delete mode 100644 code/base/cstd.h create mode 100644 code/base/namespace.h create mode 100644 code/base/platform.c create mode 100644 code/base/platform.h create mode 100644 code/base/toolchain.h diff --git a/code/base.h b/code/base.h index a27444d..a901809 100644 --- a/code/base.h +++ b/code/base.h @@ -12,4 +12,4 @@ MD_NS_BEGIN #include "base/memory.h" #include "base/zpl_memory.h" -MD_NS_END \ No newline at end of file +MD_NS_END diff --git a/code/base/command_line.h b/code/base/command_line.h index 2222cef..d2ae34c 100644 --- a/code/base/command_line.h +++ b/code/base/command_line.h @@ -1,7 +1,6 @@ #ifdef MD_INTELLISENSE_DIRECTIVES #pragma once #include "base_types.h" -#include #include "strings.h" #endif @@ -14,12 +13,12 @@ typedef struct CmdLineOpt CmdLineOpt; struct CmdLineOpt { - CmdLineOpt *next; - CmdLineOpt *hash_next; - U64 hash; - String8 string; + CmdLineOpt* next; + CmdLineOpt* hash_next; + U64 hash; + String8 string; String8List value_strings; - String8 value_string; + String8 value_string; }; typedef struct CmdLineOptList CmdLineOptList; diff --git a/code/base/arch.h b/code/base/cracking_arch.h similarity index 100% rename from code/base/arch.h rename to code/base/cracking_arch.h diff --git a/code/base/compiler.h b/code/base/cracking_compiler.h similarity index 100% rename from code/base/compiler.h rename to code/base/cracking_compiler.h diff --git a/code/base/os.h b/code/base/cracking_os.h similarity index 100% rename from code/base/os.h rename to code/base/cracking_os.h diff --git a/code/base/cstd.h b/code/base/cstd.h deleted file mode 100644 index 1b2091a..0000000 --- a/code/base/cstd.h +++ /dev/null @@ -1,22 +0,0 @@ -#if MD_INTELLISENSE_DIRECTIVES -#pragma once -#include "arch.h" -#include "os.h" -#include "compiler.h" -#endif - -#pragma region Mandatory Includes - -# include -# include - -# if defined( MD_SYSTEM_WINDOWS ) -# include -# endif - -#if MD_COMPILER_C -#include -#include -#endif - -#pragma endregion Mandatory Includes diff --git a/code/base/macros.h b/code/base/macros.h index 6646867..cc5ac1e 100644 --- a/code/base/macros.h +++ b/code/base/macros.h @@ -1,6 +1,6 @@ #if MD_INTELLISENSE_DIRECTIVES #pragma once -#include "cstd.h" +#include "platform.h" #endif #ifndef MD_API @@ -143,21 +143,3 @@ #endif #endif #endif - -#if MD_DONT_USE_NAMESPACE || MD_COMPILER_C -# if MD_COMPILER_C -# define MD_NS -# define MD_NS_BEGIN -# define MD_NS_END -# else -# define MD_NS :: -# define MD_NS_BEGIN -# define MD_NS_END -# endif -#else - namespace MD {} - namespace md = MD; -# define MD_NS MD:: -# define MD_NS_BEGIN namespace MD { -# define MD_NS_END } -#endif diff --git a/code/base/math.h b/code/base/math.h index 656d454..2480d66 100644 --- a/code/base/math.h +++ b/code/base/math.h @@ -1,6 +1,6 @@ #if MD_INTELLISENSE_DIRECTIVES #pragma once -#include "base/base_types.h" +#include "base_types.h" #endif typedef union Rng1U64 Rng1U64; diff --git a/code/base/memory.h b/code/base/memory.h index a909bb8..411b574 100644 --- a/code/base/memory.h +++ b/code/base/memory.h @@ -1,5 +1,6 @@ #ifdef MD_INTELLISENSE_DIRECTIVES #pragma once +#include "cracking_arch.h" #include "macros.h" #endif @@ -64,20 +65,216 @@ #define memory_compare(a, b, size) memcmp((a), (b), (size)) #define memory_str_len(ptr) cstr_len(ptr) -#define memory_copy_struct(d,s) memory_copy((d),(s),sizeof(*(d))) -#define memory_copy_array(d,s) memory_copy((d),(s),sizeof(d)) -#define memory_copy_type(d,s,c) memory_copy((d),(s),sizeof(*(d))*(c)) +#define memory_copy_struct(d,s) memory_copy((d), (s), sizeof( *(d))) +#define memory_copy_array(d,s) memory_copy((d), (s), sizeof( d)) +#define memory_copy_type(d,s,c) memory_copy((d), (s), sizeof( *(d)) * (c)) -#define memory_zero(s,z) mem_set((s),0,(z)) -#define memory_zero_struct(s) memory_zero((s),sizeof(*(s))) -#define memory_zero_array(a) memroy_zero((a),sizeof(a)) -#define memory_zero_type(m,c) memroy_zero((m),sizeof(*(m))*(c)) +#define memory_zero(s,z) mem_set((s), 0, (z)) +#define memory_zero_struct(s) memory_zero((s), sizeof( *(s))) +#define memory_zero_array(a) memroy_zero((a), sizeof(a)) +#define memory_zero_type(m,c) memroy_zero((m), sizeof( *(m)) * (c)) -#define memory_match(a,b,z) (memory_compare((a),(b),(z)) == 0) -#define memory_match_struct(a,b) memory_match((a),(b),sizeof(*(a))) -#define memory_match_array(a,b) memory_match((a),(b),sizeof(a)) +#define memory_match(a,b,z) (memory_compare((a), (b), (z)) == 0) +#define memory_match_struct(a,b) memory_match((a), (b), sizeof(*(a))) +#define memory_match_array(a,b) memory_match((a), (b), sizeof(a)) -#define memory_read(T,p,e) ( ((p)+sizeof(T)<=(e))?(*(T*)(p)):(0) ) -#define memory_consume(T,p,e) ( ((p)+sizeof(T)<=(e))?((p)+=sizeof(T),*(T*)((p)-sizeof(T))):((p)=(e),0) ) +#define memory_read(T,p,e) ( ((p) + sizeof(T) <= (e)) ? ( *(T*)(p)) : (0) ) +#define memory_consume(T,p,e) ( ((p) + sizeof(T) <= (e)) ? ((p) += sizeof(T), *(T*)((p) - sizeof(T))) : ((p) = (e),0) ) + +//////////////////////////////// +//~ rjf: Asserts + +#if MD_COMPILER_MSVC +# define trap() __debugbreak() +#elif MD_COMPILER_CLANG || COMPILER_GCC +# define trap() __builtin_trap() +#else +# error Unknown trap intrinsic for this compiler. +#endif + +#define assert_always(x) do { if ( !(x) ) { trap(); } } while(0) + +#if BUILD_DEBUG +# define assert(x) assert_always(x) +#else +# define assert(x) (void)(x) +#endif + +#define InvalidPath assert( ! "Invalid Path!") +#define NotImplemented assert( ! "Not Implemented!") +#define no_op ((void)0) +#define md_static_assert(C, ID) global U8 glue(ID, __LINE__)[ (C) ? 1 : -1 ] + +//////////////////////////////// +//~ rjf: Atomic Operations + +#if MD_OS_WINDOWS + +// # include +// # include +// # include +// # include + +# if MD_ARCH_X64 +# define ins_atomic_u64_eval(x) InterlockedAdd64((volatile __int64 *)(x), 0) +# define ins_atomic_u64_inc_eval(x) InterlockedIncrement64((volatile __int64 *)(x)) +# define ins_atomic_u64_dec_eval(x) InterlockedDecrement64((volatile __int64 *)(x)) +# define ins_atomic_u64_eval_assign(x,c) InterlockedExchange64((volatile __int64 *)(x), (c)) +# define ins_atomic_u64_add_eval(x,c) InterlockedAdd64((volatile __int64 *)(x), c) +# define ins_atomic_u64_eval_cond_assign(x,k,c) InterlockedCompareExchange64((volatile __int64 *)(x), (k), (c)) +# define ins_atomic_u32_eval(x,c) InterlockedAdd((volatile LONG *)(x), 0) +# define ins_atomic_u32_eval_assign(x,c) InterlockedExchange((volatile LONG *)(x), (c)) +# define ins_atomic_u32_eval_cond_assign(x,k,c) InterlockedCompareExchange((volatile LONG *)(x), (k), (c)) +# define ins_atomic_ptr_eval_assign(x,c) (void*) ins_atomic_u64_eval_assign((volatile __int64 *)(x), (__int64)(c)) +# else +# error Atomic intrinsics not defined for this operating system / architecture combination. +# endif +#elif MD_OS_LINUX +# if MD_ARCH_X64 +# define ins_atomic_u64_inc_eval(x) __sync_fetch_and_add((volatile U64 *)(x), 1) +# else +# error Atomic intrinsics not defined for this operating system / architecture combination. +# endif +#else +# error Atomic intrinsics not defined for this operating system. +#endif + +//////////////////////////////// +//~ rjf: Linked List Building Macros + +//- rjf: linked list macro helpers +#define Check``Nil(nil,p) ((p) == 0 || (p) == nil) +#define SetNil(nil,p) ((p) = nil) + +//- rjf: doubly-linked-lists +#define DLLInsert_NPZ(nil,f,l,p,n,next,prev) (CheckNil(nil,f) ? \ +((f) = (l) = (n), SetNil(nil,(n)->next), SetNil(nil,(n)->prev)) :\ +CheckNil(nil,p) ? \ +((n)->next = (f), (f)->prev = (n), (f) = (n), SetNil(nil,(n)->prev)) :\ +((p)==(l)) ? \ +((l)->next = (n), (n)->prev = (l), (l) = (n), SetNil(nil, (n)->next)) :\ +(((!CheckNil(nil,p) && CheckNil(nil,(p)->next)) ? (0) : ((p)->next->prev = (n))), ((n)->next = (p)->next), ((p)->next = (n)), ((n)->prev = (p)))) +#define DLLPushBack_NPZ(nil,f,l,n,next,prev) DLLInsert_NPZ(nil,f,l,l,n,next,prev) +#define DLLPushFront_NPZ(nil,f,l,n,next,prev) DLLInsert_NPZ(nil,l,f,f,n,prev,next) +#define DLLRemove_NPZ(nil,f,l,n,next,prev) (((n) == (f) ? (f) = (n)->next : (0)),\ +((n) == (l) ? (l) = (l)->prev : (0)),\ +(CheckNil(nil,(n)->prev) ? (0) :\ +((n)->prev->next = (n)->next)),\ +(CheckNil(nil,(n)->next) ? (0) :\ +((n)->next->prev = (n)->prev))) + +//- rjf: singly-linked, doubly-headed lists (queues) +#define SLLQueuePush_NZ(nil,f,l,n,next) (CheckNil(nil,f)?\ +((f)=(l)=(n),SetNil(nil,(n)->next)):\ +((l)->next=(n),(l)=(n),SetNil(nil,(n)->next))) +#define SLLQueuePushFront_NZ(nil,f,l,n,next) (CheckNil(nil,f)?\ +((f)=(l)=(n),SetNil(nil,(n)->next)):\ +((n)->next=(f),(f)=(n))) +#define SLLQueuePop_NZ(nil,f,l,next) ((f)==(l)?\ +(SetNil(nil,f),SetNil(nil,l)):\ +((f)=(f)->next)) + +//- rjf: singly-linked, singly-headed lists (stacks) +#define sll_stack_push_n(f,n,next) ( (n)->next = (f), (f) = (n) ) +#define sll_stack_pop_n(f,next) ( (f) = (f)->next ) + +//- rjf: doubly-linked-list helpers +#define dll_insert_np(f, l, p, n, next, prev) DLLInsert_NPZ (0, f, l, p, n, next, prev) +#define dll_push_back_np(f, l, n, next, prev) DLLPushBack_NPZ (0, f, l, n, next, prev) +#define dll_push_front_np(f, l, n, next, prev) DLLPushFront_NPZ(0, f, l, n, next, prev) +#define dll_remove_np(f, l, n, next, prev) DLLRemove_NPZ (0, f, l, n, next, prev) +#define dll_insert(f, l, p, n) DLLInsert_NPZ (0, f, l, p, n, next, prev) +#define dll_push_back(f, l, n) DLLPushBack_NPZ (0, f, l, n, next, prev) +#define dll_push_front(f, l, n) DLLPushFront_NPZ(0, f, l, n, next, prev) +#define dll_remove(f, l, n) DLLRemove_NPZ (0, f, l, n, next, prev) + +//- rjf: singly-linked, doubly-headed list helpers +#define SLLQueuePush_N(f,l,n,next) SLLQueuePush_NZ(0,f,l,n,next) +#define SLLQueuePushFront_N(f,l,n,next) SLLQueuePushFront_NZ(0,f,l,n,next) +#define SLLQueuePop_N(f,l,next) SLLQueuePop_NZ(0,f,l,next) +#define SLLQueuePush(f,l,n) SLLQueuePush_NZ(0,f,l,n,next) +#define SLLQueuePushFront(f,l,n) SLLQueuePushFront_NZ(0,f,l,n,next) +#define SLLQueuePop(f,l) SLLQueuePop_NZ(0,f,l,next) + +//- rjf: singly-linked, singly-headed list helpers +#define SLLStackPush(f,n) SLLStackPush_N(f,n,next) +#define SLLStackPop(f) SLLStackPop_N(f,next) + +//////////////////////////////// +//~ rjf: Address Sanitizer Markup + +#if COMPILER_MSVC +# if defined(__SANITIZE_ADDRESS__) +# define ASAN_ENABLED 1 +# define NO_ASAN __declspec(no_sanitize_address) +# else +# define NO_ASAN +# endif +#elif COMPILER_CLANG +# if defined(__has_feature) +# if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__) +# define ASAN_ENABLED 1 +# endif +# endif +# define NO_ASAN __attribute__((no_sanitize("address"))) +#else +# define NO_ASAN +#endif + +#if ASAN_ENABLED +#pragma comment(lib, "clang_rt.asan-x86_64.lib") +C_LINKAGE void __asan_poison_memory_region(void const volatile *addr, size_t size); +C_LINKAGE void __asan_unpoison_memory_region(void const volatile *addr, size_t size); +# define AsanPoisonMemoryRegion(addr, size) __asan_poison_memory_region((addr), (size)) +# define AsanUnpoisonMemoryRegion(addr, size) __asan_unpoison_memory_region((addr), (size)) +#else +# define AsanPoisonMemoryRegion(addr, size) ((void)(addr), (void)(size)) +# define AsanUnpoisonMemoryRegion(addr, size) ((void)(addr), (void)(size)) +#endif + +//////////////////////////////// +//~ rjf: Misc. Helper Macros + +#define Stringify_(S) #S +#define Stringify(S) Stringify_(S) + +#define Glue_(A,B) A##B +#define Glue(A,B) Glue_(A,B) + +#define ArrayCount(a) (sizeof(a) / sizeof((a)[0])) + +#define CeilIntegerDiv(a,b) (((a) + (b) - 1)/(b)) + +#define Swap(T,a,b) do{T t__ = a; a = b; b = t__;}while(0) + +#if ARCH_64BIT +# define IntFromPtr(ptr) ((U64)(ptr)) +#elif ARCH_32BIT +# define IntFromPtr(ptr) ((U32)(ptr)) +#else +# error Missing pointer-to-integer cast for this architecture. +#endif +#define PtrFromInt(i) (void*)((U8*)0 + (i)) + +#define Compose64Bit(a,b) ((((U64)a) << 32) | ((U64)b)); +#define AlignPow2(x,b) (((x) + (b) - 1)&(~((b) - 1))) +#define AlignDownPow2(x,b) ((x)&(~((b) - 1))) +#define AlignPadPow2(x,b) ((0-(x)) & ((b) - 1)) +#define IsPow2(x) ((x)!=0 && ((x)&((x)-1))==0) +#define IsPow2OrZero(x) ((((x) - 1)&(x)) == 0) + +#define ExtractBit(word, idx) (((word) >> (idx)) & 1) + +#if LANG_CPP +# define zero_struct {} +#else +# define zero_struct {0} +#endif + +#if COMPILER_MSVC && COMPILER_MSVC_YEAR < 2015 +# define this_function_name "unknown" +#else +# define this_function_name __func__ +#endif diff --git a/code/base/namespace.h b/code/base/namespace.h new file mode 100644 index 0000000..750117a --- /dev/null +++ b/code/base/namespace.h @@ -0,0 +1,19 @@ + +// C++ namespace support +#if MD_DONT_USE_NAMESPACE || MD_COMPILER_C +# if MD_COMPILER_C +# define MD_NS +# define MD_NS_BEGIN +# define MD_NS_END +# else +# define MD_NS :: +# define MD_NS_BEGIN +# define MD_NS_END +# endif +#else + namespace MD {} + namespace md = MD; +# define MD_NS MD:: +# define MD_NS_BEGIN namespace MD { +# define MD_NS_END } +#endif diff --git a/code/base/platform.c b/code/base/platform.c new file mode 100644 index 0000000..9a4f908 --- /dev/null +++ b/code/base/platform.c @@ -0,0 +1,6 @@ +#ifdef MD_INTELLISENSE_DIRECTIVES +#pragma once +#include "platform.h" +#endif + + diff --git a/code/base/platform.h b/code/base/platform.h new file mode 100644 index 0000000..3488ecb --- /dev/null +++ b/code/base/platform.h @@ -0,0 +1,22 @@ +#if MD_INTELLISENSE_DIRECTIVES +#pragma once +#include "cracking_arch.h" +#include "cracking_os.h" +#include "cracking_compiler.h" +#endif + +#pragma region Mandatory Includes + +#include +#include + +#if defined( MD_SYSTEM_WINDOWS ) +# include +#endif + +#if MD_COMPILER_C +# include +# include +#endif + +#pragma endregion Mandatory Includes diff --git a/code/base/strings.h b/code/base/strings.h index 4fa7766..5cfbc43 100644 --- a/code/base/strings.h +++ b/code/base/strings.h @@ -160,9 +160,9 @@ internal B32 char_is_lower(U8 c); internal B32 char_is_alpha(U8 c); internal B32 char_is_slash(U8 c); internal B32 char_is_digit(U8 c, U32 base); -internal U8 char_to_lower(U8 c); -internal U8 char_to_upper(U8 c); -internal U8 char_to_correct_slash(U8 c); +internal U8 char_to_lower(U8 c); +internal U8 char_to_upper(U8 c); +internal U8 char_to_correct_slash(U8 c); //////////////////////////////// //~ rjf: C-String Measurement @@ -174,9 +174,9 @@ internal U64 cstring32_length(U32 *c); //////////////////////////////// //~ rjf: String Constructors -#define str8_lit(S) str8((U8*)(S), sizeof(S) - 1) -#define str8_lit_comp(S) {(U8*)(S), sizeof(S) - 1,} -#define str8_varg(S) (int)((S).size), ((S).str) +#define str8_lit(S) str8((U8*)(S), sizeof(S) - 1) +#define str8_lit_comp(S) { (U8*)(S), sizeof(S) - 1, } +#define str8_varg(S) (int)((S).size), ((S).str) #define str8_array(S,C) str8((U8*)(S), sizeof(*(S))*(C)) #define str8_array_fixed(S) str8((U8*)(S), sizeof(S)) @@ -367,8 +367,9 @@ internal void str8_serial_push_u16(Arena *arena, String8List *srl, U16 x); internal void str8_serial_push_u8(Arena *arena, String8List *srl, U8 x); internal void str8_serial_push_cstr(Arena *arena, String8List *srl, String8 str); internal void str8_serial_push_string(Arena *arena, String8List *srl, String8 str); + #define str8_serial_push_array(arena, srl, ptr, count) str8_serial_push_data(arena, srl, ptr, sizeof(*(ptr)) * (count)) -#define str8_serial_push_struct(arena, srl, ptr) str8_serial_push_array(arena, srl, ptr, 1) +#define str8_serial_push_struct(arena, srl, ptr) str8_serial_push_array(arena, srl, ptr, 1) //////////////////////////////// //~ rjf: Deserialization Helpers @@ -378,7 +379,8 @@ internal U64 str8_deserial_find_first_match(String8 string, U64 off, U16 scan internal void * str8_deserial_get_raw_ptr(String8 string, U64 off, U64 size);internal U64 str8_deserial_read_cstr(String8 string, U64 off, String8 *cstr_out); internal U64 str8_deserial_read_windows_utf16_string16(String8 string, U64 off, String16 *str_out); internal U64 str8_deserial_read_block(String8 string, U64 off, U64 size, String8 *block_out); -#define str8_deserial_read_array(string, off, ptr, count) str8_deserial_read((string), (off), (ptr), sizeof(*(ptr))*(count), sizeof(*(ptr))) -#define str8_deserial_read_struct(string, off, ptr) str8_deserial_read((string), (off), (ptr), sizeof(*(ptr)), sizeof(*(ptr))) + +#define str8_deserial_read_array(string, off, ptr, count) str8_deserial_read((string), (off), (ptr), sizeof(*(ptr)) * (count), sizeof( *(ptr))) +#define str8_deserial_read_struct(string, off, ptr) str8_deserial_read((string), (off), (ptr), sizeof(*(ptr)), sizeof( *(ptr))) #endif // BASE_STRINGS_H diff --git a/code/base/toolchain.h b/code/base/toolchain.h new file mode 100644 index 0000000..e5ece6a --- /dev/null +++ b/code/base/toolchain.h @@ -0,0 +1,38 @@ +#ifdef MD_INTELLISENSE_DIRECTIVES +#pragma once +#endif + +//////////////////////////////// +//~ rjf: Toolchain/Environment Enums + +typedef enum OperatingSystem +{ + OperatingSystem_Null, + OperatingSystem_Windows, + OperatingSystem_Linux, + OperatingSystem_Mac, + OperatingSystem_COUNT, +} +OperatingSystem; + +typedef enum Architecture +{ + Architecture_Null, + Architecture_x64, + Architecture_x86, + Architecture_arm64, + Architecture_arm32, + Architecture_COUNT, +} +Architecture; + +typedef enum Compiler +{ + Compiler_Null, + Compiler_msvc, + Compiler_gcc, + Compiler_clang, + Compiler_COUNT, +} +Compiler; +