• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
14 #include <sys/syscall.h> // For syscall numbers.
15 
16 namespace LIBC_NAMESPACE {
17 
18 // This function is currently linux only. It has to be refactored suitably if
19 // posix_madvise is to be supported on non-linux operating systems also.
20 LLVM_LIBC_FUNCTION(int, posix_madvise, (void *addr, size_t size, int advice)) {
21   // POSIX_MADV_DONTNEED does nothing because the default MADV_DONTNEED may
22   // cause data loss, which the posix madvise does not allow.
23   if (advice == POSIX_MADV_DONTNEED) {
24     return 0;
25   }
26   int ret = LIBC_NAMESPACE::syscall_impl<int>(
27       SYS_madvise, reinterpret_cast<long>(addr), size, advice);
28   return ret < 0 ? -ret : 0;
29 }
30 
31 } // namespace LIBC_NAMESPACE
32