authorgravatar for shadeops@gmail.comjim price <shadeops@gmail.com> 2023-03-29 00:46:25-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-04-06 00:57:23-04:00
log348751462632234f2bee10f5faad3711eff89172
treec3e32c4b93cab79e6140f61b39f9c915d0f7f2ca
parent3e467c778a83fac08ae2f124af05a4cf6dc95470

std.os: add mincore syscall

The mincore syscall is available on some UNIX like operating systems and allows a user to determine if a page is resident in memory.

3 files changed, 39 insertions(+), 0 deletions(-)

lib/std/c/linux.zig+6
...@@ -289,6 +289,12 @@ pub extern "c" fn prlimit(pid: pid_t, resource: rlimit_resource, new_limit: *con...@@ -289,6 +289,12 @@ pub extern "c" fn prlimit(pid: pid_t, resource: rlimit_resource, new_limit: *con
289pub extern "c" fn posix_memalign(memptr: *?*anyopaque, alignment: usize, size: usize) c_int;289pub extern "c" fn posix_memalign(memptr: *?*anyopaque, alignment: usize, size: usize) c_int;
290pub extern "c" fn malloc_usable_size(?*const anyopaque) usize;290pub extern "c" fn malloc_usable_size(?*const anyopaque) usize;
291291
292pub extern "c" fn mincore(
293 addr: *align(std.mem.page_size) anyopaque,
294 length: usize,
295 vec: [*]u8,
296) c_int;
297
292pub extern "c" fn madvise(298pub extern "c" fn madvise(
293 addr: *align(std.mem.page_size) anyopaque,299 addr: *align(std.mem.page_size) anyopaque,
294 length: usize,300 length: usize,
lib/std/os.zig+29
...@@ -6974,6 +6974,35 @@ pub fn setrlimit(resource: rlimit_resource, limits: rlimit) SetrlimitError!void...@@ -6974,6 +6974,35 @@ pub fn setrlimit(resource: rlimit_resource, limits: rlimit) SetrlimitError!void
6974 }6974 }
6975}6975}
69766976
6977pub const MincoreError = error{
6978 /// A kernel resource was temporarily unavailable.
6979 SystemResources,
6980 /// vec points to an invalid address.
6981 InvalidAddress,
6982 /// addr is not page-aligned.
6983 InvalidSyscall,
6984 /// One of the following:
6985 /// * length is greater than user space TASK_SIZE - addr
6986 /// * addr + length contains unmapped memory
6987 OutOfMemory,
6988 /// The mincore syscall is not available on this version and configuration
6989 /// of this UNIX-like kernel.
6990 MincoreUnavailable,
6991} || UnexpectedError;
6992
6993/// Determine whether pages are resident in memory.
6994pub fn mincore(ptr: [*]align(mem.page_size) u8, length: usize, vec: [*]u8) MincoreError!void {
6995 return switch (errno(system.mincore(ptr, length, vec))) {
6996 .SUCCESS => {},
6997 .AGAIN => error.SystemResources,
6998 .FAULT => error.InvalidAddress,
6999 .INVAL => error.InvalidSyscall,
7000 .NOMEM => error.OutOfMemory,
7001 .NOSYS => error.MincoreUnavailable,
7002 else => |err| unexpectedErrno(err),
7003 };
7004}
7005
6977pub const MadviseError = error{7006pub const MadviseError = error{
6978 /// advice is MADV.REMOVE, but the specified address range is not a shared writable mapping.7007 /// advice is MADV.REMOVE, but the specified address range is not a shared writable mapping.
6979 AccessDenied,7008 AccessDenied,
lib/std/os/linux.zig+4
...@@ -1699,6 +1699,10 @@ pub fn prlimit(pid: pid_t, resource: rlimit_resource, new_limit: ?*const rlimit,...@@ -1699,6 +1699,10 @@ pub fn prlimit(pid: pid_t, resource: rlimit_resource, new_limit: ?*const rlimit,
1699 );1699 );
1700}1700}
17011701
1702pub fn mincore(address: [*]u8, len: usize, vec: [*]u8) usize {
1703 return syscall3(.mincore, @ptrToInt(address), len, @ptrToInt(vec));
1704}
1705
1702pub fn madvise(address: [*]u8, len: usize, advice: u32) usize {1706pub fn madvise(address: [*]u8, len: usize, advice: u32) usize {
1703 return syscall3(.madvise, @ptrToInt(address), len, advice);1707 return syscall3(.madvise, @ptrToInt(address), len, advice);
1704}1708}