xref: /aosp_15_r20/external/llvm-libc/src/sys/mman/linux/posix_madvise.cpp (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1 //===---------- Linux implementation of the POSIX posix_madvise function --===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "src/sys/mman/posix_madvise.h"
10 
11 #include "src/__support/OSUtil/syscall.h" // For internal syscall function.
12 #include "src/__support/common.h"
13 #include "src/__support/macros/config.h"
14 
15 #include <sys/syscall.h> // For syscall numbers.
16 
17 namespace LIBC_NAMESPACE_DECL {
18 
19 // This function is currently linux only. It has to be refactored suitably if
20 // posix_madvise is to be supported on non-linux operating systems also.
21 LLVM_LIBC_FUNCTION(int, posix_madvise, (void *addr, size_t size, int advice)) {
22   // POSIX_MADV_DONTNEED does nothing because the default MADV_DONTNEED may
23   // cause data loss, which the posix madvise does not allow.
24   if (advice == POSIX_MADV_DONTNEED) {
25     return 0;
26   }
27   int ret = LIBC_NAMESPACE::syscall_impl<int>(
28       SYS_madvise, reinterpret_cast<long>(addr), size, advice);
29   return ret < 0 ? -ret : 0;
30 }
31 
32 } // namespace LIBC_NAMESPACE_DECL
33