[libc-commits] [libc] f7c7651 - [libc] Use a syscall wrapper for madvise (#239194)
via libc-commits
libc-commits at lists.llvm.org
Thu Nov 5 22:00:42 PST 2026
Author: Connector Switch
Date: 2026-11-06T14:00:24+08:00
New Revision: f7c765159371986fa60ff7e217f9173de6044d46
URL: https://github.com/llvm/llvm-project/commit/f7c765159371986fa60ff7e217f9173de6044d46
DIFF: https://github.com/llvm/llvm-project/commit/f7c765159371986fa60ff7e217f9173de6044d46.diff
LOG: [libc] Use a syscall wrapper for madvise (#239194)
This PR move madvise behind the centralized syscall wrappers, following
libc/docs/dev/syscall_wrapper_refactor.md.
Added:
libc/src/__support/OSUtil/linux/syscall_wrappers/madvise.h
Modified:
libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
libc/src/sys/mman/linux/CMakeLists.txt
libc/src/sys/mman/linux/madvise.cpp
utils/bazel/llvm-project-overlay/libc/BUILD.bazel
Removed:
################################################################################
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index c2db5f35eddda3..32efce2565d43a 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -871,6 +871,19 @@ add_header_library(
libc.include.sys_syscall
)
+add_header_library(
+ madvise
+ HDRS
+ madvise.h
+ DEPENDS
+ libc.hdr.types.size_t
+ libc.include.sys_syscall
+ libc.src.__support.OSUtil.osutil
+ libc.src.__support.common
+ libc.src.__support.error_or
+ libc.src.__support.macros.config
+)
+
add_header_library(
memfd_create
HDRS
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/madvise.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/madvise.h
new file mode 100644
index 00000000000000..6f1309caa8cc63
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/madvise.h
@@ -0,0 +1,35 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+/// Syscall wrapper for madvise.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_MADVISE_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_MADVISE_H
+
+#include "hdr/types/size_t.h"
+#include "src/__support/OSUtil/linux/syscall.h"
+#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> madvise(void *addr, size_t size, int advice) {
+ return syscall_checked<int>(SYS_madvise, addr, size, advice);
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_MADVISE_H
diff --git a/libc/src/sys/mman/linux/CMakeLists.txt b/libc/src/sys/mman/linux/CMakeLists.txt
index 6987261e53e07c..b923eb42d15029 100644
--- a/libc/src/sys/mman/linux/CMakeLists.txt
+++ b/libc/src/sys/mman/linux/CMakeLists.txt
@@ -12,9 +12,11 @@ add_entrypoint_object(
HDRS
../madvise.h
DEPENDS
- libc.include.sys_mman
- libc.include.sys_syscall
- libc.src.__support.OSUtil.osutil
+ libc.hdr.types.size_t
+ libc.src.__support.OSUtil.linux.syscall_wrappers.madvise
+ libc.src.__support.common
+ libc.src.__support.libc_errno
+ libc.src.__support.macros.config
libc.src.errno.errno
)
diff --git a/libc/src/sys/mman/linux/madvise.cpp b/libc/src/sys/mman/linux/madvise.cpp
index 1bb284f62b892a..e765c0ea262b26 100644
--- a/libc/src/sys/mman/linux/madvise.cpp
+++ b/libc/src/sys/mman/linux/madvise.cpp
@@ -1,35 +1,33 @@
-//===---------- Linux implementation of the 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 madvise.
+///
+//===----------------------------------------------------------------------===//
#include "src/sys/mman/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/libc_errno.h"
#include "src/__support/macros/config.h"
-#include <sys/syscall.h> // For syscall numbers.
namespace LIBC_NAMESPACE_DECL {
// This function is currently linux only. It has to be refactored suitably if
// madvise is to be supported on non-linux operating systems also.
LLVM_LIBC_FUNCTION(int, madvise, (void *addr, size_t size, int advice)) {
- int ret = LIBC_NAMESPACE::syscall_impl<int>(
- SYS_madvise, reinterpret_cast<long>(addr), size, advice);
-
- // A negative return value indicates an error with the magnitude of the
- // value being the error code.
- if (ret < 0) {
- libc_errno = -ret;
+ auto result = linux_syscalls::madvise(addr, size, advice);
+ if (!result) {
+ libc_errno = result.error();
return -1;
}
-
return 0;
}
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 750e73aa4ed919..d0d9f338aeb486 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -4503,6 +4503,22 @@ libc_support_library(
],
)
+libc_support_library(
+ name = "__support_osutil_linux_syscall_wrappers_madvise",
+ hdrs = ["src/__support/OSUtil/linux/syscall_wrappers/madvise.h"],
+ target_compatible_with = select({
+ "@platforms//os:linux": [],
+ "//conditions:default": ["@platforms//:incompatible"],
+ }),
+ deps = [
+ ":__support_common",
+ ":__support_error_or",
+ ":__support_macros_config",
+ ":__support_osutil_syscall",
+ ":types_size_t",
+ ],
+)
+
libc_support_library(
name = "__support_osutil_linux_syscall_wrappers_mmap",
hdrs = ["src/__support/OSUtil/linux/syscall_wrappers/mmap.h"],
@@ -19623,9 +19639,8 @@ libc_function(
":__support_common",
":__support_libc_errno",
":__support_macros_config",
- ":__support_osutil_syscall",
+ ":__support_osutil_linux_syscall_wrappers_madvise",
":errno",
- ":hdr_sys_mman_macros",
":types_size_t",
],
)
More information about the libc-commits
mailing list