Skip to content

Commit 9c24722

Browse files
committed
os/linux: add posix_fadvise
1 parent d1d4b14 commit 9c24722

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

lib/std/os/linux.zig

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,6 +1386,62 @@ pub fn madvise(address: [*]u8, len: usize, advice: u32) usize {
13861386
return syscall3(.madvise, @ptrToInt(address), len, advice);
13871387
}
13881388

1389+
pub fn fadvise(fd: fd_t, offset: u64, len: u64, advice: usize) usize {
1390+
if (comptime std.Target.current.cpu.arch.isMIPS()) {
1391+
// MIPS requires a 7 argument syscall
1392+
1393+
const offset_halves = splitValue64(@bitCast(u64, offset));
1394+
const length_halves = splitValue64(@bitCast(u64, len));
1395+
1396+
return syscall7(
1397+
.fadvise64,
1398+
@bitCast(usize, @as(isize, fd)),
1399+
0,
1400+
offset_halves[0],
1401+
offset_halves[1],
1402+
length_halves[0],
1403+
length_halves[1],
1404+
advice,
1405+
);
1406+
} else if (comptime std.Target.current.cpu.arch.isARM()) {
1407+
// ARM reorders the arguments
1408+
1409+
const offset_halves = splitValue64(@bitCast(u64, offset));
1410+
const length_halves = splitValue64(@bitCast(u64, len));
1411+
1412+
return syscall6(
1413+
.fadvise64_64,
1414+
@bitCast(usize, @as(isize, fd)),
1415+
advice,
1416+
offset_halves[0],
1417+
offset_halves[1],
1418+
length_halves[0],
1419+
length_halves[1],
1420+
);
1421+
} else if (@hasField(SYS, "fadvise64_64")) {
1422+
const offset_halves = splitValue64(@bitCast(u64, offset));
1423+
const length_halves = splitValue64(@bitCast(u64, len));
1424+
1425+
return syscall6(
1426+
.fadvise64_64,
1427+
@bitCast(usize, @as(isize, fd)),
1428+
offset_halves[0],
1429+
offset_halves[1],
1430+
length_halves[0],
1431+
length_halves[1],
1432+
advice,
1433+
);
1434+
} else {
1435+
return syscall4(
1436+
.fadvise64,
1437+
@bitCast(usize, @as(isize, fd)),
1438+
@bitCast(usize, offset),
1439+
@bitCast(usize, len),
1440+
advice,
1441+
);
1442+
}
1443+
}
1444+
13891445
test {
13901446
if (builtin.os.tag == .linux) {
13911447
_ = @import("linux/test.zig");

lib/std/os/linux/test.zig

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,24 @@ 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 "posix_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+
_ = linux.getrandom(&buf, buf.len, 0);
122+
try file.writeAll(&buf);
123+
124+
const ret = linux.fadvise(
125+
file.handle,
126+
0,
127+
0,
128+
linux.POSIX_FADV_SEQUENTIAL,
129+
);
130+
expectEqual(@as(usize, 0), ret);
131+
}

0 commit comments

Comments
 (0)