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

Side by Side Diff: base/memory/memory_pressure_monitor_mac.cc

Issue 2570623004: [Mac] Guard against null memory pressure dispatch source (Closed)
Patch Set: Created 4 years 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 | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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/memory/memory_pressure_monitor_mac.h" 5 #include "base/memory/memory_pressure_monitor_mac.h"
6 6
7 #include <dlfcn.h> 7 #include <dlfcn.h>
8 #include <stddef.h> 8 #include <stddef.h>
9 #include <sys/sysctl.h> 9 #include <sys/sysctl.h>
10 10
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
44 DISPATCH_SOURCE_TYPE_MEMORYPRESSURE, 44 DISPATCH_SOURCE_TYPE_MEMORYPRESSURE,
45 0, 45 0,
46 DISPATCH_MEMORYPRESSURE_WARN | DISPATCH_MEMORYPRESSURE_CRITICAL | 46 DISPATCH_MEMORYPRESSURE_WARN | DISPATCH_MEMORYPRESSURE_CRITICAL |
47 DISPATCH_MEMORYPRESSURE_NORMAL, 47 DISPATCH_MEMORYPRESSURE_NORMAL,
48 dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0))), 48 dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0))),
49 dispatch_callback_( 49 dispatch_callback_(
50 base::Bind(&MemoryPressureListener::NotifyMemoryPressure)), 50 base::Bind(&MemoryPressureListener::NotifyMemoryPressure)),
51 last_statistic_report_(CFAbsoluteTimeGetCurrent()), 51 last_statistic_report_(CFAbsoluteTimeGetCurrent()),
52 last_pressure_level_(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE), 52 last_pressure_level_(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE),
53 reporting_error_(0) { 53 reporting_error_(0) {
54 dispatch_source_set_event_handler(memory_level_event_source_, ^{ 54 if (memory_level_event_source_.get() != nullptr) {
55 OnMemoryPressureChanged(memory_level_event_source_.get(), 55 dispatch_source_set_event_handler(memory_level_event_source_, ^{
56 dispatch_callback_); 56 OnMemoryPressureChanged(memory_level_event_source_.get(),
57 }); 57 dispatch_callback_);
58 dispatch_resume(memory_level_event_source_); 58 });
59 dispatch_resume(memory_level_event_source_);
60 }
59 } 61 }
60 62
61 MemoryPressureMonitor::~MemoryPressureMonitor() { 63 MemoryPressureMonitor::~MemoryPressureMonitor() {
62 dispatch_source_cancel(memory_level_event_source_); 64 if (memory_level_event_source_.get() != nullptr)
65 dispatch_source_cancel(memory_level_event_source_);
63 } 66 }
64 67
65 MemoryPressureListener::MemoryPressureLevel 68 MemoryPressureListener::MemoryPressureLevel
66 MemoryPressureMonitor::GetCurrentPressureLevel() { 69 MemoryPressureMonitor::GetCurrentPressureLevel() {
67 int mac_memory_pressure; 70 int mac_memory_pressure;
68 size_t length = sizeof(int); 71 size_t length = sizeof(int);
69 sysctlbyname("kern.memorystatus_vm_pressure_level", &mac_memory_pressure, 72 sysctlbyname("kern.memorystatus_vm_pressure_level", &mac_memory_pressure,
70 &length, nullptr, 0); 73 &length, nullptr, 0);
71 MemoryPressureListener::MemoryPressureLevel memory_pressure_level = 74 MemoryPressureListener::MemoryPressureLevel memory_pressure_level =
72 MemoryPressureLevelForMacMemoryPressure(mac_memory_pressure); 75 MemoryPressureLevelForMacMemoryPressure(mac_memory_pressure);
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
116 RecordMemoryPressure(last_pressure_level_, ticks_to_report); 119 RecordMemoryPressure(last_pressure_level_, ticks_to_report);
117 } 120 }
118 121
119 void MemoryPressureMonitor::SetDispatchCallback( 122 void MemoryPressureMonitor::SetDispatchCallback(
120 const DispatchCallback& callback) { 123 const DispatchCallback& callback) {
121 dispatch_callback_ = callback; 124 dispatch_callback_ = callback;
122 } 125 }
123 126
124 } // namespace mac 127 } // namespace mac
125 } // namespace base 128 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698