summaryrefslogtreecommitdiff
path: root/src/hooks.zig
blob: 2fda734c4220337501f4235275c8a05f3fac010f (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
const std = @import("std");

pub const Kind = enum {
    none,
    breakpoint,
};
const Self = @This();
pub const Hook = @This();

pub fn init(comptime kind: Kind, comptime alloc: std.mem.Allocator, comptime uselibc: bool) type {
    return struct {
        alloc: std.mem.Allocator = alloc,

        const Header = struct {
            len: usize,
        };
        const header_size = std.mem.alignForward(usize, @sizeOf(Header), @alignOf(std.c.max_align_t));
        const alignment: std.mem.Alignment = .fromByteUnits(@alignOf(std.c.max_align_t));

        inline fn ptrFromRaw(raw: []u8) ?*anyopaque {
            const h: *Header = @ptrCast(@alignCast(raw.ptr));
            h.* = .{ .len = raw.len };
            return @ptrFromInt(@intFromPtr(raw.ptr) + header_size);
        }

        inline fn rawFromPtr(ptr: *anyopaque) []u8 {
            const base = @intFromPtr(ptr) - header_size;
            const h: *Header = @ptrFromInt(base);
            return @as([*]u8, @ptrFromInt(base))[0..h.len];
        }

        pub fn malloc(size: usize) callconv(.c) ?*anyopaque {
            if (kind == .breakpoint) @breakpoint();
            if (uselibc) return std.c.malloc(size);
            const total = header_size + size;
            const raw = alloc.alignedAlloc(u8, alignment, total) catch return null;
            return ptrFromRaw(raw);
        }

        pub fn calloc(n: usize, size: usize) callconv(.c) ?*anyopaque {
            if (kind == .breakpoint) @breakpoint();
            if (uselibc) return std.c.calloc(n, size);
            const total_data = std.math.mul(usize, n, size) catch return null;
            const total = header_size + total_data;
            const raw = alloc.alignedAlloc(u8, alignment, total) catch return null;
            @memset(raw[header_size..], 0);
            return ptrFromRaw(raw);
        }

        pub fn realloc(ptr: ?*anyopaque, size: usize) callconv(.c) ?*anyopaque {
            if (kind == .breakpoint) @breakpoint();
            if (uselibc) return std.c.realloc(ptr, size);
            if (ptr) |p| {
                const old_raw = rawFromPtr(p);
                const total = header_size + size;
                if (alloc.resize(old_raw, total)) {
                    const h: *Header = @ptrCast(@alignCast(old_raw.ptr));
                    h.* = .{ .len = total };
                    return p;
                }
                const new_raw = alloc.alignedAlloc(u8, alignment, total) catch return null;
                const old_data = old_raw[header_size..];
                const copy_len = @min(old_data.len, size);
                @memcpy(new_raw[header_size..][0..copy_len], old_data[0..copy_len]);
                alloc.free(old_raw);
                return ptrFromRaw(new_raw);
            } else {
                return malloc(size);
            }
        }

        pub fn free(ptr: ?*anyopaque) callconv(.c) void {
            if (kind == .breakpoint) @breakpoint();
            if (uselibc) return std.c.free(ptr);
            if (ptr) |p| {
                const raw = rawFromPtr(p);
                alloc.free(raw);
            }
        }

        pub fn vsnprintf(_: [*c]u8, _: usize, _: [*c]const u8, _: [*c]u8) callconv(.c) ?*anyopaque {
            @compileError("Unimplemented!");
        }
    };
}