Fix PAGE_SIZE macro detection in autoconf

The current fix for PAGE_SIZE macro detection in autoconf does not work
correctly. I see the following output with running configure on Linux:

```
checking PAGE_SIZE is defined... no
```

Linux has PAGE_SIZE macro. This is happening because the macro exists in
sys/user.h and not in the malloc headers.
This commit is contained in:
Peter Zhu 2021-05-05 16:20:40 +00:00
parent 1921500511
commit 23a98237df
Notes: git 2021-05-06 04:31:54 +09:00
2 changed files with 29 additions and 27 deletions

View File

@ -2727,23 +2727,26 @@ main(int argc, char *argv[])
test x$rb_cv_fork_with_pthread = xyes || AC_DEFINE(CANNOT_FORK_WITH_PTHREAD)
])
AS_IF([test "x$ac_cv_func_mmap" = xyes], [
malloc_headers=`sed -n '/MALLOC_HEADERS_BEGIN/,/MALLOC_HEADERS_END/p' ${srcdir}/gc.c`
AC_CHECK_HEADERS([sys/user.h])
AS_IF([test "x$ac_cv_func_mmap:$ac_cv_header_sys_user_h" = xyes:yes], [
AC_CACHE_CHECK([PAGE_SIZE is defined], rb_cv_page_size,
[AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[${malloc_headers}
[AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
@%:@include <sys/user.h>
typedef char conftest_page[PAGE_SIZE];
]], [[]])],
[rb_cv_page_size=const],
[AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[${malloc_headers}]], [[
[AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
@%:@include <sys/user.h>
]], [[
int page_size = (int)PAGE_SIZE;
(void)page_size;
]])],
[rb_cv_page_size=variable], [rb_cv_page_size=no])])])
AS_CASE([$rb_cv_page_size],
[const], [AC_DEFINE(USE_MMAP_ALIGNED_ALLOC, [(PAGE_SIZE <= HEAP_PAGE_SIZE)])],
[no], [AC_DEFINE(USE_MMAP_ALIGNED_ALLOC, 1)],
AS_IF([test "x$rb_cv_page_size" = xconst],
[AC_DEFINE(HAVE_CONST_PAGE_SIZE, 1)],
[AC_DEFINE(HAVE_CONST_PAGE_SIZE, 0)]
)
], [AC_DEFINE(USE_MMAP_ALIGNED_ALLOC, 0)])
], [AC_DEFINE(HAVE_CONST_PAGE_SIZE, 0)])
}
: "runtime section" && {

37
gc.c
View File

@ -32,7 +32,6 @@
#include <stdarg.h>
#include <stdio.h>
/* MALLOC_HEADERS_BEGIN */
#ifndef HAVE_MALLOC_USABLE_SIZE
# ifdef _WIN32
# define HAVE_MALLOC_USABLE_SIZE
@ -54,7 +53,6 @@
# include <malloc/malloc.h>
# endif
#endif
/* MALLOC_HEADERS_END */
#ifdef HAVE_SYS_TIME_H
# include <sys/time.h>
@ -73,6 +71,10 @@
#include <sys/types.h>
#ifdef HAVE_SYS_USER_H
# include <sys/user.h>
#endif
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif
@ -832,23 +834,17 @@ enum {
};
#define HEAP_PAGE_ALIGN (1 << HEAP_PAGE_ALIGN_LOG)
#define HEAP_PAGE_SIZE HEAP_PAGE_ALIGN
#ifdef USE_MMAP_ALIGNED_ALLOC
# define Init_use_mmap_aligned_alloc() (void)0
#elif !defined(HAVE_MMAP)
# define USE_MMAP_ALIGNED_ALLOC 0
# define Init_use_mmap_aligned_alloc() (void)0
#elif defined(PAGE_MAX_SIZE) && (PAGE_MAX_SIZE <= HEAP_PAGE_SIZE)
/* PAGE_SIZE <= HEAP_PAGE_SIZE */
# define USE_MMAP_ALIGNED_ALLOC 1
# define Init_use_mmap_aligned_alloc() (void)0
#else
# define USE_MMAP_ALIGNED_ALLOC (use_mmap_aligned_alloc != false)
#if defined(HAVE_MMAP) && defined(PAGE_SIZE)
# if HAVE_CONST_PAGE_SIZE
# define USE_MMAP_ALIGNED_ALLOC (PAGE_SIZE <= HEAP_PAGE_SIZE)
# else
# define USE_MMAP_ALIGNED_ALLOC (use_mmap_aligned_alloc != false)
static bool use_mmap_aligned_alloc;
static inline void
Init_use_mmap_aligned_alloc(void)
{
use_mmap_aligned_alloc = (PAGE_SIZE <= HEAP_PAGE_SIZE);
}
# endif
#else
# define USE_MMAP_ALIGNED_ALLOC 0
#endif
struct heap_page {
@ -3212,7 +3208,10 @@ Init_heap(void)
{
rb_objspace_t *objspace = &rb_objspace;
Init_use_mmap_aligned_alloc();
#if defined(HAVE_MMAP) && defined(PAGE_SIZE) && !HAVE_CONST_PAGE_SIZE
use_mmap_aligned_alloc = PAGE_SIZE <= HEAP_PAGE_SIZE;
#endif
#if defined(HAVE_SYSCONF) && defined(_SC_PAGE_SIZE)
/* If Ruby's heap pages are not a multiple of the system page size, we
* cannot use mprotect for the read barrier, so we must disable automatic