Fix style nits in gdata directory.
[chromium-blink-merge.git] / base / sys_info_android.cc
blobb84b557ec323ad5781fff8443c32f31d18d49046
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.
5 #include "base/sys_info.h"
7 #include <sys/system_properties.h>
9 #include "base/logging.h"
10 #include "base/string_number_conversions.h"
11 #include "base/string_piece.h"
13 namespace {
15 int ParseHeapSize(const base::StringPiece& str) {
16 const int64 KB = 1024;
17 const int64 MB = 1024 * KB;
18 const int64 GB = 1024 * MB;
19 CHECK_GT(str.size(), 0u);
20 int64 factor = 1;
21 size_t length = str.size();
22 if (str[length - 1] == 'k') {
23 factor = KB;
24 length--;
25 } else if (str[length - 1] == 'm') {
26 factor = MB;
27 length--;
28 } else if (str[length - 1] == 'g') {
29 factor = GB;
30 length--;
31 } else {
32 CHECK('0' <= str[length - 1] && str[length - 1] <= '9');
34 int64 result = 0;
35 bool parsed = base::StringToInt64(str.substr(0, length), &result);
36 CHECK(parsed);
37 result = result * factor / MB;
38 // dalvik.vm.heapsize property is writable by user,
39 // truncate it to reasonable value to avoid overflows later.
40 result = std::min<int64>(std::max<int64>(32, result), 1024);
41 return static_cast<int>(result);
44 int GetDalvikHeapSizeMB() {
45 char heap_size_str[PROP_VALUE_MAX];
46 __system_property_get("dalvik.vm.heapsize", heap_size_str);
47 return ParseHeapSize(heap_size_str);
50 } // anonymous namespace
52 namespace base {
54 int SysInfo::DalvikHeapSizeMB() {
55 static int heap_size = GetDalvikHeapSizeMB();
56 return heap_size;
59 } // namespace base