From b4cfc14d3b70638a429c9f20823d68376af7454f Mon Sep 17 00:00:00 2001 From: tijani Date: Tue, 6 Aug 2024 16:49:26 -0500 Subject: [PATCH] Formatting. --- .clang-format | 30 ++++++++--------- src/base/base_arena.h | 53 ++++++++++++++++++++----------- src/base/base_core.h | 43 ++++++++++++++----------- src/base/base_strings.h | 6 ++-- src/os/core/os_core.h | 28 ++++++++-------- src/os/core/win32/os_core_win32.c | 4 +-- src/os/core/win32/os_core_win32.h | 4 +-- 7 files changed, 94 insertions(+), 74 deletions(-) diff --git a/.clang-format b/.clang-format index 2d35c7f..47d8400 100644 --- a/.clang-format +++ b/.clang-format @@ -5,29 +5,29 @@ AccessModifierOffset: -2 AlignAfterOpenBracket: Align AlignArrayOfStructures: None AlignConsecutiveAssignments: - Enabled: false - AcrossEmptyLines: false + Enabled: true + AcrossEmptyLines: true AcrossComments: false - AlignCompound: false + AlignCompound: true PadOperators: true AlignConsecutiveBitFields: - Enabled: false - AcrossEmptyLines: false + Enabled: true + AcrossEmptyLines: true AcrossComments: false - AlignCompound: false - PadOperators: false + AlignCompound: true + PadOperators: true AlignConsecutiveDeclarations: - Enabled: false - AcrossEmptyLines: false + Enabled: true + AcrossEmptyLines: true AcrossComments: false - AlignCompound: false - PadOperators: false + AlignCompound: true + PadOperators: true AlignConsecutiveMacros: - Enabled: false - AcrossEmptyLines: false + Enabled: true + AcrossEmptyLines: true AcrossComments: false - AlignCompound: false - PadOperators: false + AlignCompound: true + PadOperators: true AlignEscapedNewlines: Right AlignOperands: Align AlignTrailingComments: true diff --git a/src/base/base_arena.h b/src/base/base_arena.h index 29943a7..c53590d 100644 --- a/src/base/base_arena.h +++ b/src/base/base_arena.h @@ -1,6 +1,21 @@ #ifndef BASE_ARENA_H #define BASE_ARENA_H +// Arena allocator simulates how stack memory works. +// This allows us the ability to allocate a reasonableamount of memory +// at startup and only use that allocated amount of memory throughout +// the lifetime of the application. Obviously this still uses +// "malloc, free, VirtualAlloc, VirtualFree" but instead of having to +// request and freeing memory during runtime, we create an environment +// where we have as much memory we could ever need and them portion it +// out in small chunk as needed. This has multiple benefits that would +// make itself glaring throughout the codebase but the gist of it is that +// we have one large block of memory throughout the lifetime, +// then application specific code that could need memory grab a chunck +// memory from the big block of memory we have reserved with the operating +// system at startup do whatever operation it needs to do, then return +// that block of memory back to the arena. + // Constants #define ARENA_HEADER_SIZE 64 @@ -11,28 +26,28 @@ enum { ArenaFlag_NoChain = (1 << 0); ArenaFlag_LargePages = (1 << 1); }; typedef struct ArenaParams ArenaParams; struct ArenaParams { ArenaFlags flags; - u64 reserve_size; - u64 commite_size; - void *optional_backing_buffer; // NOTE(tijani): ?? + u64 reserve_size; + u64 commite_size; + void *optional_backing_buffer; // NOTE(tijani): ?? }; typedef struct Arena Arena; struct Arena { - Arena *previous; // Previous arena in the chain - Arena *current; // Current arena in the chain + Arena *previous; // Previous arena in the chain + Arena *current; // Current arena in the chain ArenaFlags flags; - u32 commit_size; - u64 reserve_size; - u64 base_position; - u64 position; - u64 commit; - u64 reserve; + u32 commit_size; + u64 reserve_size; + u64 base_position; + u64 position; + u64 commit; + u64 reserve; }; typedef struct Temp Temp; struct Temp { Arena *arena; - u64 position; + u64 position; }; // creation, destruction @@ -42,14 +57,14 @@ internal void arena_releas(Arena *arena); // push, pop operations internal void *arena_psuh(Arena *arena, u64 size, u64 align); -internal u64 arena_position(Arena *arena); -internal void arena_pop_to(Arena *arena, u64 position); -internal void arena_clear(Arena *arena); -internal void arena_pop(Arena *arena, u64 amount); +internal u64 arena_position(Arena *arena); +internal void arena_pop_to(Arena *arena, u64 position); +internal void arena_clear(Arena *arena); +internal void arena_pop(Arena *arena, u64 amount); #define push_array_no_zero_aligned(a, T, c, align) (T *)arena_push((a), sizeof(T) * (c), (align)) -#define push_array_aligned(a, T, c, align) (T *)MemoryZero(push_array_no_zero_aligned(a, T, c, align), sizeof(T) * (c)) -#define push_array_no_zero(a, T, c) push_array_no_zero_aligned(a, T, c, Max(8, Align_Of(T))) -#define push_array(a, T, c) push_array_aligned(a, T, c, Max(8, Align_Of(T))) +#define push_array_aligned(a, T, c, align) (T *)MemoryZero(push_array_no_zero_aligned(a, T, c, align), sizeof(T) * (c)) +#define push_array_no_zero(a, T, c) push_array_no_zero_aligned(a, T, c, Max(8, Align_Of(T))) +#define push_array(a, T, c) push_array_aligned(a, T, c, Max(8, Align_Of(T))) // Temp internal Temp temp_begin(Arena *arena); diff --git a/src/base/base_core.h b/src/base/base_core.h index 1711155..606924c 100644 --- a/src/base/base_core.h +++ b/src/base/base_core.h @@ -6,37 +6,37 @@ #include #include -#define global static -#define local static +#define global static +#define local static #define internal static // Base Types -typedef uint8_t u8; +typedef uint8_t u8; typedef uint16_t u16; typedef uint32_t u32; typedef uint64_t u64; -typedef int8_t s8; -typedef int16_t s16; -typedef int32_t s32; -typedef int64_t s64; +typedef int8_t s8; +typedef int16_t s16; +typedef int32_t s32; +typedef int64_t s64; -typedef s8 b8; -typedef s16 b16; -typedef s32 b32; -typedef s64 b64; +typedef s8 b8; +typedef s16 b16; +typedef s32 b32; +typedef s64 b64; -typedef float f32; -typedef double f64; +typedef float f32; +typedef double f64; // Units -#define KB(n) (((u64)(n)) << 10) -#define MB(n) (((u64)(n)) << 20) -#define GB(n) (((u64)(n)) << 30) -#define TB(n) (((u64)(n)) << 40) +#define KB(n) (((u64)(n)) << 10) +#define MB(n) (((u64)(n)) << 20) +#define GB(n) (((u64)(n)) << 30) +#define TB(n) (((u64)(n)) << 40) #define Thousand(n) ((n) * 1000) -#define Million(n) ((n) * 1000000) -#define Billion(n) ((n) * 1000000000) +#define Million(n) ((n) * 1000000) +#define Billion(n) ((n) * 1000000000) // Memory operations #define MemoryZero(dest, count) memset((dest), 0, (count)) @@ -56,4 +56,9 @@ typedef double f64; // ??(tijani): the calculation of how this works breaks my brain, need to bust out the pen and paper to figure it out. #define AlignPow2(x, b) (((x) + (b) - 1) & (~((b) - 1))) +// Linkedlist building macros + +// Singlely linked lists (stacks) +#define SLLStackPush_N(head, new_node, next) ((new_node)->next = (head), (head) = (new_node)) +#define SLLStackPop_N(head, next) ((head) = (head)->next) #endif // BASE_CORE_H diff --git a/src/base/base_strings.h b/src/base/base_strings.h index 29cc290..65e7991 100644 --- a/src/base/base_strings.h +++ b/src/base/base_strings.h @@ -12,15 +12,15 @@ struct String8 { typedef struct String8Node String8Node; struct String8Node { String8Node *next; - String8 string; + String8 string; }; typedef struct String8List String8List; struct String8List { String8Node *first; String8Node *last; - u64 node_count; - u64 total_size; + u64 node_count; + u64 total_size; }; #endif // BASE_STRINGS_H diff --git a/src/os/core/os_core.h b/src/os/core/os_core.h index 59939e6..e26a8d0 100644 --- a/src/os/core/os_core.h +++ b/src/os/core/os_core.h @@ -4,39 +4,39 @@ // System Information typedef struct OS_SystemInfo OS_SystemInfo; struct OS_SystemInfo { - u32 logical_processor_count; - u64 page_size; - u64 large_page_size; - u64 allocation_granularity; // NOTE(tijani): ??? What does this mean? + u32 logical_processor_count; + u64 page_size; + u64 large_page_size; + u64 allocation_granularity; // NOTE(tijani): ??? What does this mean? String8 machine_name; }; // Process Information typedef struct OS_ProcessInfo OS_ProcessInfo; struct OS_ProcessInfo { - u32 process_id; - String8 binary_path; - String8 initial_path; - String8 user_program_data_path; + u32 process_id; + String8 binary_path; + String8 initial_path; + String8 user_program_data_path; String8List module_load_paths; String8List environment; }; // %os_hooks(implemented per-os) System and process info -internal OS_SystemInfo *os_get_system_info(void); +internal OS_SystemInfo *os_get_system_info(void); internal OS_ProcessInfo *os_get_process_info(void); -internal String8 os_get_current_path(Arena *arena); +internal String8 os_get_current_path(Arena *arena); // %os_hooks(implemented per-os) Memory allocation // NOTE(tijani): For normal page size -internal b32 os_commit(void *ptr, u64 size); +internal b32 os_commit(void *ptr, u64 size); internal void *os_reserve(u64 size); -internal void os_decommit(void *ptr, u64 size); -internal void os_release(void *ptr, u64 size); +internal void os_decommit(void *ptr, u64 size); +internal void os_release(void *ptr, u64 size); // NOTE(tijani): For large page size internal void *os_reserve_large(u64 size); -internal b32 os_commit_large(void *ptr, u64 size); +internal b32 os_commit_large(void *ptr, u64 size); #endif // OS_CORE_H diff --git a/src/os/core/win32/os_core_win32.c b/src/os/core/win32/os_core_win32.c index 8413760..305d6d8 100644 --- a/src/os/core/win32/os_core_win32.c +++ b/src/os/core/win32/os_core_win32.c @@ -1,9 +1,9 @@ // %os_hooks(implemented per-os) System and process info -internal OS_SystemInfo *os_get_system_info(void) { return &os_w32_state.system_info; } +internal OS_SystemInfo *os_get_system_info(void) { return &os_w32_state.system_info; } internal OS_ProcessInfo *os_get_process_info(void) { return &os_w32_state.process_info; } -internal String8 os_get_current_path(Arena *arena) {} +internal String8 os_get_current_path(Arena *arena) {} // %os_hooks(implemented per-os) Memory allocation diff --git a/src/os/core/win32/os_core_win32.h b/src/os/core/win32/os_core_win32.h index 5690a11..7ba319e 100644 --- a/src/os/core/win32/os_core_win32.h +++ b/src/os/core/win32/os_core_win32.h @@ -9,9 +9,9 @@ struct OS_W32_State { Arena *arena; // System information - OS_SystemInfo system_info; + OS_SystemInfo system_info; OS_ProcessInfo process_info; - u64 microsecond_resolution; + u64 microsecond_resolution; }; // Globals