| ... | ... | @@ -42,12 +42,15 @@ pub fn MemoryPoolExtra(comptime Item: type, comptime pool_options: Options) type |
| 42 | 42 | /// as `@sizeOf(Item)` as the pool also uses the items for internal means. |
| 43 | 43 | pub const item_size = @max(@sizeOf(Node), @sizeOf(Item)); |
| 44 | 44 | |
| 45 | // This needs to be kept in sync with Node. |
| 46 | const node_alignment = @alignOf(*anyopaque); |
| 47 | |
| 45 | 48 | /// Alignment of the memory pool items. This is not necessarily the same |
| 46 | 49 | /// as `@alignOf(Item)` as the pool also uses the items for internal means. |
| 47 | | pub const item_alignment = @max(@alignOf(Node), pool_options.alignment orelse 0); |
| 50 | pub const item_alignment = @max(node_alignment, pool_options.alignment orelse @alignOf(Item)); |
| 48 | 51 | |
| 49 | 52 | const Node = struct { |
| 50 | | next: ?*@This(), |
| 53 | next: ?*align(item_alignment) @This(), |
| 51 | 54 | }; |
| 52 | 55 | const NodePtr = *align(item_alignment) Node; |
| 53 | 56 | const ItemPtr = *align(item_alignment) Item; |
| ... | ... | @@ -187,3 +190,27 @@ test "memory pool: growable" { |
| 187 | 190 | |
| 188 | 191 | try std.testing.expectError(error.OutOfMemory, pool.create()); |
| 189 | 192 | } |
| 193 | |
| 194 | test "memory pool: greater than pointer default alignment" { |
| 195 | const Foo = struct { |
| 196 | data: u64 align(16), |
| 197 | }; |
| 198 | |
| 199 | var pool = MemoryPool(Foo).init(std.testing.allocator); |
| 200 | defer pool.deinit(); |
| 201 | |
| 202 | const foo: *Foo = try pool.create(); |
| 203 | _ = foo; |
| 204 | } |
| 205 | |
| 206 | test "memory pool: greater than pointer manual alignment" { |
| 207 | const Foo = struct { |
| 208 | data: u64, |
| 209 | }; |
| 210 | |
| 211 | var pool = MemoryPoolAligned(Foo, 16).init(std.testing.allocator); |
| 212 | defer pool.deinit(); |
| 213 | |
| 214 | const foo: *align(16) Foo = try pool.create(); |
| 215 | _ = foo; |
| 216 | } |