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