Update example

This commit is contained in:
Krzosa Karol
2022-09-28 15:51:23 +02:00
parent aa1da4e926
commit 9cd190c816

View File

@@ -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. 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 ``` C
W :: #import "Windows.kl" //
// Virtual memory abstraction using Windows API
//
#import "kernel32.kl"
#import "base.kl" #import "base.kl"
PAGE_SIZE :: 4096 PAGE_SIZE :: 4096
@@ -16,34 +22,48 @@ Memory :: struct
reserve: SizeU reserve: SizeU
data : *U8 data : *U8
reserve :: (size: SizeU): Memory ProcessHeap: HANDLE
result := Memory{reserve=align_up(size, PAGE_SIZE)} Allocate :: (size: U64): *void
result.data = W.VirtualAlloc( if ProcessHeap == 0
flProtect = W.PAGE_READWRITE, 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, dwSize = result.reserve,
flAllocationType = W.MEM_RESERVE, flAllocationType = MEM_RESERVE,
lpAddress = 0)->*U8 lpAddress = 0)->*U8
return result return result
commit :: (m: *Memory, size: SizeU): Bool Commit :: (m: *Memory, size: SizeU): Bool
commit_size := align_up(size, PAGE_SIZE) commit_size := AlignUp(size, PAGE_SIZE)
total_commit := m.commit + commit_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 adjusted_commit := clamped_commit - m.commit
if adjusted_commit != 0 if adjusted_commit != 0
result := W.VirtualAlloc( result := VirtualAlloc(
lpAddress = (m.data + m.commit)->*void, lpAddress = (m.data + m.commit)->*void,
dwSize = adjusted_commit, dwSize = adjusted_commit,
flAllocationType = W.MEM_COMMIT, flAllocationType = MEM_COMMIT,
flProtect = W.PAGE_READWRITE, flProtect = PAGE_READWRITE,
) )
Assert(result != 0)
m.commit += adjusted_commit m.commit += adjusted_commit
return true return true
return false 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 ## Features
* Order independent declarations * Order independent declarations