authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-09-27 22:59:58-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-09-27 22:59:58-04:00
logfd5a5db400995d80015448d19e92daaca71a7f72
tree625235a0cd98cefd5958ca687ba0d956b6e54ce9
parent9ae66b4c67a66c3dcf8ce6107eca7a4744edd66d

implement IncrementingAllocator for Windows


2 files changed, 29 insertions(+), 1 deletions(-)

std/mem.zig+23-1
...@@ -86,12 +86,34 @@ pub const IncrementingAllocator = struct {...@@ -86,12 +86,34 @@ pub const IncrementingAllocator = struct {
86 .end_index = 0,86 .end_index = 0,
87 };87 };
88 },88 },
89 Os.windows => {
90 const heap_handle = os.windows.GetProcessHeap();
91 const ptr = os.windows.HeapAlloc(heap_handle, 0, capacity);
92 return IncrementingAllocator {
93 .allocator = Allocator {
94 .allocFn = alloc,
95 .reallocFn = realloc,
96 .freeFn = free,
97 },
98 .bytes = @ptrCast(&u8, ptr)[0..capacity],
99 .end_index = 0,
100 };
101 },
89 else => @compileError("Unsupported OS"),102 else => @compileError("Unsupported OS"),
90 }103 }
91 }104 }
92105
93 fn deinit(self: &IncrementingAllocator) {106 fn deinit(self: &IncrementingAllocator) {
94 _ = os.posix.munmap(self.bytes.ptr, self.bytes.len);107 switch (builtin.os) {
108 Os.linux, Os.darwin, Os.macosx, Os.ios => {
109 _ = os.posix.munmap(self.bytes.ptr, self.bytes.len);
110 },
111 Os.windows => {
112 const heap_handle = os.windows.GetProcessHeap();
113 _ = os.windows.HeapFree(heap_handle, 0, @ptrCast(os.windows.LPVOID, self.bytes.ptr));
114 },
115 else => @compileError("Unsupported OS"),
116 }
95 }117 }
96118
97 fn reset(self: &IncrementingAllocator) {119 fn reset(self: &IncrementingAllocator) {
std/os/windows/index.zig+6
...@@ -39,6 +39,11 @@ pub extern "kernel32" stdcallcc fn WriteFile(in_hFile: HANDLE, in_lpBuffer: &con...@@ -39,6 +39,11 @@ pub extern "kernel32" stdcallcc fn WriteFile(in_hFile: HANDLE, in_lpBuffer: &con
3939
40pub extern "kernel32" stdcallcc fn Sleep(dwMilliseconds: DWORD);40pub extern "kernel32" stdcallcc fn Sleep(dwMilliseconds: DWORD);
4141
42pub extern "kernel32" stdcallcc fn HeapAlloc(hHeap: HANDLE, dwFlags: DWORD, dwBytes: SIZE_T) -> LPVOID;
43
44pub extern "kernel32" stdcallcc fn HeapFree(hHeap: HANDLE, dwFlags: DWORD, lpMem: LPVOID) -> BOOL;
45
46pub extern "kernel32" stdcallcc fn GetProcessHeap() -> HANDLE;
4247
43pub extern "user32" stdcallcc fn MessageBoxA(hWnd: ?HANDLE, lpText: ?LPCTSTR, lpCaption: ?LPCTSTR, uType: UINT) -> c_int;48pub extern "user32" stdcallcc fn MessageBoxA(hWnd: ?HANDLE, lpText: ?LPCTSTR, lpCaption: ?LPCTSTR, uType: UINT) -> c_int;
4449
...@@ -50,6 +55,7 @@ pub const LPWSTR = &WCHAR;...@@ -50,6 +55,7 @@ pub const LPWSTR = &WCHAR;
50pub const LPSTR = &CHAR;55pub const LPSTR = &CHAR;
51pub const CHAR = u8;56pub const CHAR = u8;
52pub const PWSTR = &WCHAR;57pub const PWSTR = &WCHAR;
58pub const SIZE_T = usize;
5359
54pub const BOOL = bool;60pub const BOOL = bool;
55pub const BYTE = u8;61pub const BYTE = u8;