From 3d7c5cf64a3bbe55dfa943133d1a5a7a34fe388c Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Thu, 6 Feb 2025 14:14:12 -0800 Subject: [PATCH] std.heap: test smp_allocator --- lib/std/heap.zig | 11 +++++++-- lib/std/heap/SmpAllocator.zig | 45 ----------------------------------- 2 files changed, 9 insertions(+), 47 deletions(-) diff --git a/lib/std/heap.zig b/lib/std/heap.zig index 10e4cec608..290b39b624 100644 --- a/lib/std/heap.zig +++ b/lib/std/heap.zig @@ -481,7 +481,7 @@ pub fn StackFallbackAllocator(comptime size: usize) type { }; } -test "c_allocator" { +test c_allocator { if (builtin.link_libc) { try testAllocator(c_allocator); try testAllocatorAligned(c_allocator); @@ -490,12 +490,19 @@ test "c_allocator" { } } -test "raw_c_allocator" { +test raw_c_allocator { if (builtin.link_libc) { try testAllocator(raw_c_allocator); } } +test smp_allocator { + try testAllocator(smp_allocator); + try testAllocatorAligned(smp_allocator); + try testAllocatorLargeAlignment(smp_allocator); + try testAllocatorAlignedShrink(smp_allocator); +} + test PageAllocator { const allocator = page_allocator; try testAllocator(allocator); diff --git a/lib/std/heap/SmpAllocator.zig b/lib/std/heap/SmpAllocator.zig index 6fd68c19b2..b1f2b14d0a 100644 --- a/lib/std/heap/SmpAllocator.zig +++ b/lib/std/heap/SmpAllocator.zig @@ -241,48 +241,3 @@ fn slotSize(class: usize) usize { const Log2USize = std.math.Log2Int(usize); return @as(usize, 1) << @as(Log2USize, @intCast(class)); } - -test "large alloc, resize, remap, free" { - const gpa = std.heap.smp_allocator; - - const ptr1 = try gpa.alloc(u64, 42768); - const ptr2 = try gpa.alloc(u64, 52768); - gpa.free(ptr1); - const ptr3 = try gpa.alloc(u64, 62768); - gpa.free(ptr3); - gpa.free(ptr2); -} - -test "small allocations - free in same order" { - const gpa = std.heap.smp_allocator; - - var list = std.ArrayList(*u64).init(std.testing.allocator); - defer list.deinit(); - - var i: usize = 0; - while (i < 513) : (i += 1) { - const ptr = try gpa.create(u64); - try list.append(ptr); - } - - for (list.items) |ptr| { - gpa.destroy(ptr); - } -} - -test "small allocations - free in reverse order" { - const gpa = std.heap.smp_allocator; - - var list = std.ArrayList(*u64).init(std.testing.allocator); - defer list.deinit(); - - var i: usize = 0; - while (i < 513) : (i += 1) { - const ptr = try gpa.create(u64); - try list.append(ptr); - } - - while (list.popOrNull()) |ptr| { - gpa.destroy(ptr); - } -}