1 //===---------- Linux implementation of the POSIX mremap function----------===//
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
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.
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 */)) {
27 void *new_address
= nullptr;
28 if (flags
& MREMAP_FIXED
) {
30 va_start(varargs
, flags
);
31 new_address
= va_arg(varargs
, void *);
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
);
42 return reinterpret_cast<void *>(ret
);
45 } // namespace LIBC_NAMESPACE_DECL