diff options
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/cpucores.h | 52 | ||||
-rw-r--r-- | src/common/physmem.h | 21 |
2 files changed, 65 insertions, 8 deletions
diff --git a/src/common/cpucores.h b/src/common/cpucores.h new file mode 100644 index 00000000..7e1a1438 --- /dev/null +++ b/src/common/cpucores.h @@ -0,0 +1,52 @@ +/////////////////////////////////////////////////////////////////////////////// +// +/// \file cpucores.h +/// \brief Get the number of online CPU cores +// +// This code has been put into the public domain. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +// +/////////////////////////////////////////////////////////////////////////////// + +#ifndef CPUCORES_H +#define CPUCORES_H + +#if defined(HAVE_NCPU_SYSCONF) +# include <unistd.h> + +#elif defined(HAVE_NCPU_SYSCTL) +# ifdef HAVE_SYS_PARAM_H +# include <sys/param.h> +# endif +# ifdef HAVE_SYS_SYSCTL_H +# include <sys/sysctl.h> +# endif +#endif + + +static inline uint32_t +cpucores(void) +{ + uint32_t ret = 0; + +#if defined(HAVE_CPUCORES_SYSCONF) + const long cpus = sysconf(_SC_NPROCESSORS_ONLN); + if (cpus > 0) + ret = (uint32_t)(cpus); + +#elif defined(HAVE_CPUCORES_SYSCTL) + int name[2] = { CTL_HW, HW_NCPU }; + int cpus; + size_t cpus_size = sizeof(cpus); + if (!sysctl(name, &cpus, &cpus_size, NULL, NULL) + && cpus_size == sizeof(cpus) && cpus > 0) + ret = (uint32_t)(cpus); +#endif + + return ret; +} + +#endif diff --git a/src/common/physmem.h b/src/common/physmem.h index fb17eac5..7075bc56 100644 --- a/src/common/physmem.h +++ b/src/common/physmem.h @@ -14,27 +14,27 @@ #ifndef PHYSMEM_H #define PHYSMEM_H -#if defined(HAVE_PHYSMEM_SYSCTL) || defined(HAVE_NCPU_SYSCTL) +#if defined(HAVE_PHYSMEM_SYSCONF) +# include <unistd.h> + +#elif defined(HAVE_PHYSMEM_SYSCTL) # ifdef HAVE_SYS_PARAM_H # include <sys/param.h> # endif # ifdef HAVE_SYS_SYSCTL_H # include <sys/sysctl.h> # endif -#endif -#if defined(HAVE_PHYSMEM_SYSCONF) || defined(HAVE_NCPU_SYSCONF) -# include <unistd.h> -#endif +#elif defined(HAVE_PHYSMEM_SYSINFO) +# include <sys/sysinfo.h> -#ifdef _WIN32 +#elif defined(_WIN32) # ifndef _WIN32_WINNT # define _WIN32_WINNT 0x0500 # endif # include <windows.h> -#endif -#ifdef __DJGPP__ +#elif defined(__DJGPP__) # include <dpmi.h> #endif @@ -75,6 +75,11 @@ physmem(void) ret = mem.ui; } +#elif defined(HAVE_PHYSMEM_SYSINFO) + struct sysinfo si; + if (sysinfo(&si) == 0) + ret = (uint64_t)(si.totalram) * si.mem_unit; + #elif defined(_WIN32) MEMORYSTATUSEX meminfo; meminfo.dwLength = sizeof(meminfo); |