File indexing completed on 2024-11-15 09:00:50
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018 #ifndef ABSL_BASE_INTERNAL_DIRECT_MMAP_H_
0019 #define ABSL_BASE_INTERNAL_DIRECT_MMAP_H_
0020
0021 #include "absl/base/config.h"
0022
0023 #ifdef ABSL_HAVE_MMAP
0024
0025 #include <sys/mman.h>
0026
0027 #ifdef __linux__
0028
0029 #include <sys/types.h>
0030 #ifdef __BIONIC__
0031 #include <sys/syscall.h>
0032 #else
0033 #include <syscall.h>
0034 #endif
0035
0036 #include <linux/unistd.h>
0037 #include <unistd.h>
0038 #include <cerrno>
0039 #include <cstdarg>
0040 #include <cstdint>
0041
0042 #ifdef __mips__
0043
0044 #if defined(__BIONIC__) || !defined(__GLIBC__)
0045
0046
0047 #include <asm/sgidefs.h>
0048 #else
0049 #include <sgidefs.h>
0050 #endif
0051 #endif
0052
0053
0054 #ifdef __BIONIC__
0055 extern "C" void* __mmap2(void*, size_t, int, int, int, size_t);
0056 #if defined(__NR_mmap) && !defined(SYS_mmap)
0057 #define SYS_mmap __NR_mmap
0058 #endif
0059 #ifndef SYS_munmap
0060 #define SYS_munmap __NR_munmap
0061 #endif
0062 #endif
0063
0064 #if defined(__NR_mmap2) && !defined(SYS_mmap2)
0065 #define SYS_mmap2 __NR_mmap2
0066 #endif
0067
0068 namespace absl {
0069 ABSL_NAMESPACE_BEGIN
0070 namespace base_internal {
0071
0072
0073
0074 inline void* DirectMmap(void* start, size_t length, int prot, int flags, int fd,
0075 off_t offset) noexcept {
0076 #if defined(__i386__) || defined(__ARM_ARCH_3__) || defined(__ARM_EABI__) || \
0077 defined(__m68k__) || defined(__sh__) || \
0078 (defined(__hppa__) && !defined(__LP64__)) || \
0079 (defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI32) || \
0080 (defined(__PPC__) && !defined(__PPC64__)) || \
0081 (defined(__riscv) && __riscv_xlen == 32) || \
0082 (defined(__s390__) && !defined(__s390x__)) || \
0083 (defined(__sparc__) && !defined(__arch64__))
0084
0085 static int pagesize = 0;
0086 if (pagesize == 0) {
0087 #if defined(__wasm__) || defined(__asmjs__)
0088 pagesize = getpagesize();
0089 #else
0090 pagesize = sysconf(_SC_PAGESIZE);
0091 #endif
0092 }
0093 if (offset < 0 || offset % pagesize != 0) {
0094 errno = EINVAL;
0095 return MAP_FAILED;
0096 }
0097 #ifdef __BIONIC__
0098
0099
0100 return __mmap2(start, length, prot, flags, fd,
0101 static_cast<size_t>(offset / pagesize));
0102 #else
0103 return reinterpret_cast<void*>(
0104 syscall(SYS_mmap2, start, length, prot, flags, fd,
0105 static_cast<unsigned long>(offset / pagesize)));
0106 #endif
0107 #elif defined(__s390x__)
0108
0109 unsigned long buf[6] = {reinterpret_cast<unsigned long>(start),
0110 static_cast<unsigned long>(length),
0111 static_cast<unsigned long>(prot),
0112 static_cast<unsigned long>(flags),
0113 static_cast<unsigned long>(fd),
0114 static_cast<unsigned long>(offset)};
0115 return reinterpret_cast<void*>(syscall(SYS_mmap, buf));
0116 #elif defined(__x86_64__)
0117
0118
0119
0120
0121
0122
0123 #define MMAP_SYSCALL_ARG(x) ((uint64_t)(uintptr_t)(x))
0124 return reinterpret_cast<void*>(
0125 syscall(SYS_mmap, MMAP_SYSCALL_ARG(start), MMAP_SYSCALL_ARG(length),
0126 MMAP_SYSCALL_ARG(prot), MMAP_SYSCALL_ARG(flags),
0127 MMAP_SYSCALL_ARG(fd), static_cast<uint64_t>(offset)));
0128 #undef MMAP_SYSCALL_ARG
0129 #else
0130 static_assert(sizeof(unsigned long) == 8, "Platform is not 64-bit");
0131 return reinterpret_cast<void*>(
0132 syscall(SYS_mmap, start, length, prot, flags, fd, offset));
0133 #endif
0134 }
0135
0136 inline int DirectMunmap(void* start, size_t length) {
0137 return static_cast<int>(syscall(SYS_munmap, start, length));
0138 }
0139
0140 }
0141 ABSL_NAMESPACE_END
0142 }
0143
0144 #else
0145
0146
0147
0148
0149 namespace absl {
0150 ABSL_NAMESPACE_BEGIN
0151 namespace base_internal {
0152
0153 inline void* DirectMmap(void* start, size_t length, int prot, int flags, int fd,
0154 off_t offset) {
0155 return mmap(start, length, prot, flags, fd, offset);
0156 }
0157
0158 inline int DirectMunmap(void* start, size_t length) {
0159 return munmap(start, length);
0160 }
0161
0162 }
0163 ABSL_NAMESPACE_END
0164 }
0165
0166 #endif
0167
0168 #endif
0169
0170 #endif