authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-08-08 13:46:18-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-08-08 13:46:18-07:00
log5b57e35ce0c83dc48587c19cf28db509bba2226b
tree132d4a52c3986807a9fa251fc529f9360588d371
parent4d0f83e23e736ca41ad71040fb119e44bf956819

fix general purpose allocator test cases on Windows

The tests are cleverly testing some alignment stuff, but were getting thwarted by Windows choosing to allocate 64K aligned pages.

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

lib/std/heap/general_purpose_allocator.zig+17-5
...@@ -317,7 +317,7 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type {...@@ -317,7 +317,7 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type {
317 }317 }
318 }318 }
319 for (self.large_allocations.items()) |*large_alloc| {319 for (self.large_allocations.items()) |*large_alloc| {
320 std.debug.print("\nMemory leak detected:\n", .{});320 std.debug.print("\nMemory leak detected (0x{x}):\n", .{@ptrToInt(large_alloc.value.bytes.ptr)});
321 large_alloc.value.dumpStackTrace();321 large_alloc.value.dumpStackTrace();
322 leaks = true;322 leaks = true;
323 }323 }
...@@ -788,8 +788,15 @@ test "shrink large object to large object with larger alignment" {...@@ -788,8 +788,15 @@ test "shrink large object to large object with larger alignment" {
788 var slice = try allocator.alignedAlloc(u8, 16, alloc_size);788 var slice = try allocator.alignedAlloc(u8, 16, alloc_size);
789 defer allocator.free(slice);789 defer allocator.free(slice);
790790
791 const big_alignment: usize = switch (std.Target.current.os.tag) {
792 .windows => page_size * 32, // Windows aligns to 64K.
793 else => page_size * 2,
794 };
795 // This loop allocates until we find a page that is not aligned to the big
796 // alignment. Then we shrink the allocation after the loop, but increase the
797 // alignment to the higher one, that we know will force it to realloc.
791 var stuff_to_free = std.ArrayList([]align(16) u8).init(debug_allocator);798 var stuff_to_free = std.ArrayList([]align(16) u8).init(debug_allocator);
792 while (mem.isAligned(@ptrToInt(slice.ptr), page_size * 2)) {799 while (mem.isAligned(@ptrToInt(slice.ptr), big_alignment)) {
793 try stuff_to_free.append(slice);800 try stuff_to_free.append(slice);
794 slice = try allocator.alignedAlloc(u8, 16, alloc_size);801 slice = try allocator.alignedAlloc(u8, 16, alloc_size);
795 }802 }
...@@ -799,7 +806,7 @@ test "shrink large object to large object with larger alignment" {...@@ -799,7 +806,7 @@ test "shrink large object to large object with larger alignment" {
799 slice[0] = 0x12;806 slice[0] = 0x12;
800 slice[60] = 0x34;807 slice[60] = 0x34;
801808
802 slice = try allocator.reallocAdvanced(slice, page_size * 2, alloc_size / 2, .exact);809 slice = try allocator.reallocAdvanced(slice, big_alignment, alloc_size / 2, .exact);
803 std.testing.expect(slice[0] == 0x12);810 std.testing.expect(slice[0] == 0x12);
804 std.testing.expect(slice[60] == 0x34);811 std.testing.expect(slice[60] == 0x34);
805}812}
...@@ -839,8 +846,13 @@ test "realloc large object to larger alignment" {...@@ -839,8 +846,13 @@ test "realloc large object to larger alignment" {
839 var slice = try allocator.alignedAlloc(u8, 16, page_size * 2 + 50);846 var slice = try allocator.alignedAlloc(u8, 16, page_size * 2 + 50);
840 defer allocator.free(slice);847 defer allocator.free(slice);
841848
849 const big_alignment: usize = switch (std.Target.current.os.tag) {
850 .windows => page_size * 32, // Windows aligns to 64K.
851 else => page_size * 2,
852 };
853 // This loop allocates until we find a page that is not aligned to the big alignment.
842 var stuff_to_free = std.ArrayList([]align(16) u8).init(debug_allocator);854 var stuff_to_free = std.ArrayList([]align(16) u8).init(debug_allocator);
843 while (mem.isAligned(@ptrToInt(slice.ptr), page_size * 2)) {855 while (mem.isAligned(@ptrToInt(slice.ptr), big_alignment)) {
844 try stuff_to_free.append(slice);856 try stuff_to_free.append(slice);
845 slice = try allocator.alignedAlloc(u8, 16, page_size * 2 + 50);857 slice = try allocator.alignedAlloc(u8, 16, page_size * 2 + 50);
846 }858 }
...@@ -858,7 +870,7 @@ test "realloc large object to larger alignment" {...@@ -858,7 +870,7 @@ test "realloc large object to larger alignment" {
858 std.testing.expect(slice[0] == 0x12);870 std.testing.expect(slice[0] == 0x12);
859 std.testing.expect(slice[16] == 0x34);871 std.testing.expect(slice[16] == 0x34);
860872
861 slice = try allocator.reallocAdvanced(slice, page_size * 2, page_size * 2 + 100, .exact);873 slice = try allocator.reallocAdvanced(slice, big_alignment, page_size * 2 + 100, .exact);
862 std.testing.expect(slice[0] == 0x12);874 std.testing.expect(slice[0] == 0x12);
863 std.testing.expect(slice[16] == 0x34);875 std.testing.expect(slice[16] == 0x34);
864}876}