aboutsummaryrefslogtreecommitdiff
path: root/std
diff options
context:
space:
mode:
authorShawn Landden <shawn@git.icu>2019-04-09 10:48:09 -0500
committerAndrew Kelley <andrew@ziglang.org>2019-04-29 13:47:24 -0400
commit3c13aa178b007cae19800579b40e82bef127f388 (patch)
treea15b00dad61d1f488186db5f8ee1f2c1289df72c /std
parent8afa1e800b9957bff707ca8f57e3c38fc38e2597 (diff)
downloadzig-3c13aa178b007cae19800579b40e82bef127f388.tar.gz
zig-3c13aa178b007cae19800579b40e82bef127f388.zip
std.heap: do not excessively call mmap, and munmap in direct allocator
Diffstat (limited to 'std')
-rw-r--r--std/heap.zig6
1 files changed, 5 insertions, 1 deletions
diff --git a/std/heap.zig b/std/heap.zig
index 239a65961a..bb2cd4f4d8 100644
--- a/std/heap.zig
+++ b/std/heap.zig
@@ -59,6 +59,8 @@ pub const DirectAllocator = struct {
fn alloc(allocator: *Allocator, n: usize, alignment: u29) error{OutOfMemory}![]u8 {
const self = @fieldParentPtr(DirectAllocator, "allocator", allocator);
+ if (n == 0)
+ return (([*]u8)(undefined))[0..0];
switch (builtin.os) {
Os.linux, Os.macosx, Os.ios, Os.freebsd, Os.netbsd => {
@@ -140,7 +142,9 @@ pub const DirectAllocator = struct {
}
const result = try alloc(allocator, new_size, new_align);
@memcpy(result.ptr, old_mem.ptr, std.math.min(old_mem.len, result.len));
- _ = os.posix.munmap(@ptrToInt(old_mem.ptr), old_mem.len);
+ if (old_mem.len > 0) {
+ _ = os.posix.munmap(@ptrToInt(old_mem.ptr), old_mem.len);
+ }
return result;
},
Os.windows => {