feat: items with arc instead of arena

This commit is contained in:
2025-05-25 10:31:58 +02:00
parent 4a9dc12b48
commit 5cfb556dff
4 changed files with 76 additions and 38 deletions

View File

@@ -1,6 +1,6 @@
running: bool = true,
usage_number: locked(u16) = .{ .data = 0 },
current_items: locked(?[]*models.Item) = .{ .data = null },
current_items: locked(?[]Arc(*models.Item).Weak) = .{ .data = null },
allocator: std.mem.Allocator,
appState: AppState,
_private: Private,
@@ -24,20 +24,36 @@ pub fn init(model: *Self, allocator: std.mem.Allocator, appState: AppState) !voi
try model.appState.tabPreCurrentItemsUnload.attach(&model._private.preCurrentItemsUnload);
}
pub fn updateCurrentItems(self: *Self, tab_current_items: *std.ArrayList(*models.Item)) !void {
pub fn updateCurrentItems(self: *Self, tab_current_items: *std.ArrayList(Arc(*models.Item))) !void {
self.resetCurrentItems();
const items = try self.allocator.alloc(*models.Item, tab_current_items.items.len);
var items = try self.allocator.alloc(Arc(*models.Item).Weak, tab_current_items.items.len);
errdefer {
self.allocator.free(items);
self.current_items.data = null;
}
for (tab_current_items.items, 0..) |item, i| {
items[i] = item;
for (tab_current_items.items, 0..) |*item, i| {
items[i] = item.downgrade();
}
std.mem.sort(*models.Item, items, {}, struct {
fn sort(_: void, lhs: *models.Item, rhs: *models.Item) bool {
std.mem.sort(Arc(*models.Item).Weak, items, {}, struct {
fn sort(_: void, lhs_weak: Arc(*models.Item).Weak, rhs_weak: Arc(*models.Item).Weak) bool {
const lhs_arc = @constCast(&lhs_weak).upgrade();
defer if (lhs_arc) |l1| if (l1.releaseUnwrap()) |l2| l2.deinit();
const rhs_arc = @constCast(&rhs_weak).upgrade();
defer if (rhs_arc) |r1| if (r1.releaseUnwrap()) |r2| r2.deinit();
const lhs = if (lhs_arc) |l|
l.value.*
else
return false;
const rhs = if (rhs_arc) |r|
r.value.*
else
return false;
if (lhs.item == .container and rhs.item == .element) return true;
if (lhs.item == .element and rhs.item == .container) return false;
return std.mem.order(u8, lhs.displayName, rhs.displayName) == .lt;
@@ -65,6 +81,9 @@ fn resetCurrentItems(self: *@This()) void {
// self.current_items.data = null;
if (data) |currentItems| {
for (currentItems) |item| {
item.release();
}
self.allocator.free(currentItems);
}
}
@@ -82,4 +101,5 @@ const Tab = @import("../core/tab/tab.zig").Tab;
const locked = @import("../core/sync.zig").locked;
const AppState = @import("../core/app_state.zig").AppState;
const Observer = @import("../core/observable.zig").Observer;
const Arc = @import("zigrc").Arc;
const Self = @This();