Update host_memory.cpp

This commit is contained in:
wheremyfoodat 2024-11-30 19:47:34 +02:00
parent 48e277c8c0
commit 4ec776f60e

View file

@ -5,6 +5,10 @@
#define ARCHITECTURE_arm64 #define ARCHITECTURE_arm64
#endif #endif
#ifndef __ANDROID__
#define USING_FD
#endif
#ifdef _WIN32 #ifdef _WIN32
#include <windows.h> #include <windows.h>
@ -21,33 +25,34 @@
#define _GNU_SOURCE #define _GNU_SOURCE
#endif #endif
#include <fcntl.h> #include <fcntl.h>
#include <host_memory/scope_exit.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <sys/random.h> #include <sys/random.h>
#include <unistd.h> #include <unistd.h>
#include <boost/icl/interval_set.hpp> #include <boost/icl/interval_set.hpp>
#include <host_memory/scope_exit.h>
#ifndef MAP_NORESERVE #ifndef MAP_NORESERVE
#define MAP_NORESERVE 0 #define MAP_NORESERVE 0
#endif #endif
#endif // ^^^ Linux ^^^ #ifdef USING_FD
#define MAYBE_ANONYMOUS(flags) (flags)
#ifdef __ANDROID__ #else
#include <android/sharedmem.h> #define MAYBE_ANONYMOUS(flags) (flags) | MAP_ANONYMOUS
#endif #endif
#endif // ^^^ Linux ^^^
#include <host_memory/free_region_manager.h>
#include <host_memory/host_memory.h>
#include <cstring> #include <cstring>
#include <memory>
#include <mutex> #include <mutex>
#include <random> #include <random>
#include <memory>
#include "align.hpp" #include "align.hpp"
#include <host_memory/host_memory.h>
#include <host_memory/free_region_manager.h>
#define ASSERT(...) #define ASSERT(...)
#define UNIMPLEMENTED_MSG(...) #define UNIMPLEMENTED_MSG(...)
@ -441,10 +446,10 @@ namespace Common {
#if defined(__FreeBSD__) && __FreeBSD__ < 13 #if defined(__FreeBSD__) && __FreeBSD__ < 13
// XXX Drop after FreeBSD 12.* reaches EOL on 2024-06-30 // XXX Drop after FreeBSD 12.* reaches EOL on 2024-06-30
fd = shm_open(SHM_ANON, O_RDWR, 0600); fd = shm_open(SHM_ANON, O_RDWR, 0600);
#elif defined(__ANDROID__) #elif defined(USING_FD)
fd = ASharedMemory_create("HostMemory", 0);
#else
fd = memfd_create("HostMemory", 0); fd = memfd_create("HostMemory", 0);
#else
fd = -1;
#endif #endif
#ifdef USING_FD #ifdef USING_FD
@ -460,7 +465,7 @@ namespace Common {
throw std::bad_alloc{}; throw std::bad_alloc{};
} }
#endif #endif
backing_base = static_cast<u8*>(mmap(nullptr, backing_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)); backing_base = static_cast<u8*>(mmap(nullptr, backing_size, PROT_READ | PROT_WRITE, MAYBE_ANONYMOUS(MAP_SHARED), fd, 0));
if (backing_base == MAP_FAILED) { if (backing_base == MAP_FAILED) {
Helpers::warn("mmap failed: {}", strerror(errno)); Helpers::warn("mmap failed: {}", strerror(errno));
@ -504,7 +509,7 @@ namespace Common {
} }
#endif #endif
void* ret = mmap(virtual_base + virtual_offset, length, flags, MAP_SHARED | MAP_FIXED, fd, host_offset); void* ret = mmap(virtual_base + virtual_offset, length, flags, MAYBE_ANONYMOUS(MAP_SHARED | MAP_FIXED), fd, host_offset);
ASSERT_MSG(ret != MAP_FAILED, "mmap failed: {}", strerror(errno)); ASSERT_MSG(ret != MAP_FAILED, "mmap failed: {}", strerror(errno));
} }
@ -634,7 +639,9 @@ namespace Common {
try { try {
// Try to allocate a fastmem arena. // Try to allocate a fastmem arena.
// The implementation will fail with std::bad_alloc on errors. // The implementation will fail with std::bad_alloc on errors.
impl = std::make_unique<HostMemory::Impl>(Common::alignUp(backing_size, PageAlignment), Common::alignUp(virtual_size, PageAlignment) + HugePageSize); impl = std::make_unique<HostMemory::Impl>(
Common::alignUp(backing_size, PageAlignment), Common::alignUp(virtual_size, PageAlignment) + HugePageSize
);
backing_base = impl->backing_base; backing_base = impl->backing_base;
virtual_base = impl->virtual_base; virtual_base = impl->virtual_base;