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
3 // found in the LICENSE file.
5 #include "base/memory/discardable_memory.h"
9 #include "base/basictypes.h"
10 #include "base/compiler_specific.h"
11 #include "base/lazy_instance.h"
12 #include "base/logging.h"
13 #include "base/mac/mach_logging.h"
14 #include "base/mac/scoped_mach_vm.h"
15 #include "base/memory/discardable_memory_emulated.h"
16 #include "base/memory/discardable_memory_malloc.h"
17 #include "base/memory/discardable_memory_manager.h"
18 #include "base/memory/scoped_ptr.h"
23 // For Mac, have the DiscardableMemoryManager trigger userspace eviction when
24 // address space usage gets too high (e.g. 512 MBytes).
25 const size_t kMacMemoryLimit
= 512 * 1024 * 1024;
28 SharedState() : manager(kMacMemoryLimit
, kMacMemoryLimit
, TimeDelta::Max()) {}
30 internal::DiscardableMemoryManager manager
;
32 LazyInstance
<SharedState
>::Leaky g_shared_state
= LAZY_INSTANCE_INITIALIZER
;
34 // The VM subsystem allows tagging of memory and 240-255 is reserved for
35 // application use (see mach/vm_statistics.h). Pick 252 (after chromium's atomic
37 const int kDiscardableMemoryTag
= VM_MAKE_TAG(252);
39 class DiscardableMemoryMac
40 : public DiscardableMemory
,
41 public internal::DiscardableMemoryManagerAllocation
{
43 explicit DiscardableMemoryMac(size_t bytes
)
45 bytes_(mach_vm_round_page(bytes
)),
47 g_shared_state
.Pointer()->manager
.Register(this, bytes
);
50 bool Initialize() { return Lock() != DISCARDABLE_MEMORY_LOCK_STATUS_FAILED
; }
52 virtual ~DiscardableMemoryMac() {
55 g_shared_state
.Pointer()->manager
.Unregister(this);
58 // Overridden from DiscardableMemory:
59 virtual DiscardableMemoryLockStatus
Lock() OVERRIDE
{
63 if (!g_shared_state
.Pointer()->manager
.AcquireLock(this, &purged
))
64 return DISCARDABLE_MEMORY_LOCK_STATUS_FAILED
;
67 return purged
? DISCARDABLE_MEMORY_LOCK_STATUS_PURGED
68 : DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS
;
71 virtual void Unlock() OVERRIDE
{
73 g_shared_state
.Pointer()->manager
.ReleaseLock(this);
77 virtual void* Memory() const OVERRIDE
{
79 return reinterpret_cast<void*>(memory_
.address());
82 // Overridden from internal::DiscardableMemoryManagerAllocation:
83 virtual bool AllocateAndAcquireLock() OVERRIDE
{
86 if (!memory_
.size()) {
87 vm_address_t address
= 0;
92 VM_FLAGS_ANYWHERE
| VM_FLAGS_PURGABLE
| kDiscardableMemoryTag
);
93 MACH_CHECK(ret
== KERN_SUCCESS
, ret
) << "vm_allocate";
94 memory_
.reset(address
, bytes_
);
96 // When making a fresh allocation, it's impossible for |persistent| to
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.
105 ret
= vm_protect(mach_task_self(),
110 MACH_DCHECK(ret
== KERN_SUCCESS
, ret
) << "vm_protect";
114 int state
= VM_PURGABLE_NONVOLATILE
;
115 ret
= vm_purgable_control(mach_task_self(),
117 VM_PURGABLE_SET_STATE
,
119 MACH_CHECK(ret
== KERN_SUCCESS
, ret
) << "vm_purgable_control";
120 if (state
& VM_PURGABLE_EMPTY
)
126 virtual void ReleaseLock() OVERRIDE
{
127 int state
= VM_PURGABLE_VOLATILE
| VM_VOLATILE_GROUP_DEFAULT
;
128 kern_return_t ret
= vm_purgable_control(mach_task_self(),
130 VM_PURGABLE_SET_STATE
,
132 MACH_CHECK(ret
== KERN_SUCCESS
, ret
) << "vm_purgable_control";
135 ret
= vm_protect(mach_task_self(),
140 MACH_DCHECK(ret
== KERN_SUCCESS
, ret
) << "vm_protect";
144 virtual void Purge() OVERRIDE
{
149 mac::ScopedMachVM memory_
;
153 DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryMac
);
159 bool DiscardableMemory::ReduceMemoryUsage() {
160 return internal::DiscardableMemoryEmulated::ReduceMemoryUsage();
164 void DiscardableMemory::GetSupportedTypes(
165 std::vector
<DiscardableMemoryType
>* types
) {
166 const DiscardableMemoryType supported_types
[] = {
167 DISCARDABLE_MEMORY_TYPE_MAC
,
168 DISCARDABLE_MEMORY_TYPE_EMULATED
,
169 DISCARDABLE_MEMORY_TYPE_MALLOC
171 types
->assign(supported_types
, supported_types
+ arraysize(supported_types
));
175 scoped_ptr
<DiscardableMemory
> DiscardableMemory::CreateLockedMemoryWithType(
176 DiscardableMemoryType type
, size_t size
) {
178 case DISCARDABLE_MEMORY_TYPE_NONE
:
179 case DISCARDABLE_MEMORY_TYPE_ASHMEM
:
180 return scoped_ptr
<DiscardableMemory
>();
181 case DISCARDABLE_MEMORY_TYPE_MAC
: {
182 scoped_ptr
<DiscardableMemoryMac
> memory(new DiscardableMemoryMac(size
));
183 if (!memory
->Initialize())
184 return scoped_ptr
<DiscardableMemory
>();
186 return memory
.PassAs
<DiscardableMemory
>();
188 case DISCARDABLE_MEMORY_TYPE_EMULATED
: {
189 scoped_ptr
<internal::DiscardableMemoryEmulated
> memory(
190 new internal::DiscardableMemoryEmulated(size
));
191 if (!memory
->Initialize())
192 return scoped_ptr
<DiscardableMemory
>();
194 return memory
.PassAs
<DiscardableMemory
>();
196 case DISCARDABLE_MEMORY_TYPE_MALLOC
: {
197 scoped_ptr
<internal::DiscardableMemoryMalloc
> memory(
198 new internal::DiscardableMemoryMalloc(size
));
199 if (!memory
->Initialize())
200 return scoped_ptr
<DiscardableMemory
>();
202 return memory
.PassAs
<DiscardableMemory
>();
207 return scoped_ptr
<DiscardableMemory
>();
211 void DiscardableMemory::PurgeForTesting() {
213 vm_purgable_control(mach_task_self(), 0, VM_PURGABLE_PURGE_ALL
, &state
);
214 internal::DiscardableMemoryEmulated::PurgeForTesting();