refactor: improve create pattern to avoid leaks

This commit is contained in:
CJ van den Berg 2025-07-19 00:03:30 +02:00
parent de68c1a5d4
commit efdad96054
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
46 changed files with 90 additions and 30 deletions

View file

@ -159,6 +159,7 @@ pub const Leaf = struct {
if (piece.len == 0)
return if (!bol and !eol) &empty_leaf else if (bol and !eol) &empty_bol_leaf else if (!bol and eol) &empty_eol_leaf else &empty_line_leaf;
const node = try allocator.create(Node);
errdefer allocator.destroy(node);
node.* = .{ .leaf = .{ .buf = piece, .bol = bol, .eol = eol } };
return node;
}
@ -267,6 +268,7 @@ const Node = union(enum) {
fn new(allocator: Allocator, l: *const Node, r: *const Node) !*const Node {
const node = try allocator.create(Node);
errdefer allocator.destroy(node);
const l_weights_sum = l.weights_sum();
var weights_sum_ = Weights{};
weights_sum_.add(l_weights_sum);
@ -1065,6 +1067,7 @@ const Node = union(enum) {
pub fn create(allocator: Allocator) error{OutOfMemory}!*Self {
const self = try allocator.create(Self);
errdefer allocator.destroy(self);
const arena_a = if (builtin.is_test) allocator else std.heap.page_allocator;
self.* = .{
.arena = std.heap.ArenaAllocator.init(arena_a),