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

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

Issue 285493004: [Mac] A couple of improvements for DiscardableMemory (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 7 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 | Annotate | Revision Log
« 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 (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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/discardable_memory.h" 5 #include "base/memory/discardable_memory.h"
6 6
7 #include <mach/mach.h> 7 #include <mach/mach.h>
8 8
9 #include "base/basictypes.h" 9 #include "base/basictypes.h"
10 #include "base/compiler_specific.h" 10 #include "base/compiler_specific.h"
(...skipping 29 matching lines...) Expand all
40 : public DiscardableMemory, 40 : public DiscardableMemory,
41 public internal::DiscardableMemoryManagerAllocation { 41 public internal::DiscardableMemoryManagerAllocation {
42 public: 42 public:
43 explicit DiscardableMemoryMac(size_t bytes) 43 explicit DiscardableMemoryMac(size_t bytes)
44 : memory_(0, 0), 44 : memory_(0, 0),
45 bytes_(mach_vm_round_page(bytes)), 45 bytes_(mach_vm_round_page(bytes)),
46 is_locked_(false) { 46 is_locked_(false) {
47 g_shared_state.Pointer()->manager.Register(this, bytes); 47 g_shared_state.Pointer()->manager.Register(this, bytes);
48 } 48 }
49 49
50 bool Initialize() { return Lock() == DISCARDABLE_MEMORY_LOCK_STATUS_PURGED; } 50 bool Initialize() { return Lock() != DISCARDABLE_MEMORY_LOCK_STATUS_FAILED; }
reveman 2014/05/12 20:57:44 Can this ever return _SUCCESS? If you're intereste
Mark Mentovai 2014/05/12 21:35:49 reveman wrote:
51 51
52 virtual ~DiscardableMemoryMac() { 52 virtual ~DiscardableMemoryMac() {
53 if (is_locked_) 53 if (is_locked_)
54 Unlock(); 54 Unlock();
55 g_shared_state.Pointer()->manager.Unregister(this); 55 g_shared_state.Pointer()->manager.Unregister(this);
56 } 56 }
57 57
58 // Overridden from DiscardableMemory: 58 // Overridden from DiscardableMemory:
59 virtual DiscardableMemoryLockStatus Lock() OVERRIDE { 59 virtual DiscardableMemoryLockStatus Lock() OVERRIDE {
60 DCHECK(!is_locked_); 60 DCHECK(!is_locked_);
(...skipping 13 matching lines...) Expand all
74 is_locked_ = false; 74 is_locked_ = false;
75 } 75 }
76 76
77 virtual void* Memory() const OVERRIDE { 77 virtual void* Memory() const OVERRIDE {
78 DCHECK(is_locked_); 78 DCHECK(is_locked_);
79 return reinterpret_cast<void*>(memory_.address()); 79 return reinterpret_cast<void*>(memory_.address());
80 } 80 }
81 81
82 // Overridden from internal::DiscardableMemoryManagerAllocation: 82 // Overridden from internal::DiscardableMemoryManagerAllocation:
83 virtual bool AllocateAndAcquireLock() OVERRIDE { 83 virtual bool AllocateAndAcquireLock() OVERRIDE {
84 bool persistent = true; 84 DCHECK(!is_locked_);
85
85 kern_return_t ret; 86 kern_return_t ret;
87 bool persistent;
86 if (!memory_.size()) { 88 if (!memory_.size()) {
87 vm_address_t address = 0; 89 vm_address_t address = 0;
88 ret = vm_allocate( 90 ret = vm_allocate(
89 mach_task_self(), 91 mach_task_self(),
90 &address, 92 &address,
91 bytes_, 93 bytes_,
92 VM_FLAGS_ANYWHERE | VM_FLAGS_PURGABLE | kDiscardableMemoryTag); 94 VM_FLAGS_ANYWHERE | VM_FLAGS_PURGABLE | kDiscardableMemoryTag);
93 MACH_CHECK(ret == KERN_SUCCESS, ret) << "vm_allocate"; 95 MACH_CHECK(ret == KERN_SUCCESS, ret) << "vm_allocate";
94 memory_.reset(address, bytes_); 96 memory_.reset(address, bytes_);
97
98 // When making a fresh allocation, it's impossible for |persistent| to
99 // be true.
95 persistent = false; 100 persistent = false;
96 } 101 } else {
102 // |persistent| will be reset to false below if appropriate, but when
103 // reusing an existing allocation, it's possible for it to be true.
104 persistent = true;
97 105
98 #if !defined(NDEBUG) 106 #if !defined(NDEBUG)
99 ret = vm_protect(mach_task_self(), 107 ret = vm_protect(mach_task_self(),
100 memory_.address(), 108 memory_.address(),
101 memory_.size(), 109 memory_.size(),
102 FALSE, 110 FALSE,
103 VM_PROT_DEFAULT); 111 VM_PROT_DEFAULT);
104 MACH_DCHECK(ret == KERN_SUCCESS, ret) << "vm_protect"; 112 MACH_DCHECK(ret == KERN_SUCCESS, ret) << "vm_protect";
105 #endif 113 #endif
114 }
106 115
107 int state = VM_PURGABLE_NONVOLATILE; 116 int state = VM_PURGABLE_NONVOLATILE;
108 ret = vm_purgable_control(mach_task_self(), 117 ret = vm_purgable_control(mach_task_self(),
109 memory_.address(), 118 memory_.address(),
110 VM_PURGABLE_SET_STATE, 119 VM_PURGABLE_SET_STATE,
111 &state); 120 &state);
112 MACH_CHECK(ret == KERN_SUCCESS, ret) << "vm_purgable_control"; 121 MACH_CHECK(ret == KERN_SUCCESS, ret) << "vm_purgable_control";
113 if (state & VM_PURGABLE_EMPTY) 122 if (state & VM_PURGABLE_EMPTY)
114 persistent = false; 123 persistent = false;
124
115 return persistent; 125 return persistent;
116 } 126 }
117 127
118 virtual void ReleaseLock() OVERRIDE { 128 virtual void ReleaseLock() OVERRIDE {
129 DCHECK(is_locked_);
130
119 int state = VM_PURGABLE_VOLATILE | VM_VOLATILE_GROUP_DEFAULT; 131 int state = VM_PURGABLE_VOLATILE | VM_VOLATILE_GROUP_DEFAULT;
120 kern_return_t ret = vm_purgable_control(mach_task_self(), 132 kern_return_t ret = vm_purgable_control(mach_task_self(),
121 memory_.address(), 133 memory_.address(),
122 VM_PURGABLE_SET_STATE, 134 VM_PURGABLE_SET_STATE,
123 &state); 135 &state);
124 MACH_CHECK(ret == KERN_SUCCESS, ret) << "vm_purgable_control"; 136 MACH_CHECK(ret == KERN_SUCCESS, ret) << "vm_purgable_control";
125 137
126 #if !defined(NDEBUG) 138 #if !defined(NDEBUG)
127 ret = vm_protect(mach_task_self(), 139 ret = vm_protect(mach_task_self(),
128 memory_.address(), 140 memory_.address(),
129 memory_.size(), 141 memory_.size(),
130 FALSE, 142 FALSE,
131 VM_PROT_NONE); 143 VM_PROT_NONE);
132 MACH_DCHECK(ret == KERN_SUCCESS, ret) << "vm_protect"; 144 MACH_DCHECK(ret == KERN_SUCCESS, ret) << "vm_protect";
133 #endif 145 #endif
134 } 146 }
135 147
136 virtual void Purge() OVERRIDE { 148 virtual void Purge() OVERRIDE {
149 DCHECK(!is_locked_);
reveman 2014/05/12 20:57:44 There's a race here as this can be called on any t
Mark Mentovai 2014/05/12 21:35:49 reveman wrote:
137 memory_.reset(); 150 memory_.reset();
138 } 151 }
139 152
140 private: 153 private:
141 mac::ScopedMachVM memory_; 154 mac::ScopedMachVM memory_;
142 const size_t bytes_; 155 const size_t bytes_;
143 bool is_locked_; 156 bool is_locked_;
144 157
145 DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryMac); 158 DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryMac);
146 }; 159 };
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
205 } 218 }
206 219
207 // static 220 // static
208 void DiscardableMemory::PurgeForTesting() { 221 void DiscardableMemory::PurgeForTesting() {
209 int state = 0; 222 int state = 0;
210 vm_purgable_control(mach_task_self(), 0, VM_PURGABLE_PURGE_ALL, &state); 223 vm_purgable_control(mach_task_self(), 0, VM_PURGABLE_PURGE_ALL, &state);
211 internal::DiscardableMemoryEmulated::PurgeForTesting(); 224 internal::DiscardableMemoryEmulated::PurgeForTesting();
212 } 225 }
213 226
214 } // namespace base 227 } // 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