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 {...@@ -88,6 +88,7 @@ pub const IncrementingAllocator = struct {
88 allocator: Allocator,88 allocator: Allocator,
89 bytes: []u8,89 bytes: []u8,
90 end_index: usize,90 end_index: usize,
91 heap_handle: if (builtin.os == Os.windows) os.windows.HANDLE else void,
9192
92 fn init(capacity: usize) -> %IncrementingAllocator {93 fn init(capacity: usize) -> %IncrementingAllocator {
93 switch (builtin.os) {94 switch (builtin.os) {
...@@ -106,11 +107,12 @@ pub const IncrementingAllocator = struct {...@@ -106,11 +107,12 @@ pub const IncrementingAllocator = struct {
106 },107 },
107 .bytes = @intToPtr(&u8, addr)[0..capacity],108 .bytes = @intToPtr(&u8, addr)[0..capacity],
108 .end_index = 0,109 .end_index = 0,
110 .heap_handle = {},
109 };111 };
110 },112 },
111 Os.windows => {113 Os.windows => {
112 const heap_handle = os.windows.GetProcessHeap();114 const heap_handle = os.windows.GetProcessHeap() ?? return error.OutOfMemory;
113 const ptr = os.windows.HeapAlloc(heap_handle, 0, capacity);115 const ptr = os.windows.HeapAlloc(heap_handle, 0, capacity) ?? return error.OutOfMemory;
114 return IncrementingAllocator {116 return IncrementingAllocator {
115 .allocator = Allocator {117 .allocator = Allocator {
116 .allocFn = alloc,118 .allocFn = alloc,
...@@ -119,6 +121,7 @@ pub const IncrementingAllocator = struct {...@@ -119,6 +121,7 @@ pub const IncrementingAllocator = struct {
119 },121 },
120 .bytes = @ptrCast(&u8, ptr)[0..capacity],122 .bytes = @ptrCast(&u8, ptr)[0..capacity],
121 .end_index = 0,123 .end_index = 0,
124 .heap_handle = heap_handle,
122 };125 };
123 },126 },
124 else => @compileError("Unsupported OS"),127 else => @compileError("Unsupported OS"),
...@@ -131,8 +134,7 @@ pub const IncrementingAllocator = struct {...@@ -131,8 +134,7 @@ pub const IncrementingAllocator = struct {
131 _ = os.posix.munmap(self.bytes.ptr, self.bytes.len);134 _ = os.posix.munmap(self.bytes.ptr, self.bytes.len);
132 },135 },
133 Os.windows => {136 Os.windows => {
134 const heap_handle = os.windows.GetProcessHeap();137 _ = os.windows.HeapFree(self.heap_handle, 0, @ptrCast(os.windows.LPVOID, self.bytes.ptr));
135 _ = os.windows.HeapFree(heap_handle, 0, @ptrCast(os.windows.LPVOID, self.bytes.ptr));
136 },138 },
137 else => @compileError("Unsupported OS"),139 else => @compileError("Unsupported OS"),
138 }140 }