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 {
317317 }
318318 }
319319 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)});
321321 large_alloc.value.dumpStackTrace();
322322 leaks = true;
323323 }
......@@ -788,8 +788,15 @@ test "shrink large object to large object with larger alignment" {
788788 var slice = try allocator.alignedAlloc(u8, 16, alloc_size);
789789 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.
791798 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)) {
793800 try stuff_to_free.append(slice);
794801 slice = try allocator.alignedAlloc(u8, 16, alloc_size);
795802 }
......@@ -799,7 +806,7 @@ test "shrink large object to large object with larger alignment" {
799806 slice[0] = 0x12;
800807 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);
803810 std.testing.expect(slice[0] == 0x12);
804811 std.testing.expect(slice[60] == 0x34);
805812}
......@@ -839,8 +846,13 @@ test "realloc large object to larger alignment" {
839846 var slice = try allocator.alignedAlloc(u8, 16, page_size * 2 + 50);
840847 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.
842854 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)) {
844856 try stuff_to_free.append(slice);
845857 slice = try allocator.alignedAlloc(u8, 16, page_size * 2 + 50);
846858 }
......@@ -858,7 +870,7 @@ test "realloc large object to larger alignment" {
858870 std.testing.expect(slice[0] == 0x12);
859871 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);
862874 std.testing.expect(slice[0] == 0x12);
863875 std.testing.expect(slice[16] == 0x34);
864876}