Skip to content

Commit 39bc46e

Browse files
Add runtime page_size
1 parent 1206604 commit 39bc46e

33 files changed

+368
-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: 7 additions & 6 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");
@@ -1506,6 +1505,8 @@ pub extern "c" fn sigwait(set: ?*c.sigset_t, sig: ?*c_int) c_int;
15061505

15071506
pub extern "c" fn alarm(seconds: c_uint) c_uint;
15081507

1508+
pub extern "c" fn sysconf(sc: c_int) c_long;
1509+
15091510
pub const clock_getres = switch (native_os) {
15101511
.netbsd => private.__clock_getres50,
15111512
else => private.clock_getres,
@@ -1639,9 +1640,9 @@ pub extern "c" fn writev(fd: c_int, iov: [*]const iovec_const, iovcnt: c_uint) i
16391640
pub extern "c" fn pwritev(fd: c_int, iov: [*]const iovec_const, iovcnt: c_uint, offset: c.off_t) isize;
16401641
pub extern "c" fn write(fd: c.fd_t, buf: [*]const u8, nbyte: usize) isize;
16411642
pub extern "c" fn pwrite(fd: c.fd_t, buf: [*]const u8, nbyte: usize, offset: c.off_t) isize;
1642-
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;
1643-
pub extern "c" fn munmap(addr: *align(page_size) const anyopaque, len: usize) c_int;
1644-
pub extern "c" fn mprotect(addr: *align(page_size) anyopaque, len: usize, prot: c_uint) c_int;
1643+
pub extern "c" fn mmap(addr: ?*anyopaque, len: usize, prot: c_uint, flags: MAP, fd: c.fd_t, offset: c.off_t) *anyopaque;
1644+
pub extern "c" fn munmap(addr: *const anyopaque, len: usize) c_int;
1645+
pub extern "c" fn mprotect(addr: *anyopaque, len: usize, prot: c_uint) c_int;
16451646
pub extern "c" fn link(oldpath: [*:0]const u8, newpath: [*:0]const u8, flags: c_int) c_int;
16461647
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;
16471648
pub extern "c" fn unlink(path: [*:0]const u8) c_int;
@@ -1928,7 +1929,7 @@ const private = struct {
19281929
extern "c" fn getdirentries(fd: c.fd_t, buf_ptr: [*]u8, nbytes: usize, basep: *i64) isize;
19291930
extern "c" fn getrusage(who: c_int, usage: *c.rusage) c_int;
19301931
extern "c" fn gettimeofday(noalias tv: ?*c.timeval, noalias tz: ?*c.timezone) c_int;
1931-
extern "c" fn msync(addr: *align(page_size) const anyopaque, len: usize, flags: c_int) c_int;
1932+
extern "c" fn msync(addr: *const anyopaque, len: usize, flags: c_int) c_int;
19321933
extern "c" fn nanosleep(rqtp: *const c.timespec, rmtp: ?*c.timespec) c_int;
19331934
extern "c" fn readdir(dir: *c.DIR) ?*c.dirent;
19341935
extern "c" fn realpath(noalias file_name: [*:0]const u8, noalias resolved_name: [*]u8) ?[*:0]u8;
@@ -1958,7 +1959,7 @@ const private = struct {
19581959
extern "c" fn __getrusage50(who: c_int, usage: *c.rusage) c_int;
19591960
extern "c" fn __gettimeofday50(noalias tv: ?*c.timeval, noalias tz: ?*c.timezone) c_int;
19601961
extern "c" fn __libc_thr_yield() c_int;
1961-
extern "c" fn __msync13(addr: *align(std.mem.page_size) const anyopaque, len: usize, flags: c_int) c_int;
1962+
extern "c" fn __msync13(addr: *const anyopaque, len: usize, flags: c_int) c_int;
19621963
extern "c" fn __nanosleep50(rqtp: *const c.timespec, rmtp: ?*c.timespec) c_int;
19631964
extern "c" fn __sigaction14(sig: c_int, noalias act: ?*const c.Sigaction, noalias oact: ?*c.Sigaction) c_int;
19641965
extern "c" fn __sigfillset14(set: ?*c.sigset_t) void;

lib/std/c/darwin.zig

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3409,7 +3409,7 @@ pub const MachTask = extern struct {
34093409
return left;
34103410
}
34113411

3412-
fn getPageSize(task: MachTask) MachError!usize {
3412+
pub fn getPageSize(task: MachTask) MachError!usize {
34133413
if (task.isValid()) {
34143414
var info_count = TASK_VM_INFO_COUNT;
34153415
var vm_info: task_vm_info_data_t = undefined;
@@ -3562,3 +3562,5 @@ pub extern "c" fn os_signpost_interval_begin(log: os_log_t, signpos: os_signpost
35623562
pub extern "c" fn os_signpost_interval_end(log: os_log_t, signpos: os_signpost_id_t, func: [*]const u8, ...) void;
35633563
pub extern "c" fn os_signpost_id_make_with_pointer(log: os_log_t, ptr: ?*anyopaque) os_signpost_id_t;
35643564
pub extern "c" fn os_signpost_enabled(log: os_log_t) bool;
3565+
3566+
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/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;
@@ -1142,7 +1142,7 @@ pub fn readElfDebugInfo(
11421142
build_id: ?[]const u8,
11431143
expected_crc: ?u32,
11441144
parent_sections: *DW.DwarfInfo.SectionArray,
1145-
parent_mapped_mem: ?[]align(mem.page_size) const u8,
1145+
parent_mapped_mem: ?[]const u8,
11461146
) !ModuleDebugInfo {
11471147
nosuspend {
11481148
const elf_file = (if (elf_filename) |filename| blk: {
@@ -1155,7 +1155,7 @@ pub fn readElfDebugInfo(
11551155
const mapped_mem = try mapWholeFile(elf_file);
11561156
if (expected_crc) |crc| if (crc != std.hash.crc.Crc32SmallWithPoly(.IEEE).hash(mapped_mem)) return error.InvalidDebugInfo;
11571157

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

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

1463-
var buf: [mem.page_size]u8 = undefined;
1463+
var buf: [1024]u8 = undefined;
14641464
var amt_read = try f.read(buf[0..]);
14651465
const line_start = seek: {
14661466
var current_line_start: usize = 0;
@@ -1560,7 +1560,7 @@ test "printLineFromFileAnyOs" {
15601560

15611561
const overlap = 10;
15621562
var writer = file.writer();
1563-
try writer.writeByteNTimes('a', mem.page_size - overlap);
1563+
try writer.writeByteNTimes('a', std.heap.pageSize() - overlap);
15641564
try writer.writeByte('\n');
15651565
try writer.writeByteNTimes('a', overlap);
15661566

@@ -1575,10 +1575,16 @@ test "printLineFromFileAnyOs" {
15751575
defer allocator.free(path);
15761576

15771577
var writer = file.writer();
1578-
try writer.writeByteNTimes('a', mem.page_size);
1578+
try writer.writeByteNTimes('a', std.heap.pageSize());
1579+
1580+
const expected = try allocator.alloc(u8, std.heap.pageSize() + 1);
1581+
defer allocator.free(expected);
1582+
1583+
@memset(expected, 'a');
1584+
expected[expected.len - 1] = '\n';
15791585

15801586
try printLineFromFileAnyOs(output_stream, .{ .file_name = path, .line = 1, .column = 0 });
1581-
try expectEqualStrings(("a" ** mem.page_size) ++ "\n", output.items);
1587+
try expectEqualStrings(expected, output.items);
15821588
output.clearRetainingCapacity();
15831589
}
15841590
{
@@ -1588,18 +1594,28 @@ test "printLineFromFileAnyOs" {
15881594
defer allocator.free(path);
15891595

15901596
var writer = file.writer();
1591-
try writer.writeByteNTimes('a', 3 * mem.page_size);
1597+
try writer.writeByteNTimes('a', 3 * std.heap.pageSize());
15921598

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

1601+
var expected = try allocator.alloc(u8, (3 * std.heap.pageSize()) + 1);
1602+
defer allocator.free(expected);
1603+
1604+
@memset(expected, 'a');
1605+
expected[expected.len - 1] = '\n';
1606+
15951607
try printLineFromFileAnyOs(output_stream, .{ .file_name = path, .line = 1, .column = 0 });
1596-
try expectEqualStrings(("a" ** (3 * mem.page_size)) ++ "\n", output.items);
1608+
try expectEqualStrings(expected, output.items);
15971609
output.clearRetainingCapacity();
15981610

15991611
try writer.writeAll("a\na");
16001612

1613+
expected = try allocator.realloc(expected, (3 * std.heap.pageSize()) + 2);
1614+
@memset(expected, 'a');
1615+
expected[expected.len - 1] = '\n';
1616+
16011617
try printLineFromFileAnyOs(output_stream, .{ .file_name = path, .line = 1, .column = 0 });
1602-
try expectEqualStrings(("a" ** (3 * mem.page_size)) ++ "a\n", output.items);
1618+
try expectEqualStrings(expected, output.items);
16031619
output.clearRetainingCapacity();
16041620

16051621
try printLineFromFileAnyOs(output_stream, .{ .file_name = path, .line = 2, .column = 0 });
@@ -1613,7 +1629,7 @@ test "printLineFromFileAnyOs" {
16131629
defer allocator.free(path);
16141630

16151631
var writer = file.writer();
1616-
const real_file_start = 3 * mem.page_size;
1632+
const real_file_start = 3 * std.heap.pageSize();
16171633
try writer.writeByteNTimes('\n', real_file_start);
16181634
try writer.writeAll("abc\ndef");
16191635

@@ -1646,7 +1662,7 @@ const MachoSymbol = struct {
16461662

16471663
/// Takes ownership of file, even on error.
16481664
/// TODO it's weird to take ownership even on error, rework this code.
1649-
fn mapWholeFile(file: File) ![]align(mem.page_size) const u8 {
1665+
fn mapWholeFile(file: File) ![]const u8 {
16501666
nosuspend {
16511667
defer file.close();
16521668

@@ -2135,7 +2151,7 @@ pub const ModuleDebugInfo = switch (native_os) {
21352151
.macos, .ios, .watchos, .tvos => struct {
21362152
base_address: usize,
21372153
vmaddr_slide: usize,
2138-
mapped_memory: []align(mem.page_size) const u8,
2154+
mapped_memory: []const u8,
21392155
symbols: []const MachoSymbol,
21402156
strings: [:0]const u8,
21412157
ofiles: OFileTable,
@@ -2428,8 +2444,8 @@ pub const ModuleDebugInfo = switch (native_os) {
24282444
.linux, .netbsd, .freebsd, .dragonfly, .openbsd, .haiku, .solaris, .illumos => struct {
24292445
base_address: usize,
24302446
dwarf: DW.DwarfInfo,
2431-
mapped_memory: []align(mem.page_size) const u8,
2432-
external_mapped_memory: ?[]align(mem.page_size) const u8,
2447+
mapped_memory: []const u8,
2448+
external_mapped_memory: ?[]const u8,
24332449

24342450
pub fn deinit(self: *@This(), allocator: mem.Allocator) void {
24352451
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)