Skip to content

Commit d78b3c6

Browse files
Add runtime page_size
1 parent 6734d21 commit d78b3c6

File tree

6 files changed

+34
-1
lines changed

6 files changed

+34
-1
lines changed

lib/std/c.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ pub usingnamespace switch (builtin.os.tag) {
7979
pub extern "c" fn getrusage(who: c_int, usage: *c.rusage) c_int;
8080

8181
pub extern "c" fn sched_yield() c_int;
82+
pub extern "c" fn sysconf(sc: c_int) c_long;
8283

8384
pub extern "c" fn sigaction(sig: c_int, noalias act: ?*const c.Sigaction, noalias oact: ?*c.Sigaction) c_int;
8485
pub extern "c" fn sigprocmask(how: c_int, noalias set: ?*const c.sigset_t, noalias oset: ?*c.sigset_t) c_int;

lib/std/mem.zig

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,19 @@ const testing = std.testing;
1010
const Endian = std.builtin.Endian;
1111
const native_endian = builtin.cpu.arch.endian();
1212

13+
/// Runtime known minimum page size.
14+
/// If the page size cannot be determined at runtime,
15+
/// it will use the comptime known one.
16+
pub fn getPageSize() usize {
17+
return switch (builtin.os.tag) {
18+
.linux => if (builtin.link_libc) @intCast(std.c.sysconf(30)) else std.os.linux.getauxval(std.elf.AT_PAGESZ),
19+
else => page_size,
20+
};
21+
}
22+
1323
/// Compile time known minimum page size.
1424
/// https://github.com/ziglang/zig/issues/4082
15-
pub const page_size = switch (builtin.cpu.arch) {
25+
pub const page_size = builtin.target.page_size orelse switch (builtin.cpu.arch) {
1626
.wasm32, .wasm64 => 64 * 1024,
1727
.aarch64 => switch (builtin.os.tag) {
1828
.macos, .ios, .watchos, .tvos => 16 * 1024,

lib/std/target.zig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub const Target = struct {
88
os: Os,
99
abi: Abi,
1010
ofmt: ObjectFormat,
11+
page_size: ?usize = null,
1112

1213
pub const Os = struct {
1314
tag: Tag,
@@ -449,6 +450,10 @@ pub const Target = struct {
449450
=> false,
450451
};
451452
}
453+
454+
pub fn compare(self: Os, other: Os) bool {
455+
return self.tag == other.tag and self.isAtLeast(other.tag, other.getVersionRange());
456+
}
452457
};
453458

454459
pub const aarch64 = @import("target/aarch64.zig");
@@ -1360,8 +1365,16 @@ pub const Target = struct {
13601365
pub fn baseline(arch: Arch) Cpu {
13611366
return Model.baseline(arch).toCpu(arch);
13621367
}
1368+
1369+
pub fn compare(self: Cpu, other: Cpu) bool {
1370+
return self.arch == other.arch and std.mem.eql(u8, self.model.name, other.model.name) and self.features.eql(other.features);
1371+
}
13631372
};
13641373

1374+
pub fn useHostPageSize(self: Target, other: Target) bool {
1375+
return self.cpu.compare(other.cpu) and self.os.compare(other.os);
1376+
}
1377+
13651378
pub fn zigTriple(self: Target, allocator: mem.Allocator) ![]u8 {
13661379
return std.zig.CrossTarget.fromTarget(self).zigTriple(allocator);
13671380
}

lib/std/zig/CrossTarget.zig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ dynamic_linker: DynamicLinker = DynamicLinker{},
4545
/// `null` means default for the cpu/arch/os combo.
4646
ofmt: ?Target.ObjectFormat = null,
4747

48+
/// `null` means to dynamically get the page size or use the platform default.
49+
page_size: ?usize = null,
50+
4851
pub const CpuModel = union(enum) {
4952
/// Always native
5053
native,
@@ -81,6 +84,7 @@ pub fn fromTarget(target: Target) CrossTarget {
8184
target.os.version_range.linux.glibc
8285
else
8386
null,
87+
.page_size = target.page_size,
8488
};
8589
result.updateOsVersionRange(target.os);
8690

@@ -176,6 +180,7 @@ pub fn toTarget(self: CrossTarget) Target {
176180
.os = self.getOs(),
177181
.abi = self.getAbi(),
178182
.ofmt = self.getObjectFormat(),
183+
.page_size = self.page_size,
179184
};
180185
}
181186

lib/std/zig/system/NativeTargetInfo.zig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,7 @@ pub fn abiAndDynamicLinkerFromFile(
644644
.os = os,
645645
.abi = cross_target.abi orelse Target.Abi.default(cpu.arch, os),
646646
.ofmt = cross_target.ofmt orelse Target.ObjectFormat.default(os.tag, cpu.arch),
647+
.page_size = cross_target.page_size,
647648
},
648649
.dynamic_linker = cross_target.dynamic_linker,
649650
};
@@ -921,6 +922,7 @@ fn defaultAbiAndDynamicLinker(cpu: Target.Cpu, os: Target.Os, cross_target: Cros
921922
.os = os,
922923
.abi = cross_target.abi orelse Target.Abi.default(cpu.arch, os),
923924
.ofmt = cross_target.ofmt orelse Target.ObjectFormat.default(os.tag, cpu.arch),
925+
.page_size = cross_target.page_size,
924926
};
925927
return NativeTargetInfo{
926928
.target = target,

src/Compilation.zig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6267,6 +6267,7 @@ pub fn generateBuiltinZigSource(comp: *Compilation, allocator: Allocator) Alloca
62676267
\\ .os = os,
62686268
\\ .abi = abi,
62696269
\\ .ofmt = object_format,
6270+
\\ .page_size = {?},
62706271
\\}};
62716272
\\pub const object_format = std.Target.ObjectFormat.{};
62726273
\\pub const mode = std.builtin.OptimizeMode.{};
@@ -6282,6 +6283,7 @@ pub fn generateBuiltinZigSource(comp: *Compilation, allocator: Allocator) Alloca
62826283
\\pub const omit_frame_pointer = {};
62836284
\\
62846285
, .{
6286+
target.page_size orelse (if (comp.bin_file.options.is_native_os and comp.bin_file.options.is_native_abi) std.mem.getPageSize() else null),
62856287
std.zig.fmtId(@tagName(target.ofmt)),
62866288
std.zig.fmtId(@tagName(comp.bin_file.options.optimize_mode)),
62876289
link_libc,

0 commit comments

Comments
 (0)