diff --git a/src/base/base_arena.h b/src/base/base_arena.h index 2a0c980..75179a6 100644 --- a/src/base/base_arena.h +++ b/src/base/base_arena.h @@ -68,7 +68,7 @@ internal void arena_clear(Arena *arena); internal void arena_pop_off(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_no_zero(arena, Type, count) push_array_no_zero_aligned(arena, Type, count, Max(8, Align_Of(Type))) #define push_array(a, T, c) push_array_aligned(a, T, c, Max(8, Align_Of(T))) // Temp diff --git a/src/base/base_core.h b/src/base/base_core.h index 5bf9dca..fee4269 100644 --- a/src/base/base_core.h +++ b/src/base/base_core.h @@ -10,6 +10,11 @@ #define local static #define internal static +#if COMPILER_MSVC + #pragma section(".rdata$", read) + #define read_only __declspec(allocate(".rdata$")) +#endif + // Base Types typedef uint8_t u8; typedef uint16_t u16; @@ -53,7 +58,6 @@ global u64 maxu64 = 0xffffffffffffffffull; #define C_LINK_BEGIN #define C_LINK_END #define C_LINK - #endif // Threads stuff @@ -63,6 +67,73 @@ global u64 maxu64 = 0xffffffffffffffffull; #error "Thread local storage supported this platform." #endif +// Bitmasks +global const u32 bitmask1 = 0x00000001; +global const u32 bitmask2 = 0x00000003; +global const u32 bitmask3 = 0x00000007; +global const u32 bitmask4 = 0x0000000f; +global const u32 bitmask5 = 0x0000001f; +global const u32 bitmask6 = 0x0000003f; +global const u32 bitmask7 = 0x0000007f; +global const u32 bitmask8 = 0x000000ff; +global const u32 bitmask9 = 0x000001ff; +global const u32 bitmask10 = 0x000003ff; +global const u32 bitmask11 = 0x000007ff; +global const u32 bitmask12 = 0x00000fff; +global const u32 bitmask13 = 0x00001fff; +global const u32 bitmask14 = 0x00003fff; +global const u32 bitmask15 = 0x00007fff; +global const u32 bitmask16 = 0x0000ffff; +global const u32 bitmask17 = 0x0001ffff; +global const u32 bitmask18 = 0x0003ffff; +global const u32 bitmask19 = 0x0007ffff; +global const u32 bitmask20 = 0x000fffff; +global const u32 bitmask21 = 0x001fffff; +global const u32 bitmask22 = 0x003fffff; +global const u32 bitmask23 = 0x007fffff; +global const u32 bitmask24 = 0x00ffffff; +global const u32 bitmask25 = 0x01ffffff; +global const u32 bitmask26 = 0x03ffffff; +global const u32 bitmask27 = 0x07ffffff; +global const u32 bitmask28 = 0x0fffffff; +global const u32 bitmask29 = 0x1fffffff; +global const u32 bitmask30 = 0x3fffffff; +global const u32 bitmask31 = 0x7fffffff; +global const u32 bitmask32 = 0xffffffff; + +global const u32 bit1 = (1 << 0); +global const u32 bit2 = (1 << 1); +global const u32 bit3 = (1 << 2); +global const u32 bit4 = (1 << 3); +global const u32 bit5 = (1 << 4); +global const u32 bit6 = (1 << 5); +global const u32 bit7 = (1 << 6); +global const u32 bit8 = (1 << 7); +global const u32 bit9 = (1 << 8); +global const u32 bit10 = (1 << 9); +global const u32 bit11 = (1 << 10); +global const u32 bit12 = (1 << 11); +global const u32 bit13 = (1 << 12); +global const u32 bit14 = (1 << 13); +global const u32 bit15 = (1 << 14); +global const u32 bit16 = (1 << 15); +global const u32 bit17 = (1 << 16); +global const u32 bit18 = (1 << 17); +global const u32 bit19 = (1 << 18); +global const u32 bit20 = (1 << 19); +global const u32 bit21 = (1 << 20); +global const u32 bit22 = (1 << 21); +global const u32 bit23 = (1 << 22); +global const u32 bit24 = (1 << 23); +global const u32 bit25 = (1 << 24); +global const u32 bit26 = (1 << 25); +global const u32 bit27 = (1 << 26); +global const u32 bit28 = (1 << 27); +global const u32 bit29 = (1 << 28); +global const u32 bit30 = (1 << 29); +global const u32 bit31 = (1 << 30); +global const u32 bit32 = (1 << 31); + // Misc helpers // NOTE(tijani): Divides total size of array by size of one element in the array gives // the number of elements in the array. diff --git a/src/base/base_markup.c b/src/base/base_markup.c index 6075fb0..d12105b 100644 --- a/src/base/base_markup.c +++ b/src/base/base_markup.c @@ -4,7 +4,7 @@ internal void set_thread_name(String8 str) { } internal void set_thread_namef(char *fmt, ...) { - Temp scratch = temp_begin(0, 0); + Temp scratch = scratch_begin(0, 0); va_list args; va_start(args, fmt); String8 string = push_str8fv(scratch.arena, fmt, args); diff --git a/src/base/base_strings.c b/src/base/base_strings.c index c4b8d43..4c4f6fa 100644 --- a/src/base/base_strings.c +++ b/src/base/base_strings.c @@ -3,13 +3,19 @@ internal String8 str8(u8 *str, u64 size) { return (result); } +internal String16 str16(u16 *str, u64 size) { + String16 result = {str, size}; + return (result); +} + // Unicode // UTF-8 Decoding/Encoding // NOTE(tijani): Lookup table // clang-format off -read_only global U8 utf8_class[32] = { +// NOTE(tijani): Lookup table, stored in read only segment +read_only global u8 utf8_class[32] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 3, 3, 4, 5, }; @@ -29,7 +35,7 @@ internal u32 utf8_encode(u8 *str, u32 codepoint) { str[1] = bit8 | ((codepoint >> 6) & bitmask6); str[2] = bit8 | (codepoint & bitmask6); inc = 3; - } else if (codpoint <= 0x10FFFF) { + } else if (codepoint <= 0x10FFFF) { str[0] = (bitmask4 << 4) | ((codepoint >> 18) & bitmask3); str[1] = bit8 | ((codepoint >> 12) & bitmask6); str[2] = bit8 | (codepoint >> 6 & bitmask6); @@ -65,10 +71,10 @@ internal UnicodeDecode utf8_decode(u8 *str, u64 max) { case 3: { if (2 < max) { - u8 cont_byte = {str[1], str[2]}; + u8 cont_byte[2] = {str[1], str[2]}; if (utf8_class[cont_byte[0] >> 3] == 0 && utf8_class[cont_byte[1] >> 3] == 0) { result.codepoint = (byte & bitmask4) << 12; - result.codepoint |= ((cont_byte[0] & bytemask6) << 6); + result.codepoint |= ((cont_byte[0] & bitmask6) << 6); result.codepoint |= (cont_byte[1] & bitmask6); result.inc = 3; } @@ -80,11 +86,11 @@ internal UnicodeDecode utf8_decode(u8 *str, u64 max) { u8 cont_byte[3] = {str[1], str[2], str[3]}; if (utf8_class[cont_byte[0] >> 3] == 0 && utf8_class[cont_byte[1] >> 3] == 0 && utf8_class[cont_byte[2] >> 3] == 0) { - result.codeponit = (byte & bitmask3) << 18; + result.codepoint = (byte & bitmask3) << 18; result.codepoint |= ((cont_byte[0] & bitmask6) << 12); result.codepoint |= ((cont_byte[1] * bitmask6) << 6); - result.codepoint |= (cont - byte[2] & bitmask6); - result.inc = 4 + result.codepoint |= (cont_byte[2] & bitmask6); + result.inc = 4; } } } @@ -129,7 +135,7 @@ internal String8 str8_from_16(Arena *arena, String16 input) { UnicodeDecode consume; for (; ptr < one_past_last; ptr += consume.inc) { - consume = u16_decode(ptr, one_past_last - ptr); + consume = utf16_decode(ptr, one_past_last - ptr); size += utf8_encode(string + size, consume.codepoint); } @@ -138,7 +144,7 @@ internal String8 str8_from_16(Arena *arena, String16 input) { return (str8(string, size)); } -internal String8 str16_from_8(Arena *arena, String8 input) { +internal String16 str16_from_8(Arena *arena, String8 input) { u64 capacity = input.size * 2; u16 *string = push_array_no_zero(arena, u16, capacity + 1); u8 *ptr = input.str; @@ -161,8 +167,20 @@ internal String8 push_str8_copy(Arena *arena, String8 string_to_copy) { l_string.size = string_to_copy.size; l_string.str = push_array_no_zero(arena, u8, l_string.size + 1); MemoryCopy(l_string.str, string_to_copy.str, string_to_copy.size); - l_string.str[l_string.str] = 0; + l_string.str[l_string.size] = 0; return (l_string); } -internal String8 push_str8fv(Arena *arena, char *fmt, va_list) +internal String8 push_str8fv(Arena *arena, char *fmt, va_list args) { + va_list l_args; + va_copy(l_args, args); + u32 needed_bytes = vsnprintf(0, 0, fmt, args) + 1; // NOTE(tijani): might need to switch to STB's version + String8 result = {0}; + + result.str = push_array_no_zero(arena, u8, needed_bytes); + result.size = + vsnprintf((char *)result.str, needed_bytes, fmt, l_args); // NOTE(tijani): might need to switch to STB's version + result.str[result.size] = 0; + va_end(l_args); + return (result); +} diff --git a/src/base/base_strings.h b/src/base/base_strings.h index 98af209..a8d596d 100644 --- a/src/base/base_strings.h +++ b/src/base/base_strings.h @@ -50,10 +50,11 @@ internal u32 utf16_encode(u16 *str, u32 codepoint); internal UnicodeDecode utf16_decode(u16 *str, u64 max); // String Conversion -internal String8 str8_from_16(Arena *arena, String16 input); -internal String8 str16_from_8(Arena *arena, String8 input); +internal String8 str8_from_16(Arena *arena, String16 input); +internal String16 str16_from_8(Arena *arena, String8 input); // String formatting & copying internal String8 push_str8_copy(Arena *arena, String8 string_to_copy); +internal String8 push_str8fv(Arena *arena, char *fmt, va_list args); #endif // BASE_STRINGS_H diff --git a/src/base/base_threading_context.c b/src/base/base_threading_context.c index 5d25e82..b0c6753 100644 --- a/src/base/base_threading_context.c +++ b/src/base/base_threading_context.c @@ -16,7 +16,7 @@ internal void thctx_release(void) { } } -internal THCTX *thctx_get_equipped(void){return (thctx_thread_local)} +internal THCTX *thctx_get_equipped(void) { return (thctx_thread_local); } // NOTE(tijani): This is used by a thread get temporary memory // for use (scratch space). It makes sure their are no conflics @@ -29,7 +29,7 @@ internal Arena *thctx_get_scratch(Arena **conflicts, u64 count) { for (u64 i = 0; i < ArrayCount(thctx->arenas); i += 1, arena_ptr += 1) { Arena **conflicts_ptr = conflicts; - b32 has_conflict = 0; + b32 has_conflicts = 0; for (u64 j = 0; j < count; j += 1, conflicts_ptr += 1) { if (*arena_ptr == *conflicts_ptr) { diff --git a/src/os/core/os_core.h b/src/os/core/os_core.h index e26a8d0..9690d4f 100644 --- a/src/os/core/os_core.h +++ b/src/os/core/os_core.h @@ -39,4 +39,10 @@ internal void os_release(void *ptr, u64 size); internal void *os_reserve_large(u64 size); internal b32 os_commit_large(void *ptr, u64 size); +// %os_hooks(implemented per-os) Threads +internal u32 os_thread_id(void); +internal void os_set_thread_name(String8 name); + +// %os_hooks(implemented per-os) Aborting +internal void os_abort(s32 exit_code); #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 f8f5778..40a49e5 100644 --- a/src/os/core/win32/os_core_win32.c +++ b/src/os/core/win32/os_core_win32.c @@ -56,7 +56,7 @@ internal void os_set_thread_name(String8 name) { // ATTENTION(tijani): modern way of setting thread description if (w32_SetThreadDescription_func) { String16 name16 = str16_from_8(scratch.arena, name); - HRESULT = w32_SetThreadDescription_func(GetCurrentThread(), (WCHAR *)name16.str); + HRESULT hr = w32_SetThreadDescription_func(GetCurrentThread(), (WCHAR *)name16.str); } // ATTENTION(tijani): setting thread descrption on older windows version @@ -87,3 +87,6 @@ internal void os_set_thread_name(String8 name) { scratch_end(scratch); } + +// %os_hooks(implemented per-os) Aborting +internal void os_abort(s32 exit_code) { ExitProcess(exit_code); } diff --git a/src/os/core/win32/os_core_win32.h b/src/os/core/win32/os_core_win32.h index e89e6f0..76de155 100644 --- a/src/os/core/win32/os_core_win32.h +++ b/src/os/core/win32/os_core_win32.h @@ -1,9 +1,12 @@ #ifndef OS_CORE_WIN32_H #define OS_CORE_WIN32_H +// clang-format off #define WIN32_LEAN_AND_MEAN -#include #include +#include + +// clang-format on // Application State typedef struct OS_W32_State OS_W32_State; @@ -19,8 +22,4 @@ struct OS_W32_State { // Globals global OS_W32_State os_w32_state = {0}; -// %os_hooks(implemented per-os) Threads -internal u32 os_thread_id(void); -internal void os_set_thread_name(String8 name); - #endif // OS_CORE_WIN32_H