Change to globals are PascalCase, locals are snake_case
This commit is contained in:
@@ -1,74 +1,31 @@
|
||||
Os :: #import "os_windows.kl"
|
||||
OS :: #import "os_windows.kl"
|
||||
#import "arena.kl"
|
||||
SizeU :: U64
|
||||
arena_di: U64
|
||||
|
||||
ADDITIONAL_COMMIT_SIZE :: 1024*1024
|
||||
DEFAULT_RESERVE_SIZE :: 1024*1024*1024
|
||||
DEFAULT_ALIGNMENT :: 8
|
||||
|
||||
Arena :: struct
|
||||
di: U64 // @debug_id
|
||||
memory: Os.Memory
|
||||
alignment: U64
|
||||
len: U64
|
||||
|
||||
clamp_top_sizeu :: (val: SizeU, max: SizeU): SizeU
|
||||
ClampTopSizeU :: (val: SizeU, max: SizeU): SizeU
|
||||
if val > max
|
||||
return max
|
||||
return val
|
||||
|
||||
get_align_offset :: (size: SizeU, align: SizeU): SizeU
|
||||
GetAlignOffset :: (size: SizeU, align: SizeU): SizeU
|
||||
mask := align - 1
|
||||
val := size & mask
|
||||
if val != 0
|
||||
val = align - val
|
||||
return val
|
||||
|
||||
align_up :: (size: SizeU, align: SizeU): SizeU
|
||||
result := size + get_align_offset(size, align)
|
||||
AlignUp :: (size: SizeU, align: SizeU): SizeU
|
||||
result := size + GetAlignOffset(size, align)
|
||||
return result
|
||||
|
||||
arena_init :: (a: *Arena)
|
||||
a.memory = Os.reserve(DEFAULT_RESERVE_SIZE)
|
||||
a.alignment = DEFAULT_ALIGNMENT
|
||||
a.di = arena_di++
|
||||
// a.allocator.proc = arena_allocator_proc
|
||||
|
||||
arena_push_size :: (a: *Arena, size: SizeU): *void
|
||||
generous_size := size + a.alignment
|
||||
if a.len + generous_size > a.memory.commit
|
||||
if a.memory.reserve == 0
|
||||
arena_init(a)
|
||||
result := Os.commit(&a.memory, generous_size + ADDITIONAL_COMMIT_SIZE)
|
||||
assert(result == true)
|
||||
a.len = align_up(a.len, a.alignment)
|
||||
assert(a.memory.reserve > a.len + a.memory.commit)
|
||||
result: *void = a.memory.data + a.len
|
||||
a.len += size
|
||||
return result
|
||||
|
||||
// @todo: Make this compile time thing!!!
|
||||
// This probably will wait till polymorphism stuff
|
||||
// something like this:
|
||||
// arena_push_type :: (a: *Arena, type: $T): *T
|
||||
//
|
||||
arena_push_type :: (a: *Arena, type: Type): *void
|
||||
type_info := get_type_info(type)
|
||||
assert(type_info != 0)
|
||||
return arena_push_size(a, type_info.size->SizeU)
|
||||
|
||||
arena_release :: (a: *Arena)
|
||||
Os.release(&a.memory)
|
||||
|
||||
|
||||
//
|
||||
// Unicode
|
||||
//
|
||||
question_mark16 :: 0x003f
|
||||
QuestionMark16 :: 0x003f
|
||||
String32 :: struct;; str: *U32; len: S64
|
||||
String16 :: struct;; str: *U16; len: S64
|
||||
|
||||
utf8_to_utf32 :: (c: *U8, max_advance: S64): U32, S64
|
||||
Utf8ToUtf32 :: (c: *U8, max_advance: S64): U32, S64
|
||||
out_str: U32
|
||||
advance: S64
|
||||
if (c[0] & 0b10000000) == 0
|
||||
@@ -100,7 +57,7 @@ utf8_to_utf32 :: (c: *U8, max_advance: S64): U32, S64
|
||||
|
||||
return out_str, advance
|
||||
|
||||
utf32_to_utf16 :: (codepoint: U32): [2]U16, S64
|
||||
Utf32ToUtf16 :: (codepoint: U32): [2]U16, S64
|
||||
str: [2]U16
|
||||
len := 0
|
||||
if codepoint < 0x10000
|
||||
@@ -114,24 +71,24 @@ utf32_to_utf16 :: (codepoint: U32): [2]U16, S64
|
||||
|
||||
return str, len
|
||||
|
||||
string_to_string16 :: (arena: *Arena, in: String): String16
|
||||
StringToString16 :: (arena: *Arena, in: String): String16
|
||||
in_str := &in[0]
|
||||
// @Note(Krzosa): Should be more then enough space
|
||||
alloc_size := (length_of(in)*2)+1
|
||||
result := String16{str = arena_push_size(arena, alloc_size->U64)}
|
||||
result := String16{str = PushSize(arena, alloc_size->U64)}
|
||||
for i := 0, i < length_of(in)
|
||||
s32, s32_len := utf8_to_utf32(in_str + i, length_of(in) - i)
|
||||
s32, s32_len := Utf8ToUtf32(in_str + i, length_of(in) - i)
|
||||
if s32_len != 0
|
||||
i += s32_len
|
||||
s16, s16_len := utf32_to_utf16(s32)
|
||||
s16, s16_len := Utf32ToUtf16(s32)
|
||||
if s16_len != 0
|
||||
for j := 0, j < s16_len, j++
|
||||
result.str[result.len++] = s16[j]
|
||||
else
|
||||
result.str[result.len++] = question_mark16
|
||||
result.str[result.len++] = QuestionMark16
|
||||
break
|
||||
else
|
||||
result.str[result.len++] = question_mark16
|
||||
result.str[result.len++] = QuestionMark16
|
||||
break
|
||||
|
||||
result.str[result.len] = 0
|
||||
@@ -139,14 +96,14 @@ string_to_string16 :: (arena: *Arena, in: String): String16
|
||||
|
||||
test_unicode :: (arena: *Arena)
|
||||
string := " 豈 更 車 賈 滑 串 句 龜 龜 契 金 喇 奈 懶 癩 羅 蘿 螺 裸 邏 樂 洛 烙 珞 落 酪 駱 亂 卵 欄 爛 蘭 鸞 嵐 濫 藍 襤 拉 臘 蠟 廊 朗 浪 狼 郎 來 冷 勞 擄 櫓 爐 盧 老 蘆 虜 路 露 魯 鷺 碌 祿 綠 菉 錄 鹿 論 壟 弄 籠 聾 牢 磊 賂 雷 壘 屢 樓 淚 漏 累 縷 陋 勒 肋 凜 凌 稜 綾 菱 陵 讀 拏 樂 諾 丹 寧 怒 率 異 北 磻 便 復 不 泌 數 索 參 塞 省 葉 說 殺 辰 沈 拾 若 掠 略 亮 兩 凉 梁 糧 良 諒 量 勵 ..."
|
||||
string_result := string_to_string16(arena, string)
|
||||
string_result := StringToString16(arena, string)
|
||||
print(string_result)
|
||||
|
||||
s32, s32_len := utf8_to_utf32('A', 1)
|
||||
s32, s32_len := Utf8ToUtf32('A', 1)
|
||||
assert(s32 == 'A', "Invalid decode")
|
||||
|
||||
s32_2, s32_len_2 := utf8_to_utf32('ć', 2)
|
||||
s32_2, s32_len_2 := Utf8ToUtf32('ć', 2)
|
||||
assert(s32_2 == 0x107, "Invalid decode")
|
||||
|
||||
s32_3, s32_len_3 := utf8_to_utf32('ó', 2)
|
||||
s32_3, s32_len_3 := Utf8ToUtf32('ó', 2)
|
||||
assert(s32_3 == 0xF3, "Invalid decode")
|
||||
|
||||
@@ -7,14 +7,14 @@ Memory :: struct
|
||||
reserve: SizeU
|
||||
data : *U8
|
||||
|
||||
process_heap: HANDLE
|
||||
allocate :: (size: U64): *void
|
||||
if process_heap == 0
|
||||
process_heap = GetProcessHeap()
|
||||
return HeapAlloc(process_heap, 0, size)
|
||||
ProcessHeap: HANDLE
|
||||
Allocate :: (size: U64): *void
|
||||
if ProcessHeap == 0
|
||||
ProcessHeap = GetProcessHeap()
|
||||
return HeapAlloc(ProcessHeap, 0, size)
|
||||
|
||||
reserve :: (size: SizeU): Memory
|
||||
result := Memory{reserve=align_up(size, PAGE_SIZE)}
|
||||
Reserve :: (size: SizeU): Memory
|
||||
result := Memory{reserve=AlignUp(size, PAGE_SIZE)}
|
||||
result.data = VirtualAlloc(
|
||||
flProtect = PAGE_READWRITE,
|
||||
dwSize = result.reserve,
|
||||
@@ -22,10 +22,10 @@ reserve :: (size: SizeU): Memory
|
||||
lpAddress = 0)->*U8
|
||||
return result
|
||||
|
||||
commit :: (m: *Memory, size: SizeU): Bool
|
||||
commit_size := align_up(size, PAGE_SIZE)
|
||||
Commit :: (m: *Memory, size: SizeU): Bool
|
||||
commit_size := AlignUp(size, PAGE_SIZE)
|
||||
total_commit := m.commit + commit_size
|
||||
clamped_commit := clamp_top_sizeu(total_commit, m.reserve)
|
||||
clamped_commit := ClampTopSizeU(total_commit, m.reserve)
|
||||
adjusted_commit := clamped_commit - m.commit
|
||||
if adjusted_commit != 0
|
||||
result := VirtualAlloc(
|
||||
@@ -38,28 +38,28 @@ commit :: (m: *Memory, size: SizeU): Bool
|
||||
return true
|
||||
return false
|
||||
|
||||
release :: (m: *Memory)
|
||||
Release :: (m: *Memory)
|
||||
result := VirtualFree(m.data->*void, 0, MEM_RELEASE)
|
||||
if result != 0
|
||||
m.data = 0
|
||||
m.commit = 0
|
||||
m.reserve = 0
|
||||
|
||||
write_console :: (string: String16)
|
||||
WriteConsole :: (string: String16)
|
||||
handle := GetStdHandle(STD_OUTPUT_HANDLE)
|
||||
WriteConsoleW(handle, string.str->*void, string.len->DWORD, 0, 0)
|
||||
|
||||
performance_frequency: F64
|
||||
time :: (): F64
|
||||
PerformanceFrequency: F64
|
||||
Time :: (): F64
|
||||
query: LARGE_INTEGER
|
||||
if !performance_frequency
|
||||
if !PerformanceFrequency
|
||||
err := QueryPerformanceFrequency(&query)
|
||||
assert(err != 0)
|
||||
performance_frequency = query->F64
|
||||
Assert(err != 0)
|
||||
PerformanceFrequency = query->F64
|
||||
|
||||
err := QueryPerformanceCounter(&query)
|
||||
assert(err != 0)
|
||||
result := query->F64 / performance_frequency
|
||||
Assert(err != 0)
|
||||
result := query->F64 / PerformanceFrequency
|
||||
return result
|
||||
|
||||
/**
|
||||
@@ -67,7 +67,7 @@ time :: (): F64
|
||||
* Written by Lukás Chmela
|
||||
* Released under GPLv3.
|
||||
*/
|
||||
itoa :: (value: S64, result: *U8, base: S64): *U8
|
||||
Itoa :: (value: S64, result: *U8, base: S64): *U8
|
||||
// check that the base if valid
|
||||
if (base < 2) || (base > 36)
|
||||
*result = 0 // '
|
||||
@@ -94,14 +94,14 @@ itoa :: (value: S64, result: *U8, base: S64): *U8
|
||||
return result
|
||||
|
||||
|
||||
print :: (string: String, args: ..)
|
||||
Print :: (string: String, args: ..)
|
||||
buffer: [1024]U8
|
||||
buffer_len: S64
|
||||
|
||||
arg_counter := 0
|
||||
for i := 0, i < length_of(string), i+=1
|
||||
if string[i] == '%'
|
||||
assert(arg_counter < length_of(args), "Passing too many [%] to a print lambda")
|
||||
Assert(arg_counter < length_of(args), "Passing too many [%] to a print lambda")
|
||||
arg := args[arg_counter++]
|
||||
|
||||
if arg.type == S64
|
||||
@@ -110,6 +110,6 @@ print :: (string: String, args: ..)
|
||||
p := itoa(value, &itoa_buff[0], 10)
|
||||
for *p != 0
|
||||
buffer[buffer_len++] = *p++
|
||||
else;; assert(false)
|
||||
else;; Assert(false)
|
||||
else
|
||||
buffer[buffer_len++] = string[i]
|
||||
Reference in New Issue
Block a user