Skip to content

Commit 47d004a

Browse files
Add runtime page_size
1 parent 54c0857 commit 47d004a

33 files changed

+372
-228
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(
@@ -1066,7 +1066,7 @@ const LinuxThreadImpl = struct {
10661066
completion: Completion = Completion.init(.running),
10671067
child_tid: std.atomic.Value(i32) = std.atomic.Value(i32).init(1),
10681068
parent_tid: i32 = undefined,
1069-
mapped: []align(std.mem.page_size) u8,
1069+
mapped: []u8,
10701070

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

12121212
fn spawn(config: SpawnConfig, comptime f: anytype, args: anytype) !Impl {
1213-
const page_size = std.mem.page_size;
1213+
const page_size = std.heap.pageSize();
12141214
const Args = @TypeOf(args);
12151215
const Instance = struct {
12161216
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.posix.iovec;
65
const iovec_const = std.posix.iovec_const;
76
const wasi = @import("c/wasi.zig");
@@ -1507,6 +1506,8 @@ pub extern "c" fn sigwait(set: ?*c.sigset_t, sig: ?*c_int) c_int;
15071506

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

1509+
pub extern "c" fn sysconf(sc: c_int) c_long;
1510+
15101511
pub const clock_getres = switch (native_os) {
15111512
.netbsd => private.__clock_getres50,
15121513
else => private.clock_getres,
@@ -1632,9 +1633,9 @@ pub extern "c" fn writev(fd: c_int, iov: [*]const iovec_const, iovcnt: c_uint) i
16321633
pub extern "c" fn pwritev(fd: c_int, iov: [*]const iovec_const, iovcnt: c_uint, offset: c.off_t) isize;
16331634
pub extern "c" fn write(fd: c.fd_t, buf: [*]const u8, nbyte: usize) isize;
16341635
pub extern "c" fn pwrite(fd: c.fd_t, buf: [*]const u8, nbyte: usize, offset: c.off_t) isize;
1635-
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;
1636-
pub extern "c" fn munmap(addr: *align(page_size) const anyopaque, len: usize) c_int;
1637-
pub extern "c" fn mprotect(addr: *align(page_size) anyopaque, len: usize, prot: c_uint) c_int;
1636+
pub extern "c" fn mmap(addr: ?*anyopaque, len: usize, prot: c_uint, flags: MAP, fd: c.fd_t, offset: c.off_t) *anyopaque;
1637+
pub extern "c" fn munmap(addr: *const anyopaque, len: usize) c_int;
1638+
pub extern "c" fn mprotect(addr: *anyopaque, len: usize, prot: c_uint) c_int;
16381639
pub extern "c" fn link(oldpath: [*:0]const u8, newpath: [*:0]const u8, flags: c_int) c_int;
16391640
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;
16401641
pub extern "c" fn unlink(path: [*:0]const u8) c_int;
@@ -1922,7 +1923,7 @@ const private = struct {
19221923
extern "c" fn getdirentries(fd: c.fd_t, buf_ptr: [*]u8, nbytes: usize, basep: *i64) isize;
19231924
extern "c" fn getrusage(who: c_int, usage: *c.rusage) c_int;
19241925
extern "c" fn gettimeofday(noalias tv: ?*c.timeval, noalias tz: ?*c.timezone) c_int;
1925-
extern "c" fn msync(addr: *align(page_size) const anyopaque, len: usize, flags: c_int) c_int;
1926+
extern "c" fn msync(addr: *const anyopaque, len: usize, flags: c_int) c_int;
19261927
extern "c" fn nanosleep(rqtp: *const c.timespec, rmtp: ?*c.timespec) c_int;
19271928
extern "c" fn readdir(dir: *c.DIR) ?*c.dirent;
19281929
extern "c" fn realpath(noalias file_name: [*:0]const u8, noalias resolved_name: [*]u8) ?[*:0]u8;
@@ -1952,7 +1953,7 @@ const private = struct {
19521953
extern "c" fn __getrusage50(who: c_int, usage: *c.rusage) c_int;
19531954
extern "c" fn __gettimeofday50(noalias tv: ?*c.timeval, noalias tz: ?*c.timezone) c_int;
19541955
extern "c" fn __libc_thr_yield() c_int;
1955-
extern "c" fn __msync13(addr: *align(std.mem.page_size) const anyopaque, len: usize, flags: c_int) c_int;
1956+
extern "c" fn __msync13(addr: *const anyopaque, len: usize, flags: c_int) c_int;
19561957
extern "c" fn __nanosleep50(rqtp: *const c.timespec, rmtp: ?*c.timespec) c_int;
19571958
extern "c" fn __sigaction14(sig: c_int, noalias act: ?*const c.Sigaction, noalias oact: ?*c.Sigaction) c_int;
19581959
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
@@ -3184,7 +3184,7 @@ pub const MachTask = extern struct {
31843184
return left;
31853185
}
31863186

3187-
fn getPageSize(task: MachTask) MachError!usize {
3187+
pub fn getPageSize(task: MachTask) MachError!usize {
31883188
if (task.isValid()) {
31893189
var info_count = TASK_VM_INFO_COUNT;
31903190
var vm_info: task_vm_info_data_t = undefined;
@@ -3337,3 +3337,5 @@ pub extern "c" fn os_signpost_interval_begin(log: os_log_t, signpos: os_signpost
33373337
pub extern "c" fn os_signpost_interval_end(log: os_log_t, signpos: os_signpost_id_t, func: [*]const u8, ...) void;
33383338
pub extern "c" fn os_signpost_id_make_with_pointer(log: os_log_t, ptr: ?*anyopaque) os_signpost_id_t;
33393339
pub extern "c" fn os_signpost_enabled(log: os_log_t) bool;
3340+
3341+
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
@@ -61,7 +61,7 @@ var install_atfork_handler = std.once(struct {
6161
}
6262
}.do);
6363

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

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

109109
switch (ctx.init_state) {
110110
.uninitialized => {
@@ -160,7 +160,7 @@ fn childAtForkHandler() callconv(.C) void {
160160
}
161161

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

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

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

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
var memory_info: windows.MEMORY_BASIC_INFORMATION = undefined;
@@ -1139,7 +1139,7 @@ pub fn readElfDebugInfo(
11391139
build_id: ?[]const u8,
11401140
expected_crc: ?u32,
11411141
parent_sections: *DW.DwarfInfo.SectionArray,
1142-
parent_mapped_mem: ?[]align(mem.page_size) const u8,
1142+
parent_mapped_mem: ?[]const u8,
11431143
) !ModuleDebugInfo {
11441144
nosuspend {
11451145
const elf_file = (if (elf_filename) |filename| blk: {
@@ -1152,7 +1152,7 @@ pub fn readElfDebugInfo(
11521152
const mapped_mem = try mapWholeFile(elf_file);
11531153
if (expected_crc) |crc| if (crc != std.hash.crc.Crc32SmallWithPoly(.IEEE).hash(mapped_mem)) return error.InvalidDebugInfo;
11541154

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

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

1460-
var buf: [mem.page_size]u8 = undefined;
1460+
var buf: [1024]u8 = undefined;
14611461
var amt_read = try f.read(buf[0..]);
14621462
const line_start = seek: {
14631463
var current_line_start: usize = 0;
@@ -1557,7 +1557,7 @@ test "printLineFromFileAnyOs" {
15571557

15581558
const overlap = 10;
15591559
var writer = file.writer();
1560-
try writer.writeByteNTimes('a', mem.page_size - overlap);
1560+
try writer.writeByteNTimes('a', std.heap.pageSize() - overlap);
15611561
try writer.writeByte('\n');
15621562
try writer.writeByteNTimes('a', overlap);
15631563

@@ -1572,10 +1572,16 @@ test "printLineFromFileAnyOs" {
15721572
defer allocator.free(path);
15731573

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

15771583
try printLineFromFileAnyOs(output_stream, .{ .file_name = path, .line = 1, .column = 0 });
1578-
try expectEqualStrings(("a" ** mem.page_size) ++ "\n", output.items);
1584+
try expectEqualStrings(expected, output.items);
15791585
output.clearRetainingCapacity();
15801586
}
15811587
{
@@ -1585,18 +1591,28 @@ test "printLineFromFileAnyOs" {
15851591
defer allocator.free(path);
15861592

15871593
var writer = file.writer();
1588-
try writer.writeByteNTimes('a', 3 * mem.page_size);
1594+
try writer.writeByteNTimes('a', 3 * std.heap.pageSize());
15891595

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

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

15961608
try writer.writeAll("a\na");
15971609

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

16021618
try printLineFromFileAnyOs(output_stream, .{ .file_name = path, .line = 2, .column = 0 });
@@ -1610,7 +1626,7 @@ test "printLineFromFileAnyOs" {
16101626
defer allocator.free(path);
16111627

16121628
var writer = file.writer();
1613-
const real_file_start = 3 * mem.page_size;
1629+
const real_file_start = 3 * std.heap.pageSize();
16141630
try writer.writeByteNTimes('\n', real_file_start);
16151631
try writer.writeAll("abc\ndef");
16161632

@@ -1643,7 +1659,7 @@ const MachoSymbol = struct {
16431659

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

@@ -2132,7 +2148,7 @@ pub const ModuleDebugInfo = switch (native_os) {
21322148
.macos, .ios, .watchos, .tvos => struct {
21332149
base_address: usize,
21342150
vmaddr_slide: usize,
2135-
mapped_memory: []align(mem.page_size) const u8,
2151+
mapped_memory: []const u8,
21362152
symbols: []const MachoSymbol,
21372153
strings: [:0]const u8,
21382154
ofiles: OFileTable,
@@ -2425,8 +2441,8 @@ pub const ModuleDebugInfo = switch (native_os) {
24252441
.linux, .netbsd, .freebsd, .dragonfly, .openbsd, .haiku, .solaris, .illumos => struct {
24262442
base_address: usize,
24272443
dwarf: DW.DwarfInfo,
2428-
mapped_memory: []align(mem.page_size) const u8,
2429-
external_mapped_memory: ?[]align(mem.page_size) const u8,
2444+
mapped_memory: []const u8,
2445+
external_mapped_memory: ?[]const u8,
24302446

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

lib/std/dynamic_library.zig

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ pub const ElfDynLib = struct {
128128
hashtab: [*]posix.Elf_Symndx,
129129
versym: ?[*]u16,
130130
verdef: ?*elf.Verdef,
131-
memory: []align(mem.page_size) u8,
131+
memory: []u8,
132132

133133
pub const Error = error{
134134
FileTooBig,
@@ -152,15 +152,15 @@ pub const ElfDynLib = struct {
152152
// corresponding to the actual LOAD sections.
153153
const file_bytes = try posix.mmap(
154154
null,
155-
mem.alignForward(usize, size, mem.page_size),
155+
mem.alignForward(usize, size, std.heap.pageSize()),
156156
posix.PROT.READ,
157157
.{ .TYPE = .PRIVATE },
158158
fd,
159159
0,
160160
);
161161
defer posix.munmap(file_bytes);
162162

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

@@ -213,10 +213,10 @@ pub const ElfDynLib = struct {
213213
elf.PT_LOAD => {
214214
// The VirtAddr may not be page-aligned; in such case there will be
215215
// extra nonsense mapped before/after the VirtAddr,MemSiz
216-
const aligned_addr = (base + ph.p_vaddr) & ~(@as(usize, mem.page_size) - 1);
216+
const aligned_addr = (base + ph.p_vaddr) & ~(@as(usize, std.heap.pageSize()) - 1);
217217
const extra_bytes = (base + ph.p_vaddr) - aligned_addr;
218-
const extended_memsz = mem.alignForward(usize, ph.p_memsz + extra_bytes, mem.page_size);
219-
const ptr = @as([*]align(mem.page_size) u8, @ptrFromInt(aligned_addr));
218+
const extended_memsz = mem.alignForward(usize, ph.p_memsz + extra_bytes, std.heap.pageSize());
219+
const ptr = @as([*]u8, @ptrFromInt(aligned_addr));
220220
const prot = elfToMmapProt(ph.p_flags);
221221
if ((ph.p_flags & elf.PF_W) == 0) {
222222
// 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)