xref: /llvm-project/libc/src/sys/mman/linux/posix_madvise.cpp (revision 5ff3ff33ff930e4ec49da7910612d8a41eb068cb)
185c70da7SMichael Jones //===---------- Linux implementation of the POSIX posix_madvise function --===//
285c70da7SMichael Jones //
385c70da7SMichael Jones // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
485c70da7SMichael Jones // See https://llvm.org/LICENSE.txt for license information.
585c70da7SMichael Jones // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
685c70da7SMichael Jones //
785c70da7SMichael Jones //===----------------------------------------------------------------------===//
885c70da7SMichael Jones 
985c70da7SMichael Jones #include "src/sys/mman/posix_madvise.h"
1085c70da7SMichael Jones 
1185c70da7SMichael Jones #include "src/__support/OSUtil/syscall.h" // For internal syscall function.
1285c70da7SMichael Jones #include "src/__support/common.h"
13*5ff3ff33SPetr Hosek #include "src/__support/macros/config.h"
1485c70da7SMichael Jones 
1585c70da7SMichael Jones #include <sys/syscall.h> // For syscall numbers.
1685c70da7SMichael Jones 
17*5ff3ff33SPetr Hosek namespace LIBC_NAMESPACE_DECL {
1885c70da7SMichael Jones 
1985c70da7SMichael Jones // This function is currently linux only. It has to be refactored suitably if
2085c70da7SMichael Jones // posix_madvise is to be supported on non-linux operating systems also.
2185c70da7SMichael Jones LLVM_LIBC_FUNCTION(int, posix_madvise, (void *addr, size_t size, int advice)) {
2285c70da7SMichael Jones   // POSIX_MADV_DONTNEED does nothing because the default MADV_DONTNEED may
2385c70da7SMichael Jones   // cause data loss, which the posix madvise does not allow.
2485c70da7SMichael Jones   if (advice == POSIX_MADV_DONTNEED) {
2585c70da7SMichael Jones     return 0;
2685c70da7SMichael Jones   }
27b6bc9d72SGuillaume Chatelet   int ret = LIBC_NAMESPACE::syscall_impl<int>(
281801c356SMichael Jones       SYS_madvise, reinterpret_cast<long>(addr), size, advice);
29f0a3954eSMichael Jones   return ret < 0 ? -ret : 0;
3085c70da7SMichael Jones }
3185c70da7SMichael Jones 
32*5ff3ff33SPetr Hosek } // namespace LIBC_NAMESPACE_DECL
33