authorgravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2019-04-19 17:27:20-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-04-20 02:21:42-04:00
log1030cc97cacbf7f7389ff56601a1bde9e33d3763
tree328b5a1fb1d87ceadeaa765b33b0cf23fa7532b6
parentd44d2784e6a41ccd5549d4badea527a35169945d

fix DirectAllocator not unmapping unused pages on large alignments

Fixes #2306

1 files changed, 16 insertions(+), 15 deletions(-)

std/heap.zig+16-15
......@@ -68,21 +68,22 @@ pub const DirectAllocator = struct {
6868 if (addr == p.MAP_FAILED) return error.OutOfMemory;
6969 if (alloc_size == n) return @intToPtr([*]u8, addr)[0..n];
7070
71 const aligned_addr = (addr & ~usize(alignment - 1)) + alignment;
72
73 // We can unmap the unused portions of our mmap, but we must only
74 // pass munmap bytes that exist outside our allocated pages or it
75 // will happily eat us too.
76
77 // Since alignment > page_size, we are by definition on a page boundary.
78 const unused_start = addr;
79 const unused_len = aligned_addr - 1 - unused_start;
80
81 const err = p.munmap(unused_start, unused_len);
82 assert(p.getErrno(err) == 0);
83
84 // It is impossible that there is an unoccupied page at the top of our
85 // mmap.
71 const aligned_addr = mem.alignForward(addr, alignment);
72
73 // Unmap the extra bytes that were only requested in order to guarantee
74 // that the range of memory we were provided had a proper alignment in
75 // it somewhere. The extra bytes could be at the beginning, or end, or both.
76 const unused_start_len = aligned_addr - addr;
77 if (unused_start_len != 0) {
78 const err = p.munmap(addr, unused_start_len);
79 assert(p.getErrno(err) == 0);
80 }
81 const aligned_end_addr = std.mem.alignForward(aligned_addr + n, os.page_size);
82 const unused_end_len = addr + alloc_size - aligned_end_addr;
83 if (unused_end_len != 0) {
84 const err = p.munmap(aligned_end_addr, unused_end_len);
85 assert(p.getErrno(err) == 0);
86 }
8687
8788 return @intToPtr([*]u8, aligned_addr)[0..n];
8889 },