diff options
author | deanm@chromium.org <deanm@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-09-17 13:10:45 +0000 |
---|---|---|
committer | deanm@chromium.org <deanm@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-09-17 13:10:45 +0000 |
commit | d632798e22600557d9bb2c84450dd110cb9d6230 (patch) | |
tree | 14a3498d0262e8e87a684c0e97d4d62db3a90491 /base | |
parent | 2a758d61d9ee5c8ae43bf5a6d974d27cc6779ef2 (diff) | |
download | chromium_src-d632798e22600557d9bb2c84450dd110cb9d6230.zip chromium_src-d632798e22600557d9bb2c84450dd110cb9d6230.tar.gz chromium_src-d632798e22600557d9bb2c84450dd110cb9d6230.tar.bz2 |
Add support for querying the amount of memory on a system to sys_info.
Review URL: http://codereview.chromium.org/2927
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@2303 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/sys_info.h | 5 | ||||
-rw-r--r-- | base/sys_info_posix.cc | 36 | ||||
-rw-r--r-- | base/sys_info_unittest.cc | 5 | ||||
-rw-r--r-- | base/sys_info_win.cc | 14 |
4 files changed, 59 insertions, 1 deletions
diff --git a/base/sys_info.h b/base/sys_info.h index 65949a0..c82b0d3 100644 --- a/base/sys_info.h +++ b/base/sys_info.h @@ -5,12 +5,17 @@ #ifndef BASE_SYS_INFO_H_ #define BASE_SYS_INFO_H_ +#include "base/basictypes.h" + namespace base { class SysInfo { public: // Return the number of logical processors/cores on the current machine. static int NumberOfProcessors(); + + // Return the number of bytes of physical memory on the current machine. + static int64 AmountOfPhysicalMemory(); }; } // namespace base diff --git a/base/sys_info_posix.cc b/base/sys_info_posix.cc index 093e607..a9a91cd 100644 --- a/base/sys_info_posix.cc +++ b/base/sys_info_posix.cc @@ -3,12 +3,17 @@ // found in the LICENSE file. #include "base/sys_info.h" +#include "base/basictypes.h" #include <errno.h> #include <string.h> #include <unistd.h> -#include "base/basictypes.h" +#if defined(OS_MACOSX) +#include <mach/mach_host.h> +#include <mach/mach_init.h> +#endif + #include "base/logging.h" namespace base { @@ -25,4 +30,33 @@ int SysInfo::NumberOfProcessors() { return static_cast<int>(res); } +// static +int64 SysInfo::AmountOfPhysicalMemory() { + // _SC_PHYS_PAGES is not part of POSIX and not available on OS X +#if defined(OS_MACOSX) + struct host_basic_info hostinfo; + mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT; + int result = host_info(mach_host_self(), + HOST_BASIC_INFO, + reinterpret_cast<host_info_t>(&hostinfo), + &count); + DCHECK_EQ(HOST_BASIC_INFO_COUNT, count); + if (result != KERN_SUCCESS) { + NOTREACHED(); + return 0; + } + + return static_cast<int64>(hostinfo.max_mem); +#else + long pages = sysconf(_SC_PHYS_PAGES); + long page_size = sysconf(_SC_PAGE_SIZE); + if (pages == -1 || page_size == -1) { + NOTREACHED(); + return 0; + } + + return static_cast<int64>(pages) * page_size; +#endif +} + } // namespace base diff --git a/base/sys_info_unittest.cc b/base/sys_info_unittest.cc index 4ed2834..edaf2aa 100644 --- a/base/sys_info_unittest.cc +++ b/base/sys_info_unittest.cc @@ -9,3 +9,8 @@ TEST(SysInfoTest, NumProcs) { // We aren't actually testing that it's correct, just that it's sane. EXPECT_GE(base::SysInfo::NumberOfProcessors(), 1); } + +TEST(SysInfoTest, AmountOfMem) { + // We aren't actually testing that it's correct, just that it's sane. + EXPECT_GT(base::SysInfo::AmountOfPhysicalMemory(), 0); +} diff --git a/base/sys_info_win.cc b/base/sys_info_win.cc index 4d32489..8b6dfea 100644 --- a/base/sys_info_win.cc +++ b/base/sys_info_win.cc @@ -6,6 +6,8 @@ #include <windows.h> +#include "base/logging.h" + namespace base { // static @@ -15,4 +17,16 @@ int SysInfo::NumberOfProcessors() { return static_cast<int>(info.dwNumberOfProcessors); } +// static +int64 SysInfo::AmountOfPhysicalMemory() { + MEMORYSTATUSEX memory_info; + memory_info.dwLength = sizeof(memory_info); + if (!GlobalMemoryStatusEx(&memory_info)) { + NOTREACHED(); + return 0; + } + + return static_cast<int64>(memory_info.ullTotalPhys); +} + } // namespace base |