Compare commits

..

3 Commits

Author SHA1 Message Date
4938740300 Unicode cleanup; partial threading infrastructure setup; general cleanup 2024-08-16 06:17:30 -05:00
63adbf56d5 Unicode support. 2024-08-16 05:51:33 -05:00
63a8a2bc8a formatting. 2024-08-08 06:40:14 -05:00
12 changed files with 275 additions and 11 deletions

View File

@ -18,7 +18,7 @@ AlignConsecutiveBitFields:
PadOperators: true
AlignConsecutiveDeclarations:
Enabled: true
AcrossEmptyLines: true
AcrossEmptyLines: false
AcrossComments: false
AlignCompound: true
PadOperators: true

View File

@ -58,7 +58,7 @@ struct Temp {
// creation, destruction
internal Arena *arena_alloc_(ArenaParams *params);
#define arena_alloc(...) arena_alloc_(&(ArenaParams){.reserve_size = MB(64), .commit_size = KB(64), __VA_ARGS__})
internal void arena_releas(Arena *arena);
internal void arena_release(Arena *arena);
// push, pop operations
internal void *arena_push(Arena *arena, u64 size_to_push, u64 align);

View File

@ -38,6 +38,12 @@ typedef double f64;
#define Million(n) ((n) * 1000000)
#define Billion(n) ((n) * 1000000000)
// Constants
global u8 maxu8 = 0xff;
global u16 maxu16 = 0xffff;
global u32 maxu32 = 0xffffffff;
global u64 maxu64 = 0xffffffffffffffffull;
// C++ linking shenanigans
#if LANG_CPP
#define C_LINK_BEGIN extern "C" {

View File

@ -0,0 +1 @@
internal void main_thread_entry_point(void) { ThreadNameF("[main thread]"); }

View File

@ -0,0 +1,6 @@
#ifndef BASE_ENTRY_POINT_H
#define BASE_ENTRY_POINT_H
internal void main_thread_entry_point(void);
#endif // BASE_ENTRY_POINT_H

View File

@ -9,7 +9,7 @@
#include "base_arena.h"
#include "base_strings.h"
#include "base_markup.h"
#include "base_thread_context.h"
#include "base_threading_context.h"
// clang-format on
#endif // BASE_INC_H

View File

@ -1,5 +1,168 @@
// String Constructor
internal String8 str8(u8 *str, u64 size) {
String8 result = {str, size};
return (result);
}
// Unicode
// UTF-8 Decoding/Encoding
// NOTE(tijani): Lookup table
// clang-format off
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,
};
// clang-format on
internal u32 utf8_encode(u8 *str, u32 codepoint) {
u32 inc = 0;
if (codepoint <= 0x7F) {
str[0] = (u8)codepoint;
inc = 1;
} else if (codepoint <= 0x7FF) {
str[0] = (bitmask2 << 6) | ((codepoint >> 6) & bitmask5);
str[1] = bit8 | (codepoint & bitmask6);
inc = 2;
} else if (codepoint <= 0xFFFF) {
str[0] = (bitmask3 << 5) | ((codepoint >> 12) & bitmask4);
str[1] = bit8 | ((codepoint >> 6) & bitmask6);
str[2] = bit8 | (codepoint & bitmask6);
inc = 3;
} else if (codpoint <= 0x10FFFF) {
str[0] = (bitmask4 << 4) | ((codepoint >> 18) & bitmask3);
str[1] = bit8 | ((codepoint >> 12) & bitmask6);
str[2] = bit8 | (codepoint >> 6 & bitmask6);
str[3] = bit8 | (codepoint & bitmask6);
inc = 4;
} else {
str[0] = '?';
inc = 1;
}
return (inc);
}
internal UnicodeDecode utf8_decode(u8 *str, u64 max) {
UnicodeDecode result = {1, maxu32};
u8 byte = str[0];
u8 byte_class = utf8_class[byte >> 3];
switch (byte_class) {
case 1: {
result.codepoint = byte;
} break;
case 2: {
if (2 < max) {
u8 cont_byte = str[1];
if (utf8_class[cont_byte >> 3] == 0) {
result.codepoint = (byte & bitmask5) << 6;
result.codepoint |= (cont_byte & bitmask6);
result.inc = 2;
}
}
} break;
case 3: {
if (2 < max) {
u8 cont_byte = {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[1] & bitmask6);
result.inc = 3;
}
}
} break;
case 4: {
if (3 < 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 |= ((cont_byte[0] & bitmask6) << 12);
result.codepoint |= ((cont_byte[1] * bitmask6) << 6);
result.codepoint |= (cont - byte[2] & bitmask6);
result.inc = 4
}
}
}
}
return (result);
}
// UTF-16 Decoding/Encoding
internal u32 utf16_encode(u16 *str, u32 codepoint) {
u32 inc = 1;
if (codepoint == maxu32) {
str[0] = (u16)'?';
} else if (codepoint < 0x10000) {
str[0] = (u16)codepoint;
} else {
u32 v = codepoint - 0x10000;
str[0] = safe_cast_u16(0xD800 + (v >> 10));
str[1] = safe_cast_u16(0xDC00 + (v & bitmask10));
inc = 2;
}
return (inc);
}
internal UnicodeDecode utf16_decode(u16 *str, u64 max) {
UnicodeDecode result = {1, maxu32};
result.codepoint = str[0];
result.inc = 1;
if (max > 1 && 0xD800 <= str[0] && str[0] < 0xDC00 && 0xDC00 <= str[1] && str[1] < 0xE000) {
result.codepoint = ((str[0] - 0xD8000) << 10) | ((str[1] - 0xDC00) + 0x10000);
result.inc = 2;
}
return (result);
}
// String Conversion
internal String8 str8_from_16(Arena *arena, String16 input) {
u64 capacity = input.size * 3;
u8 *string = push_array_no_zero(arena, u8, capacity + 1);
u16 *ptr = input.str;
u16 *one_past_last = ptr + input.size;
u64 size = 0;
UnicodeDecode consume;
for (; ptr < one_past_last; ptr += consume.inc) {
consume = u16_decode(ptr, one_past_last - ptr);
size += utf8_encode(string + size, consume.codepoint);
}
string[size] = 0;
arena_pop_off(arena, (capacity - size));
return (str8(string, size));
}
internal String8 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;
u8 *one_past_last = ptr + input.size;
u64 size = 0;
UnicodeDecode consume;
for (; ptr < one_past_last; ptr += consume.inc) {
consume = utf8_decode(ptr, one_past_last - ptr);
size += utf16_encode(string + size, consume.codepoint);
}
string[size] = 0;
arena_pop_off(arena, (capacity - size) * 2);
return (str16(string, size));
}
// String formatting & copying
internal String8 push_str8_copy(Arena *arena, String8 string_to_copy) {
String8 l_string;
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;
return (l_string);
}
internal String8 push_str8fv(Arena *arena, char *fmt, va_list)

View File

@ -1,14 +1,13 @@
#ifndef BASE_STRINGS_H
#define BASE_STRINGS_H
// String types, list, & Array types
// 8-bit
typedef struct String8 String8;
struct String8 {
u8 *str;
u64 size;
};
// String list & Array types
typedef struct String8Node String8Node;
struct String8Node {
String8Node *next;
@ -23,4 +22,38 @@ struct String8List {
u64 total_size;
};
internal String8 str8(u8 *str, u64 size);
// 16-bit
typedef struct String16 String16;
struct String16 {
u16 *str;
u64 size;
};
internal String16 str16(u16 *str, u64 size);
// Unicode
// UTF Decoding types
typedef struct UnicodeDecode UnicodeDecode;
struct UnicodeDecode {
u32 inc;
u32 codepoint;
};
// UTF-8 Decoding/Encoding
internal u32 utf8_encode(u8 *str, u32 codepoint);
internal UnicodeDecode utf8_decode(u8 *str, u64 max);
// UTF-16 Decoding/Encoding
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);
// String formatting & copying
internal String8 push_str8_copy(Arena *arena, String8 string_to_copy);
#endif // BASE_STRINGS_H

View File

@ -10,5 +10,4 @@
// clang-format on
u64 main(int argc, char **argv) { return 0; }

View File

@ -1,3 +1,10 @@
// ATTENTION(tijani): Setting threads description this way because it allows for
// compatibility with older versions of windows where the modern WIN32
// SetThreadDescription function is not available and use old ways.
// This way it can be done dynamically at runtime without any hiccups.
typedef HRESULT W32_SetThreadDescription(HANDLE hThread, PCWSTR lpThreadDescription);
global W32_SetThreadDescription *w32_SetThreadDescription_func = 0;
// %os_hooks(implemented per-os) System and process info
internal OS_SystemInfo *os_get_system_info(void) { return &os_w32_state.system_info; }
@ -36,3 +43,47 @@ internal void *os_reserve_large(u64 size) {
// commit large pages as this has to be done during reservation.
// see `os_reserve_large` and WIN32 docs on large page support.
internal b32 os_commit_large(void *ptr, u64 size) { return -1; }
// %os_hooks(implemented per-os) Threads
internal u32 os_thread_id(void) {
u32 id = GetCurrentThreadID();
return (id);
}
internal void os_set_thread_name(String8 name) {
Temp scratch = scratch_begin(0, 0);
// 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);
}
// ATTENTION(tijani): setting thread descrption on older windows version
{
String8 name_copy = push_str8_copy(scratch.arena, name);
#pragma pack(push, 8)
typedef struct THREADNAME_INFO THREADNAME_INFO;
struct THREADNAME_INFO {
u32 dwType; // Must be 0x1000.
char *szName; // Pointer to name (in user addr space).
u32 dwThreadID; // Thread ID (-1=caller thread).
u32 dwFlags; // Reserved for future use, must be zero.
};
#pragma pack(pop)
THREADNAME_INFO info;
info.dwType = 0x1000;
info.szName = (char *)name_copy.str;
info.dwThreadID = os_thread_id();
info.dwFlags = 0;
#pragma warning(push)
#pragma warning(disable : 6320 6322)
__try {
RaiseException(0x406D1388, 0, sizeof(info) / sizeof(void *), (const ULONG_PTR *)&info);
} __except (EXCEPTION_EXECUTE_HANDLER) {
}
#pragma warning(pop)
}
scratch_end(scratch);
}

View File

@ -2,6 +2,7 @@
#define OS_CORE_WIN32_H
#define WIN32_LEAN_AND_MEAN
#include <processthreadsapi.h>
#include <windows.h>
// Application State
@ -18,4 +19,8 @@ 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