Files
bun.sh/src/ref_count.zig
Jarred Sumner c0dd284136 Upgrade to latest Zig (#1610)
* @min and @max

* builtins and some trivial ones

* Most of them

* more

* more!

* More Progress

* wip

* Update tagged_pointer.zig

* Update http_client_async.zig

* Most of the iterable dir changes

* alright

* Remove usages of deprecated formatters

* 📷

* fmt

* Update shimmer.zig

* wip

* wip

* wip

* progress

* more

* Latest

* stuck on error

* latest

* workaround stage2

* wip

* Update string_immutable.zig

* wip

* Migrate `Dirent` and `require("fs')` to use JSC<>Zig bindings

* Fix build errors

* Fixup most of the test failures

* Fix `make headers`

* Fix "outside package path" error

* Fixup aligned alloc

* Add missing file

* linux

* More linux fixes

* use latest peechy

* Fix transpiler test failure

* Forgot about these

* Fixup test failure

* Update node-timers.test.ts

* [node:htt] Fix `undefined is not an object` error

Fixes https://github.com/oven-sh/bun/issues/1618

* Update http.exports.js

* Make this test less flaky

* fix hashes

* Fix hex formatting and zls issues

* Download zig version

* Update Dockerfile

* Update Dockerfile

* Update uws

* Update Dockerfile

* Set llvm version

* Update README.md

* Update uws

* Update Dockerfile

* Update io_linux.zig

* Update bun.zig

* Log output

* workaround strange @cInclude error

* Make ffi tests better

* Don't use cImport

* Update c.zig

* Update c-bindings.cpp

* call setOutputDir

* Update Dockerfile

* Use a longer name

* latest

* Update serve.test.ts

Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
Co-authored-by: Veikka Tuominen <git@vexu.eu>
2022-12-28 00:51:22 -08:00

81 lines
2.1 KiB
Zig

const std = @import("std");
pub fn RefCount(comptime TypeName: type, comptime deinit_on_zero: bool) type {
return struct {
const AllocatorType = if (deinit_on_zero) std.mem.Allocator else void;
value: Type,
count: i32 = 1,
allocator: AllocatorType = undefined,
pub inline fn ref(this: *@This()) void {
this.count += 1;
}
/// Create a new reference counted value.
pub inline fn init(
value: Type,
allocator: std.mem.Allocator,
) !*@This() {
var ptr = try allocator.create(@This());
ptr.create(value, allocator);
return ptr;
}
/// Get the value & increment the reference count.
pub inline fn get(this: *@This()) *Type {
std.debug.assert(this.count >= 0);
this.count += 1;
return this.leak();
}
/// Get the value without incrementing the reference count.
pub inline fn leak(this: *@This()) *Type {
return &this.value;
}
pub inline fn getRef(this: *@This()) *@This() {
this.count += 1;
return this;
}
pub inline fn create(
this: *@This(),
value: Type,
allocator: AllocatorType,
) void {
this.* = .{
.value = value,
.allocator = allocator,
.count = 1,
};
}
pub inline fn deinit(this: *@This()) void {
if (comptime @hasDecl(Type, "deinit")) {
this.value.deinit();
}
if (comptime deinit_on_zero) {
var allocator = this.allocator;
allocator.destroy(this);
}
}
pub inline fn deref(this: *@This()) void {
this.count -= 1;
std.debug.assert(this.count >= 0);
if (comptime deinit_on_zero) {
if (this.count <= 0) {
this.deinit();
}
}
}
pub const Type = TypeName;
};
}