authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-02-06 14:14:12-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-02-07 12:20:12-08:00
log3d7c5cf64a3bbe55dfa943133d1a5a7a34fe388c
tree5a712e05fa67142922ad9ca5f1368e61d0433857
parent51c4ffa410d2cf51b7b45dab4dfd033db2190b7e

std.heap: test smp_allocator


2 files changed, 9 insertions(+), 47 deletions(-)

lib/std/heap.zig+9-2
......@@ -481,7 +481,7 @@ pub fn StackFallbackAllocator(comptime size: usize) type {
481481 };
482482}
483483
484test "c_allocator" {
484test c_allocator {
485485 if (builtin.link_libc) {
486486 try testAllocator(c_allocator);
487487 try testAllocatorAligned(c_allocator);
......@@ -490,12 +490,19 @@ test "c_allocator" {
490490 }
491491}
492492
493test "raw_c_allocator" {
493test raw_c_allocator {
494494 if (builtin.link_libc) {
495495 try testAllocator(raw_c_allocator);
496496 }
497497}
498498
499test smp_allocator {
500 try testAllocator(smp_allocator);
501 try testAllocatorAligned(smp_allocator);
502 try testAllocatorLargeAlignment(smp_allocator);
503 try testAllocatorAlignedShrink(smp_allocator);
504}
505
499506test PageAllocator {
500507 const allocator = page_allocator;
501508 try testAllocator(allocator);
lib/std/heap/SmpAllocator.zig-45
......@@ -241,48 +241,3 @@ fn slotSize(class: usize) usize {
241241 const Log2USize = std.math.Log2Int(usize);
242242 return @as(usize, 1) << @as(Log2USize, @intCast(class));
243243}
244
245test "large alloc, resize, remap, free" {
246 const gpa = std.heap.smp_allocator;
247
248 const ptr1 = try gpa.alloc(u64, 42768);
249 const ptr2 = try gpa.alloc(u64, 52768);
250 gpa.free(ptr1);
251 const ptr3 = try gpa.alloc(u64, 62768);
252 gpa.free(ptr3);
253 gpa.free(ptr2);
254}
255
256test "small allocations - free in same order" {
257 const gpa = std.heap.smp_allocator;
258
259 var list = std.ArrayList(*u64).init(std.testing.allocator);
260 defer list.deinit();
261
262 var i: usize = 0;
263 while (i < 513) : (i += 1) {
264 const ptr = try gpa.create(u64);
265 try list.append(ptr);
266 }
267
268 for (list.items) |ptr| {
269 gpa.destroy(ptr);
270 }
271}
272
273test "small allocations - free in reverse order" {
274 const gpa = std.heap.smp_allocator;
275
276 var list = std.ArrayList(*u64).init(std.testing.allocator);
277 defer list.deinit();
278
279 var i: usize = 0;
280 while (i < 513) : (i += 1) {
281 const ptr = try gpa.create(u64);
282 try list.append(ptr);
283 }
284
285 while (list.popOrNull()) |ptr| {
286 gpa.destroy(ptr);
287 }
288}