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

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

Issue 278923002: Use the new ScopedMachVM class and the MACH_LOG family of logging macros (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
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 #include <sys/mman.h> 8 #include <mach/mach_vm.h>
9 9
10 #include "base/basictypes.h" 10 #include "base/basictypes.h"
11 #include "base/compiler_specific.h" 11 #include "base/compiler_specific.h"
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "base/mac/mach_logging.h"
14 #include "base/mac/scoped_mach_vm.h"
13 #include "base/memory/discardable_memory_emulated.h" 15 #include "base/memory/discardable_memory_emulated.h"
14 #include "base/memory/discardable_memory_malloc.h" 16 #include "base/memory/discardable_memory_malloc.h"
15 #include "base/memory/scoped_ptr.h" 17 #include "base/memory/scoped_ptr.h"
16 18
17 namespace base { 19 namespace base {
18 namespace { 20 namespace {
19 21
20 // The VM subsystem allows tagging of memory and 240-255 is reserved for 22 // The VM subsystem allows tagging of memory and 240-255 is reserved for
21 // application use (see mach/vm_statistics.h). Pick 252 (after chromium's atomic 23 // application use (see mach/vm_statistics.h). Pick 252 (after chromium's atomic
22 // weight of ~52). 24 // weight of ~52).
23 const int kDiscardableMemoryTag = VM_MAKE_TAG(252); 25 const int kDiscardableMemoryTag = VM_MAKE_TAG(252);
24 26
25 class DiscardableMemoryMac : public DiscardableMemory { 27 class DiscardableMemoryMac : public DiscardableMemory {
26 public: 28 public:
27 explicit DiscardableMemoryMac(size_t size) 29 explicit DiscardableMemoryMac(size_t size)
28 : buffer_(0), 30 : memory_(0, 0),
29 size_(size) { 31 size_(size) {
30 } 32 }
31 33
32 bool Initialize() { 34 bool Initialize() {
33 kern_return_t ret = vm_allocate(mach_task_self(), 35 DCHECK_EQ(memory_.size(), 0u);
34 &buffer_, 36 mach_vm_address_t address = 0;
35 size_, 37 kern_return_t ret = mach_vm_allocate(mach_task_self(),
36 VM_FLAGS_PURGABLE | 38 &address,
37 VM_FLAGS_ANYWHERE | 39 size_,
38 kDiscardableMemoryTag); 40 VM_FLAGS_PURGABLE |
41 VM_FLAGS_ANYWHERE |
42 kDiscardableMemoryTag);
39 if (ret != KERN_SUCCESS) { 43 if (ret != KERN_SUCCESS) {
40 DLOG(ERROR) << "vm_allocate() failed"; 44 MACH_DLOG(ERROR, ret) << "mach_vm_allocate";
41 return false; 45 return false;
42 } 46 }
43 47
48 memory_.reset(address, mach_vm_round_page(size_));
Robert Sesek 2014/05/09 20:40:01 You don't want to round the size in the initialize
49
44 return true; 50 return true;
45 } 51 }
46 52
47 virtual ~DiscardableMemoryMac() { 53 virtual ~DiscardableMemoryMac() {
48 if (buffer_)
49 vm_deallocate(mach_task_self(), buffer_, size_);
50 } 54 }
51 55
52 virtual DiscardableMemoryLockStatus Lock() OVERRIDE { 56 virtual DiscardableMemoryLockStatus Lock() OVERRIDE {
53 DCHECK_EQ(0, mprotect(reinterpret_cast<void*>(buffer_), 57 kern_return_t ret;
54 size_, 58 MACH_DCHECK((ret = mach_vm_protect(mach_task_self(),
55 PROT_READ | PROT_WRITE)); 59 memory_.address(),
60 memory_.size(),
61 FALSE,
62 VM_PROT_DEFAULT)) == KERN_SUCCESS, ret);
56 int state = VM_PURGABLE_NONVOLATILE; 63 int state = VM_PURGABLE_NONVOLATILE;
57 kern_return_t ret = vm_purgable_control(mach_task_self(), 64 ret = mach_vm_purgable_control(mach_task_self(),
58 buffer_, 65 memory_.address(),
59 VM_PURGABLE_SET_STATE, 66 VM_PURGABLE_SET_STATE,
60 &state); 67 &state);
61 if (ret != KERN_SUCCESS) 68 if (ret != KERN_SUCCESS)
62 return DISCARDABLE_MEMORY_LOCK_STATUS_FAILED; 69 return DISCARDABLE_MEMORY_LOCK_STATUS_FAILED;
63 70
64 return state & VM_PURGABLE_EMPTY ? DISCARDABLE_MEMORY_LOCK_STATUS_PURGED 71 return state & VM_PURGABLE_EMPTY ? DISCARDABLE_MEMORY_LOCK_STATUS_PURGED
65 : DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS; 72 : DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS;
66 } 73 }
67 74
68 virtual void Unlock() OVERRIDE { 75 virtual void Unlock() OVERRIDE {
69 int state = VM_PURGABLE_VOLATILE | VM_VOLATILE_GROUP_DEFAULT; 76 int state = VM_PURGABLE_VOLATILE | VM_VOLATILE_GROUP_DEFAULT;
70 kern_return_t ret = vm_purgable_control(mach_task_self(), 77 kern_return_t ret = mach_vm_purgable_control(mach_task_self(),
71 buffer_, 78 memory_.address(),
72 VM_PURGABLE_SET_STATE, 79 VM_PURGABLE_SET_STATE,
73 &state); 80 &state);
74 DCHECK_EQ(0, mprotect(reinterpret_cast<void*>(buffer_), size_, PROT_NONE)); 81 MACH_DLOG_IF(ERROR, ret != KERN_SUCCESS, ret) << "mach_vm_purgable_control";
75 if (ret != KERN_SUCCESS) 82 MACH_DCHECK((ret = mach_vm_protect(mach_task_self(),
76 DLOG(ERROR) << "Failed to unlock memory."; 83 memory_.address(),
84 memory_.size(),
85 FALSE,
86 VM_PROT_NONE)) == KERN_SUCCESS, ret);
77 } 87 }
78 88
79 virtual void* Memory() const OVERRIDE { 89 virtual void* Memory() const OVERRIDE {
80 return reinterpret_cast<void*>(buffer_); 90 return reinterpret_cast<void*>(memory_.address());
81 } 91 }
82 92
83 private: 93 private:
84 vm_address_t buffer_; 94 mac::ScopedMachVM memory_;
85 const size_t size_; 95 size_t size_;
86 96
87 DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryMac); 97 DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryMac);
88 }; 98 };
89 99
90 } // namespace 100 } // namespace
91 101
92 // static 102 // static
93 void DiscardableMemory::RegisterMemoryPressureListeners() { 103 void DiscardableMemory::RegisterMemoryPressureListeners() {
94 internal::DiscardableMemoryEmulated::RegisterMemoryPressureListeners(); 104 internal::DiscardableMemoryEmulated::RegisterMemoryPressureListeners();
95 } 105 }
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 } 152 }
143 } 153 }
144 154
145 NOTREACHED(); 155 NOTREACHED();
146 return scoped_ptr<DiscardableMemory>(); 156 return scoped_ptr<DiscardableMemory>();
147 } 157 }
148 158
149 // static 159 // static
150 void DiscardableMemory::PurgeForTesting() { 160 void DiscardableMemory::PurgeForTesting() {
151 int state = 0; 161 int state = 0;
152 vm_purgable_control(mach_task_self(), 0, VM_PURGABLE_PURGE_ALL, &state); 162 mach_vm_purgable_control(mach_task_self(), 0, VM_PURGABLE_PURGE_ALL, &state);
153 internal::DiscardableMemoryEmulated::PurgeForTesting(); 163 internal::DiscardableMemoryEmulated::PurgeForTesting();
154 } 164 }
155 165
156 } // namespace base 166 } // namespace base
OLDNEW
« no previous file with comments | « base/mac/mach_logging.h ('k') | base/process/memory_mac.mm » ('j') | base/process/memory_mac.mm » ('J')

Powered by Google App Engine
This is Rietveld 408576698