18 #ifndef ABSL_BASE_INTERNAL_DIRECT_MMAP_H_ 19 #define ABSL_BASE_INTERNAL_DIRECT_MMAP_H_ 29 #include <sys/types.h> 31 #include <sys/syscall.h> 36 #include <linux/unistd.h> 47 #include <asm/sgidefs.h> 55 extern "C" void* __mmap2(
void*,
size_t,
int,
int,
int,
size_t);
56 #if defined(__NR_mmap) && !defined(SYS_mmap) 57 #define SYS_mmap __NR_mmap 60 #define SYS_munmap __NR_munmap 65 namespace base_internal {
69 inline void* DirectMmap(
void* start,
size_t length,
int prot,
int flags,
int fd,
70 off64_t offset) noexcept {
71 #if defined(__i386__) || defined(__ARM_ARCH_3__) || defined(__ARM_EABI__) || \ 72 (defined(__mips__) && _MIPS_SIM == _MIPS_SIM_ABI32) || \ 73 (defined(__PPC__) && !defined(__PPC64__)) || \ 74 (defined(__s390__) && !defined(__s390x__)) 76 static int pagesize = 0;
78 #if defined(__wasm__) || defined(__asmjs__) 79 pagesize = getpagesize();
81 pagesize = sysconf(_SC_PAGESIZE);
84 if (offset < 0 || offset % pagesize != 0) {
91 return __mmap2(start,
length, prot,
flags, fd, offset / pagesize);
93 return reinterpret_cast<void*
>(
95 static_cast<off_t>(offset / pagesize)));
97 #elif defined(__s390x__) 99 unsigned long buf[6] = {
reinterpret_cast<unsigned long>(start),
100 static_cast<unsigned long>(
length),
101 static_cast<unsigned long>(prot),
102 static_cast<unsigned long>(
flags),
103 static_cast<unsigned long>(fd),
104 static_cast<unsigned long>(offset)};
105 return reinterpret_cast<void*
>(syscall(SYS_mmap, buf));
106 #elif defined(__x86_64__) 113 #define MMAP_SYSCALL_ARG(x) ((uint64_t)(uintptr_t)(x)) 114 return reinterpret_cast<void*
>(
115 syscall(SYS_mmap, MMAP_SYSCALL_ARG(start), MMAP_SYSCALL_ARG(
length),
116 MMAP_SYSCALL_ARG(prot), MMAP_SYSCALL_ARG(
flags),
117 MMAP_SYSCALL_ARG(fd), static_cast<uint64_t>(offset)));
118 #undef MMAP_SYSCALL_ARG 119 #else // Remaining 64-bit aritectures. 120 static_assert(
sizeof(
unsigned long) == 8,
"Platform is not 64-bit");
121 return reinterpret_cast<void*
>(
122 syscall(SYS_mmap, start,
length, prot,
flags, fd, offset));
126 inline int DirectMunmap(
void* start,
size_t length) {
127 return static_cast<int>(syscall(SYS_munmap, start, length));
139 namespace base_internal {
141 inline void* DirectMmap(
void* start,
size_t length,
int prot,
int flags,
int fd,
143 return mmap(start, length, prot, flags, fd, offset);
146 inline int DirectMunmap(
void* start,
size_t length) {
147 return munmap(start, length);
155 #endif // ABSL_HAVE_MMAP 157 #endif // ABSL_BASE_INTERNAL_DIRECT_MMAP_H_