[前][次][番号順一覧][スレッド一覧]

ruby-changes:65382

From: Peter <ko1@a...>
Date: Wed, 3 Mar 2021 03:05:07 +0900 (JST)
Subject: [ruby-changes:65382] 0bd1bc559f (master): Don't use mmap on platforms that have large OS page sizes

https://git.ruby-lang.org/ruby.git/commit/?id=0bd1bc559f

From 0bd1bc559f7a904e7fb64d41b98a9c27ddec7298 Mon Sep 17 00:00:00 2001
From: Peter Zhu <peter@p...>
Date: Mon, 1 Mar 2021 10:05:55 -0500
Subject: Don't use mmap on platforms that have large OS page sizes

---
 configure.ac |  2 ++
 gc.c         | 21 ++++++++++++++++++---
 2 files changed, 20 insertions(+), 3 deletions(-)

diff --git a/configure.ac b/configure.ac
index 911ebdf..1dcf48b 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1931,6 +1931,7 @@ AC_CHECK_FUNCS(lutimes) https://github.com/ruby/ruby/blob/trunk/configure.ac#L1931
 AC_CHECK_FUNCS(malloc_usable_size)
 AC_CHECK_FUNCS(malloc_size)
 AC_CHECK_FUNCS(mblen)
+AC_CHECK_FUNCS(memalign)
 AC_CHECK_FUNCS(memset_s)
 AC_CHECK_FUNCS(writev)
 AC_CHECK_FUNCS(memrchr)
@@ -1943,6 +1944,7 @@ AC_CHECK_FUNCS(openat) https://github.com/ruby/ruby/blob/trunk/configure.ac#L1944
 AC_CHECK_FUNCS(pipe2)
 AC_CHECK_FUNCS(poll)
 AC_CHECK_FUNCS(posix_fadvise)
+AC_CHECK_FUNCS(posix_memalign)
 AC_CHECK_FUNCS(ppoll)
 AC_CHECK_FUNCS(pread)
 AC_CHECK_FUNCS(pwrite)
diff --git a/gc.c b/gc.c
index 30ded93..b9f49db 100644
--- a/gc.c
+++ b/gc.c
@@ -823,6 +823,13 @@ enum { https://github.com/ruby/ruby/blob/trunk/gc.c#L823
     HEAP_PAGE_BITMAP_SIZE = (BITS_SIZE * HEAP_PAGE_BITMAP_LIMIT),
     HEAP_PAGE_BITMAP_PLANES = 4 /* RGENGC: mark, unprotected, uncollectible, marking */
 };
+#define HEAP_PAGE_ALIGN (1 << HEAP_PAGE_ALIGN_LOG)
+#define HEAP_PAGE_SIZE HEAP_PAGE_ALIGN
+#if defined(HAVE_MMAP) && (PAGE_SIZE <= HEAP_PAGE_SIZE)
+# define USE_MMAP_ALIGNED_ALLOC 1
+#else
+# define USE_MMAP_ALIGNED_ALLOC 0
+#endif
 
 struct heap_page {
     short total_slots;
@@ -9989,7 +9996,7 @@ gc_set_auto_compact(rb_execution_context_t *ec, VALUE _, VALUE v) https://github.com/ruby/ruby/blob/trunk/gc.c#L9996
 
     /* If not MinGW, Windows, or does not have mmap, we cannot use mprotect for
      * the read barrier, so we must disable automatic compaction. */
-#if !defined(__MINGW32__) && !defined(_WIN32) && !defined(HAVE_MMAP)
+#if !defined(__MINGW32__) && !defined(_WIN32) && !USE_MMAP_ALIGNED_ALLOC
     rb_raise(rb_eNotImpError, "Automatic compaction isn't available on this platform");
 #endif
 
@@ -10389,7 +10396,7 @@ rb_aligned_malloc(size_t alignment, size_t size) https://github.com/ruby/ruby/blob/trunk/gc.c#L10396
 #elif defined _WIN32
     void *_aligned_malloc(size_t, size_t);
     res = _aligned_malloc(size, alignment);
-#elif defined(HAVE_MMAP)
+#elif USE_MMAP_ALIGNED_ALLOC
     GC_ASSERT(alignment % sysconf(_SC_PAGE_SIZE) == 0);
 
     char *ptr = mmap(NULL, alignment + size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
@@ -10419,6 +10426,12 @@ rb_aligned_malloc(size_t alignment, size_t size) https://github.com/ruby/ruby/blob/trunk/gc.c#L10426
     }
 
     res = (void *)aligned;
+#elif defined(HAVE_POSIX_MEMALIGN)
+    if (posix_memalign(&res, alignment, size) != 0) {
+        return NULL;
+    }
+#elif defined(HAVE_MEMALIGN)
+    res = memalign(alignment, size);
 #else
     char* aligned;
     res = malloc(alignment + size + sizeof(void*));
@@ -10441,11 +10454,13 @@ rb_aligned_free(void *ptr, size_t size) https://github.com/ruby/ruby/blob/trunk/gc.c#L10454
     __mingw_aligned_free(ptr);
 #elif defined _WIN32
     _aligned_free(ptr);
-#elif defined HAVE_MMAP
+#elif USE_MMAP_ALIGNED_ALLOC
     GC_ASSERT(size % sysconf(_SC_PAGE_SIZE) == 0);
     if (munmap(ptr, size)) {
         rb_bug("rb_aligned_free: munmap failed");
     }
+#elif defined(HAVE_POSIX_MEMALIGN) || defined(HAVE_MEMALIGN)
+    free(ptr);
 #else
     free(((void**)ptr)[-1]);
 #endif
-- 
cgit v1.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]