xref: /aosp_15_r20/external/llvm-libc/src/sys/mman/linux/mremap.cpp (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
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 //===----------------------------------------------------------------------===//
8 
9 #include "src/sys/mman/mremap.h"
10 
11 #include "src/__support/OSUtil/syscall.h" // For internal syscall function.
12 #include "src/__support/common.h"
13 
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.
19 
20 namespace LIBC_NAMESPACE_DECL {
21 
22 LLVM_LIBC_FUNCTION(void *, mremap,
23                    (void *old_address, size_t old_size, size_t new_size,
24                     int flags, ... /* void *new_address */)) {
25 
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);
33   }
34   ret = LIBC_NAMESPACE::syscall_impl<long>(SYS_mremap, old_address, old_size,
35                                            new_size, flags, new_address);
36 
37   if (ret < 0 && ret > -EXEC_PAGESIZE) {
38     libc_errno = static_cast<int>(-ret);
39     return MAP_FAILED;
40   }
41 
42   return reinterpret_cast<void *>(ret);
43 }
44 
45 } // namespace LIBC_NAMESPACE_DECL
46