aboutsummaryrefslogtreecommitdiff
path: root/lib/std/heap/logging_allocator.zig
blob: 0d15986a76a94ed881caad2b53cfa27c039045ea (plain)
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
const std = @import("../std.zig");
const Allocator = std.mem.Allocator;

/// This allocator is used in front of another allocator and logs to the provided stream
/// on every call to the allocator. Stream errors are ignored.
/// If https://github.com/ziglang/zig/issues/2586 is implemented, this API can be improved.
pub fn LoggingAllocator(comptime OutStreamType: type) type {
    return struct {
        allocator: Allocator,
        parent_allocator: *Allocator,
        out_stream: OutStreamType,

        const Self = @This();

        pub fn init(parent_allocator: *Allocator, out_stream: OutStreamType) Self {
            return Self{
                .allocator = Allocator{
                    .reallocFn = realloc,
                    .shrinkFn = shrink,
                },
                .parent_allocator = parent_allocator,
                .out_stream = out_stream,
            };
        }

        fn realloc(allocator: *Allocator, old_mem: []u8, old_align: u29, new_size: usize, new_align: u29) ![]u8 {
            const self = @fieldParentPtr(Self, "allocator", allocator);
            if (old_mem.len == 0) {
                self.out_stream.print("allocation of {} ", .{new_size}) catch {};
            } else {
                self.out_stream.print("resize from {} to {} ", .{ old_mem.len, new_size }) catch {};
            }
            const result = self.parent_allocator.reallocFn(self.parent_allocator, old_mem, old_align, new_size, new_align);
            if (result) |buff| {
                self.out_stream.print("success!\n", .{}) catch {};
            } else |err| {
                self.out_stream.print("failure!\n", .{}) catch {};
            }
            return result;
        }

        fn shrink(allocator: *Allocator, old_mem: []u8, old_align: u29, new_size: usize, new_align: u29) []u8 {
            const self = @fieldParentPtr(Self, "allocator", allocator);
            const result = self.parent_allocator.shrinkFn(self.parent_allocator, old_mem, old_align, new_size, new_align);
            if (new_size == 0) {
                self.out_stream.print("free of {} bytes success!\n", .{old_mem.len}) catch {};
            } else {
                self.out_stream.print("shrink from {} bytes to {} bytes success!\n", .{ old_mem.len, new_size }) catch {};
            }
            return result;
        }
    };
}

pub fn loggingAllocator(
    parent_allocator: *Allocator,
    out_stream: var,
) LoggingAllocator(@TypeOf(out_stream)) {
    return LoggingAllocator(@TypeOf(out_stream)).init(parent_allocator, out_stream);
}

test "LoggingAllocator" {
    var buf: [255]u8 = undefined;
    var fbs = std.io.fixedBufferStream(&buf);

    const allocator = &loggingAllocator(std.testing.allocator, fbs.outStream()).allocator;

    const ptr = try allocator.alloc(u8, 10);
    allocator.free(ptr);

    std.testing.expectEqualSlices(u8,
        \\allocation of 10 success!
        \\free of 10 bytes success!
        \\
    , fbs.getWritten());
}