authorgravatar for shawn@git.icuShawn Landden <shawn@git.icu> 2019-04-09 10:48:09-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-04-29 13:47:24-04:00
log3c13aa178b007cae19800579b40e82bef127f388
treea15b00dad61d1f488186db5f8ee1f2c1289df72c
parent8afa1e800b9957bff707ca8f57e3c38fc38e2597

std.heap: do not excessively call mmap, and munmap in direct allocator


1 files changed, 5 insertions(+), 1 deletions(-)

std/heap.zig+5-1
......@@ -59,6 +59,8 @@ pub const DirectAllocator = struct {
5959
6060 fn alloc(allocator: *Allocator, n: usize, alignment: u29) error{OutOfMemory}![]u8 {
6161 const self = @fieldParentPtr(DirectAllocator, "allocator", allocator);
62 if (n == 0)
63 return (([*]u8)(undefined))[0..0];
6264
6365 switch (builtin.os) {
6466 Os.linux, Os.macosx, Os.ios, Os.freebsd, Os.netbsd => {
......@@ -140,7 +142,9 @@ pub const DirectAllocator = struct {
140142 }
141143 const result = try alloc(allocator, new_size, new_align);
142144 @memcpy(result.ptr, old_mem.ptr, std.math.min(old_mem.len, result.len));
143 _ = os.posix.munmap(@ptrToInt(old_mem.ptr), old_mem.len);
145 if (old_mem.len > 0) {
146 _ = os.posix.munmap(@ptrToInt(old_mem.ptr), old_mem.len);
147 }
144148 return result;
145149 },
146150 Os.windows => {