Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/graph/hot_cache.zig
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ pub fn LruCache(comptime V: type) type {

// Insert new entry
const entry = try self.pool.create();
errdefer self.pool.destroy(entry);
entry.* = .{ .key = key, .value = value };
try self.map.put(key, entry);
self.pushFront(entry);
Expand Down Expand Up @@ -156,6 +157,13 @@ pub const CachedSymbol = struct {
line: u32,
};

fn hotCachePutNoLeakOnFailure(allocator: std.mem.Allocator) !void {
var cache = LruCache(u32).init(allocator, 4);
defer cache.deinit();

try cache.put(1, 100);
}

// ── Tests ───────────────────────────────────────────────────────────────────

test "basic put and get" {
Expand Down Expand Up @@ -218,6 +226,10 @@ test "put updates existing key" {
try std.testing.expectEqual(@as(?u32, 200), cache.get(1));
}

test "put cleans up pool entry when map.put fails" {
try std.testing.checkAllAllocationFailures(std.testing.allocator, hotCachePutNoLeakOnFailure, .{});
}

test "remove deletes entry" {
var cache = LruCache(u32).init(std.testing.allocator, 4);
defer cache.deinit();
Expand Down
Loading