Skip to content

Commit 6f32e2f

Browse files
committed
os/linux: add fadvise
1 parent 385b4df commit 6f32e2f

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

lib/std/os/linux.zig

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1434,6 +1434,65 @@ pub fn process_vm_writev(pid: pid_t, local: [*]const iovec, local_count: usize,
14341434
);
14351435
}
14361436

1437+
pub fn fadvise(fd: fd_t, offset: u64, len: u64, advice: usize) usize {
1438+
if (comptime std.Target.current.cpu.arch.isMIPS()) {
1439+
// MIPS requires a 7 argument syscall
1440+
1441+
const offset_halves = splitValue64(@bitCast(u64, offset));
1442+
const length_halves = splitValue64(@bitCast(u64, len));
1443+
1444+
return syscall7(
1445+
.fadvise64,
1446+
@bitCast(usize, @as(isize, fd)),
1447+
0,
1448+
offset_halves[0],
1449+
offset_halves[1],
1450+
length_halves[0],
1451+
length_halves[1],
1452+
advice,
1453+
);
1454+
} else if (comptime std.Target.current.cpu.arch.isARM()) {
1455+
// ARM reorders the arguments
1456+
1457+
const offset_halves = splitValue64(@bitCast(u64, offset));
1458+
const length_halves = splitValue64(@bitCast(u64, len));
1459+
1460+
return syscall6(
1461+
.fadvise64_64,
1462+
@bitCast(usize, @as(isize, fd)),
1463+
advice,
1464+
offset_halves[0],
1465+
offset_halves[1],
1466+
length_halves[0],
1467+
length_halves[1],
1468+
);
1469+
} else if (@hasField(SYS, "fadvise64_64") and usize_bits != 64) {
1470+
// The extra usize check is needed to avoid SPARC64 because it provides both
1471+
// fadvise64 and fadvise64_64 but the latter behaves differently than other platforms.
1472+
1473+
const offset_halves = splitValue64(@bitCast(u64, offset));
1474+
const length_halves = splitValue64(@bitCast(u64, len));
1475+
1476+
return syscall6(
1477+
.fadvise64_64,
1478+
@bitCast(usize, @as(isize, fd)),
1479+
offset_halves[0],
1480+
offset_halves[1],
1481+
length_halves[0],
1482+
length_halves[1],
1483+
advice,
1484+
);
1485+
} else {
1486+
return syscall4(
1487+
.fadvise64,
1488+
@bitCast(usize, @as(isize, fd)),
1489+
@bitCast(usize, offset),
1490+
@bitCast(usize, len),
1491+
advice,
1492+
);
1493+
}
1494+
}
1495+
14371496
test {
14381497
if (builtin.os.tag == .linux) {
14391498
_ = @import("linux/test.zig");

lib/std/os/linux/test.zig

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,23 @@ test "user and group ids" {
108108
expectEqual(linux.getauxval(elf.AT_EUID), linux.geteuid());
109109
expectEqual(linux.getauxval(elf.AT_EGID), linux.getegid());
110110
}
111+
112+
test "fadvise" {
113+
const tmp_file_name = "temp_posix_fadvise.txt";
114+
var file = try fs.cwd().createFile(tmp_file_name, .{});
115+
defer {
116+
file.close();
117+
fs.cwd().deleteFile(tmp_file_name) catch {};
118+
}
119+
120+
var buf: [2048]u8 = undefined;
121+
try file.writeAll(&buf);
122+
123+
const ret = linux.fadvise(
124+
file.handle,
125+
0,
126+
0,
127+
linux.POSIX_FADV_SEQUENTIAL,
128+
);
129+
expectEqual(@as(usize, 0), ret);
130+
}

0 commit comments

Comments
 (0)