| ... | ... | @@ -75,13 +75,22 @@ pub const ArenaAllocator = struct { |
| 75 | 75 | const adjusted_addr = mem.alignForward(addr, ptr_align); |
| 76 | 76 | const adjusted_index = self.state.end_index + (adjusted_addr - addr); |
| 77 | 77 | const new_end_index = adjusted_index + n; |
| 78 | | if (new_end_index > cur_buf.len) { |
| 79 | | cur_node = try self.createNode(cur_buf.len, n + ptr_align); |
| 80 | | continue; |
| 78 | |
| 79 | if (new_end_index <= cur_buf.len) { |
| 80 | const result = cur_buf[adjusted_index..new_end_index]; |
| 81 | self.state.end_index = new_end_index; |
| 82 | return result; |
| 81 | 83 | } |
| 82 | | const result = cur_buf[adjusted_index..new_end_index]; |
| 83 | | self.state.end_index = new_end_index; |
| 84 | | return result; |
| 84 | |
| 85 | const bigger_buf_size = @sizeOf(BufNode) + new_end_index; |
| 86 | // Try to grow the buffer in-place |
| 87 | cur_node.data = self.child_allocator.resize(cur_node.data, bigger_buf_size) catch |err| switch (err) { |
| 88 | error.OutOfMemory => { |
| 89 | // Allocate a new node if that's not possible |
| 90 | cur_node = try self.createNode(cur_buf.len, n + ptr_align); |
| 91 | continue; |
| 92 | }, |
| 93 | }; |
| 85 | 94 | } |
| 86 | 95 | } |
| 87 | 96 | |