| ... | @@ -804,33 +804,38 @@ test "c_allocator" { | ... | @@ -804,33 +804,38 @@ test "c_allocator" { |
| 804 | | 804 | |
| 805 | test "WasmPageAllocator internals" { | 805 | test "WasmPageAllocator internals" { |
| 806 | if (comptime std.Target.current.isWasm()) { | 806 | if (comptime std.Target.current.isWasm()) { |
| 807 | const none_free = WasmPageAllocator.PageStatus.none_free; | 807 | const conventional_memsize = WasmPageAllocator.conventional.totalPages() * std.mem.page_size; |
| 808 | std.debug.assert(none_free == WasmPageAllocator.conventional.data[0]); // Passes if this test runs first | 808 | const initial = try page_allocator.alloc(u8, std.mem.page_size); |
| 809 | std.debug.assert(!WasmPageAllocator.extended.isInitialized()); // Passes if this test runs first | 809 | std.debug.assert(@ptrToInt(initial.ptr) < conventional_memsize); // If this isn't conventional, the rest of these tests don't make sense. Also we have a serious memory leak in the test suite. |
| 810 | | 810 | |
| 811 | const tmp = try page_allocator.alloc(u8, 1); | 811 | var inplace = try page_allocator.realloc(initial, 1); |
| 812 | testing.expect(none_free == WasmPageAllocator.conventional.data[0]); | 812 | testing.expectEqual(initial.ptr, inplace.ptr); |
| 813 | page_allocator.free(tmp); | 813 | inplace = try page_allocator.realloc(inplace, 4); |
| 814 | testing.expect(none_free < WasmPageAllocator.conventional.data[0]); | 814 | testing.expectEqual(initial.ptr, inplace.ptr); |
| 815 | | 815 | page_allocator.free(inplace); |
| 816 | const a_little_free = WasmPageAllocator.conventional.data[0]; | 816 | |
| 817 | const tmp_large = try page_allocator.alloc(u8, std.mem.page_size + 1); | 817 | const reuse = try page_allocator.alloc(u8, 1); |
| 818 | testing.expect(a_little_free == WasmPageAllocator.conventional.data[0]); | 818 | testing.expectEqual(initial.ptr, reuse.ptr); |
| 819 | const tmp_small = try page_allocator.alloc(u8, 1); | 819 | page_allocator.free(reuse); |
| 820 | testing.expect(none_free == WasmPageAllocator.conventional.data[0]); | 820 | |
| 821 | | 821 | // This segment may span conventional and extended which has really complex rules so we're just ignoring it for now. |
| 822 | page_allocator.free(tmp_small); | 822 | const padding = try page_allocator.alloc(u8, conventional_memsize); |
| 823 | testing.expect(a_little_free == WasmPageAllocator.conventional.data[0]); | 823 | page_allocator.free(padding); |
| 824 | page_allocator.free(tmp_large); | 824 | |
| 825 | testing.expect(a_little_free < WasmPageAllocator.conventional.data[0]); | 825 | const extended = try page_allocator.alloc(u8, conventional_memsize); |
| 826 | | 826 | testing.expect(@ptrToInt(extended.ptr) >= conventional_memsize); |
| 827 | const more_free = WasmPageAllocator.conventional.data[0]; | 827 | |
| 828 | const supersize = try page_allocator.alloc(u8, std.mem.page_size * (WasmPageAllocator.conventional.totalPages() + 1)); | 828 | const use_small = try page_allocator.alloc(u8, 1); |
| 829 | testing.expect(more_free == WasmPageAllocator.conventional.data[0]); | 829 | testing.expectEqual(initial.ptr, use_small.ptr); |
| 830 | testing.expect(!WasmPageAllocator.extended.isInitialized()); | 830 | page_allocator.free(use_small); |
| 831 | page_allocator.free(supersize); | 831 | |
| 832 | testing.expect(WasmPageAllocator.extended.isInitialized()); | 832 | inplace = try page_allocator.realloc(extended, 1); |
| 833 | testing.expect(more_free < WasmPageAllocator.conventional.data[0]); | 833 | testing.expectEqual(extended.ptr, inplace.ptr); |
| | 834 | page_allocator.free(inplace); |
| | 835 | |
| | 836 | const reuse_extended = try page_allocator.alloc(u8, conventional_memsize); |
| | 837 | testing.expectEqual(extended.ptr, reuse_extended.ptr); |
| | 838 | page_allocator.free(reuse_extended); |
| 834 | } | 839 | } |
| 835 | } | 840 | } |
| 836 | | 841 | |