aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/libghostty-vt/src/datastruct/limited_allocator.zig
blob: 11c4fbeaf090fdc95b89d0845671e26e24fd4bd9 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const std = @import("std");
const Allocator = std.mem.Allocator;

/// An allocator that rejects any single allocation or resize larger than a
/// configured byte limit. The limit applies to each request independently,
/// not to the total amount of memory currently allocated.
pub const LimitedAllocator = struct {
    child: Allocator,
    limit: usize,

    /// Set after an allocation, resize, or remap is rejected for exceeding
    /// the limit. This remains set until the caller clears it.
    limit_exceeded: bool = false,

    pub fn init(child: Allocator, limit: usize) LimitedAllocator {
        return .{
            .child = child,
            .limit = limit,
        };
    }

    pub fn allocator(self: *LimitedAllocator) Allocator {
        return .{
            .ptr = self,
            .vtable = &.{
                .alloc = alloc,
                .resize = resize,
                .remap = remap,
                .free = free,
            },
        };
    }

    fn check(self: *LimitedAllocator, size: usize) bool {
        if (size <= self.limit) return true;
        self.limit_exceeded = true;
        return false;
    }

    fn alloc(
        ctx: *anyopaque,
        len: usize,
        alignment: std.mem.Alignment,
        ret_addr: usize,
    ) ?[*]u8 {
        const self: *LimitedAllocator = @ptrCast(@alignCast(ctx));
        if (!self.check(len)) return null;
        return self.child.rawAlloc(len, alignment, ret_addr);
    }

    fn resize(
        ctx: *anyopaque,
        memory: []u8,
        alignment: std.mem.Alignment,
        new_len: usize,
        ret_addr: usize,
    ) bool {
        const self: *LimitedAllocator = @ptrCast(@alignCast(ctx));
        if (!self.check(new_len)) return false;
        return self.child.rawResize(memory, alignment, new_len, ret_addr);
    }

    fn remap(
        ctx: *anyopaque,
        memory: []u8,
        alignment: std.mem.Alignment,
        new_len: usize,
        ret_addr: usize,
    ) ?[*]u8 {
        const self: *LimitedAllocator = @ptrCast(@alignCast(ctx));
        if (!self.check(new_len)) return null;
        return self.child.rawRemap(memory, alignment, new_len, ret_addr);
    }

    fn free(
        ctx: *anyopaque,
        memory: []u8,
        alignment: std.mem.Alignment,
        ret_addr: usize,
    ) void {
        const self: *LimitedAllocator = @ptrCast(@alignCast(ctx));
        self.child.rawFree(memory, alignment, ret_addr);
    }
};

test "LimitedAllocator allows allocations through the limit" {
    const testing = std.testing;

    var limited: LimitedAllocator = .init(testing.allocator, 8);
    const alloc = limited.allocator();
    const data = try alloc.alloc(u8, 8);
    defer alloc.free(data);

    try testing.expect(!limited.limit_exceeded);
}

test "LimitedAllocator rejects allocations before the child" {
    const testing = std.testing;

    var failing = testing.FailingAllocator.init(testing.allocator, .{
        .fail_index = 0,
    });
    var limited: LimitedAllocator = .init(failing.allocator(), 8);

    try testing.expectError(error.OutOfMemory, limited.allocator().alloc(u8, 9));
    try testing.expect(limited.limit_exceeded);
    try testing.expect(!failing.has_induced_failure);
}

test "LimitedAllocator rejects oversized resize and remap" {
    const testing = std.testing;

    var limited: LimitedAllocator = .init(testing.allocator, 8);
    const alloc = limited.allocator();
    const data = try alloc.alloc(u8, 8);
    defer alloc.free(data);

    try testing.expect(!alloc.resize(data, 9));
    try testing.expect(limited.limit_exceeded);

    limited.limit_exceeded = false;
    try testing.expect(alloc.remap(data, 9) == null);
    try testing.expect(limited.limit_exceeded);
}