Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions lib/std/os/bits/linux.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2244,3 +2244,24 @@ pub const MADV_COLD = 20;
pub const MADV_PAGEOUT = 21;
pub const MADV_HWPOISON = 100;
pub const MADV_SOFT_OFFLINE = 101;

pub const POSIX_FADV_NORMAL = 0;
pub const POSIX_FADV_RANDOM = 1;
pub const POSIX_FADV_SEQUENTIAL = 2;
pub const POSIX_FADV_WILLNEED = 3;
pub usingnamespace switch (builtin.arch) {
.s390x => if (@typeInfo(usize).Int.bits == 64)
struct {
pub const POSIX_FADV_DONTNEED = 6;
pub const POSIX_FADV_NOREUSE = 7;
}
else
struct {
pub const POSIX_FADV_DONTNEED = 4;
pub const POSIX_FADV_NOREUSE = 5;
},
else => struct {
pub const POSIX_FADV_DONTNEED = 4;
pub const POSIX_FADV_NOREUSE = 5;
},
};
25 changes: 25 additions & 0 deletions lib/std/os/linux.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1386,6 +1386,31 @@ pub fn madvise(address: [*]u8, len: usize, advice: u32) usize {
return syscall3(.madvise, @ptrToInt(address), len, advice);
}

pub fn posix_fadvise(fd: fd_t, offset: off_t, len: off_t, advice: usize) usize {
if (@hasField(SYS, "fadvise64_64")) {
const offset_halves = splitValue64(@bitCast(u64, offset));
const length_halves = splitValue64(@bitCast(u64, len));

return syscall6(
.fadvise64_64,
@bitCast(usize, @as(isize, fd)),
offset_halves[0],
offset_halves[1],
length_halves[0],
length_halves[1],
advice,
);
} else {
return syscall4(
.fadvise64,
@bitCast(usize, @as(isize, fd)),
@bitCast(usize, offset),
@bitCast(usize, len),
advice,
);
}
}

test {
if (builtin.os.tag == .linux) {
_ = @import("linux/test.zig");
Expand Down
19 changes: 19 additions & 0 deletions lib/std/os/linux/test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,22 @@ test "user and group ids" {
expectEqual(linux.getauxval(elf.AT_EUID), linux.geteuid());
expectEqual(linux.getauxval(elf.AT_EGID), linux.getegid());
}

test "posix_fadvise" {
const tmp_file_name = "temp_posix_fadvise.txt";
var file = try fs.cwd().createFile(tmp_file_name, .{});
defer {
file.close();
fs.cwd().deleteFile(tmp_file_name) catch {};
}

const stat = try file.stat();

const ret = linux.posix_fadvise(
file.handle,
0,
@bitCast(i64, stat.size),
linux.POSIX_FADV_SEQUENTIAL,
);
expectEqual(@as(usize, 0), ret);
}