1 # SPDX-License-Identifier: 0BSD
3 #############################################################################
11 # Check how to get the amount of physical memory.
12 # This information is used in tuklib_physmem.c.
16 # - Windows (including Cygwin), OS/2, DJGPP (DOS), OpenVMS, AROS,
17 # and QNX have operating-system specific functions.
19 # - AIX has _system_configuration.physmem.
21 # - sysconf() works on GNU/Linux and Solaris, and possibly on
24 # - BSDs use sysctl().
26 # - Tru64 uses getsysinfo().
28 # - HP-UX uses pstat_getstatic().
30 # - IRIX has setinvent_r(), getinvent_r(), and endinvent_r().
32 # - sysinfo() works on Linux/dietlibc and probably on other Linux
33 # systems whose libc may lack sysconf().
35 #############################################################################
37 # Author: Lasse Collin
39 #############################################################################
41 AC_DEFUN_ONCE([TUKLIB_PHYSMEM], [
42 AC_REQUIRE([TUKLIB_COMMON])
44 # sys/param.h might be needed by sys/sysctl.h.
45 AC_CHECK_HEADERS([sys/param.h])
47 AC_CACHE_CHECK([how to detect the amount of physical memory],
48 [tuklib_cv_physmem_method], [
50 # Maybe checking $host_os would be enough but this matches what
51 # tuklib_physmem.c does.
53 # NOTE: IRIX has a compiler that doesn't error out with #error, so use
54 # a non-compilable text instead of #error to generate an error.
55 AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
56 #if defined(_WIN32) || defined(__CYGWIN__) || defined(__OS2__) \
57 || defined(__DJGPP__) || defined(__VMS) \
58 || defined(AMIGA) || defined(__AROS__) || defined(__QNX__)
59 int main(void) { return 0; }
63 ]])], [tuklib_cv_physmem_method=special], [
65 # Look for AIX-specific solution before sysconf(), because the test
66 # for sysconf() will pass on AIX but won't actually work
67 # (sysconf(_SC_PHYS_PAGES) compiles but always returns -1 on AIX).
69 # NOTE: There is no need to link the check program because it's not calling
70 # any functions and thus implicit function declarations aren't a problem.
71 # The unused reference to _system_configuration.physmem might get optimized
72 # away, and thus the linker might not see that symbol anyway.
73 AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
74 #include <sys/systemcfg.h>
79 (void)_system_configuration.physmem;
82 ]])], [tuklib_cv_physmem_method=aix], [
84 AC_LINK_IFELSE([AC_LANG_SOURCE([[
90 i = sysconf(_SC_PAGESIZE);
91 i = sysconf(_SC_PHYS_PAGES);
94 ]])], [tuklib_cv_physmem_method=sysconf], [
96 AC_LINK_IFELSE([AC_LANG_SOURCE([[
97 #ifdef HAVE_SYS_PARAM_H
98 # include <sys/param.h>
100 #include <sys/sysctl.h>
104 int name[2] = { CTL_HW, HW_PHYSMEM };
106 size_t mem_ptr_size = sizeof(mem);
107 sysctl(name, 2, &mem, &mem_ptr_size, NULL, 0);
110 ]])], [tuklib_cv_physmem_method=sysctl], [
112 AC_LINK_IFELSE([AC_LANG_SOURCE([[
113 #include <sys/sysinfo.h>
114 #include <machine/hal_sysinfo.h>
121 getsysinfo(GSI_PHYSMEM, (caddr_t)&memkb, sizeof(memkb), &start);
124 ]])], [tuklib_cv_physmem_method=getsysinfo],[
126 AC_LINK_IFELSE([AC_LANG_SOURCE([[
127 #include <sys/param.h>
128 #include <sys/pstat.h>
133 struct pst_static pst;
134 pstat_getstatic(&pst, sizeof(pst), 1, 0);
135 (void)pst.physical_memory;
139 ]])], [tuklib_cv_physmem_method=pstat_getstatic],[
141 AC_LINK_IFELSE([AC_LANG_SOURCE([[
146 inv_state_t *st = NULL;
152 ]])], [tuklib_cv_physmem_method=getinvent_r], [
154 # This version of sysinfo() is Linux-specific. Some non-Linux systems have
155 # different sysinfo() so we must check $host_os.
158 AC_LINK_IFELSE([AC_LANG_SOURCE([[
159 #include <sys/sysinfo.h>
168 tuklib_cv_physmem_method=sysinfo
170 tuklib_cv_physmem_method=unknown
174 tuklib_cv_physmem_method=unknown
179 case $tuklib_cv_physmem_method in
181 AC_DEFINE([TUKLIB_PHYSMEM_AIX], [1],
182 [Define to 1 if the amount of physical memory
183 can be detected with _system_configuration.physmem.])
186 AC_DEFINE([TUKLIB_PHYSMEM_SYSCONF], [1],
187 [Define to 1 if the amount of physical memory can
188 be detected with sysconf(_SC_PAGESIZE) and
189 sysconf(_SC_PHYS_PAGES).])
192 AC_DEFINE([TUKLIB_PHYSMEM_SYSCTL], [1],
193 [Define to 1 if the amount of physical memory can
194 be detected with sysctl().])
197 AC_DEFINE([TUKLIB_PHYSMEM_GETSYSINFO], [1],
198 [Define to 1 if the amount of physical memory can
199 be detected with getsysinfo().])
202 AC_DEFINE([TUKLIB_PHYSMEM_PSTAT_GETSTATIC], [1],
203 [Define to 1 if the amount of physical memory can
204 be detected with pstat_getstatic().])
207 AC_DEFINE([TUKLIB_PHYSMEM_GETINVENT_R], [1],
208 [Define to 1 if the amount of physical memory
209 can be detected with getinvent_r().])
212 AC_DEFINE([TUKLIB_PHYSMEM_SYSINFO], [1],
213 [Define to 1 if the amount of physical memory
214 can be detected with Linux sysinfo().])