aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorMITSUNARI Shigeo <[email protected]>2021-08-15 17:28:12 +0900
committerMITSUNARI Shigeo <[email protected]>2021-08-15 17:28:12 +0900
commitc7bb6638376b68e454992435cdf208fb37448cad (patch)
tree02fd5df2ff6ae9ae57e2ddeaa98f670db4ccf1ab
parentccd4130ec1e7bbdcd897acbb1f503317c8ca4d65 (diff)
downloadxbyak-c7bb6638376b68e454992435cdf208fb37448cad.tar.gz
xbyak-c7bb6638376b68e454992435cdf208fb37448cad.zip
fix for mac and enable it if XBYAK_MEMFD_CREATE is defined
-rw-r--r--xbyak/xbyak.h26
1 files changed, 10 insertions, 16 deletions
diff --git a/xbyak/xbyak.h b/xbyak/xbyak.h
index 503a3fa..bb57007 100644
--- a/xbyak/xbyak.h
+++ b/xbyak/xbyak.h
@@ -79,7 +79,6 @@
#include <malloc.h>
#define XBYAK_TLS __declspec(thread)
#elif defined(__GNUC__)
- #include <syscall.h>
#include <unistd.h>
#include <sys/mman.h>
#include <stdlib.h>
@@ -96,6 +95,10 @@
#include <stdint.h>
#endif
+#if !defined(MFD_CLOEXEC) // defined only linux 3.17 or later
+ #undef XBYAK_MEMFD_CREATE
+#endif
+
#if defined(_WIN64) || defined(__MINGW64__) || (defined(__CYGWIN__) && defined(__x86_64__))
#define XBYAK64_WIN
#elif defined(__x86_64__)
@@ -414,26 +417,11 @@ inline int getMacOsVersion()
class MmapAllocator : Allocator {
typedef XBYAK_STD_UNORDERED_MAP<uintptr_t, size_t> SizeList;
SizeList sizeList_;
-
- static int memfd_fd()
- {
-#ifdef __NR_memfd_create
-#ifdef MFD_CLOEXEC
- int flags = MFD_CLOEXEC;
-#else
- int flags = 0;
-#endif
- return syscall(__NR_memfd_create, "xbyak", flags);
-#else
- return -1;
-#endif
- }
public:
uint8_t *alloc(size_t size)
{
const size_t alignedSizeM1 = inner::ALIGN_PAGE_SIZE - 1;
size = (size + alignedSizeM1) & ~alignedSizeM1;
- int fd = memfd_fd();
#if defined(MAP_ANONYMOUS)
int mode = MAP_PRIVATE | MAP_ANONYMOUS;
#elif defined(MAP_ANON)
@@ -445,12 +433,18 @@ public:
const int mojaveVersion = 18;
if (util::getMacOsVersion() >= mojaveVersion) mode |= MAP_JIT;
#endif
+ int fd = -1;
+#if defined(XBYAK_MEMFD_CREATE) && XBYAK_MEMFD_CREATE == 1
+ fd = memfd_create("xbyak", MFD_CLOEXEC);
if (fd != -1) {
mode = MAP_SHARED;
if (ftruncate(fd, size) != 0) XBYAK_THROW_RET(ERR_CANT_ALLOC, 0)
}
+#endif
void *p = mmap(NULL, size, PROT_READ | PROT_WRITE, mode, fd, 0);
+#if defined(XBYAK_MEMFD_CREATE) && XBYAK_MEMFD_CREATE == 1
if (fd != -1) close(fd);
+#endif
if (p == MAP_FAILED) XBYAK_THROW_RET(ERR_CANT_ALLOC, 0)
assert(p);
sizeList_[(uintptr_t)p] = size;