OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/sys_info.h" | 5 #include "base/sys_info.h" |
6 | 6 |
| 7 #include <limits> |
| 8 |
7 #include "base/file_util.h" | 9 #include "base/file_util.h" |
8 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/string_number_conversions.h" |
9 | 12 |
10 namespace base { | 13 namespace base { |
11 | 14 |
12 int64 SysInfo::AmountOfPhysicalMemory() { | 15 int64 SysInfo::AmountOfPhysicalMemory() { |
13 long pages = sysconf(_SC_PHYS_PAGES); | 16 long pages = sysconf(_SC_PHYS_PAGES); |
14 long page_size = sysconf(_SC_PAGE_SIZE); | 17 long page_size = sysconf(_SC_PAGE_SIZE); |
15 if (pages == -1 || page_size == -1) { | 18 if (pages == -1 || page_size == -1) { |
16 NOTREACHED(); | 19 NOTREACHED(); |
17 return 0; | 20 return 0; |
18 } | 21 } |
19 | 22 |
20 return static_cast<int64>(pages) * page_size; | 23 return static_cast<int64>(pages) * page_size; |
21 } | 24 } |
22 | 25 |
23 // static | 26 // static |
24 size_t SysInfo::MaxSharedMemorySize() { | 27 size_t SysInfo::MaxSharedMemorySize() { |
25 static size_t limit; | 28 static int64 limit; |
26 static bool limit_valid = false; | 29 static bool limit_valid = false; |
27 if (!limit_valid) { | 30 if (!limit_valid) { |
28 std::string contents; | 31 std::string contents; |
29 file_util::ReadFileToString(FilePath("/proc/sys/kernel/shmmax"), &contents); | 32 file_util::ReadFileToString(FilePath("/proc/sys/kernel/shmmax"), &contents); |
30 limit = strtoul(contents.c_str(), NULL, 0); | 33 DCHECK(!contents.empty()); |
31 limit_valid = true; | 34 if (!contents.empty() && contents[contents.length() - 1] == '\n') { |
| 35 contents.erase(contents.length() - 1); |
| 36 } |
| 37 if (base::StringToInt64(contents, &limit)) { |
| 38 DCHECK(limit >= 0); |
| 39 DCHECK(static_cast<uint64>(limit) <= std::numeric_limits<size_t>::max()); |
| 40 limit_valid = true; |
| 41 } else { |
| 42 NOTREACHED(); |
| 43 return 0; |
| 44 } |
32 } | 45 } |
33 return limit; | 46 return static_cast<size_t>(limit); |
34 } | 47 } |
35 | 48 |
36 } // namespace base | 49 } // namespace base |
OLD | NEW |