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

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

Issue 650233002: base: Use LazyInstanceTraits instead of SharedState class for discardable memory. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 6 years, 2 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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_mach.h" 5 #include "base/memory/discardable_memory_mach.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"
11 #include "base/lazy_instance.h" 11 #include "base/lazy_instance.h"
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "base/mac/mach_logging.h" 13 #include "base/mac/mach_logging.h"
14 14
15 namespace base { 15 namespace base {
16 namespace { 16 namespace {
17 17
18 // For Mach, have the DiscardableMemoryManager trigger userspace eviction when 18 // For Mach, have the DiscardableMemoryManager trigger userspace eviction when
19 // address space usage gets too high (e.g. 512 MBytes). 19 // address space usage gets too high (e.g. 512 MBytes).
20 const size_t kMachMemoryLimit = 512 * 1024 * 1024; 20 const size_t kMachMemoryLimit = 512 * 1024 * 1024;
21 21
22 struct SharedState { 22 // internal::DiscardableMemoryManager has an explicit constructor that takes
23 SharedState() 23 // a number of memory limit parameters. The DefaultLazyInstanceTraits doesn't
24 : manager(kMachMemoryLimit, kMachMemoryLimit, TimeDelta::Max()) {} 24 // handle the case. Thus, we need our own class here.
25 struct DiscardableMemoryManagerLazyInstanceTraits {
26 static const bool kRegisterOnExit = false;
27 #ifndef NDEBUG
28 static const bool kAllowedToAccessOnNonjoinableThread = true;
29 #endif
25 30
26 internal::DiscardableMemoryManager manager; 31 static internal::DiscardableMemoryManager* New(void* instance) {
32 return new (instance) internal::DiscardableMemoryManager(
33 kMacMemoryLimit, kMacMemoryLimit, TimeDelta::Max());
34 }
35 static void Delete(internal::DiscardableMemoryManager* instance) {
36 instance->~DiscardableMemoryManager();
37 }
27 }; 38 };
28 LazyInstance<SharedState>::Leaky g_shared_state = LAZY_INSTANCE_INITIALIZER; 39
40 LazyInstance<internal::DiscardableMemoryManager,
41 DiscardableMemoryManagerLazyInstanceTraits>
42 g_manager = LAZY_INSTANCE_INITIALIZER;
29 43
30 // The VM subsystem allows tagging of memory and 240-255 is reserved for 44 // The VM subsystem allows tagging of memory and 240-255 is reserved for
31 // application use (see mach/vm_statistics.h). Pick 252 (after chromium's atomic 45 // application use (see mach/vm_statistics.h). Pick 252 (after chromium's atomic
32 // weight of ~52). 46 // weight of ~52).
33 const int kDiscardableMemoryTag = VM_MAKE_TAG(252); 47 const int kDiscardableMemoryTag = VM_MAKE_TAG(252);
34 48
35 } // namespace 49 } // namespace
36 50
37 namespace internal { 51 namespace internal {
38 52
39 DiscardableMemoryMach::DiscardableMemoryMach(size_t bytes) 53 DiscardableMemoryMach::DiscardableMemoryMach(size_t bytes)
40 : memory_(0, 0), bytes_(mach_vm_round_page(bytes)), is_locked_(false) { 54 : memory_(0, 0), bytes_(mach_vm_round_page(bytes)), is_locked_(false) {
41 g_shared_state.Pointer()->manager.Register(this, bytes); 55 g_manager.Pointer()->Register(this, bytes);
42 } 56 }
43 57
44 DiscardableMemoryMach::~DiscardableMemoryMach() { 58 DiscardableMemoryMach::~DiscardableMemoryMach() {
45 if (is_locked_) 59 if (is_locked_)
46 Unlock(); 60 Unlock();
47 g_shared_state.Pointer()->manager.Unregister(this); 61 g_manager.Pointer()->Unregister(this);
48 } 62 }
49 63
50 // static 64 // static
51 void DiscardableMemoryMach::PurgeForTesting() { 65 void DiscardableMemoryMach::PurgeForTesting() {
52 int state = 0; 66 int state = 0;
53 vm_purgable_control(mach_task_self(), 0, VM_PURGABLE_PURGE_ALL, &state); 67 vm_purgable_control(mach_task_self(), 0, VM_PURGABLE_PURGE_ALL, &state);
54 } 68 }
55 69
56 bool DiscardableMemoryMach::Initialize() { 70 bool DiscardableMemoryMach::Initialize() {
57 return Lock() != DISCARDABLE_MEMORY_LOCK_STATUS_FAILED; 71 return Lock() != DISCARDABLE_MEMORY_LOCK_STATUS_FAILED;
58 } 72 }
59 73
60 DiscardableMemoryLockStatus DiscardableMemoryMach::Lock() { 74 DiscardableMemoryLockStatus DiscardableMemoryMach::Lock() {
61 DCHECK(!is_locked_); 75 DCHECK(!is_locked_);
62 76
63 bool purged = false; 77 bool purged = false;
64 if (!g_shared_state.Pointer()->manager.AcquireLock(this, &purged)) 78 if (!g_manager.Pointer()->AcquireLock(this, &purged))
65 return DISCARDABLE_MEMORY_LOCK_STATUS_FAILED; 79 return DISCARDABLE_MEMORY_LOCK_STATUS_FAILED;
66 80
67 is_locked_ = true; 81 is_locked_ = true;
68 return purged ? DISCARDABLE_MEMORY_LOCK_STATUS_PURGED 82 return purged ? DISCARDABLE_MEMORY_LOCK_STATUS_PURGED
69 : DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS; 83 : DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS;
70 } 84 }
71 85
72 void DiscardableMemoryMach::Unlock() { 86 void DiscardableMemoryMach::Unlock() {
73 DCHECK(is_locked_); 87 DCHECK(is_locked_);
74 g_shared_state.Pointer()->manager.ReleaseLock(this); 88 g_manager.Pointer()->ReleaseLock(this);
75 is_locked_ = false; 89 is_locked_ = false;
76 } 90 }
77 91
78 void* DiscardableMemoryMach::Memory() const { 92 void* DiscardableMemoryMach::Memory() const {
79 DCHECK(is_locked_); 93 DCHECK(is_locked_);
80 return reinterpret_cast<void*>(memory_.address()); 94 return reinterpret_cast<void*>(memory_.address());
81 } 95 }
82 96
83 bool DiscardableMemoryMach::AllocateAndAcquireLock() { 97 bool DiscardableMemoryMach::AllocateAndAcquireLock() {
84 kern_return_t ret; 98 kern_return_t ret;
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
133 MACH_DCHECK(ret == KERN_SUCCESS, ret) << "vm_protect"; 147 MACH_DCHECK(ret == KERN_SUCCESS, ret) << "vm_protect";
134 #endif 148 #endif
135 } 149 }
136 150
137 void DiscardableMemoryMach::Purge() { 151 void DiscardableMemoryMach::Purge() {
138 memory_.reset(); 152 memory_.reset();
139 } 153 }
140 154
141 } // namespace internal 155 } // namespace internal
142 } // namespace base 156 } // namespace base
OLDNEW
« base/memory/discardable_memory_emulated.cc ('K') | « base/memory/discardable_memory_emulated.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698