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 |
10 #include "base/basictypes.h" | |
11 #include "base/compiler_specific.h" | |
9 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/memory/scoped_ptr.h" | |
10 | 14 |
11 namespace base { | 15 namespace base { |
12 | |
13 namespace { | 16 namespace { |
14 | 17 |
15 // The VM subsystem allows tagging of memory and 240-255 is reserved for | 18 // The VM subsystem allows tagging of memory and 240-255 is reserved for |
16 // application use (see mach/vm_statistics.h). Pick 252 (after chromium's atomic | 19 // application use (see mach/vm_statistics.h). Pick 252 (after chromium's atomic |
17 // weight of ~52). | 20 // weight of ~52). |
18 const int kDiscardableMemoryTag = VM_MAKE_TAG(252); | 21 const int kDiscardableMemoryTag = VM_MAKE_TAG(252); |
19 | 22 |
23 class DiscardableMemoryMac : public DiscardableMemory { | |
24 public: | |
25 DiscardableMemoryMac(void* memory, size_t size) | |
26 : memory_(memory), | |
27 size_(size) { | |
28 DCHECK(memory_); | |
29 } | |
30 | |
31 virtual ~DiscardableMemoryMac() { | |
32 vm_deallocate(mach_task_self(), | |
33 reinterpret_cast<vm_address_t>(memory_), | |
34 size_); | |
35 } | |
36 | |
37 virtual LockDiscardableMemoryStatus Lock() OVERRIDE { | |
38 DCHECK_EQ(0, mprotect(memory_, size_, PROT_READ | PROT_WRITE)); | |
39 int state = VM_PURGABLE_NONVOLATILE; | |
40 kern_return_t ret = vm_purgable_control( | |
41 mach_task_self(), | |
42 reinterpret_cast<vm_address_t>(memory_), | |
43 VM_PURGABLE_SET_STATE, | |
44 &state); | |
45 if (ret != KERN_SUCCESS) | |
46 return DISCARDABLE_MEMORY_FAILED; | |
47 return state & VM_PURGABLE_EMPTY ? DISCARDABLE_MEMORY_PURGED | |
Avi (use Gerrit)
2013/10/17 17:58:55
I liked the extra line before this one.
Philippe
2013/10/18 08:25:21
Yeah, sorry. I tend to be quite aggressive at remo
| |
48 : DISCARDABLE_MEMORY_SUCCESS; | |
49 } | |
50 | |
51 virtual void Unlock() OVERRIDE { | |
52 int state = VM_PURGABLE_VOLATILE | VM_VOLATILE_GROUP_DEFAULT; | |
53 kern_return_t ret = vm_purgable_control( | |
54 mach_task_self(), | |
55 reinterpret_cast<vm_address_t>(memory_), | |
56 VM_PURGABLE_SET_STATE, | |
57 &state); | |
58 DCHECK_EQ(0, mprotect(memory_, size_, PROT_NONE)); | |
59 if (ret != KERN_SUCCESS) | |
60 DLOG(ERROR) << "Failed to unlock memory."; | |
61 } | |
62 | |
63 virtual void* Memory() const OVERRIDE { | |
64 return memory_; | |
65 } | |
66 | |
67 private: | |
68 void* const memory_; | |
69 const size_t size_; | |
70 | |
71 DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryMac); | |
72 }; | |
73 | |
20 } // namespace | 74 } // namespace |
21 | 75 |
22 // static | 76 // static |
23 bool DiscardableMemory::Supported() { | 77 bool DiscardableMemory::Supported() { |
24 return true; | 78 return true; |
25 } | 79 } |
26 | 80 |
27 DiscardableMemory::~DiscardableMemory() { | 81 // static |
28 if (memory_) { | 82 scoped_ptr<DiscardableMemory> DiscardableMemory::CreateLockedMemory( |
29 vm_deallocate(mach_task_self(), | 83 size_t size) { |
30 reinterpret_cast<vm_address_t>(memory_), | |
31 size_); | |
32 } | |
33 } | |
34 | |
35 bool DiscardableMemory::InitializeAndLock(size_t size) { | |
36 DCHECK(!memory_); | |
37 size_ = size; | |
38 | |
39 vm_address_t buffer = 0; | 84 vm_address_t buffer = 0; |
40 kern_return_t ret = vm_allocate(mach_task_self(), | 85 kern_return_t ret = vm_allocate(mach_task_self(), |
41 &buffer, | 86 &buffer, |
42 size, | 87 size, |
43 VM_FLAGS_PURGABLE | | 88 VM_FLAGS_PURGABLE | |
44 VM_FLAGS_ANYWHERE | | 89 VM_FLAGS_ANYWHERE | |
45 kDiscardableMemoryTag); | 90 kDiscardableMemoryTag); |
46 | |
47 if (ret != KERN_SUCCESS) { | 91 if (ret != KERN_SUCCESS) { |
48 DLOG(ERROR) << "vm_allocate() failed"; | 92 DLOG(ERROR) << "vm_allocate() failed"; |
49 return false; | 93 return scoped_ptr<DiscardableMemory>(); |
50 } | 94 } |
51 | 95 return scoped_ptr<DiscardableMemory>( |
52 is_locked_ = true; | 96 new DiscardableMemoryMac(reinterpret_cast<void*>(buffer), size)); |
53 memory_ = reinterpret_cast<void*>(buffer); | |
54 return true; | |
55 } | |
56 | |
57 LockDiscardableMemoryStatus DiscardableMemory::Lock() { | |
58 DCHECK(!is_locked_); | |
59 | |
60 int state = VM_PURGABLE_NONVOLATILE; | |
61 kern_return_t ret = vm_purgable_control( | |
62 mach_task_self(), | |
63 reinterpret_cast<vm_address_t>(memory_), | |
64 VM_PURGABLE_SET_STATE, | |
65 &state); | |
66 | |
67 if (ret != KERN_SUCCESS) | |
68 return DISCARDABLE_MEMORY_FAILED; | |
69 | |
70 is_locked_ = true; | |
71 return state & VM_PURGABLE_EMPTY ? DISCARDABLE_MEMORY_PURGED | |
72 : DISCARDABLE_MEMORY_SUCCESS; | |
73 } | |
74 | |
75 void DiscardableMemory::Unlock() { | |
76 DCHECK(is_locked_); | |
77 | |
78 int state = VM_PURGABLE_VOLATILE | VM_VOLATILE_GROUP_DEFAULT; | |
79 kern_return_t ret = vm_purgable_control( | |
80 mach_task_self(), | |
81 reinterpret_cast<vm_address_t>(memory_), | |
82 VM_PURGABLE_SET_STATE, | |
83 &state); | |
84 | |
85 if (ret != KERN_SUCCESS) | |
86 DLOG(ERROR) << "Failed to unlock memory."; | |
87 | |
88 is_locked_ = false; | |
89 } | 97 } |
90 | 98 |
91 // static | 99 // static |
92 bool DiscardableMemory::PurgeForTestingSupported() { | 100 bool DiscardableMemory::PurgeForTestingSupported() { |
93 return true; | 101 return true; |
94 } | 102 } |
95 | 103 |
96 // static | 104 // static |
97 void DiscardableMemory::PurgeForTesting() { | 105 void DiscardableMemory::PurgeForTesting() { |
98 int state = 0; | 106 int state = 0; |
99 vm_purgable_control(mach_task_self(), 0, VM_PURGABLE_PURGE_ALL, &state); | 107 vm_purgable_control(mach_task_self(), 0, VM_PURGABLE_PURGE_ALL, &state); |
100 } | 108 } |
101 | 109 |
102 } // namespace base | 110 } // namespace base |
OLD | NEW |