diff --git a/Examples/Smoke/Src/Main.rux b/Examples/Smoke/Src/Main.rux index af6bd62..d170f6f 100644 --- a/Examples/Smoke/Src/Main.rux +++ b/Examples/Smoke/Src/Main.rux @@ -4,7 +4,7 @@ Licensed under the MIT License */ -import MacOS::{ GetStdHandle, StdHandle, WriteFile, ReadFile, GetProcessHeap, HeapAlloc, HeapFree }; +import MacOS::{ GetStdHandle, StdHandle, WriteFile, ReadFile, GetProcessHeap, HeapAlloc, HeapFree, Munmap }; // Exercises the macOS bindings end-to-end: a non-zero exit code identifies the // step that failed; 0 means every binding behaved as expected. @@ -22,7 +22,7 @@ func Main() -> int32 { // heap: allocate, write, read back, free let heap = GetProcessHeap(); - let buf = HeapAlloc(heap, 0, 32u) as *char8; + let buf = HeapAlloc(heap, 0u32, 32u) as *char8; if buf == null { return 3i32; } @@ -31,7 +31,7 @@ func Main() -> int32 { if *buf != c8'R' || *(buf + 1u) != c8'X' { return 4i32; } - if HeapFree(heap, 0, buf as *opaque) == 0 { + if HeapFree(heap, 0u32, buf as *opaque) == 0 { return 5i32; } @@ -44,5 +44,18 @@ func Main() -> int32 { return 6i32; } + // mmap/munmap roundtrip: allocate a page, write/read, then unmap it. + let page = HeapAlloc(heap, 0u32, 4096u) as *char8; + if page == null { + return 7i32; + } + *page = c8'M'; + if *page != c8'M' { + return 8i32; + } + if Munmap(page as *opaque, 4096u) != 0i64 { + return 9i32; + } + return 0i32; } diff --git a/Src/Platform.rux b/Src/Platform.rux index 5b3098a..b40b08d 100644 --- a/Src/Platform.rux +++ b/Src/Platform.rux @@ -57,5 +57,10 @@ module MacOS { /// Frees a memory block. Currently a no-op on macOS (the backing pages /// are not unmapped), so it always reports success. func HeapFree(heap: *opaque, flags: uint32, mem: *opaque) -> bool32; + + /// Unmaps a memory region. Backed by SYS_munmap; addr must be the base + /// of a prior HeapAlloc mapping and length its full size. Returns 0 on + /// success. + func Munmap(addr: *opaque, length: uint) -> int64; } }