[libc-commits] [libc] [llvm] [libc] Use a syscall wrapper for posix_madvise (PR #240073)

Connector Switch via libc-commits libc-commits at lists.llvm.org
Sat Nov 7 16:20:33 PST 2026


https://github.com/c8ef created https://github.com/llvm/llvm-project/pull/240073

This PR move posix_madvise behind the centralized syscall wrappers, following libc/docs/dev/syscall_wrapper_refactor.md.


>From 306b3e13da04738f42623eee322701f187f33e85 Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Sun, 8 Nov 2026 08:19:30 +0800
Subject: [PATCH] [libc] Use a syscall wrapper for posix_madvise

This PR move posix_madvise behind the centralized syscall wrappers, following libc/docs/dev/syscall_wrapper_refactor.md.
---
 libc/src/sys/mman/linux/CMakeLists.txt           |  5 +++--
 libc/src/sys/mman/linux/posix_madvise.cpp        | 16 +++++++++-------
 .../bazel/llvm-project-overlay/libc/BUILD.bazel  |  2 +-
 3 files changed, 13 insertions(+), 10 deletions(-)

diff --git a/libc/src/sys/mman/linux/CMakeLists.txt b/libc/src/sys/mman/linux/CMakeLists.txt
index d8426921f85dac..c13d482e50600e 100644
--- a/libc/src/sys/mman/linux/CMakeLists.txt
+++ b/libc/src/sys/mman/linux/CMakeLists.txt
@@ -94,8 +94,9 @@ add_entrypoint_object(
   DEPENDS
     libc.hdr.sys_mman_macros
     libc.hdr.types.size_t
-    libc.include.sys_syscall
-    libc.src.__support.OSUtil.osutil
+    libc.src.__support.OSUtil.linux.syscall_wrappers.madvise
+    libc.src.__support.common
+    libc.src.__support.macros.config
 )
 
 add_entrypoint_object(
diff --git a/libc/src/sys/mman/linux/posix_madvise.cpp b/libc/src/sys/mman/linux/posix_madvise.cpp
index 18427e25911fd5..265ef8400f4a06 100644
--- a/libc/src/sys/mman/linux/posix_madvise.cpp
+++ b/libc/src/sys/mman/linux/posix_madvise.cpp
@@ -1,19 +1,22 @@
-//===---------- Linux implementation of the POSIX posix_madvise function --===//
+//===----------------------------------------------------------------------===//
 //
 // 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_madvise.
+///
+//===----------------------------------------------------------------------===//
 
 #include "src/sys/mman/posix_madvise.h"
 
-#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
+#include "src/__support/OSUtil/linux/syscall_wrappers/madvise.h"
 #include "src/__support/common.h"
 #include "src/__support/macros/config.h"
 
-#include <sys/syscall.h> // For syscall numbers.
-
 #include "hdr/sys_mman_macros.h" // For POSIX_MADV_DONTNEED.
 
 namespace LIBC_NAMESPACE_DECL {
@@ -26,9 +29,8 @@ LLVM_LIBC_FUNCTION(int, posix_madvise, (void *addr, size_t size, int advice)) {
   if (advice == POSIX_MADV_DONTNEED) {
     return 0;
   }
-  int ret = LIBC_NAMESPACE::syscall_impl<int>(
-      SYS_madvise, reinterpret_cast<long>(addr), size, advice);
-  return ret < 0 ? -ret : 0;
+  auto result = linux_syscalls::madvise(addr, size, advice);
+  return result ? 0 : result.error();
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 89c312fa4fe87b..c50e9193ac4248 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -19998,7 +19998,7 @@ libc_function(
     deps = [
         ":__support_common",
         ":__support_macros_config",
-        ":__support_osutil_syscall",
+        ":__support_osutil_linux_syscall_wrappers_madvise",
         ":hdr_sys_mman_macros",
         ":types_size_t",
     ],



More information about the libc-commits mailing list