authorgravatar for mark.l.barbone@gmail.comMark Barbone <mark.l.barbone@gmail.com> 2020-09-07 23:24:35-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-09-08 13:04:14-04:00
log42b1b6be90fc1034875a6e16cf3cbe1c9d6030ca
tree2f29c3fa76ebd5a43848515bbd4c96a453991c5f
parent5bf3e540188d06b06a0f18043c2685d3593c8107

Add resize for arena allocator


1 files changed, 23 insertions(+), 1 deletions(-)

lib/std/heap/arena_allocator.zig+23-1
...@@ -26,7 +26,7 @@ pub const ArenaAllocator = struct {...@@ -26,7 +26,7 @@ pub const ArenaAllocator = struct {
26 return .{26 return .{
27 .allocator = Allocator{27 .allocator = Allocator{
28 .allocFn = alloc,28 .allocFn = alloc,
29 .resizeFn = Allocator.noResize,29 .resizeFn = resize,
30 },30 },
31 .child_allocator = child_allocator,31 .child_allocator = child_allocator,
32 .state = self,32 .state = self,
...@@ -84,4 +84,26 @@ pub const ArenaAllocator = struct {...@@ -84,4 +84,26 @@ pub const ArenaAllocator = struct {
84 return result;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};