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
|
const builtin = @import("builtin");
const std = @import("std");
pub const png = @import("png.zig");
pub const jpeg = @import("jpeg.zig");
pub const swizzle = @import("swizzle.zig");
pub const Error = @import("error.zig").Error;
/// The maximum image size, based on the 4G limit of Ghostty's
/// `image-storage-limit` config.
pub const maximum_image_size = 4 * 1024 * 1024 * 1024;
pub const ImageData = struct {
width: u32,
height: u32,
data: []u8,
};
// Wuffs' generated `wuffs_foo__bar__alloc()` convenience functions are the
// only code that references libc's calloc/free. We never call them
// and linker garbage collection strips them, but that isn't guaranteed.
// When libc isn't linked there would be nothing to provide calloc/free if
// they survive, so export stubs to satisfy the link. Weak so that any real
// definition wins. Hidden keeps them out of the export table where the
// format honors it (e.g. wasm).
//
// Only in a final executable, though. "Weak so that any real definition
// wins" holds for dynamic linking, but not for a static archive: once the
// linker pulls this member in for some other symbol, the weak `calloc` here
// *satisfies* the reference, and it never searches libc.a for musl's strong
// one. A C-API consumer that links libc itself then silently gets an
// allocator that returns null for every request -- which is a crash at the
// first store into the result, a long way from here. When we are the library
// rather than the executable, the binary being built is the one that owns
// the choice of allocator, so leave the symbols to it.
comptime {
if (!builtin.link_libc and builtin.output_mode == .Exe) {
@export(&callocStub, .{
.name = "calloc",
.linkage = .weak,
.visibility = .hidden,
});
@export(&freeStub, .{
.name = "free",
.linkage = .weak,
.visibility = .hidden,
});
}
}
fn callocStub(count: usize, size: usize) callconv(.c) ?*anyopaque {
_ = count;
_ = size;
return null;
}
fn freeStub(ptr: ?*anyopaque) callconv(.c) void {
_ = ptr;
}
test {
refAllDeclsRecursive(@This());
}
/// Copied from 0.15.2 stdlib (MIT license).
fn refAllDeclsRecursive(comptime T: type) void {
if (!builtin.is_test) return;
inline for (comptime std.meta.declarations(T)) |decl| {
if (@TypeOf(@field(T, decl.name)) == type) {
switch (@typeInfo(@field(T, decl.name))) {
.@"struct", .@"enum", .@"union", .@"opaque" => refAllDeclsRecursive(@field(T, decl.name)),
else => {},
}
}
_ = &@field(T, decl.name);
}
}
|