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 | 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 Loading... |
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; } |
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 Loading... |
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; | |
85 kern_return_t ret; | 84 kern_return_t ret; |
| 85 bool persistent; |
86 if (!memory_.size()) { | 86 if (!memory_.size()) { |
87 vm_address_t address = 0; | 87 vm_address_t address = 0; |
88 ret = vm_allocate( | 88 ret = vm_allocate( |
89 mach_task_self(), | 89 mach_task_self(), |
90 &address, | 90 &address, |
91 bytes_, | 91 bytes_, |
92 VM_FLAGS_ANYWHERE | VM_FLAGS_PURGABLE | kDiscardableMemoryTag); | 92 VM_FLAGS_ANYWHERE | VM_FLAGS_PURGABLE | kDiscardableMemoryTag); |
93 MACH_CHECK(ret == KERN_SUCCESS, ret) << "vm_allocate"; | 93 MACH_CHECK(ret == KERN_SUCCESS, ret) << "vm_allocate"; |
94 memory_.reset(address, bytes_); | 94 memory_.reset(address, bytes_); |
| 95 |
| 96 // When making a fresh allocation, it's impossible for |persistent| to |
| 97 // be true. |
95 persistent = false; | 98 persistent = false; |
96 } | 99 } else { |
| 100 // |persistent| will be reset to false below if appropriate, but when |
| 101 // reusing an existing allocation, it's possible for it to be true. |
| 102 persistent = true; |
97 | 103 |
98 #if !defined(NDEBUG) | 104 #if !defined(NDEBUG) |
99 ret = vm_protect(mach_task_self(), | 105 ret = vm_protect(mach_task_self(), |
100 memory_.address(), | 106 memory_.address(), |
101 memory_.size(), | 107 memory_.size(), |
102 FALSE, | 108 FALSE, |
103 VM_PROT_DEFAULT); | 109 VM_PROT_DEFAULT); |
104 MACH_DCHECK(ret == KERN_SUCCESS, ret) << "vm_protect"; | 110 MACH_DCHECK(ret == KERN_SUCCESS, ret) << "vm_protect"; |
105 #endif | 111 #endif |
| 112 } |
106 | 113 |
107 int state = VM_PURGABLE_NONVOLATILE; | 114 int state = VM_PURGABLE_NONVOLATILE; |
108 ret = vm_purgable_control(mach_task_self(), | 115 ret = vm_purgable_control(mach_task_self(), |
109 memory_.address(), | 116 memory_.address(), |
110 VM_PURGABLE_SET_STATE, | 117 VM_PURGABLE_SET_STATE, |
111 &state); | 118 &state); |
112 MACH_CHECK(ret == KERN_SUCCESS, ret) << "vm_purgable_control"; | 119 MACH_CHECK(ret == KERN_SUCCESS, ret) << "vm_purgable_control"; |
113 if (state & VM_PURGABLE_EMPTY) | 120 if (state & VM_PURGABLE_EMPTY) |
114 persistent = false; | 121 persistent = false; |
| 122 |
115 return persistent; | 123 return persistent; |
116 } | 124 } |
117 | 125 |
118 virtual void ReleaseLock() OVERRIDE { | 126 virtual void ReleaseLock() OVERRIDE { |
119 int state = VM_PURGABLE_VOLATILE | VM_VOLATILE_GROUP_DEFAULT; | 127 int state = VM_PURGABLE_VOLATILE | VM_VOLATILE_GROUP_DEFAULT; |
120 kern_return_t ret = vm_purgable_control(mach_task_self(), | 128 kern_return_t ret = vm_purgable_control(mach_task_self(), |
121 memory_.address(), | 129 memory_.address(), |
122 VM_PURGABLE_SET_STATE, | 130 VM_PURGABLE_SET_STATE, |
123 &state); | 131 &state); |
124 MACH_CHECK(ret == KERN_SUCCESS, ret) << "vm_purgable_control"; | 132 MACH_CHECK(ret == KERN_SUCCESS, ret) << "vm_purgable_control"; |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
205 } | 213 } |
206 | 214 |
207 // static | 215 // static |
208 void DiscardableMemory::PurgeForTesting() { | 216 void DiscardableMemory::PurgeForTesting() { |
209 int state = 0; | 217 int state = 0; |
210 vm_purgable_control(mach_task_self(), 0, VM_PURGABLE_PURGE_ALL, &state); | 218 vm_purgable_control(mach_task_self(), 0, VM_PURGABLE_PURGE_ALL, &state); |
211 internal::DiscardableMemoryEmulated::PurgeForTesting(); | 219 internal::DiscardableMemoryEmulated::PurgeForTesting(); |
212 } | 220 } |
213 | 221 |
214 } // namespace base | 222 } // namespace base |
OLD | NEW |