Skip to content

Commit 53eff28

Browse files
Add runtime page_size
1 parent 5c0766b commit 53eff28

33 files changed

+338
-187
lines changed

lib/std/Thread.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ const PosixThreadImpl = struct {
686686
// Use the same set of parameters used by the libc-less impl.
687687
const stack_size = @max(config.stack_size, 16 * 1024);
688688
assert(c.pthread_attr_setstacksize(&attr, stack_size) == .SUCCESS);
689-
assert(c.pthread_attr_setguardsize(&attr, std.mem.page_size) == .SUCCESS);
689+
assert(c.pthread_attr_setguardsize(&attr, std.heap.pageSize()) == .SUCCESS);
690690

691691
var handle: c.pthread_t = undefined;
692692
switch (c.pthread_create(
@@ -1039,7 +1039,7 @@ const LinuxThreadImpl = struct {
10391039
completion: Completion = Completion.init(.running),
10401040
child_tid: std.atomic.Value(i32) = std.atomic.Value(i32).init(1),
10411041
parent_tid: i32 = undefined,
1042-
mapped: []align(std.mem.page_size) u8,
1042+
mapped: []u8,
10431043

10441044
/// Calls `munmap(mapped.ptr, mapped.len)` then `exit(1)` without touching the stack (which lives in `mapped.ptr`).
10451045
/// Ported over from musl libc's pthread detached implementation:
@@ -1183,7 +1183,7 @@ const LinuxThreadImpl = struct {
11831183
};
11841184

11851185
fn spawn(config: SpawnConfig, comptime f: anytype, args: anytype) !Impl {
1186-
const page_size = std.mem.page_size;
1186+
const page_size = std.heap.pageSize();
11871187
const Args = @TypeOf(args);
11881188
const Instance = struct {
11891189
fn_args: Args,

lib/std/c.zig

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const std = @import("std");
22
const builtin = @import("builtin");
33
const c = @This();
4-
const page_size = std.mem.page_size;
54
const iovec = std.os.iovec;
65
const iovec_const = std.os.iovec_const;
76
const wasi = @import("c/wasi.zig");
@@ -1513,6 +1512,7 @@ pub usingnamespace switch (native_os) {
15131512
pub extern "c" fn getrusage(who: c_int, usage: *c.rusage) c_int;
15141513

15151514
pub extern "c" fn sched_yield() c_int;
1515+
pub extern "c" fn sysconf(sc: c_int) c_long;
15161516

15171517
pub extern "c" fn sigaction(sig: c_int, noalias act: ?*const c.Sigaction, noalias oact: ?*c.Sigaction) c_int;
15181518
pub extern "c" fn sigprocmask(how: c_int, noalias set: ?*const c.sigset_t, noalias oset: ?*c.sigset_t) c_int;
@@ -1523,7 +1523,7 @@ pub usingnamespace switch (native_os) {
15231523

15241524
pub extern "c" fn alarm(seconds: c_uint) c_uint;
15251525

1526-
pub extern "c" fn msync(addr: *align(page_size) const anyopaque, len: usize, flags: c_int) c_int;
1526+
pub extern "c" fn msync(addr: *const anyopaque, len: usize, flags: c_int) c_int;
15271527
},
15281528
};
15291529

@@ -1600,9 +1600,9 @@ pub extern "c" fn writev(fd: c_int, iov: [*]const iovec_const, iovcnt: c_uint) i
16001600
pub extern "c" fn pwritev(fd: c_int, iov: [*]const iovec_const, iovcnt: c_uint, offset: c.off_t) isize;
16011601
pub extern "c" fn write(fd: c.fd_t, buf: [*]const u8, nbyte: usize) isize;
16021602
pub extern "c" fn pwrite(fd: c.fd_t, buf: [*]const u8, nbyte: usize, offset: c.off_t) isize;
1603-
pub extern "c" fn mmap(addr: ?*align(page_size) anyopaque, len: usize, prot: c_uint, flags: MAP, fd: c.fd_t, offset: c.off_t) *anyopaque;
1604-
pub extern "c" fn munmap(addr: *align(page_size) const anyopaque, len: usize) c_int;
1605-
pub extern "c" fn mprotect(addr: *align(page_size) anyopaque, len: usize, prot: c_uint) c_int;
1603+
pub extern "c" fn mmap(addr: ?*anyopaque, len: usize, prot: c_uint, flags: MAP, fd: c.fd_t, offset: c.off_t) *anyopaque;
1604+
pub extern "c" fn munmap(addr: *const anyopaque, len: usize) c_int;
1605+
pub extern "c" fn mprotect(addr: *anyopaque, len: usize, prot: c_uint) c_int;
16061606
pub extern "c" fn link(oldpath: [*:0]const u8, newpath: [*:0]const u8, flags: c_int) c_int;
16071607
pub extern "c" fn linkat(oldfd: c.fd_t, oldpath: [*:0]const u8, newfd: c.fd_t, newpath: [*:0]const u8, flags: c_int) c_int;
16081608
pub extern "c" fn unlink(path: [*:0]const u8) c_int;

lib/std/c/darwin.zig

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3412,7 +3412,7 @@ pub const MachTask = extern struct {
34123412
return left;
34133413
}
34143414

3415-
fn getPageSize(task: MachTask) MachError!usize {
3415+
pub fn getPageSize(task: MachTask) MachError!usize {
34163416
if (task.isValid()) {
34173417
var info_count = TASK_VM_INFO_COUNT;
34183418
var vm_info: task_vm_info_data_t = undefined;
@@ -3565,3 +3565,5 @@ pub extern "c" fn os_signpost_interval_begin(log: os_log_t, signpos: os_signpost
35653565
pub extern "c" fn os_signpost_interval_end(log: os_log_t, signpos: os_signpost_id_t, func: [*]const u8, ...) void;
35663566
pub extern "c" fn os_signpost_id_make_with_pointer(log: os_log_t, ptr: ?*anyopaque) os_signpost_id_t;
35673567
pub extern "c" fn os_signpost_enabled(log: os_log_t) bool;
3568+
3569+
pub extern "c" fn getpagesize() c_int;

lib/std/c/linux.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ pub extern "c" fn fstatat64(dirfd: fd_t, noalias path: [*:0]const u8, noalias st
238238
pub extern "c" fn ftruncate64(fd: c_int, length: off_t) c_int;
239239
pub extern "c" fn getrlimit64(resource: rlimit_resource, rlim: *rlimit) c_int;
240240
pub extern "c" fn lseek64(fd: fd_t, offset: i64, whence: c_int) i64;
241-
pub extern "c" fn mmap64(addr: ?*align(std.mem.page_size) anyopaque, len: usize, prot: c_uint, flags: c_uint, fd: fd_t, offset: i64) *anyopaque;
241+
pub extern "c" fn mmap64(addr: ?*anyopaque, len: usize, prot: c_uint, flags: c_uint, fd: fd_t, offset: i64) *anyopaque;
242242
pub extern "c" fn open64(path: [*:0]const u8, oflag: linux.O, ...) c_int;
243243
pub extern "c" fn openat64(fd: c_int, path: [*:0]const u8, oflag: linux.O, ...) c_int;
244244
pub extern "c" fn pread64(fd: fd_t, buf: [*]u8, nbyte: usize, offset: i64) isize;
@@ -295,13 +295,13 @@ pub extern "c" fn posix_memalign(memptr: *?*anyopaque, alignment: usize, size: u
295295
pub extern "c" fn malloc_usable_size(?*const anyopaque) usize;
296296

297297
pub extern "c" fn mincore(
298-
addr: *align(std.mem.page_size) anyopaque,
298+
addr: *anyopaque,
299299
length: usize,
300300
vec: [*]u8,
301301
) c_int;
302302

303303
pub extern "c" fn madvise(
304-
addr: *align(std.mem.page_size) anyopaque,
304+
addr: *anyopaque,
305305
length: usize,
306306
advice: c_uint,
307307
) c_int;

lib/std/c/netbsd.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ pub const sched_yield = __libc_thr_yield;
5656

5757
pub extern "c" fn posix_memalign(memptr: *?*anyopaque, alignment: usize, size: usize) c_int;
5858

59-
pub extern "c" fn __msync13(addr: *align(std.mem.page_size) const anyopaque, len: usize, flags: c_int) c_int;
59+
pub extern "c" fn __msync13(addr: *const anyopaque, len: usize, flags: c_int) c_int;
6060
pub const msync = __msync13;
6161

6262
pub const pthread_spin_t = switch (builtin.cpu.arch) {

lib/std/crypto/tlcsprng.zig

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ var install_atfork_handler = std.once(struct {
6060
}
6161
}.do);
6262

63-
threadlocal var wipe_mem: []align(mem.page_size) u8 = &[_]u8{};
63+
threadlocal var wipe_mem: []u8 = &[_]u8{};
6464

6565
fn tlsCsprngFill(_: *anyopaque, buffer: []u8) void {
6666
if (builtin.link_libc and @hasDecl(std.c, "arc4random_buf")) {
@@ -95,15 +95,15 @@ fn tlsCsprngFill(_: *anyopaque, buffer: []u8) void {
9595
} else {
9696
// Use a static thread-local buffer.
9797
const S = struct {
98-
threadlocal var buf: Context align(mem.page_size) = .{
98+
threadlocal var buf: Context = .{
9999
.init_state = .uninitialized,
100100
.rng = undefined,
101101
};
102102
};
103103
wipe_mem = mem.asBytes(&S.buf);
104104
}
105105
}
106-
const ctx = @as(*Context, @ptrCast(wipe_mem.ptr));
106+
const ctx = @as(*Context, @ptrCast(@alignCast(wipe_mem.ptr)));
107107

108108
switch (ctx.init_state) {
109109
.uninitialized => {
@@ -159,7 +159,7 @@ fn childAtForkHandler() callconv(.C) void {
159159
}
160160

161161
fn fillWithCsprng(buffer: []u8) void {
162-
const ctx = @as(*Context, @ptrCast(wipe_mem.ptr));
162+
const ctx = @as(*Context, @ptrCast(@alignCast(wipe_mem.ptr)));
163163
return ctx.rng.fill(buffer);
164164
}
165165

@@ -175,7 +175,7 @@ fn initAndFill(buffer: []u8) void {
175175
// the `std.options.cryptoRandomSeed` function is provided.
176176
std.options.cryptoRandomSeed(&seed);
177177

178-
const ctx = @as(*Context, @ptrCast(wipe_mem.ptr));
178+
const ctx = @as(*Context, @ptrCast(@alignCast(wipe_mem.ptr)));
179179
ctx.rng = Rng.init(seed);
180180
std.crypto.utils.secureZero(u8, &seed);
181181

lib/std/debug.zig

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -663,9 +663,9 @@ pub const StackIterator = struct {
663663
// We are unable to determine validity of memory for freestanding targets
664664
if (native_os == .freestanding) return true;
665665

666-
const aligned_address = address & ~@as(usize, @intCast((mem.page_size - 1)));
666+
const aligned_address = address & ~@as(usize, @intCast((std.heap.pageSize() - 1)));
667667
if (aligned_address == 0) return false;
668-
const aligned_memory = @as([*]align(mem.page_size) u8, @ptrFromInt(aligned_address))[0..mem.page_size];
668+
const aligned_memory = @as([*]u8, @ptrFromInt(aligned_address))[0..std.heap.pageSize()];
669669

670670
if (native_os == .windows) {
671671
const w = os.windows;
@@ -1137,7 +1137,7 @@ pub fn readElfDebugInfo(
11371137
build_id: ?[]const u8,
11381138
expected_crc: ?u32,
11391139
parent_sections: *DW.DwarfInfo.SectionArray,
1140-
parent_mapped_mem: ?[]align(mem.page_size) const u8,
1140+
parent_mapped_mem: ?[]const u8,
11411141
) !ModuleDebugInfo {
11421142
nosuspend {
11431143
const elf_file = (if (elf_filename) |filename| blk: {
@@ -1150,7 +1150,7 @@ pub fn readElfDebugInfo(
11501150
const mapped_mem = try mapWholeFile(elf_file);
11511151
if (expected_crc) |crc| if (crc != std.hash.crc.Crc32SmallWithPoly(.IEEE).hash(mapped_mem)) return error.InvalidDebugInfo;
11521152

1153-
const hdr: *const elf.Ehdr = @ptrCast(&mapped_mem[0]);
1153+
const hdr: *const elf.Ehdr = @ptrCast(@alignCast(&mapped_mem[0]));
11541154
if (!mem.eql(u8, hdr.e_ident[0..4], elf.MAGIC)) return error.InvalidElfMagic;
11551155
if (hdr.e_ident[elf.EI_VERSION] != 1) return error.InvalidElfVersion;
11561156

@@ -1455,7 +1455,7 @@ fn printLineFromFileAnyOs(out_stream: anytype, line_info: LineInfo) !void {
14551455
defer f.close();
14561456
// TODO fstat and make sure that the file has the correct size
14571457

1458-
var buf: [mem.page_size]u8 = undefined;
1458+
var buf: [1024]u8 = undefined;
14591459
var amt_read = try f.read(buf[0..]);
14601460
const line_start = seek: {
14611461
var current_line_start: usize = 0;
@@ -1555,7 +1555,7 @@ test "printLineFromFileAnyOs" {
15551555

15561556
const overlap = 10;
15571557
var writer = file.writer();
1558-
try writer.writeByteNTimes('a', mem.page_size - overlap);
1558+
try writer.writeByteNTimes('a', std.heap.pageSize() - overlap);
15591559
try writer.writeByte('\n');
15601560
try writer.writeByteNTimes('a', overlap);
15611561

@@ -1570,10 +1570,16 @@ test "printLineFromFileAnyOs" {
15701570
defer allocator.free(path);
15711571

15721572
var writer = file.writer();
1573-
try writer.writeByteNTimes('a', mem.page_size);
1573+
try writer.writeByteNTimes('a', std.heap.pageSize());
1574+
1575+
const expected = try allocator.alloc(u8, std.heap.pageSize() + 1);
1576+
defer allocator.free(expected);
1577+
1578+
@memset(expected, 'a');
1579+
expected[expected.len - 1] = '\n';
15741580

15751581
try printLineFromFileAnyOs(output_stream, .{ .file_name = path, .line = 1, .column = 0 });
1576-
try expectEqualStrings(("a" ** mem.page_size) ++ "\n", output.items);
1582+
try expectEqualStrings(expected, output.items);
15771583
output.clearRetainingCapacity();
15781584
}
15791585
{
@@ -1583,18 +1589,28 @@ test "printLineFromFileAnyOs" {
15831589
defer allocator.free(path);
15841590

15851591
var writer = file.writer();
1586-
try writer.writeByteNTimes('a', 3 * mem.page_size);
1592+
try writer.writeByteNTimes('a', 3 * std.heap.pageSize());
15871593

15881594
try expectError(error.EndOfFile, printLineFromFileAnyOs(output_stream, .{ .file_name = path, .line = 2, .column = 0 }));
15891595

1596+
var expected = try allocator.alloc(u8, (3 * std.heap.pageSize()) + 1);
1597+
defer allocator.free(expected);
1598+
1599+
@memset(expected, 'a');
1600+
expected[expected.len - 1] = '\n';
1601+
15901602
try printLineFromFileAnyOs(output_stream, .{ .file_name = path, .line = 1, .column = 0 });
1591-
try expectEqualStrings(("a" ** (3 * mem.page_size)) ++ "\n", output.items);
1603+
try expectEqualStrings(expected, output.items);
15921604
output.clearRetainingCapacity();
15931605

15941606
try writer.writeAll("a\na");
15951607

1608+
expected = try allocator.realloc(expected, (3 * std.heap.pageSize()) + 2);
1609+
@memset(expected, 'a');
1610+
expected[expected.len - 1] = '\n';
1611+
15961612
try printLineFromFileAnyOs(output_stream, .{ .file_name = path, .line = 1, .column = 0 });
1597-
try expectEqualStrings(("a" ** (3 * mem.page_size)) ++ "a\n", output.items);
1613+
try expectEqualStrings(expected, output.items);
15981614
output.clearRetainingCapacity();
15991615

16001616
try printLineFromFileAnyOs(output_stream, .{ .file_name = path, .line = 2, .column = 0 });
@@ -1608,7 +1624,7 @@ test "printLineFromFileAnyOs" {
16081624
defer allocator.free(path);
16091625

16101626
var writer = file.writer();
1611-
const real_file_start = 3 * mem.page_size;
1627+
const real_file_start = 3 * std.heap.pageSize();
16121628
try writer.writeByteNTimes('\n', real_file_start);
16131629
try writer.writeAll("abc\ndef");
16141630

@@ -1641,7 +1657,7 @@ const MachoSymbol = struct {
16411657

16421658
/// Takes ownership of file, even on error.
16431659
/// TODO it's weird to take ownership even on error, rework this code.
1644-
fn mapWholeFile(file: File) ![]align(mem.page_size) const u8 {
1660+
fn mapWholeFile(file: File) ![]const u8 {
16451661
nosuspend {
16461662
defer file.close();
16471663

@@ -2130,7 +2146,7 @@ pub const ModuleDebugInfo = switch (native_os) {
21302146
.macos, .ios, .watchos, .tvos => struct {
21312147
base_address: usize,
21322148
vmaddr_slide: usize,
2133-
mapped_memory: []align(mem.page_size) const u8,
2149+
mapped_memory: []const u8,
21342150
symbols: []const MachoSymbol,
21352151
strings: [:0]const u8,
21362152
ofiles: OFileTable,
@@ -2423,8 +2439,8 @@ pub const ModuleDebugInfo = switch (native_os) {
24232439
.linux, .netbsd, .freebsd, .dragonfly, .openbsd, .haiku, .solaris, .illumos => struct {
24242440
base_address: usize,
24252441
dwarf: DW.DwarfInfo,
2426-
mapped_memory: []align(mem.page_size) const u8,
2427-
external_mapped_memory: ?[]align(mem.page_size) const u8,
2442+
mapped_memory: []const u8,
2443+
external_mapped_memory: ?[]const u8,
24282444

24292445
pub fn deinit(self: *@This(), allocator: mem.Allocator) void {
24302446
self.dwarf.deinit(allocator);

lib/std/dynamic_library.zig

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ pub const ElfDynLib = struct {
101101
hashtab: [*]os.Elf_Symndx,
102102
versym: ?[*]u16,
103103
verdef: ?*elf.Verdef,
104-
memory: []align(mem.page_size) u8,
104+
memory: []u8,
105105

106106
pub const Error = error{
107107
FileTooBig,
@@ -125,15 +125,15 @@ pub const ElfDynLib = struct {
125125
// corresponding to the actual LOAD sections.
126126
const file_bytes = try os.mmap(
127127
null,
128-
mem.alignForward(usize, size, mem.page_size),
128+
mem.alignForward(usize, size, std.heap.pageSize()),
129129
os.PROT.READ,
130130
.{ .TYPE = .PRIVATE },
131131
fd,
132132
0,
133133
);
134134
defer os.munmap(file_bytes);
135135

136-
const eh = @as(*elf.Ehdr, @ptrCast(file_bytes.ptr));
136+
const eh = @as(*elf.Ehdr, @ptrCast(@alignCast(file_bytes.ptr)));
137137
if (!mem.eql(u8, eh.e_ident[0..4], elf.MAGIC)) return error.NotElfFile;
138138
if (eh.e_type != elf.ET.DYN) return error.NotDynamicLibrary;
139139

@@ -186,10 +186,10 @@ pub const ElfDynLib = struct {
186186
elf.PT_LOAD => {
187187
// The VirtAddr may not be page-aligned; in such case there will be
188188
// extra nonsense mapped before/after the VirtAddr,MemSiz
189-
const aligned_addr = (base + ph.p_vaddr) & ~(@as(usize, mem.page_size) - 1);
189+
const aligned_addr = (base + ph.p_vaddr) & ~(@as(usize, std.heap.pageSize()) - 1);
190190
const extra_bytes = (base + ph.p_vaddr) - aligned_addr;
191-
const extended_memsz = mem.alignForward(usize, ph.p_memsz + extra_bytes, mem.page_size);
192-
const ptr = @as([*]align(mem.page_size) u8, @ptrFromInt(aligned_addr));
191+
const extended_memsz = mem.alignForward(usize, ph.p_memsz + extra_bytes, std.heap.pageSize());
192+
const ptr = @as([*]u8, @ptrFromInt(aligned_addr));
193193
const prot = elfToMmapProt(ph.p_flags);
194194
if ((ph.p_flags & elf.PF_W) == 0) {
195195
// If it does not need write access, it can be mapped from the fd.

lib/std/fifo.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ pub fn LinearFifo(
9191
mem.copyForwards(T, self.buf[0..self.count], self.buf[self.head..][0..self.count]);
9292
self.head = 0;
9393
} else {
94-
var tmp: [mem.page_size / 2 / @sizeOf(T)]T = undefined;
94+
var tmp: [4096 / 2 / @sizeOf(T)]T = undefined;
9595

9696
while (self.head != 0) {
9797
const n = @min(self.head, tmp.len);

0 commit comments

Comments
 (0)