Skip to content

Commit c71e8a3

Browse files
vrischmannVexu
authored andcommitted
os/linux: add fadvise
1 parent 7f32c79 commit c71e8a3

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
@@ -1454,6 +1454,65 @@ pub fn process_vm_writev(pid: pid_t, local: [*]const iovec, local_count: usize,
14541454
);
14551455
}
14561456

1457+
pub fn fadvise(fd: fd_t, offset: i64, len: i64, advice: usize) usize {
1458+
if (comptime std.Target.current.cpu.arch.isMIPS()) {
1459+
// MIPS requires a 7 argument syscall
1460+
1461+
const offset_halves = splitValue64(offset);
1462+
const length_halves = splitValue64(len);
1463+
1464+
return syscall7(
1465+
.fadvise64,
1466+
@bitCast(usize, @as(isize, fd)),
1467+
0,
1468+
offset_halves[0],
1469+
offset_halves[1],
1470+
length_halves[0],
1471+
length_halves[1],
1472+
advice,
1473+
);
1474+
} else if (comptime std.Target.current.cpu.arch.isARM()) {
1475+
// ARM reorders the arguments
1476+
1477+
const offset_halves = splitValue64(offset);
1478+
const length_halves = splitValue64(len);
1479+
1480+
return syscall6(
1481+
.fadvise64_64,
1482+
@bitCast(usize, @as(isize, fd)),
1483+
advice,
1484+
offset_halves[0],
1485+
offset_halves[1],
1486+
length_halves[0],
1487+
length_halves[1],
1488+
);
1489+
} else if (@hasField(SYS, "fadvise64_64") and usize_bits != 64) {
1490+
// The extra usize check is needed to avoid SPARC64 because it provides both
1491+
// fadvise64 and fadvise64_64 but the latter behaves differently than other platforms.
1492+
1493+
const offset_halves = splitValue64(offset);
1494+
const length_halves = splitValue64(len);
1495+
1496+
return syscall6(
1497+
.fadvise64_64,
1498+
@bitCast(usize, @as(isize, fd)),
1499+
offset_halves[0],
1500+
offset_halves[1],
1501+
length_halves[0],
1502+
length_halves[1],
1503+
advice,
1504+
);
1505+
} else {
1506+
return syscall4(
1507+
.fadvise64,
1508+
@bitCast(usize, @as(isize, fd)),
1509+
@bitCast(usize, offset),
1510+
@bitCast(usize, len),
1511+
advice,
1512+
);
1513+
}
1514+
}
1515+
14571516
test {
14581517
if (std.Target.current.os.tag == .linux) {
14591518
_ = @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
try expectEqual(linux.getauxval(elf.AT_EUID), linux.geteuid());
109109
try 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+
try expectEqual(@as(usize, 0), ret);
130+
}

0 commit comments

Comments
 (0)