2020-05-10 06:05:54 +00:00
|
|
|
const std = @import("../std.zig");
|
|
|
|
const assert = std.debug.assert;
|
|
|
|
const mem = std.mem;
|
|
|
|
const Allocator = std.mem.Allocator;
|
|
|
|
|
|
|
|
/// This allocator takes an existing allocator, wraps it, and provides an interface
|
|
|
|
/// where you can allocate without freeing, and then free it all together.
|
|
|
|
pub const ArenaAllocator = struct {
|
2021-10-28 23:37:25 +00:00
|
|
|
child_allocator: Allocator,
|
2020-05-10 06:05:54 +00:00
|
|
|
state: State,
|
|
|
|
|
|
|
|
/// Inner state of ArenaAllocator. Can be stored rather than the entire ArenaAllocator
|
|
|
|
/// as a memory-saving optimization.
|
|
|
|
pub const State = struct {
|
|
|
|
buffer_list: std.SinglyLinkedList([]u8) = @as(std.SinglyLinkedList([]u8), .{}),
|
|
|
|
end_index: usize = 0,
|
|
|
|
|
2021-10-28 23:37:25 +00:00
|
|
|
pub fn promote(self: State, child_allocator: Allocator) ArenaAllocator {
|
2020-05-10 06:05:54 +00:00
|
|
|
return .{
|
|
|
|
.child_allocator = child_allocator,
|
|
|
|
.state = self,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-10-29 01:08:41 +00:00
|
|
|
pub fn allocator(self: *ArenaAllocator) Allocator {
|
2021-11-06 00:54:35 +00:00
|
|
|
return Allocator.init(self, alloc, resize, free);
|
2021-10-28 23:37:25 +00:00
|
|
|
}
|
|
|
|
|
2020-05-10 06:05:54 +00:00
|
|
|
const BufNode = std.SinglyLinkedList([]u8).Node;
|
|
|
|
|
2021-10-28 23:37:25 +00:00
|
|
|
pub fn init(child_allocator: Allocator) ArenaAllocator {
|
2020-05-10 06:05:54 +00:00
|
|
|
return (State{}).promote(child_allocator);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn deinit(self: ArenaAllocator) void {
|
|
|
|
var it = self.state.buffer_list.first;
|
|
|
|
while (it) |node| {
|
|
|
|
// this has to occur before the free because the free frees node
|
|
|
|
const next_it = node.next;
|
|
|
|
self.child_allocator.free(node.data);
|
|
|
|
it = next_it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-29 05:42:42 +00:00
|
|
|
fn createNode(self: *ArenaAllocator, prev_len: usize, minimum_size: usize) !*BufNode {
|
|
|
|
const actual_min_size = minimum_size + (@sizeOf(BufNode) + 16);
|
|
|
|
const big_enough_len = prev_len + actual_min_size;
|
|
|
|
const len = big_enough_len + big_enough_len / 2;
|
2021-11-06 00:54:35 +00:00
|
|
|
const buf = try self.child_allocator.rawAlloc(len, @alignOf(BufNode), 1, @returnAddress());
|
2020-06-29 05:44:48 +00:00
|
|
|
const buf_node = @ptrCast(*BufNode, @alignCast(@alignOf(BufNode), buf.ptr));
|
2020-06-29 05:42:42 +00:00
|
|
|
buf_node.* = BufNode{
|
|
|
|
.data = buf,
|
|
|
|
.next = null,
|
|
|
|
};
|
2020-05-10 06:05:54 +00:00
|
|
|
self.state.buffer_list.prepend(buf_node);
|
2020-06-29 05:42:42 +00:00
|
|
|
self.state.end_index = 0;
|
|
|
|
return buf_node;
|
2020-05-10 06:05:54 +00:00
|
|
|
}
|
|
|
|
|
2021-10-28 23:37:25 +00:00
|
|
|
fn alloc(self: *ArenaAllocator, n: usize, ptr_align: u29, len_align: u29, ra: usize) ![]u8 {
|
2021-06-20 01:10:22 +00:00
|
|
|
_ = len_align;
|
|
|
|
_ = ra;
|
2020-05-10 06:05:54 +00:00
|
|
|
|
2020-06-29 05:42:42 +00:00
|
|
|
var cur_node = if (self.state.buffer_list.first) |first_node| first_node else try self.createNode(0, n + ptr_align);
|
|
|
|
while (true) {
|
|
|
|
const cur_buf = cur_node.data[@sizeOf(BufNode)..];
|
2020-05-10 06:05:54 +00:00
|
|
|
const addr = @ptrToInt(cur_buf.ptr) + self.state.end_index;
|
2020-06-29 05:42:42 +00:00
|
|
|
const adjusted_addr = mem.alignForward(addr, ptr_align);
|
|
|
|
const adjusted_index = self.state.end_index + (adjusted_addr - addr);
|
|
|
|
const new_end_index = adjusted_index + n;
|
2020-09-24 22:21:57 +00:00
|
|
|
|
|
|
|
if (new_end_index <= cur_buf.len) {
|
|
|
|
const result = cur_buf[adjusted_index..new_end_index];
|
|
|
|
self.state.end_index = new_end_index;
|
|
|
|
return result;
|
2020-05-10 06:05:54 +00:00
|
|
|
}
|
2020-09-24 22:21:57 +00:00
|
|
|
|
|
|
|
const bigger_buf_size = @sizeOf(BufNode) + new_end_index;
|
|
|
|
// Try to grow the buffer in-place
|
2021-11-07 01:40:06 +00:00
|
|
|
cur_node.data = self.child_allocator.resize(cur_node.data, bigger_buf_size) orelse {
|
|
|
|
// Allocate a new node if that's not possible
|
|
|
|
cur_node = try self.createNode(cur_buf.len, n + ptr_align);
|
|
|
|
continue;
|
2020-09-24 22:21:57 +00:00
|
|
|
};
|
2020-05-10 06:05:54 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-08 03:24:35 +00:00
|
|
|
|
2021-11-07 01:40:06 +00:00
|
|
|
fn resize(self: *ArenaAllocator, buf: []u8, buf_align: u29, new_len: usize, len_align: u29, ret_addr: usize) ?usize {
|
2021-06-20 01:10:22 +00:00
|
|
|
_ = buf_align;
|
|
|
|
_ = len_align;
|
|
|
|
_ = ret_addr;
|
2020-09-08 03:24:35 +00:00
|
|
|
|
2021-11-07 01:40:06 +00:00
|
|
|
const cur_node = self.state.buffer_list.first orelse return null;
|
2020-09-08 03:24:35 +00:00
|
|
|
const cur_buf = cur_node.data[@sizeOf(BufNode)..];
|
|
|
|
if (@ptrToInt(cur_buf.ptr) + self.state.end_index != @ptrToInt(buf.ptr) + buf.len) {
|
2021-11-07 01:40:06 +00:00
|
|
|
if (new_len > buf.len) return null;
|
2020-09-08 03:24:35 +00:00
|
|
|
return new_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buf.len >= new_len) {
|
|
|
|
self.state.end_index -= buf.len - new_len;
|
|
|
|
return new_len;
|
|
|
|
} else if (cur_buf.len - self.state.end_index >= new_len - buf.len) {
|
|
|
|
self.state.end_index += new_len - buf.len;
|
|
|
|
return new_len;
|
|
|
|
} else {
|
2021-11-07 01:40:06 +00:00
|
|
|
return null;
|
2020-09-08 03:24:35 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-06 00:54:35 +00:00
|
|
|
|
|
|
|
fn free(self: *ArenaAllocator, buf: []u8, buf_align: u29, ret_addr: usize) void {
|
|
|
|
_ = buf_align;
|
|
|
|
_ = ret_addr;
|
|
|
|
|
|
|
|
const cur_node = self.state.buffer_list.first orelse return;
|
|
|
|
const cur_buf = cur_node.data[@sizeOf(BufNode)..];
|
|
|
|
|
|
|
|
if (@ptrToInt(cur_buf.ptr) + self.state.end_index == @ptrToInt(buf.ptr) + buf.len) {
|
|
|
|
self.state.end_index -= buf.len;
|
|
|
|
}
|
|
|
|
}
|
2020-05-10 06:05:54 +00:00
|
|
|
};
|