authorgravatar for vincent@rischmann.frVincent Rischmann <vincent@rischmann.fr> 2021-02-27 23:30:43+01:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2021-06-09 18:15:50+03:00
logc71e8a30cdf050e45c2ca2dc81e93b10cf00dc5f
tree3641124b53b25db7d628b28d47b423cabb35488b
parent7f32c799a95dd8a54ca7f984f41524021ceee2ce

os/linux: add fadvise


2 files changed, 79 insertions(+), 0 deletions(-)

lib/std/os/linux.zig+59
...@@ -1454,6 +1454,65 @@ pub fn process_vm_writev(pid: pid_t, local: [*]const iovec, local_count: usize,...@@ -1454,6 +1454,65 @@ pub fn process_vm_writev(pid: pid_t, local: [*]const iovec, local_count: usize,
1454 );1454 );
1455}1455}
14561456
1457pub 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
1457test {1516test {
1458 if (std.Target.current.os.tag == .linux) {1517 if (std.Target.current.os.tag == .linux) {
1459 _ = @import("linux/test.zig");1518 _ = @import("linux/test.zig");
lib/std/os/linux/test.zig+20
...@@ -108,3 +108,23 @@ test "user and group ids" {...@@ -108,3 +108,23 @@ test "user and group ids" {
108 try expectEqual(linux.getauxval(elf.AT_EUID), linux.geteuid());108 try expectEqual(linux.getauxval(elf.AT_EUID), linux.geteuid());
109 try expectEqual(linux.getauxval(elf.AT_EGID), linux.getegid());109 try expectEqual(linux.getauxval(elf.AT_EGID), linux.getegid());
110}110}
111
112test "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}