xref: /llvm-project/libc/src/sys/mman/linux/madvise.cpp (revision 5ff3ff33ff930e4ec49da7910612d8a41eb068cb)
185c70da7SMichael Jones //===---------- Linux implementation of the 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/madvise.h"
1085c70da7SMichael Jones 
1185c70da7SMichael Jones #include "src/__support/OSUtil/syscall.h" // For internal syscall function.
1285c70da7SMichael Jones #include "src/__support/common.h"
1385c70da7SMichael Jones 
14*5ff3ff33SPetr Hosek #include "src/__support/macros/config.h"
15204587a3SSiva Chandra Reddy #include "src/errno/libc_errno.h"
1685c70da7SMichael Jones #include <sys/syscall.h> // For syscall numbers.
1785c70da7SMichael Jones 
18*5ff3ff33SPetr Hosek namespace LIBC_NAMESPACE_DECL {
1985c70da7SMichael Jones 
2085c70da7SMichael Jones // This function is currently linux only. It has to be refactored suitably if
2185c70da7SMichael Jones // madvise is to be supported on non-linux operating systems also.
2285c70da7SMichael Jones LLVM_LIBC_FUNCTION(int, madvise, (void *addr, size_t size, int advice)) {
23b6bc9d72SGuillaume Chatelet   int ret = LIBC_NAMESPACE::syscall_impl<int>(
241801c356SMichael Jones       SYS_madvise, reinterpret_cast<long>(addr), size, advice);
2585c70da7SMichael Jones 
2685c70da7SMichael Jones   // A negative return value indicates an error with the magnitude of the
2785c70da7SMichael Jones   // value being the error code.
28f0a3954eSMichael Jones   if (ret < 0) {
29f0a3954eSMichael Jones     libc_errno = -ret;
3085c70da7SMichael Jones     return -1;
3185c70da7SMichael Jones   }
3285c70da7SMichael Jones 
3385c70da7SMichael Jones   return 0;
3485c70da7SMichael Jones }
3585c70da7SMichael Jones 
36*5ff3ff33SPetr Hosek } // namespace LIBC_NAMESPACE_DECL
37