#import "kernel32.kl" #import "base.kl" PAGE_SIZE :: 4096 Memory :: struct commit : SizeU reserve: SizeU data : *U8 process_heap: HANDLE allocate :: (size: U64): *void if process_heap == 0 process_heap = GetProcessHeap() return HeapAlloc(process_heap, 0, size) 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: String16) handle := GetStdHandle(STD_OUTPUT_HANDLE) WriteConsoleW(handle, string.str->*void, string.len->DWORD, 0, 0) performance_frequency: F64 time :: (): F64 query: LARGE_INTEGER if !performance_frequency err := QueryPerformanceFrequency(&query) assert(err != 0) performance_frequency = query->F64 err := QueryPerformanceCounter(&query) assert(err != 0) result := query->F64 / performance_frequency return result