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 "webkit/glue/dalvik_heap_size_android.h" |
| 6 |
| 7 #include <sys/system_properties.h> |
| 8 |
| 9 #include "base/memory/singleton.h" |
| 10 #include "base/string_number_conversions.h" |
| 11 |
| 12 namespace webkit_glue { |
| 13 DalvikHeapSize* DalvikHeapSize::GetInstance() { |
| 14 return Singleton<DalvikHeapSize>::get(); |
| 15 } |
| 16 |
| 17 DalvikHeapSize::DalvikHeapSize() : heap_size_mb_(0) { |
| 18 char heap_size_str[PROP_VALUE_MAX]; |
| 19 __system_property_get("dalvik.vm.heapsize", heap_size_str); |
| 20 heap_size_mb_ = ParseHeapSize(std::string(heap_size_str)); |
| 21 } |
| 22 |
| 23 DalvikHeapSize::~DalvikHeapSize() {} |
| 24 |
| 25 int DalvikHeapSize::HeapSizeMB() const { |
| 26 return heap_size_mb_; |
| 27 } |
| 28 |
| 29 int DalvikHeapSize::ParseHeapSize(const std::string& str) const { |
| 30 const int64 KB = 1024; |
| 31 const int64 MB = 1024 * KB; |
| 32 const int64 GB = 1024 * MB; |
| 33 CHECK_GT(str.size(), 0u); |
| 34 int64 factor = 1; |
| 35 size_t length = str.size(); |
| 36 if (str[length - 1] == 'k') { |
| 37 factor = KB; |
| 38 length--; |
| 39 } else if (str[length - 1] == 'm') { |
| 40 factor = MB; |
| 41 length--; |
| 42 } else if (str[length - 1] == 'g') { |
| 43 factor = GB; |
| 44 length--; |
| 45 } else { |
| 46 CHECK('0' <= str[length - 1] && str[length - 1] <= '9'); |
| 47 } |
| 48 int64 result = 0; |
| 49 bool parsed = base::StringToInt64(str.substr(0, length), &result); |
| 50 CHECK(parsed); |
| 51 result = result * factor / MB; |
| 52 // dalvik.vm.heapsize property is writable by user, |
| 53 // truncate it to reasonable value to avoid overflows later. |
| 54 result = std::min<int64>(std::max<int64>(32, result), 1024); |
| 55 return static_cast<int>(result); |
| 56 } |
| 57 |
| 58 } // webkit_glue namespace |
OLD | NEW |