| ... | @@ -139,6 +139,7 @@ pub const ArenaAllocator = struct { | ... | @@ -139,6 +139,7 @@ pub const ArenaAllocator = struct { |
| 139 | // reset the state before we try resizing the buffers, so we definitely have reset the arena to 0. | 139 | // reset the state before we try resizing the buffers, so we definitely have reset the arena to 0. |
| 140 | self.state.end_index = 0; | 140 | self.state.end_index = 0; |
| 141 | if (maybe_first_node) |first_node| { | 141 | if (maybe_first_node) |first_node| { |
| | 142 | self.state.buffer_list.first = first_node; |
| 142 | // perfect, no need to invoke the child_allocator | 143 | // perfect, no need to invoke the child_allocator |
| 143 | if (first_node.data == total_size) | 144 | if (first_node.data == total_size) |
| 144 | return true; | 145 | return true; |
| ... | @@ -270,3 +271,19 @@ test "ArenaAllocator (reset with preheating)" { | ... | @@ -270,3 +271,19 @@ test "ArenaAllocator (reset with preheating)" { |
| 270 | } | 271 | } |
| 271 | } | 272 | } |
| 272 | } | 273 | } |
| | 274 | |
| | 275 | test "ArenaAllocator (reset while retaining a buffer)" { |
| | 276 | var arena_allocator = ArenaAllocator.init(std.testing.allocator); |
| | 277 | defer arena_allocator.deinit(); |
| | 278 | const a = arena_allocator.allocator(); |
| | 279 | |
| | 280 | // Create two internal buffers |
| | 281 | _ = try a.alloc(u8, 1); |
| | 282 | _ = try a.alloc(u8, 1000); |
| | 283 | |
| | 284 | // Check that we have at least two buffers |
| | 285 | try std.testing.expect(arena_allocator.state.buffer_list.first.?.next != null); |
| | 286 | |
| | 287 | // This retains the first allocated buffer |
| | 288 | try std.testing.expect(arena_allocator.reset(.{ .retain_with_limit = 1 })); |
| | 289 | } |