[PATCH] Core: use sysconf(_SC_PAGESIZE) instead of getpagesize()

Ruslan Ermilov ru at nginx.com
Fri Dec 4 07:00:45 UTC 2015


On Thu, Dec 03, 2015 at 07:24:51PM -0800, Piotr Sikora wrote:
> Hey Maxim,
> 
> > Any practical reasons for the change?
> 
> Kind of... OS X doesn't expose getpagesize() when it's compiled with
> _POSIX_C_SOURCE >= 200112L and no other define (not even
> _DARWIN_C_SOURCE) can change that.
> 
> Side note: I considered adding _DARWIN_C_SOURCE to
> ngx_darwin_config.h, like it's done for _GNU_SOURCE, but I decided
> against it, because I found the ability to enforce strict POSIX
> interface (even if it doesn't really work right now) quite handy...
> and it's off by default, so someone needs to force it anyway.
> 
> > While getpagesize() isn't required by the current issue of POSIX,
> > it is easier to use and used to be more widely available than
> > sysconf(_SC_PAGESIZE).
> 
> Considering that sysconf(_SC_PAGESIZE) is part of POSIX and
> getpagesize() isn't, I don't believe this is true anymore.
> 
> > The cast looks unneeded.
> 
> It's signed-to-unsigned conversion... it's a little bit on the
> pedantic side, but I wouldn't say it's unneeded... and it follows
> style from similar cast just few lines below.

diff --git a/auto/unix b/auto/unix
--- a/auto/unix
+++ b/auto/unix
@@ -842,6 +842,16 @@ ngx_feature_test="sysconf(_SC_NPROCESSOR
 . auto/feature
 
 
+ngx_feature="sysconf(_SC_PAGESIZE)"
+ngx_feature_name="NGX_HAVE_SC_PAGESIZE"
+ngx_feature_run=no
+ngx_feature_incs=
+ngx_feature_path=
+ngx_feature_libs=
+ngx_feature_test="sysconf(_SC_PAGESIZE)"
+. auto/feature
+
+
 ngx_feature="openat(), fstatat()"
 ngx_feature_name="NGX_HAVE_OPENAT"
 ngx_feature_run=no
diff --git a/src/os/unix/ngx_posix_init.c b/src/os/unix/ngx_posix_init.c
--- a/src/os/unix/ngx_posix_init.c
+++ b/src/os/unix/ngx_posix_init.c
@@ -44,7 +44,12 @@ ngx_os_init(ngx_log_t *log)
         return NGX_ERROR;
     }
 
+#if (NGX_HAVE_SC_PAGESIZE)
+    ngx_pagesize = sysconf(_SC_PAGESIZE);
+#else
     ngx_pagesize = getpagesize();
+#endif
+
     ngx_cacheline_size = NGX_CPU_CACHE_LINE;
 
     for (n = ngx_pagesize; n >>= 1; ngx_pagesize_shift++) { /* void */ }



More information about the nginx-devel mailing list