aboutsummaryrefslogtreecommitdiff
path: root/std
diff options
context:
space:
mode:
authorAndrew Kelley <superjoe30@gmail.com>2017-09-27 22:59:58 -0400
committerAndrew Kelley <superjoe30@gmail.com>2017-09-27 22:59:58 -0400
commitfd5a5db400995d80015448d19e92daaca71a7f72 (patch)
tree625235a0cd98cefd5958ca687ba0d956b6e54ce9 /std
parent9ae66b4c67a66c3dcf8ce6107eca7a4744edd66d (diff)
downloadzig-fd5a5db400995d80015448d19e92daaca71a7f72.tar.gz
zig-fd5a5db400995d80015448d19e92daaca71a7f72.zip
implement IncrementingAllocator for Windows
Diffstat (limited to 'std')
-rw-r--r--std/mem.zig24
-rw-r--r--std/os/windows/index.zig6
2 files changed, 29 insertions, 1 deletions
diff --git a/std/mem.zig b/std/mem.zig
index 59ebfadebb..e16e947c25 100644
--- a/std/mem.zig
+++ b/std/mem.zig
@@ -86,12 +86,34 @@ pub const IncrementingAllocator = struct {
.end_index = 0,
};
},
+ Os.windows => {
+ const heap_handle = os.windows.GetProcessHeap();
+ const ptr = os.windows.HeapAlloc(heap_handle, 0, capacity);
+ return IncrementingAllocator {
+ .allocator = Allocator {
+ .allocFn = alloc,
+ .reallocFn = realloc,
+ .freeFn = free,
+ },
+ .bytes = @ptrCast(&u8, ptr)[0..capacity],
+ .end_index = 0,
+ };
+ },
else => @compileError("Unsupported OS"),
}
}
fn deinit(self: &IncrementingAllocator) {
- _ = os.posix.munmap(self.bytes.ptr, self.bytes.len);
+ switch (builtin.os) {
+ Os.linux, Os.darwin, Os.macosx, Os.ios => {
+ _ = os.posix.munmap(self.bytes.ptr, self.bytes.len);
+ },
+ Os.windows => {
+ const heap_handle = os.windows.GetProcessHeap();
+ _ = os.windows.HeapFree(heap_handle, 0, @ptrCast(os.windows.LPVOID, self.bytes.ptr));
+ },
+ else => @compileError("Unsupported OS"),
+ }
}
fn reset(self: &IncrementingAllocator) {
diff --git a/std/os/windows/index.zig b/std/os/windows/index.zig
index 2fc264f31b..8922de8599 100644
--- a/std/os/windows/index.zig
+++ b/std/os/windows/index.zig
@@ -39,6 +39,11 @@ pub extern "kernel32" stdcallcc fn WriteFile(in_hFile: HANDLE, in_lpBuffer: &con
pub extern "kernel32" stdcallcc fn Sleep(dwMilliseconds: DWORD);
+pub extern "kernel32" stdcallcc fn HeapAlloc(hHeap: HANDLE, dwFlags: DWORD, dwBytes: SIZE_T) -> LPVOID;
+
+pub extern "kernel32" stdcallcc fn HeapFree(hHeap: HANDLE, dwFlags: DWORD, lpMem: LPVOID) -> BOOL;
+
+pub extern "kernel32" stdcallcc fn GetProcessHeap() -> HANDLE;
pub extern "user32" stdcallcc fn MessageBoxA(hWnd: ?HANDLE, lpText: ?LPCTSTR, lpCaption: ?LPCTSTR, uType: UINT) -> c_int;
@@ -50,6 +55,7 @@ pub const LPWSTR = &WCHAR;
pub const LPSTR = &CHAR;
pub const CHAR = u8;
pub const PWSTR = &WCHAR;
+pub const SIZE_T = usize;
pub const BOOL = bool;
pub const BYTE = u8;