aboutsummaryrefslogtreecommitdiff
path: root/std
diff options
context:
space:
mode:
authorAndrew Kelley <superjoe30@gmail.com>2017-10-15 01:22:36 -0400
committerAndrew Kelley <superjoe30@gmail.com>2017-10-15 01:22:36 -0400
commit55e8bbd1671f9c8e2f195f8f27a4cbe32665253e (patch)
tree794902447bc34aeedb89654265ee0d029eb09ed3 /std
parent9c44dd7db3cfbbdaa082c14ed7fbe1273339a2c6 (diff)
downloadzig-55e8bbd1671f9c8e2f195f8f27a4cbe32665253e.tar.gz
zig-55e8bbd1671f9c8e2f195f8f27a4cbe32665253e.zip
std.mem.IncrementingAllocator: check for errors
Diffstat (limited to 'std')
-rw-r--r--std/mem.zig10
1 files changed, 6 insertions, 4 deletions
diff --git a/std/mem.zig b/std/mem.zig
index 3975ad275e..2b8f74b60c 100644
--- a/std/mem.zig
+++ b/std/mem.zig
@@ -88,6 +88,7 @@ pub const IncrementingAllocator = struct {
allocator: Allocator,
bytes: []u8,
end_index: usize,
+ heap_handle: if (builtin.os == Os.windows) os.windows.HANDLE else void,
fn init(capacity: usize) -> %IncrementingAllocator {
switch (builtin.os) {
@@ -106,11 +107,12 @@ pub const IncrementingAllocator = struct {
},
.bytes = @intToPtr(&u8, addr)[0..capacity],
.end_index = 0,
+ .heap_handle = {},
};
},
Os.windows => {
- const heap_handle = os.windows.GetProcessHeap();
- const ptr = os.windows.HeapAlloc(heap_handle, 0, capacity);
+ const heap_handle = os.windows.GetProcessHeap() ?? return error.OutOfMemory;
+ const ptr = os.windows.HeapAlloc(heap_handle, 0, capacity) ?? return error.OutOfMemory;
return IncrementingAllocator {
.allocator = Allocator {
.allocFn = alloc,
@@ -119,6 +121,7 @@ pub const IncrementingAllocator = struct {
},
.bytes = @ptrCast(&u8, ptr)[0..capacity],
.end_index = 0,
+ .heap_handle = heap_handle,
};
},
else => @compileError("Unsupported OS"),
@@ -131,8 +134,7 @@ pub const IncrementingAllocator = struct {
_ = 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));
+ _ = os.windows.HeapFree(self.heap_handle, 0, @ptrCast(os.windows.LPVOID, self.bytes.ptr));
},
else => @compileError("Unsupported OS"),
}