Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5)

Side by Side Diff: base/sys_info_mac.mm

Issue 2558043007: Fix free memory calculation. (Closed)
Patch Set: Fix integer overflow in unittest. Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/sys_info_linux.cc ('k') | base/sys_info_openbsd.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 <ApplicationServices/ApplicationServices.h> 7 #include <ApplicationServices/ApplicationServices.h>
8 #include <CoreServices/CoreServices.h> 8 #include <CoreServices/CoreServices.h>
9 #import <Foundation/Foundation.h> 9 #import <Foundation/Foundation.h>
10 #include <mach/mach_host.h> 10 #include <mach/mach_host.h>
11 #include <mach/mach_init.h> 11 #include <mach/mach_init.h>
12 #include <stddef.h> 12 #include <stddef.h>
13 #include <stdint.h> 13 #include <stdint.h>
14 #include <sys/sysctl.h> 14 #include <sys/sysctl.h>
15 #include <sys/types.h> 15 #include <sys/types.h>
16 16
17 #include "base/logging.h" 17 #include "base/logging.h"
18 #include "base/mac/mac_util.h" 18 #include "base/mac/mac_util.h"
19 #include "base/mac/scoped_mach_port.h" 19 #include "base/mac/scoped_mach_port.h"
20 #import "base/mac/sdk_forward_declarations.h" 20 #import "base/mac/sdk_forward_declarations.h"
21 #include "base/macros.h" 21 #include "base/macros.h"
22 #include "base/process/process_metrics.h"
22 #include "base/strings/stringprintf.h" 23 #include "base/strings/stringprintf.h"
23 24
24 namespace base { 25 namespace base {
25 26
26 // static 27 // static
27 std::string SysInfo::OperatingSystemName() { 28 std::string SysInfo::OperatingSystemName() {
28 return "Mac OS X"; 29 return "Mac OS X";
29 } 30 }
30 31
31 // static 32 // static
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
76 if (result != KERN_SUCCESS) { 77 if (result != KERN_SUCCESS) {
77 NOTREACHED(); 78 NOTREACHED();
78 return 0; 79 return 0;
79 } 80 }
80 DCHECK_EQ(HOST_BASIC_INFO_COUNT, count); 81 DCHECK_EQ(HOST_BASIC_INFO_COUNT, count);
81 return static_cast<int64_t>(hostinfo.max_mem); 82 return static_cast<int64_t>(hostinfo.max_mem);
82 } 83 }
83 84
84 // static 85 // static
85 int64_t SysInfo::AmountOfAvailablePhysicalMemory() { 86 int64_t SysInfo::AmountOfAvailablePhysicalMemory() {
86 base::mac::ScopedMachSendRight host(mach_host_self()); 87 SystemMemoryInfoKB info;
87 vm_statistics_data_t vm_info; 88 if (!GetSystemMemoryInfo(&info))
88 mach_msg_type_number_t count = HOST_VM_INFO_COUNT;
89
90 if (host_statistics(host.get(),
91 HOST_VM_INFO,
92 reinterpret_cast<host_info_t>(&vm_info),
93 &count) != KERN_SUCCESS) {
94 NOTREACHED();
95 return 0; 89 return 0;
96 } 90 // We should add inactive file-backed memory also but there is no such
97 91 // information from Mac OS unfortunately.
98 return static_cast<int64_t>(vm_info.free_count - vm_info.speculative_count) * 92 return static_cast<int64_t>(info.free + info.speculative) * 1024;
99 PAGE_SIZE;
100 } 93 }
101 94
102 // static 95 // static
103 std::string SysInfo::CPUModelName() { 96 std::string SysInfo::CPUModelName() {
104 char name[256]; 97 char name[256];
105 size_t len = arraysize(name); 98 size_t len = arraysize(name);
106 if (sysctlbyname("machdep.cpu.brand_string", &name, &len, NULL, 0) == 0) 99 if (sysctlbyname("machdep.cpu.brand_string", &name, &len, NULL, 0) == 0)
107 return name; 100 return name;
108 return std::string(); 101 return std::string();
109 } 102 }
110 103
111 std::string SysInfo::HardwareModelName() { 104 std::string SysInfo::HardwareModelName() {
112 char model[256]; 105 char model[256];
113 size_t len = sizeof(model); 106 size_t len = sizeof(model);
114 if (sysctlbyname("hw.model", model, &len, NULL, 0) == 0) 107 if (sysctlbyname("hw.model", model, &len, NULL, 0) == 0)
115 return std::string(model, 0, len); 108 return std::string(model, 0, len);
116 return std::string(); 109 return std::string();
117 } 110 }
118 111
119 } // namespace base 112 } // namespace base
OLDNEW
« no previous file with comments | « base/sys_info_linux.cc ('k') | base/sys_info_openbsd.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698