[mlir] Update Ch-2.md (#121379)
[llvm-project.git] / libc / src / sys / mman / linux / mremap.cpp
blob38bcfce833d3d9323111667778f669109acc2c64
1 //===---------- Linux implementation of the POSIX mremap 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 //===----------------------------------------------------------------------===//
9 #include "src/sys/mman/mremap.h"
11 #include "src/__support/OSUtil/syscall.h" // For internal syscall function.
12 #include "src/__support/common.h"
14 #include "src/__support/macros/config.h"
15 #include "src/errno/libc_errno.h"
16 #include <linux/param.h> // For EXEC_PAGESIZE.
17 #include <stdarg.h>
18 #include <sys/syscall.h> // For syscall numbers.
20 namespace LIBC_NAMESPACE_DECL {
22 LLVM_LIBC_FUNCTION(void *, mremap,
23 (void *old_address, size_t old_size, size_t new_size,
24 int flags, ... /* void *new_address */)) {
26 long ret = 0;
27 void *new_address = nullptr;
28 if (flags & MREMAP_FIXED) {
29 va_list varargs;
30 va_start(varargs, flags);
31 new_address = va_arg(varargs, void *);
32 va_end(varargs);
34 ret = LIBC_NAMESPACE::syscall_impl<long>(SYS_mremap, old_address, old_size,
35 new_size, flags, new_address);
37 if (ret < 0 && ret > -EXEC_PAGESIZE) {
38 libc_errno = static_cast<int>(-ret);
39 return MAP_FAILED;
42 return reinterpret_cast<void *>(ret);
45 } // namespace LIBC_NAMESPACE_DECL