From 9cd190c816cc810e992152669a9ee5ca3e957fca Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Wed, 28 Sep 2022 15:51:23 +0200 Subject: [PATCH] Update example --- README.md | 48 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 077f5cb..8007635 100644 --- a/README.md +++ b/README.md @@ -4,10 +4,16 @@ A compiled language that assumes C as base reality but it also has lots of ideas The language is currently **very debuggable**. It can produce readable C code with line directives. This allows you to debug the programs with Visual Studio with full source mapping, exactly like you would debug C programs. -## Language example code +## Language examples + +* More examples can be found in /examples and /modules ``` C -W :: #import "Windows.kl" +// +// Virtual memory abstraction using Windows API +// + +#import "kernel32.kl" #import "base.kl" PAGE_SIZE :: 4096 @@ -16,34 +22,48 @@ Memory :: struct reserve: SizeU data : *U8 -reserve :: (size: SizeU): Memory - result := Memory{reserve=align_up(size, PAGE_SIZE)} - result.data = W.VirtualAlloc( - flProtect = W.PAGE_READWRITE, +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 = W.MEM_RESERVE, + flAllocationType = MEM_RESERVE, 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 := W.VirtualAlloc( + result := VirtualAlloc( lpAddress = (m.data + m.commit)->*void, dwSize = adjusted_commit, - flAllocationType = W.MEM_COMMIT, - flProtect = W.PAGE_READWRITE, + flAllocationType = MEM_COMMIT, + flProtect = PAGE_READWRITE, ) + Assert(result != 0) m.commit += adjusted_commit return true return false -// Examples that showcase language features can be found in /examples +Release :: (m: *Memory) + result := VirtualFree(m.data->*void, 0, MEM_RELEASE) + if result != 0 + m.data = 0 + m.commit = 0 + m.reserve = 0 + ``` + ## Features * Order independent declarations