Commit 6e525dbc by Nicolas Capens Committed by Nicolas Capens

Allow configuring Reactor's anonymous mmap name

This removes the explicit mention of SwiftShader in Reactor's code, while still allowing to set a name that identifies the usage precisely. Bug: b/174801963 Change-Id: Ic6cdd9d36ef9093aea62f571ecd37334ec32d3f2 Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/50890Reviewed-by: 's avatarAntonio Maiorano <amaiorano@google.com> Tested-by: 's avatarNicolas Capens <nicolascapens@google.com>
parent 8bab33e3
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
# ifndef WIN32_LEAN_AND_MEAN # ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN # define WIN32_LEAN_AND_MEAN
# endif # endif
# include <windows.h> # include <Windows.h>
# include <intrin.h> # include <intrin.h>
#elif defined(__Fuchsia__) #elif defined(__Fuchsia__)
# include <unistd.h> # include <unistd.h>
...@@ -55,7 +55,7 @@ void *allocateRaw(size_t bytes, size_t alignment) ...@@ -55,7 +55,7 @@ void *allocateRaw(size_t bytes, size_t alignment)
{ {
ASSERT((alignment & (alignment - 1)) == 0); // Power of 2 alignment. ASSERT((alignment & (alignment - 1)) == 0); // Power of 2 alignment.
#if defined(LINUX_ENABLE_NAMED_MMAP) #if defined(REACTOR_ANONYMOUS_MMAP_NAME)
if(alignment < sizeof(void *)) if(alignment < sizeof(void *))
{ {
return malloc(bytes); return malloc(bytes);
...@@ -128,11 +128,11 @@ int permissionsToMmapProt(int permissions) ...@@ -128,11 +128,11 @@ int permissionsToMmapProt(int permissions)
} }
#endif // !defined(_WIN32) && !defined(__Fuchsia__) #endif // !defined(_WIN32) && !defined(__Fuchsia__)
#if defined(LINUX_ENABLE_NAMED_MMAP) #if defined(REACTOR_ANONYMOUS_MMAP_NAME)
// Create a file descriptor for anonymous memory with the given // Create a file descriptor for anonymous memory with the given
// name. Returns -1 on failure. // name. Returns -1 on failure.
// TODO: remove once libc wrapper exists. // TODO: remove once libc wrapper exists.
int memfd_create(const char *name, unsigned int flags) static int memfd_create(const char *name, unsigned int flags)
{ {
# if __aarch64__ # if __aarch64__
# define __NR_memfd_create 279 # define __NR_memfd_create 279
...@@ -159,7 +159,7 @@ int memfd_create(const char *name, unsigned int flags) ...@@ -159,7 +159,7 @@ int memfd_create(const char *name, unsigned int flags)
// MAP_PRIVATE so that underlying pages aren't shared. // MAP_PRIVATE so that underlying pages aren't shared.
int anonymousFd() int anonymousFd()
{ {
static int fd = memfd_create("SwiftShader JIT", 0); static int fd = memfd_create(#REACTOR_ANONYMOUS_MMAP_NAME, 0);
return fd; return fd;
} }
...@@ -173,7 +173,7 @@ void ensureAnonFileSize(int anonFd, size_t length) ...@@ -173,7 +173,7 @@ void ensureAnonFileSize(int anonFd, size_t length)
fileSize = length; fileSize = length;
} }
} }
#endif // defined(LINUX_ENABLE_NAMED_MMAP) #endif // defined(REACTOR_ANONYMOUS_MMAP_NAME)
#if defined(__Fuchsia__) #if defined(__Fuchsia__)
zx_vm_option_t permissionsToZxVmOptions(int permissions) zx_vm_option_t permissionsToZxVmOptions(int permissions)
...@@ -226,7 +226,7 @@ void *allocate(size_t bytes, size_t alignment) ...@@ -226,7 +226,7 @@ void *allocate(size_t bytes, size_t alignment)
void deallocate(void *memory) void deallocate(void *memory)
{ {
#if defined(LINUX_ENABLE_NAMED_MMAP) #if defined(REACTOR_ANONYMOUS_MMAP_NAME)
free(memory); free(memory);
#else #else
if(memory) if(memory)
...@@ -252,7 +252,7 @@ void *allocateMemoryPages(size_t bytes, int permissions, bool need_exec) ...@@ -252,7 +252,7 @@ void *allocateMemoryPages(size_t bytes, int permissions, bool need_exec)
size_t length = roundUp(bytes, pageSize); size_t length = roundUp(bytes, pageSize);
void *mapping = nullptr; void *mapping = nullptr;
#if defined(LINUX_ENABLE_NAMED_MMAP) #if defined(REACTOR_ANONYMOUS_MMAP_NAME)
int flags = MAP_PRIVATE; int flags = MAP_PRIVATE;
// Try to name the memory region for the executable code, // Try to name the memory region for the executable code,
...@@ -329,7 +329,10 @@ void *allocateMemoryPages(size_t bytes, int permissions, bool need_exec) ...@@ -329,7 +329,10 @@ void *allocateMemoryPages(size_t bytes, int permissions, bool need_exec)
void protectMemoryPages(void *memory, size_t bytes, int permissions) void protectMemoryPages(void *memory, size_t bytes, int permissions)
{ {
if(bytes == 0) if(bytes == 0)
{
return; return;
}
bytes = roundUp(bytes, memoryPageSize()); bytes = roundUp(bytes, memoryPageSize());
#if defined(_WIN32) #if defined(_WIN32)
...@@ -358,7 +361,7 @@ void deallocateMemoryPages(void *memory, size_t bytes) ...@@ -358,7 +361,7 @@ void deallocateMemoryPages(void *memory, size_t bytes)
VirtualProtect(memory, bytes, PAGE_READWRITE, &oldProtection); VirtualProtect(memory, bytes, PAGE_READWRITE, &oldProtection);
ASSERT(result); ASSERT(result);
deallocate(memory); deallocate(memory);
#elif defined(LINUX_ENABLE_NAMED_MMAP) || defined(__APPLE__) #elif defined(__APPLE__) || defined(REACTOR_ANONYMOUS_MMAP_NAME)
size_t pageSize = memoryPageSize(); size_t pageSize = memoryPageSize();
size_t length = (bytes + pageSize - 1) & ~(pageSize - 1); size_t length = (bytes + pageSize - 1) & ~(pageSize - 1);
int result = munmap(memory, length); int result = munmap(memory, length);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment