| ... | ... | @@ -26,7 +26,7 @@ pub const ArenaAllocator = struct { |
| 26 | 26 | return .{ |
| 27 | 27 | .allocator = Allocator{ |
| 28 | 28 | .allocFn = alloc, |
| 29 | | .resizeFn = Allocator.noResize, |
| 29 | .resizeFn = resize, |
| 30 | 30 | }, |
| 31 | 31 | .child_allocator = child_allocator, |
| 32 | 32 | .state = self, |
| ... | ... | @@ -84,4 +84,26 @@ pub const ArenaAllocator = struct { |
| 84 | 84 | return result; |
| 85 | 85 | } |
| 86 | 86 | } |
| 87 | |
| 88 | fn resize(allocator: *Allocator, buf: []u8, buf_align: u29, new_len: usize, len_align: u29, ret_addr: usize) Allocator.Error!usize { |
| 89 | const self = @fieldParentPtr(ArenaAllocator, "allocator", allocator); |
| 90 | |
| 91 | const cur_node = self.state.buffer_list.first orelse return error.OutOfMemory; |
| 92 | const cur_buf = cur_node.data[@sizeOf(BufNode)..]; |
| 93 | if (@ptrToInt(cur_buf.ptr) + self.state.end_index != @ptrToInt(buf.ptr) + buf.len) { |
| 94 | if (new_len > buf.len) |
| 95 | return error.OutOfMemory; |
| 96 | return new_len; |
| 97 | } |
| 98 | |
| 99 | if (buf.len >= new_len) { |
| 100 | self.state.end_index -= buf.len - new_len; |
| 101 | return new_len; |
| 102 | } else if (cur_buf.len - self.state.end_index >= new_len - buf.len) { |
| 103 | self.state.end_index += new_len - buf.len; |
| 104 | return new_len; |
| 105 | } else { |
| 106 | return error.OutOfMemory; |
| 107 | } |
| 108 | } |
| 87 | 109 | }; |