Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/sys_info.h" | |
| 6 | |
| 7 #include <sys/system_properties.h> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "base/string_number_conversions.h" | |
| 11 | |
| 12 namespace { | |
| 13 | |
| 14 int ParseHeapSize(const std::string& str) { | |
|
darin (slow to review)
2012/05/24 22:27:43
nit: consider changing the parameter to base::Stri
ulan
2012/05/25 14:14:36
Done.
| |
| 15 const int64 KB = 1024; | |
| 16 const int64 MB = 1024 * KB; | |
| 17 const int64 GB = 1024 * MB; | |
| 18 CHECK_GT(str.size(), 0u); | |
| 19 int64 factor = 1; | |
| 20 size_t length = str.size(); | |
| 21 if (str[length - 1] == 'k') { | |
| 22 factor = KB; | |
| 23 length--; | |
| 24 } else if (str[length - 1] == 'm') { | |
| 25 factor = MB; | |
| 26 length--; | |
| 27 } else if (str[length - 1] == 'g') { | |
| 28 factor = GB; | |
| 29 length--; | |
| 30 } else { | |
| 31 CHECK('0' <= str[length - 1] && str[length - 1] <= '9'); | |
| 32 } | |
| 33 int64 result = 0; | |
| 34 bool parsed = base::StringToInt64(str.substr(0, length), &result); | |
| 35 CHECK(parsed); | |
| 36 result = result * factor / MB; | |
| 37 // dalvik.vm.heapsize property is writable by user, | |
| 38 // truncate it to reasonable value to avoid overflows later. | |
| 39 result = std::min<int64>(std::max<int64>(32, result), 1024); | |
| 40 return static_cast<int>(result); | |
| 41 } | |
| 42 | |
| 43 int GetDalvikHeapSizeMB() { | |
| 44 char heap_size_str[PROP_VALUE_MAX]; | |
| 45 __system_property_get("dalvik.vm.heapsize", heap_size_str); | |
| 46 return ParseHeapSize(std::string(heap_size_str)); | |
| 47 } | |
| 48 | |
| 49 } // anonymous namespace | |
| 50 | |
| 51 namespace base { | |
| 52 | |
| 53 int SysInfo::DalvikHeapSizeMB() { | |
| 54 static int heap_size = GetDalvikHeapSizeMB(); | |
| 55 return heap_size; | |
| 56 } | |
| 57 | |
| 58 } // namespace base | |
| OLD | NEW |