diff options
Diffstat (limited to 'src/memory.zig')
| -rw-r--r-- | src/memory.zig | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/src/memory.zig b/src/memory.zig index 7adcf914..cd29308f 100644 --- a/src/memory.zig +++ b/src/memory.zig @@ -422,6 +422,7 @@ pub fn freeUnfilled(allocator: Allocator, bytes: []const u8) void { if (bytes.len == 0) return; // A blob's digest word goes with it (walk.zig). walk.forget(@intFromPtr(bytes.ptr)); + if (pinned(allocator, bytes, .of(u8))) return; allocator.rawFree(@constCast(bytes), .of(u8), @returnAddress()); } @@ -433,9 +434,72 @@ pub fn freeRaw(allocator: Allocator, items: anytype) void { const bytes = std.mem.sliceAsBytes(items); if (bytes.len == 0) return; walk.forget(@intFromPtr(bytes.ptr)); + if (pinned(allocator, bytes, .of(E))) return; allocator.rawFree(@constCast(bytes), .of(E), @returnAddress()); } +// ---- a join's pins (src/replica.zig) ---- +// +// A replica client's join dumps the session's mutable state in its step and +// leaves its blobs out (walk.Deferred); its writer thread streams them after. +// A blob is never written in place and is freed raw (above), so until the +// writer has sent it, its free is put off: `pin` it, `unpin` it after. +var pins_lock: std.atomic.Value(bool) = .init(false); +var pins: std.AutoHashMapUnmanaged(usize, u32) = .empty; +var pin_count: std.atomic.Value(usize) = .init(0); +const PutOff = struct { allocator: Allocator, bytes: []const u8, alignment: std.mem.Alignment }; +var put_off: std.ArrayListUnmanaged(PutOff) = .empty; + +fn lockPins() void { + while (pins_lock.cmpxchgWeak(false, true, .acquire, .monotonic) != null) std.atomic.spinLoopHint(); +} + +/// The blob at `bytes.ptr` kept alive until it is unpinned as many times. +pub fn pin(bytes: []const u8) void { + lockPins(); + defer pins_lock.store(false, .release); + const got = pins.getOrPut(std.heap.page_allocator, @intFromPtr(bytes.ptr)) catch return; + got.value_ptr.* = if (got.found_existing) got.value_ptr.* + 1 else 1; + pin_count.store(pins.count(), .release); +} + +/// One pin of the blob at `bytes.ptr` let go: its free, put off, made now +/// the last one goes. +pub fn unpin(bytes: []const u8) void { + const addr = @intFromPtr(bytes.ptr); + lockPins(); + const n = pins.getPtr(addr) orelse { + pins_lock.store(false, .release); + return; + }; + n.* -= 1; + var due: ?PutOff = null; + if (n.* == 0) { + _ = pins.remove(addr); + pin_count.store(pins.count(), .release); + for (put_off.items, 0..) |f, i| if (@intFromPtr(f.bytes.ptr) == addr) { + due = put_off.swapRemove(i); + break; + }; + } + pins_lock.store(false, .release); + if (due) |f| { + // A word made for it while it was pinned (walk.makeWords) goes too. + walk.forget(addr); + f.allocator.rawFree(@constCast(f.bytes), f.alignment, @returnAddress()); + } +} + +/// Whether the free of `bytes` is put off (it is pinned). +fn pinned(allocator: Allocator, bytes: []const u8, alignment: std.mem.Alignment) bool { + if (pin_count.load(.acquire) == 0) return false; + lockPins(); + defer pins_lock.store(false, .release); + if (!pins.contains(@intFromPtr(bytes.ptr))) return false; + put_off.append(std.heap.page_allocator, .{ .allocator = allocator, .bytes = bytes, .alignment = alignment }) catch return false; + return true; +} + /// Gives free memory back to the system, the arenas' and libc's, inside its /// heap too: after a big result is formatted or a big pane torn down, the /// tree-sitter arena kept 73 MiB of emptied buckets resident and glibc what @@ -568,3 +632,18 @@ test "memory limits preserve desktop capacities" { try std.testing.expectEqual(256 * MiB, limits.arena.image); try std.testing.expectEqual(if (config.mupdf) 512 * MiB else 64 * KiB, limits.arena.pdf); } + +test "a pinned blob's free is put off until its last pin goes" { + const gpa = std.testing.allocator; + const b = try gpa.alloc(u8, 8192); + @memset(b, 7); + pin(b); + pin(b); + freeUnfilled(gpa, b); + // Still the blob's: a join's writer reads it. + try std.testing.expectEqual(@as(u8, 7), b[100]); + unpin(b); + try std.testing.expectEqual(@as(u8, 7), b[8000]); + // The last pin: freed now (the testing allocator says if not). + unpin(b); +} |
