Emitting proper lines and files, coding in the language!

This commit is contained in:
Krzosa Karol
2022-06-14 14:15:21 +02:00
parent f885abe3f5
commit d63a327e3e
5 changed files with 62 additions and 49 deletions

44
os.kl Normal file
View File

@@ -0,0 +1,44 @@
#import "Windows.kl"
#import "base.kl"
PAGE_SIZE :: 4096
Memory :: struct
commit : SizeU
reserve: SizeU
data : *U8
reserve :: (size: SizeU): Memory
result := Memory{reserve=align_up(size, PAGE_SIZE)}
result.data = VirtualAlloc(
flProtect = PAGE_READWRITE,
dwSize = result.reserve,
flAllocationType = MEM_RESERVE,
lpAddress = 0)->*U8
return result
commit :: (m: *Memory, size: SizeU): Bool
commit_size := align_up(size, 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 := VirtualAlloc(
lpAddress = (m.data + m.commit)->*void,
dwSize = adjusted_commit,
flAllocationType = MEM_COMMIT,
flProtect = PAGE_READWRITE,
)
m.commit += adjusted_commit
return true
return false
release :: (m: *Memory)
result := VirtualFree(m.data->*void, 0, MEM_RELEASE)
if result != 0
m.data = 0
m.commit = 0
m.reserve = 0
print :: (string: String)
handle := GetStdHandle(STD_OUTPUT_HANDLE)
WriteConsoleA(handle, &string[0]->*void, length_of(string)->DWORD, 0, 0)