Writing more real program and squashing bugs

This commit is contained in:
Krzosa Karol
2022-06-13 20:22:40 +02:00
parent 955167ce18
commit 2c431e3207
6 changed files with 68 additions and 15 deletions

41
base.kl
View File

@@ -1,12 +1,47 @@
#import "Windows.kl"
SizeU :: #strict U64
OS_PAGE_SIZE :: 4096
OS_Memory :: struct
commit : U64
reserve: U64
commit : SizeU
reserve: SizeU
data : *U8
Arena :: struct
memory: OS_Memory
alignment: U64
len: U64
len: SizeU
get_align_offset :: (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)
return result
reserve :: (size: SizeU): OS_Memory
result := OS_Memory{reserve=align_up(size, OS_PAGE_SIZE)}
result.data = VirtualAlloc(0, result.reserve, MEM_RESERVE, PAGE_READWRITE)->*U8
return result
clamp_top_sizeu :: (val: SizeU, max: SizeU): SizeU
if val > max
return max
return val
commit :: (m: *OS_Memory, size: SizeU): Bool
commit_size := align_up(size, OS_PAGE_SIZE)
total_commit := m.commit + commit_size
clamped_commit := clamp_top_sizeu(total_commit, m.reserve)
adjusted_commit := clamped_commit - m.commit
if adjusted_commit != 0
result := VirtualFree(m.data->*void, 0, MEM_RELEASE)
m.commit += adjusted_commit
return true
return false