diff --git a/lib/std/heap/memory_pool.zig b/lib/std/heap/memory_pool.zig index af86adacc1..01adde470b 100644 --- a/lib/std/heap/memory_pool.zig +++ b/lib/std/heap/memory_pool.zig @@ -42,12 +42,15 @@ pub fn MemoryPoolExtra(comptime Item: type, comptime pool_options: Options) type /// as `@sizeOf(Item)` as the pool also uses the items for internal means. pub const item_size = @max(@sizeOf(Node), @sizeOf(Item)); + // This needs to be kept in sync with Node. + const node_alignment = @alignOf(*anyopaque); + /// Alignment of the memory pool items. This is not necessarily the same /// as `@alignOf(Item)` as the pool also uses the items for internal means. - pub const item_alignment = @max(@alignOf(Node), pool_options.alignment orelse 0); + pub const item_alignment = @max(node_alignment, pool_options.alignment orelse @alignOf(Item)); const Node = struct { - next: ?*@This(), + next: ?*align(item_alignment) @This(), }; const NodePtr = *align(item_alignment) Node; const ItemPtr = *align(item_alignment) Item; @@ -187,3 +190,27 @@ test "memory pool: growable" { try std.testing.expectError(error.OutOfMemory, pool.create()); } + +test "memory pool: greater than pointer default alignment" { + const Foo = struct { + data: u64 align(16), + }; + + var pool = MemoryPool(Foo).init(std.testing.allocator); + defer pool.deinit(); + + const foo: *Foo = try pool.create(); + _ = foo; +} + +test "memory pool: greater than pointer manual alignment" { + const Foo = struct { + data: u64, + }; + + var pool = MemoryPoolAligned(Foo, 16).init(std.testing.allocator); + defer pool.deinit(); + + const foo: *align(16) Foo = try pool.create(); + _ = foo; +}