From: Matthew Fernandez Date: Sun, 16 Oct 2022 21:48:06 +0000 (-0700) Subject: sfio: use a more portable way of determining page size X-Git-Tag: 7.0.2~19^2~1 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=fc890507da4c25ceb0493c5cccbd1648fe0338c8;p=graphviz sfio: use a more portable way of determining page size This code was using a legacy API, `getpagesize`, to opportunistically find the system virtual memory page size. This change updates to the newer POSIX `sysconf` API that is available almost everywhere, and adds a workaround for Windows. This code now has an accurate understanding of the page size on platforms that did not have `getpagesize` (of which, Windows was one), rather than falling back on 8192. --- diff --git a/cmake/config_checks.cmake b/cmake/config_checks.cmake index 135df6b5e..ab4150d95 100644 --- a/cmake/config_checks.cmake +++ b/cmake/config_checks.cmake @@ -20,7 +20,6 @@ include(CheckFunctionExists) check_function_exists( dl_iterate_phdr HAVE_DL_ITERATE_PHDR ) check_function_exists( drand48 HAVE_DRAND48 ) -check_function_exists( getpagesize HAVE_GETPAGESIZE ) check_function_exists( lrand48 HAVE_LRAND48 ) check_function_exists( setenv HAVE_SETENV ) check_function_exists( setmode HAVE_SETMODE ) diff --git a/config-cmake.h.in b/config-cmake.h.in index 485450354..af276ecac 100644 --- a/config-cmake.h.in +++ b/config-cmake.h.in @@ -16,7 +16,6 @@ // Functions #cmakedefine HAVE_DL_ITERATE_PHDR #cmakedefine HAVE_DRAND48 -#cmakedefine HAVE_GETPAGESIZE #cmakedefine HAVE_LRAND48 #cmakedefine HAVE_SETENV #cmakedefine HAVE_SETMODE diff --git a/configure.ac b/configure.ac index 9c667e714..82f7e4a15 100644 --- a/configure.ac +++ b/configure.ac @@ -465,7 +465,6 @@ LIBS=$save_LIBS # Checks for library functions AC_CHECK_FUNCS([lrand48 drand48 srand48 setmode setenv \ - getpagesize \ ftruncate lseek64 stat64 select dl_iterate_phdr]) AC_REPLACE_FUNCS([strcasestr]) diff --git a/lib/sfio/sfhdr.h b/lib/sfio/sfhdr.h index 8c80b1b95..0ca76f3f0 100644 --- a/lib/sfio/sfhdr.h +++ b/lib/sfio/sfhdr.h @@ -313,7 +313,6 @@ extern "C" { /* grain size for buffer increment */ #define SF_GRAIN 1024 -#define SF_PAGE ((ssize_t)(SF_GRAIN*sizeof(int)*2)) /* when the buffer is empty, certain io requests may be better done directly on the given application buffers. The below condition determines when. diff --git a/lib/sfio/sfsetbuf.c b/lib/sfio/sfsetbuf.c index a4dceb0af..2e219b9f2 100644 --- a/lib/sfio/sfsetbuf.c +++ b/lib/sfio/sfsetbuf.c @@ -10,10 +10,23 @@ #include #include +#include -#ifdef HAVE_GETPAGESIZE - extern int getpagesize(void); +#ifdef _WIN32 +#include +#include #endif + +static ssize_t get_page_size(void) { +#ifdef _WIN32 + SYSTEM_INFO info; + GetSystemInfo(&info); + return (ssize_t)info.dwPageSize; +#else + return (ssize_t)sysconf(_SC_PAGESIZE); +#endif +} + /* Set a (new) buffer for a stream. ** If size < 0, it is assigned a suitable value depending on the ** kind of stream. The actual buffer size allocated is dependent @@ -185,10 +198,7 @@ void *sfsetbuf(Sfio_t * f, void * buf, size_t size) /* set page size, this is also the desired default buffer size */ if (_Sfpage <= 0) { -#ifdef HAVE_GETPAGESIZE - if ((_Sfpage = (size_t) getpagesize()) <= 0) -#endif - _Sfpage = SF_PAGE; + _Sfpage = get_page_size(); } }