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

Pavel Labath via libc-commits libc-commits at lists.llvm.org
Thu Sep 3 09:54:19 PDT 2026


================
@@ -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);
----------------
labath wrote:

Please convert these to `syscall_checked`. Then this can just do a `return syscall_checked<int>(...)`.

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


More information about the libc-commits mailing list