aboutsummaryrefslogtreecommitdiff
path: root/src/nullable_allocator.zig
blob: 8af65d6bbfd6dbfc82da982239b853574d1f8b5b (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
const std = @import("std");

/// A nullable allocator the same size as `std.mem.Allocator`.
pub const NullableAllocator = struct {
    ptr: *anyopaque = undefined,
    // Utilize the null pointer optimization on the vtable instead of
    // the regular ptr because some allocator implementations might tag their
    // `ptr` property.
    vtable: ?*const std.mem.Allocator.VTable = null,

    pub inline fn init(a: std.mem.Allocator) @This() {
        return .{
            .ptr = a.ptr,
            .vtable = a.vtable,
        };
    }

    pub inline fn isNull(this: @This()) bool {
        return this.vtable == null;
    }

    pub inline fn get(this: @This()) ?std.mem.Allocator {
        return if (this.vtable) |vt| std.mem.Allocator{ .ptr = this.ptr, .vtable = vt } else null;
    }
};

comptime {
    if (@sizeOf(NullableAllocator) != @sizeOf(std.mem.Allocator)) {
        @compileError("Expected the sizes to be the same.");
    }
}
mit/src/string_immutable.zig?h=dylan/github-api-option&id=819a63e3ec905d1f013ff9c7b7480ff649b78477&follow=1'>Fix build issueGravatar Jarred Sumner 2-15/+17 2022-11-26[bun:sqlite] Fix bug with latin1 supplemental charactersGravatar Jarred Sumner 2-2/+31 2022-11-26prettier?Gravatar Jarred Sumner 1-336/+339 2022-11-26Update mimallocGravatar Jarred Sumner 1-0/+0 2022-11-26Update string_immutable.zigGravatar Jarred Sumner 1-1/+1 2022-11-26Faster UTF16 -> UTF8 and UTF8 -> UTF16 (#1552)Gravatar Jarred Sumner 14-6/+31329 2022-11-26Make HTTP status text more consistent with other HTTP serversGravatar Jarred Sumner 2-2/+153