diff --git a/modules/OSWin32.core b/modules/OSWin32.core new file mode 100644 index 0000000..56393cf --- /dev/null +++ b/modules/OSWin32.core @@ -0,0 +1,65 @@ +#import "KERNEL32.core" +#import "Base.core" + +PAGE_SIZE :: 4096 +Memory :: struct + commit : SizeU + reserve: SizeU + data : *U8 + +ProcessHeap: HANDLE +Allocate :: (size: U64): *void + if ProcessHeap == 0 + ProcessHeap = GetProcessHeap() + return HeapAlloc(ProcessHeap, 0, size) + +Reserve :: (size: SizeU): Memory + result := Memory{reserve=AlignUp(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 := AlignUp(size, PAGE_SIZE) + total_commit := m.commit + commit_size + clamped_commit := ClampTopSizeU(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, + ) + Assert(result != 0) + 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 + +WriteConsole :: (string: String16) + handle := GetStdHandle(STD_OUTPUT_HANDLE) + WriteConsoleW(handle, string.str->*void, string.len->DWORD, 0, 0) + +PerformanceFrequency: F64 +PerformanceFrequency_S64: S64 +Time :: (): F64 + query: LARGE_INTEGER + if PerformanceFrequency_S64 == 0 + err := QueryPerformanceFrequency(&PerformanceFrequency_S64) + Assert(err != 0) + PerformanceFrequency = PerformanceFrequency_S64->F64 + + err := QueryPerformanceCounter(&query) + Assert(err != 0) + result := query->F64 / PerformanceFrequency + return result