From a9aef11b4bdfd3478d746f71ff3cf82dfc6aefe6 Mon Sep 17 00:00:00 2001 From: Qwerasd Date: Mon, 8 Jul 2024 22:15:31 -0400 Subject: [PATCH] RefCountedSet: add some missing context delete callbacks --- src/terminal/ref_counted_set.zig | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/terminal/ref_counted_set.zig b/src/terminal/ref_counted_set.zig index 5fe5fa542..e19f8f70c 100644 --- a/src/terminal/ref_counted_set.zig +++ b/src/terminal/ref_counted_set.zig @@ -232,10 +232,16 @@ pub fn RefCountedSet( // we're reusing the existing value in the set. This allows // callers to clean up any resources associated with the value. if (comptime @hasDecl(Context, "deleted")) ctx.deleted(value); + items[id].meta.ref += 1; return id; } + // Notify the context that the value is "deleted" if we return an + // error. This allows callers to clean up any resources associated + // with the value. + errdefer if (comptime @hasDecl(Context, "deleted")) ctx.deleted(value); + // If the item doesn't exist, we need an available ID. if (self.next_id >= self.layout.cap) { // Arbitrarily chosen, threshold for rehashing. @@ -279,6 +285,8 @@ pub fn RefCountedSet( pub fn addWithIdContext(self: *Self, base: anytype, value: T, id: Id, ctx: Context) AddError!?Id { const items = self.items.ptr(base); + assert(id > 0); + if (id < self.next_id) { if (items[id].meta.ref == 0) { self.deleteItem(base, id, ctx); @@ -291,6 +299,11 @@ pub fn RefCountedSet( return if (added_id == id) null else added_id; } else if (ctx.eql(value, items[id].value)) { + // Notify the context that the value is "deleted" because + // we're reusing the existing value in the set. This allows + // callers to clean up any resources associated with the value. + if (comptime @hasDecl(Context, "deleted")) ctx.deleted(value); + items[id].meta.ref += 1; return null; @@ -501,6 +514,7 @@ pub fn RefCountedSet( // we're reusing the existing value in the set. This allows // callers to clean up any resources associated with the value. if (comptime @hasDecl(Context, "deleted")) ctx.deleted(value); + return id; }