aboutsummaryrefslogtreecommitdiff
path: root/src/ref_count.zig
blob: 2cc5c09fd300d3b98060c419d3605544d5e30d00 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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;
    };
}
nsg191/bun/commit/src/napi/napi.zig?h=ciro/queue-response-experiment&id=ad0bee532c9748c944562b5ade76021138eb4fa7&follow=1'>Fix castGravatar Jarred Sumner 1-15/+17 2023-02-15ensure we allocate for > 6 argumentsGravatar Jarred Sumner 1-6/+13 2023-02-15Update async_hooks.exports.jsGravatar Jarred Sumner 1-2/+2 2023-02-15workaround prisma's usage of `eval("__dirname")`Gravatar Jarred Sumner 1-1/+23 2023-02-15some cleanupGravatar Jarred Sumner 2-15/+9 2023-02-15ED25519 WebCrypto (#1971)Gravatar Jarred Sumner 12-11/+1167 * ed25519 * Register the algorithm * try this? * fix(webcrypto): fix ed25519 keypair gen (#1985) * fix: import and export ed25519 (#2004) * fix(webcrypto): allow import and export ed25519 * fix(webcrypto): copy exportkey * fix(webcrypto): fix use after stack free --------- Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> Co-authored-by: Derrick Farris <mr.dcfarris@gmail.com> 2023-02-14Fix up async_hooks polyfillGravatar Jarred Sumner 2-8/+63 2023-02-14Add temporary polyfill for async_hooksGravatar Jarred Sumner 5-108/+324 2023-02-14:mask: async_hooksGravatar Jarred Sumner 1-0/+4 2023-02-14[install] link network-delayed `.bin` scripts correctly (#2076)Gravatar Alex Lam S.L 3-16/+21 2023-02-14don't break esbuildGravatar Jarred Sumner 7-75/+50 2023-02-14Add workaround for `tls` and `worker_threads`Gravatar Jarred Sumner 3-1/+64 2023-02-14[install] improve `package.json` validation (#2074)Gravatar Alex Lam S.L 6-104/+342 - report error and exit gracefully instead of crashing 2023-02-14[WIP] fix(node:fs): export `fs.ReadStream` and `fs.WriteStream` (#1798)Gravatar Derrick Farris 4-72/+326 * fix(node:fs): export fs.WriteStream and fs.ReadStream * test(node:fs): add tests for fs.ReadStream and fs.WriteStream * test(node:fs): prevent opening fd w/o closing * fix(node:fs): hack ESM export for fs streams to keep lazy loading * fix(node:fs): = -> ===, fix hasInstance comparison * test(node:fs): add test that actually checks that re-exported streams work * fix(fs): eagerly load our slow lazy fns (thanks esm) * fix(fs): employ @alexlamsl 's constructor w/o new trick on Read/WriteStream 2023-02-14Reject with error when invalid fetch() body (#2047)Gravatar Eric Zhang 2-12/+44 * Reject with error when invalid fetch() body Resolves #2014 * Make sure the test actually throws an exception * Update fetch error paths to return TypeErrors 2023-02-13fix(FormData): make String explicit, thanks @dylan-conway (#2065)Gravatar Derrick Farris 1-1/+1 2023-02-13fix(FormData): add string literal operator (#2064)Gravatar Derrick Farris 1-2/+2 2023-02-13Add pretty printer for FormDataGravatar Jarred Sumner 5-1/+101 2023-02-13Add dynamic port assigning to Bun.serve (#2062)Gravatar Michał Warda 3-5/+40 2023-02-13feat(napi): add `napi_get_value_bigint_words` (#2061)Gravatar Derrick Farris 3-0/+44 * feat(napi): add `napi_get_value_bigint_words` * fix(napi): handle `napi_get_value_bigint_words` arr too small 2023-02-13Fixes https://github.com/oven-sh/bun/issues/1456Gravatar Jarred Sumner 8-1/+148