[libc-commits] [libc] [libc][fcntl] Implement posix_fadvise function. (PR #220790)

via libc-commits libc-commits at lists.llvm.org
Thu Sep 3 06:59:30 PDT 2026


https://github.com/lntue updated https://github.com/llvm/llvm-project/pull/220790

>From ae27d9f431367e498f6b61ea69fc94d60633d3cd Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue.h at gmail.com>
Date: Thu, 3 Sep 2026 03:14:50 +0000
Subject: [PATCH 1/2] [libc][fcntl] Implement posix_fadvise function.

---
 libc/config/linux/aarch64/entrypoints.txt     |  1 +
 libc/config/linux/riscv/entrypoints.txt       |  1 +
 libc/config/linux/x86_64/entrypoints.txt      |  1 +
 libc/include/fcntl.yaml                       | 21 +++++
 .../llvm-libc-macros/linux/fcntl-macros.h     |  8 ++
 libc/src/fcntl/CMakeLists.txt                 |  7 ++
 libc/src/fcntl/linux/CMakeLists.txt           | 15 ++++
 libc/src/fcntl/linux/posix_fadvise.cpp        | 74 ++++++++++++++++++
 libc/src/fcntl/posix_fadvise.h                | 26 +++++++
 libc/test/src/fcntl/CMakeLists.txt            | 18 +++++
 libc/test/src/fcntl/posix_fadvise_test.cpp    | 76 +++++++++++++++++++
 11 files changed, 248 insertions(+)
 create mode 100644 libc/src/fcntl/linux/posix_fadvise.cpp
 create mode 100644 libc/src/fcntl/posix_fadvise.h
 create mode 100644 libc/test/src/fcntl/posix_fadvise_test.cpp

diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index fbd9239728bac..3b6ea7219b8eb 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -41,6 +41,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.fcntl.fcntl
     libc.src.fcntl.open
     libc.src.fcntl.openat
+    libc.src.fcntl.posix_fadvise
 
     # poll.h entrypoints
     libc.src.poll.poll
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index bd906c6900008..70e5f1fe102c3 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -52,6 +52,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.fcntl.fcntl
     libc.src.fcntl.open
     libc.src.fcntl.openat
+    libc.src.fcntl.posix_fadvise
 
     # net/if.h entrypoints
     libc.src.net.if_indextoname
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index e6aeb3abe0672..d9ddd0435749d 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -52,6 +52,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.fcntl.fcntl
     libc.src.fcntl.open
     libc.src.fcntl.openat
+    libc.src.fcntl.posix_fadvise
 
     # net/if.h entrypoints
     libc.src.net.if_indextoname
diff --git a/libc/include/fcntl.yaml b/libc/include/fcntl.yaml
index 43226b455051a..e2f174447b6ba 100644
--- a/libc/include/fcntl.yaml
+++ b/libc/include/fcntl.yaml
@@ -10,6 +10,18 @@ macros:
     macro_header: fcntl-macros.h
   - macro_name: AT_EACCESS
     macro_header: fcntl-macros.h
+  - macro_name: POSIX_FADV_NORMAL
+    macro_header: fcntl-macros.h
+  - macro_name: POSIX_FADV_RANDOM
+    macro_header: fcntl-macros.h
+  - macro_name: POSIX_FADV_SEQUENTIAL
+    macro_header: fcntl-macros.h
+  - macro_name: POSIX_FADV_WILLNEED
+    macro_header: fcntl-macros.h
+  - macro_name: POSIX_FADV_DONTNEED
+    macro_header: fcntl-macros.h
+  - macro_name: POSIX_FADV_NOREUSE
+    macro_header: fcntl-macros.h
 types:
   - type_name: mode_t
   - type_name: off_t
@@ -52,3 +64,12 @@ functions:
       - type: const char *
       - type: int
       - type: '...'
+  - name: posix_fadvise
+    standards:
+      - posix
+    return_type: int
+    arguments:
+      - type: int
+      - type: off_t
+      - type: off_t
+      - type: int
diff --git a/libc/include/llvm-libc-macros/linux/fcntl-macros.h b/libc/include/llvm-libc-macros/linux/fcntl-macros.h
index 8dcc177434cec..648c4a9ec00da 100644
--- a/libc/include/llvm-libc-macros/linux/fcntl-macros.h
+++ b/libc/include/llvm-libc-macros/linux/fcntl-macros.h
@@ -105,4 +105,12 @@
 #define F_SETLKW F_SETLKW64
 #endif
 
+// Advice values for posix_fadvise.
+#define POSIX_FADV_NORMAL 0
+#define POSIX_FADV_RANDOM 1
+#define POSIX_FADV_SEQUENTIAL 2
+#define POSIX_FADV_WILLNEED 3
+#define POSIX_FADV_DONTNEED 4
+#define POSIX_FADV_NOREUSE 5
+
 #endif // LLVM_LIBC_MACROS_LINUX_FCNTL_MACROS_H
diff --git a/libc/src/fcntl/CMakeLists.txt b/libc/src/fcntl/CMakeLists.txt
index 77400e9050d08..1d6c31cf98978 100644
--- a/libc/src/fcntl/CMakeLists.txt
+++ b/libc/src/fcntl/CMakeLists.txt
@@ -29,3 +29,10 @@ add_entrypoint_object(
   DEPENDS
     .${LIBC_TARGET_OS}.openat
 )
+
+add_entrypoint_object(
+  posix_fadvise
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.posix_fadvise
+)
diff --git a/libc/src/fcntl/linux/CMakeLists.txt b/libc/src/fcntl/linux/CMakeLists.txt
index a235a99272636..37324a74b5c44 100644
--- a/libc/src/fcntl/linux/CMakeLists.txt
+++ b/libc/src/fcntl/linux/CMakeLists.txt
@@ -48,3 +48,18 @@ add_entrypoint_object(
     libc.src.__support.OSUtil.osutil
     libc.src.errno.errno
 )
+
+add_entrypoint_object(
+  posix_fadvise
+  SRCS
+    posix_fadvise.cpp
+  HDRS
+    ../posix_fadvise.h
+  DEPENDS
+    libc.hdr.types.off_t
+    libc.hdr.stdint_proxy
+    libc.include.sys_syscall
+    libc.src.__support.CPP.bit
+    libc.src.__support.OSUtil.osutil
+    libc.src.__support.common
+)
diff --git a/libc/src/fcntl/linux/posix_fadvise.cpp b/libc/src/fcntl/linux/posix_fadvise.cpp
new file mode 100644
index 0000000000000..a0be70f78ca45
--- /dev/null
+++ b/libc/src/fcntl/linux/posix_fadvise.cpp
@@ -0,0 +1,74 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Linux implementation of posix_fadvise.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/fcntl/posix_fadvise.h"
+
+#include "hdr/stdint_proxy.h"
+#include "hdr/types/off_t.h"
+#include "src/__support/CPP/bit.h"
+#include "src/__support/OSUtil/syscall.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+#include <sys/syscall.h>
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, posix_fadvise,
+                   (int fd, off_t offset, off_t len, int advice)) {
+  int ret;
+  if constexpr (sizeof(long) == sizeof(uint32_t) &&
+                sizeof(off_t) == sizeof(uint64_t)) {
+    uint64_t offset_bits = cpp::bit_cast<uint64_t>(offset);
+    long offset_low = static_cast<long>(offset_bits & UINT32_MAX);
+    long offset_high = static_cast<long>(offset_bits >> 32);
+
+#if defined(SYS_fadvise64_64) || defined(SYS_arm_fadvise64_64)
+    uint64_t len_bits = cpp::bit_cast<uint64_t>(len);
+    long len_low = static_cast<long>(len_bits & UINT32_MAX);
+    long len_high = static_cast<long>(len_bits >> 32);
+#endif
+
+#if defined(SYS_fadvise64_64)
+    ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_fadvise64_64, fd, offset_low,
+                                            offset_high, len_low, len_high,
+                                            advice);
+#elif defined(SYS_arm_fadvise64_64)
+    ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_arm_fadvise64_64, fd, advice,
+                                            offset_low, offset_high, len_low,
+                                            len_high);
+#elif defined(SYS_fadvise64)
+    ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_fadvise64, fd, offset_low,
+                                            offset_high,
+                                            static_cast<size_t>(len), advice);
+#else
+#error "fadvise64 syscall not available."
+#endif
+  } else {
+#if defined(SYS_fadvise64)
+    ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_fadvise64, fd, offset, len,
+                                            advice);
+#elif defined(SYS_fadvise64_64)
+    ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_fadvise64_64, fd, offset, len,
+                                            advice);
+#else
+#error "fadvise64 syscall not available."
+#endif
+  }
+
+  if (ret < 0)
+    return -ret;
+  return 0;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/fcntl/posix_fadvise.h b/libc/src/fcntl/posix_fadvise.h
new file mode 100644
index 0000000000000..1a93521846a92
--- /dev/null
+++ b/libc/src/fcntl/posix_fadvise.h
@@ -0,0 +1,26 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation header for posix_fadvise.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_FCNTL_POSIX_FADVISE_H
+#define LLVM_LIBC_SRC_FCNTL_POSIX_FADVISE_H
+
+#include "hdr/types/off_t.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int posix_fadvise(int fd, off_t offset, off_t len, int advice);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_FCNTL_POSIX_FADVISE_H
diff --git a/libc/test/src/fcntl/CMakeLists.txt b/libc/test/src/fcntl/CMakeLists.txt
index dd2b473d954c5..d285a7875267c 100644
--- a/libc/test/src/fcntl/CMakeLists.txt
+++ b/libc/test/src/fcntl/CMakeLists.txt
@@ -56,3 +56,21 @@ add_libc_test(
     libc.test.UnitTest.ErrnoCheckingTest
     libc.test.UnitTest.ErrnoSetterMatcher
 )
+
+add_libc_test(
+  posix_fadvise_test
+  SUITE
+    libc_fcntl_unittests
+  SRCS
+    posix_fadvise_test.cpp
+  DEPENDS
+    libc.hdr.errno_macros
+    libc.hdr.fcntl_macros
+    libc.hdr.sys_stat_macros
+    libc.src.errno.errno
+    libc.src.fcntl.creat
+    libc.src.fcntl.posix_fadvise
+    libc.src.unistd.close
+    libc.src.unistd.pipe
+    libc.test.UnitTest.ErrnoCheckingTest
+)
diff --git a/libc/test/src/fcntl/posix_fadvise_test.cpp b/libc/test/src/fcntl/posix_fadvise_test.cpp
new file mode 100644
index 0000000000000..b0b410bb51f64
--- /dev/null
+++ b/libc/test/src/fcntl/posix_fadvise_test.cpp
@@ -0,0 +1,76 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Unittests for posix_fadvise.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/errno_macros.h"
+#include "hdr/fcntl_macros.h"
+#include "hdr/sys_stat_macros.h"
+#include "src/fcntl/creat.h"
+#include "src/fcntl/posix_fadvise.h"
+#include "src/unistd/close.h"
+#include "src/unistd/pipe.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/Test.h"
+
+using LlvmLibcPosixFadviseTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+
+TEST_F(LlvmLibcPosixFadviseTest, InvalidFileDescriptor) {
+  EXPECT_EQ(LIBC_NAMESPACE::posix_fadvise(-1, 0, 0, POSIX_FADV_NORMAL), EBADF);
+  // posix_fadvise must return the error directly and not set errno.
+  ASSERT_ERRNO_SUCCESS();
+}
+
+TEST_F(LlvmLibcPosixFadviseTest, ValidFile) {
+  constexpr const char *TEST_FILE = "testdata/posix_fadvise.test";
+  int fd = LIBC_NAMESPACE::creat(TEST_FILE, S_IRWXU);
+  ASSERT_ERRNO_SUCCESS();
+  ASSERT_GT(fd, 0);
+
+  EXPECT_EQ(LIBC_NAMESPACE::posix_fadvise(fd, 0, 0, POSIX_FADV_NORMAL), 0);
+  EXPECT_EQ(LIBC_NAMESPACE::posix_fadvise(fd, 0, 0, POSIX_FADV_RANDOM), 0);
+  EXPECT_EQ(LIBC_NAMESPACE::posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL), 0);
+  EXPECT_EQ(LIBC_NAMESPACE::posix_fadvise(fd, 0, 0, POSIX_FADV_WILLNEED), 0);
+  EXPECT_EQ(LIBC_NAMESPACE::posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED), 0);
+  EXPECT_EQ(LIBC_NAMESPACE::posix_fadvise(fd, 0, 0, POSIX_FADV_NOREUSE), 0);
+
+  // Non-zero offset and length
+  EXPECT_EQ(LIBC_NAMESPACE::posix_fadvise(fd, 10, 20, POSIX_FADV_NORMAL), 0);
+
+  // Invalid advice
+  EXPECT_EQ(LIBC_NAMESPACE::posix_fadvise(fd, 0, 0, -1), EINVAL);
+  ASSERT_ERRNO_SUCCESS();
+
+  // Negative len
+  EXPECT_EQ(LIBC_NAMESPACE::posix_fadvise(fd, 0, -1, POSIX_FADV_NORMAL),
+            EINVAL);
+  ASSERT_ERRNO_SUCCESS();
+
+  EXPECT_EQ(LIBC_NAMESPACE::close(fd), 0);
+  ASSERT_ERRNO_SUCCESS();
+}
+
+TEST_F(LlvmLibcPosixFadviseTest, Pipe) {
+  int pipefd[2];
+  ASSERT_EQ(LIBC_NAMESPACE::pipe(pipefd), 0);
+  ASSERT_ERRNO_SUCCESS();
+
+  // fadvise on a pipe should return ESPIPE
+  EXPECT_EQ(LIBC_NAMESPACE::posix_fadvise(pipefd[0], 0, 0, POSIX_FADV_NORMAL),
+            ESPIPE);
+  EXPECT_EQ(LIBC_NAMESPACE::posix_fadvise(pipefd[1], 0, 0, POSIX_FADV_NORMAL),
+            ESPIPE);
+  ASSERT_ERRNO_SUCCESS();
+
+  EXPECT_EQ(LIBC_NAMESPACE::close(pipefd[0]), 0);
+  EXPECT_EQ(LIBC_NAMESPACE::close(pipefd[1]), 0);
+  ASSERT_ERRNO_SUCCESS();
+}

>From 5260dcb3206009e04663d2c824a77e5453ea1924 Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue.h at gmail.com>
Date: Thu, 3 Sep 2026 13:59:12 +0000
Subject: [PATCH 2/2] Address comments.

---
 .../linux/syscall_wrappers/CMakeLists.txt     | 15 ++++
 .../linux/syscall_wrappers/posix_fadvise.h    | 75 +++++++++++++++++++
 libc/src/fcntl/linux/CMakeLists.txt           |  5 +-
 libc/src/fcntl/linux/posix_fadvise.cpp        | 51 +------------
 libc/test/src/fcntl/CMakeLists.txt            |  1 +
 libc/test/src/fcntl/posix_fadvise_test.cpp    |  9 +--
 6 files changed, 98 insertions(+), 58 deletions(-)
 create mode 100644 libc/src/__support/OSUtil/linux/syscall_wrappers/posix_fadvise.h

diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index f4a3d1fa7c20e..6c13f929c195a 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -445,6 +445,21 @@ add_header_library(
     libc.include.sys_syscall
 )
 
+add_header_library(
+  posix_fadvise
+  HDRS
+    posix_fadvise.h
+  DEPENDS
+    libc.hdr.stdint_proxy
+    libc.hdr.types.off_t
+    libc.include.sys_syscall
+    libc.src.__support.CPP.bit
+    libc.src.__support.OSUtil.osutil
+    libc.src.__support.common
+    libc.src.__support.error_or
+    libc.src.__support.macros.config
+)
+
 add_header_library(
   ptrace
   HDRS
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/posix_fadvise.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/posix_fadvise.h
new file mode 100644
index 0000000000000..ee97b3839b70f
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/posix_fadvise.h
@@ -0,0 +1,75 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// ErrorOr-returning syscall wrapper for posix_fadvise.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_POSIX_FADVISE_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_POSIX_FADVISE_H
+
+#include "hdr/stdint_proxy.h"
+#include "hdr/types/off_t.h"
+#include "src/__support/CPP/bit.h"
+#include "src/__support/OSUtil/linux/syscall.h" // syscall_impl
+#include "src/__support/common.h"
+#include "src/__support/error_or.h"
+#include "src/__support/macros/config.h"
+
+#include <sys/syscall.h> // For syscall numbers
+
+namespace LIBC_NAMESPACE_DECL {
+namespace linux_syscalls {
+
+LIBC_INLINE ErrorOr<int> posix_fadvise(int fd, off_t offset, off_t len,
+                                       int advice) {
+  int ret;
+  if constexpr (sizeof(long) == sizeof(uint32_t) &&
+                sizeof(off_t) == sizeof(uint64_t)) {
+    uint64_t offset_bits = cpp::bit_cast<uint64_t>(offset);
+    long offset_low = static_cast<long>(offset_bits & UINT32_MAX);
+    long offset_high = static_cast<long>(offset_bits >> 32);
+
+#if defined(SYS_fadvise64_64) || defined(SYS_arm_fadvise64_64)
+    uint64_t len_bits = cpp::bit_cast<uint64_t>(len);
+    long len_low = static_cast<long>(len_bits & UINT32_MAX);
+    long len_high = static_cast<long>(len_bits >> 32);
+#endif
+
+#if defined(SYS_fadvise64_64)
+    ret = syscall_impl<int>(SYS_fadvise64_64, fd, offset_low, offset_high,
+                            len_low, len_high, advice);
+#elif defined(SYS_arm_fadvise64_64)
+    ret = syscall_impl<int>(SYS_arm_fadvise64_64, fd, advice, offset_low,
+                            offset_high, len_low, len_high);
+#elif defined(SYS_fadvise64)
+    ret = syscall_impl<int>(SYS_fadvise64, fd, offset_low, offset_high,
+                            static_cast<size_t>(len), advice);
+#else
+#error "fadvise64 syscall not available."
+#endif
+  } else {
+#if defined(SYS_fadvise64)
+    ret = syscall_impl<int>(SYS_fadvise64, fd, offset, len, advice);
+#elif defined(SYS_fadvise64_64)
+    ret = syscall_impl<int>(SYS_fadvise64_64, fd, offset, len, advice);
+#else
+#error "fadvise64 syscall not available."
+#endif
+  }
+
+  if (ret < 0)
+    return Error(-ret);
+  return 0;
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_POSIX_FADVISE_H
diff --git a/libc/src/fcntl/linux/CMakeLists.txt b/libc/src/fcntl/linux/CMakeLists.txt
index 37324a74b5c44..bba33928da17a 100644
--- a/libc/src/fcntl/linux/CMakeLists.txt
+++ b/libc/src/fcntl/linux/CMakeLists.txt
@@ -57,9 +57,6 @@ add_entrypoint_object(
     ../posix_fadvise.h
   DEPENDS
     libc.hdr.types.off_t
-    libc.hdr.stdint_proxy
-    libc.include.sys_syscall
-    libc.src.__support.CPP.bit
-    libc.src.__support.OSUtil.osutil
+    libc.src.__support.OSUtil.linux.syscall_wrappers.posix_fadvise
     libc.src.__support.common
 )
diff --git a/libc/src/fcntl/linux/posix_fadvise.cpp b/libc/src/fcntl/linux/posix_fadvise.cpp
index a0be70f78ca45..ed0a539204d0a 100644
--- a/libc/src/fcntl/linux/posix_fadvise.cpp
+++ b/libc/src/fcntl/linux/posix_fadvise.cpp
@@ -13,61 +13,18 @@
 
 #include "src/fcntl/posix_fadvise.h"
 
-#include "hdr/stdint_proxy.h"
 #include "hdr/types/off_t.h"
-#include "src/__support/CPP/bit.h"
-#include "src/__support/OSUtil/syscall.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/posix_fadvise.h"
 #include "src/__support/common.h"
 #include "src/__support/macros/config.h"
 
-#include <sys/syscall.h>
-
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(int, posix_fadvise,
                    (int fd, off_t offset, off_t len, int advice)) {
-  int ret;
-  if constexpr (sizeof(long) == sizeof(uint32_t) &&
-                sizeof(off_t) == sizeof(uint64_t)) {
-    uint64_t offset_bits = cpp::bit_cast<uint64_t>(offset);
-    long offset_low = static_cast<long>(offset_bits & UINT32_MAX);
-    long offset_high = static_cast<long>(offset_bits >> 32);
-
-#if defined(SYS_fadvise64_64) || defined(SYS_arm_fadvise64_64)
-    uint64_t len_bits = cpp::bit_cast<uint64_t>(len);
-    long len_low = static_cast<long>(len_bits & UINT32_MAX);
-    long len_high = static_cast<long>(len_bits >> 32);
-#endif
-
-#if defined(SYS_fadvise64_64)
-    ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_fadvise64_64, fd, offset_low,
-                                            offset_high, len_low, len_high,
-                                            advice);
-#elif defined(SYS_arm_fadvise64_64)
-    ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_arm_fadvise64_64, fd, advice,
-                                            offset_low, offset_high, len_low,
-                                            len_high);
-#elif defined(SYS_fadvise64)
-    ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_fadvise64, fd, offset_low,
-                                            offset_high,
-                                            static_cast<size_t>(len), advice);
-#else
-#error "fadvise64 syscall not available."
-#endif
-  } else {
-#if defined(SYS_fadvise64)
-    ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_fadvise64, fd, offset, len,
-                                            advice);
-#elif defined(SYS_fadvise64_64)
-    ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_fadvise64_64, fd, offset, len,
-                                            advice);
-#else
-#error "fadvise64 syscall not available."
-#endif
-  }
-
-  if (ret < 0)
-    return -ret;
+  auto result = linux_syscalls::posix_fadvise(fd, offset, len, advice);
+  if (!result)
+    return result.error();
   return 0;
 }
 
diff --git a/libc/test/src/fcntl/CMakeLists.txt b/libc/test/src/fcntl/CMakeLists.txt
index d285a7875267c..522a288216f2b 100644
--- a/libc/test/src/fcntl/CMakeLists.txt
+++ b/libc/test/src/fcntl/CMakeLists.txt
@@ -72,5 +72,6 @@ add_libc_test(
     libc.src.fcntl.posix_fadvise
     libc.src.unistd.close
     libc.src.unistd.pipe
+    libc.src.unistd.unlink
     libc.test.UnitTest.ErrnoCheckingTest
 )
diff --git a/libc/test/src/fcntl/posix_fadvise_test.cpp b/libc/test/src/fcntl/posix_fadvise_test.cpp
index b0b410bb51f64..a857969f6f4b5 100644
--- a/libc/test/src/fcntl/posix_fadvise_test.cpp
+++ b/libc/test/src/fcntl/posix_fadvise_test.cpp
@@ -18,6 +18,7 @@
 #include "src/fcntl/posix_fadvise.h"
 #include "src/unistd/close.h"
 #include "src/unistd/pipe.h"
+#include "src/unistd/unlink.h"
 #include "test/UnitTest/ErrnoCheckingTest.h"
 #include "test/UnitTest/Test.h"
 
@@ -32,8 +33,8 @@ TEST_F(LlvmLibcPosixFadviseTest, InvalidFileDescriptor) {
 TEST_F(LlvmLibcPosixFadviseTest, ValidFile) {
   constexpr const char *TEST_FILE = "testdata/posix_fadvise.test";
   int fd = LIBC_NAMESPACE::creat(TEST_FILE, S_IRWXU);
-  ASSERT_ERRNO_SUCCESS();
   ASSERT_GT(fd, 0);
+  ASSERT_EQ(LIBC_NAMESPACE::unlink(TEST_FILE), 0);
 
   EXPECT_EQ(LIBC_NAMESPACE::posix_fadvise(fd, 0, 0, POSIX_FADV_NORMAL), 0);
   EXPECT_EQ(LIBC_NAMESPACE::posix_fadvise(fd, 0, 0, POSIX_FADV_RANDOM), 0);
@@ -47,30 +48,24 @@ TEST_F(LlvmLibcPosixFadviseTest, ValidFile) {
 
   // Invalid advice
   EXPECT_EQ(LIBC_NAMESPACE::posix_fadvise(fd, 0, 0, -1), EINVAL);
-  ASSERT_ERRNO_SUCCESS();
 
   // Negative len
   EXPECT_EQ(LIBC_NAMESPACE::posix_fadvise(fd, 0, -1, POSIX_FADV_NORMAL),
             EINVAL);
-  ASSERT_ERRNO_SUCCESS();
 
   EXPECT_EQ(LIBC_NAMESPACE::close(fd), 0);
-  ASSERT_ERRNO_SUCCESS();
 }
 
 TEST_F(LlvmLibcPosixFadviseTest, Pipe) {
   int pipefd[2];
   ASSERT_EQ(LIBC_NAMESPACE::pipe(pipefd), 0);
-  ASSERT_ERRNO_SUCCESS();
 
   // fadvise on a pipe should return ESPIPE
   EXPECT_EQ(LIBC_NAMESPACE::posix_fadvise(pipefd[0], 0, 0, POSIX_FADV_NORMAL),
             ESPIPE);
   EXPECT_EQ(LIBC_NAMESPACE::posix_fadvise(pipefd[1], 0, 0, POSIX_FADV_NORMAL),
             ESPIPE);
-  ASSERT_ERRNO_SUCCESS();
 
   EXPECT_EQ(LIBC_NAMESPACE::close(pipefd[0]), 0);
   EXPECT_EQ(LIBC_NAMESPACE::close(pipefd[1]), 0);
-  ASSERT_ERRNO_SUCCESS();
 }



More information about the libc-commits mailing list