diff options
| author | Jingyu Qiu <51221277+SoftJing1@users.noreply.github.com> | 2024-11-06 13:25:50 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-11-06 10:25:50 -0800 |
| commit | 5a6cc509215b62e94de3b798ea26944a375ce6cb (patch) | |
| tree | 62e811f63762146116dd48c6b2d0f9e5ee69df1a /libc/src/sys | |
| parent | 38cc03f78e3046837d8fc29d729bc2cee0c31e89 (diff) | |
[libc] add mremap (#112804)
Diffstat (limited to 'libc/src/sys')
| -rw-r--r-- | libc/src/sys/mman/CMakeLists.txt | 7 | ||||
| -rw-r--r-- | libc/src/sys/mman/linux/CMakeLists.txt | 13 | ||||
| -rw-r--r-- | libc/src/sys/mman/linux/mremap.cpp | 45 | ||||
| -rw-r--r-- | libc/src/sys/mman/mremap.h | 22 |
4 files changed, 87 insertions, 0 deletions
diff --git a/libc/src/sys/mman/CMakeLists.txt b/libc/src/sys/mman/CMakeLists.txt index 4ea43e14be02..4d4c2ad37605 100644 --- a/libc/src/sys/mman/CMakeLists.txt +++ b/libc/src/sys/mman/CMakeLists.txt @@ -106,3 +106,10 @@ add_entrypoint_object( DEPENDS .${LIBC_TARGET_OS}.shm_unlink ) + +add_entrypoint_object( + mremap + ALIAS + DEPENDS + .${LIBC_TARGET_OS}.mremap +) diff --git a/libc/src/sys/mman/linux/CMakeLists.txt b/libc/src/sys/mman/linux/CMakeLists.txt index 47c16f79bc8d..89a0ad1527a0 100644 --- a/libc/src/sys/mman/linux/CMakeLists.txt +++ b/libc/src/sys/mman/linux/CMakeLists.txt @@ -25,6 +25,19 @@ add_entrypoint_object( ) add_entrypoint_object( + mremap + SRCS + mremap.cpp + HDRS + ../mremap.h + DEPENDS + libc.include.sys_mman + libc.include.sys_syscall + libc.src.__support.OSUtil.osutil + libc.src.errno.errno +) + +add_entrypoint_object( munmap SRCS munmap.cpp diff --git a/libc/src/sys/mman/linux/mremap.cpp b/libc/src/sys/mman/linux/mremap.cpp new file mode 100644 index 000000000000..38bcfce833d3 --- /dev/null +++ b/libc/src/sys/mman/linux/mremap.cpp @@ -0,0 +1,45 @@ +//===---------- Linux implementation of the POSIX mremap function----------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/sys/mman/mremap.h" + +#include "src/__support/OSUtil/syscall.h" // For internal syscall function. +#include "src/__support/common.h" + +#include "src/__support/macros/config.h" +#include "src/errno/libc_errno.h" +#include <linux/param.h> // For EXEC_PAGESIZE. +#include <stdarg.h> +#include <sys/syscall.h> // For syscall numbers. + +namespace LIBC_NAMESPACE_DECL { + +LLVM_LIBC_FUNCTION(void *, mremap, + (void *old_address, size_t old_size, size_t new_size, + int flags, ... /* void *new_address */)) { + + long ret = 0; + void *new_address = nullptr; + if (flags & MREMAP_FIXED) { + va_list varargs; + va_start(varargs, flags); + new_address = va_arg(varargs, void *); + va_end(varargs); + } + ret = LIBC_NAMESPACE::syscall_impl<long>(SYS_mremap, old_address, old_size, + new_size, flags, new_address); + + if (ret < 0 && ret > -EXEC_PAGESIZE) { + libc_errno = static_cast<int>(-ret); + return MAP_FAILED; + } + + return reinterpret_cast<void *>(ret); +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/sys/mman/mremap.h b/libc/src/sys/mman/mremap.h new file mode 100644 index 000000000000..208946bc58a2 --- /dev/null +++ b/libc/src/sys/mman/mremap.h @@ -0,0 +1,22 @@ +//===-- Implementation header for mremap function -------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SRC_SYS_MMAN_MREMAP_H +#define LLVM_LIBC_SRC_SYS_MMAN_MREMAP_H + +#include "src/__support/macros/config.h" +#include <sys/mman.h> // For size_t and off_t + +namespace LIBC_NAMESPACE_DECL { + +void *mremap(void *old_address, size_t old_size, size_t new_size, int flags, + ... /* void *new_address */); + +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SRC_SYS_MMAN_MREMAP_H |
