authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-10-15 01:22:36-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-10-15 01:22:36-04:00
log55e8bbd1671f9c8e2f195f8f27a4cbe32665253e
tree794902447bc34aeedb89654265ee0d029eb09ed3
parent9c44dd7db3cfbbdaa082c14ed7fbe1273339a2c6

std.mem.IncrementingAllocator: check for errors


1 files changed, 6 insertions(+), 4 deletions(-)

std/mem.zig+6-4
......@@ -88,6 +88,7 @@ pub const IncrementingAllocator = struct {
8888 allocator: Allocator,
8989 bytes: []u8,
9090 end_index: usize,
91 heap_handle: if (builtin.os == Os.windows) os.windows.HANDLE else void,
9192
9293 fn init(capacity: usize) -> %IncrementingAllocator {
9394 switch (builtin.os) {
......@@ -106,11 +107,12 @@ pub const IncrementingAllocator = struct {
106107 },
107108 .bytes = @intToPtr(&u8, addr)[0..capacity],
108109 .end_index = 0,
110 .heap_handle = {},
109111 };
110112 },
111113 Os.windows => {
112 const heap_handle = os.windows.GetProcessHeap();
113 const ptr = os.windows.HeapAlloc(heap_handle, 0, capacity);
114 const heap_handle = os.windows.GetProcessHeap() ?? return error.OutOfMemory;
115 const ptr = os.windows.HeapAlloc(heap_handle, 0, capacity) ?? return error.OutOfMemory;
114116 return IncrementingAllocator {
115117 .allocator = Allocator {
116118 .allocFn = alloc,
......@@ -119,6 +121,7 @@ pub const IncrementingAllocator = struct {
119121 },
120122 .bytes = @ptrCast(&u8, ptr)[0..capacity],
121123 .end_index = 0,
124 .heap_handle = heap_handle,
122125 };
123126 },
124127 else => @compileError("Unsupported OS"),
......@@ -131,8 +134,7 @@ pub const IncrementingAllocator = struct {
131134 _ = os.posix.munmap(self.bytes.ptr, self.bytes.len);
132135 },
133136 Os.windows => {
134 const heap_handle = os.windows.GetProcessHeap();
135 _ = os.windows.HeapFree(heap_handle, 0, @ptrCast(os.windows.LPVOID, self.bytes.ptr));
137 _ = os.windows.HeapFree(self.heap_handle, 0, @ptrCast(os.windows.LPVOID, self.bytes.ptr));
136138 },
137139 else => @compileError("Unsupported OS"),
138140 }