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

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

Issue 1001833005: Update from https://crrev.com/320343 (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Supress Created 5 years, 9 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
« no previous file with comments | « base/memory/discardable_memory_emulated.h ('k') | base/memory/discardable_memory_linux.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 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.
4
5 #include "base/memory/discardable_memory_emulated.h"
6
7 #include "base/lazy_instance.h"
8 #include "base/memory/discardable_memory_manager.h"
9
10 namespace base {
11 namespace {
12
13 // This is admittedly pretty magical.
14 const size_t kEmulatedMemoryLimit = 512 * 1024 * 1024;
15 const size_t kEmulatedSoftMemoryLimit = 32 * 1024 * 1024;
16 const size_t kEmulatedHardMemoryLimitExpirationTimeMs = 1000;
17
18 // internal::DiscardableMemoryManager has an explicit constructor that takes
19 // a number of memory limit parameters. The LeakyLazyInstanceTraits doesn't
20 // handle the case. Thus, we need our own class here.
21 struct DiscardableMemoryManagerLazyInstanceTraits {
22 // Leaky as discardable memory clients can use this after the exit handler
23 // has been called.
24 static const bool kRegisterOnExit = false;
25 #ifndef NDEBUG
26 static const bool kAllowedToAccessOnNonjoinableThread = true;
27 #endif
28
29 static internal::DiscardableMemoryManager* New(void* instance) {
30 return new (instance) internal::DiscardableMemoryManager(
31 kEmulatedMemoryLimit,
32 kEmulatedSoftMemoryLimit,
33 TimeDelta::FromMilliseconds(kEmulatedHardMemoryLimitExpirationTimeMs));
34 }
35 static void Delete(internal::DiscardableMemoryManager* instance) {
36 instance->~DiscardableMemoryManager();
37 }
38 };
39
40 LazyInstance<internal::DiscardableMemoryManager,
41 DiscardableMemoryManagerLazyInstanceTraits>
42 g_manager = LAZY_INSTANCE_INITIALIZER;
43
44 } // namespace
45
46 namespace internal {
47
48 DiscardableMemoryEmulated::DiscardableMemoryEmulated(size_t bytes)
49 : bytes_(bytes),
50 is_locked_(false) {
51 g_manager.Pointer()->Register(this, bytes);
52 }
53
54 DiscardableMemoryEmulated::~DiscardableMemoryEmulated() {
55 if (is_locked_)
56 Unlock();
57 g_manager.Pointer()->Unregister(this);
58 }
59
60 // static
61 bool DiscardableMemoryEmulated::ReduceMemoryUsage() {
62 return g_manager.Pointer()->ReduceMemoryUsage();
63 }
64
65 // static
66 void DiscardableMemoryEmulated::ReduceMemoryUsageUntilWithinLimit(
67 size_t bytes) {
68 g_manager.Pointer()->ReduceMemoryUsageUntilWithinLimit(bytes);
69 }
70
71 bool DiscardableMemoryEmulated::Initialize() {
72 return Lock() != DISCARDABLE_MEMORY_LOCK_STATUS_FAILED;
73 }
74
75 DiscardableMemoryLockStatus DiscardableMemoryEmulated::Lock() {
76 DCHECK(!is_locked_);
77
78 bool purged = false;
79 if (!g_manager.Pointer()->AcquireLock(this, &purged))
80 return DISCARDABLE_MEMORY_LOCK_STATUS_FAILED;
81
82 is_locked_ = true;
83 return purged ? DISCARDABLE_MEMORY_LOCK_STATUS_PURGED
84 : DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS;
85 }
86
87 void DiscardableMemoryEmulated::Unlock() {
88 DCHECK(is_locked_);
89 g_manager.Pointer()->ReleaseLock(this);
90 is_locked_ = false;
91 }
92
93 void* DiscardableMemoryEmulated::Memory() const {
94 DCHECK(is_locked_);
95 DCHECK(memory_);
96 return memory_.get();
97 }
98
99 bool DiscardableMemoryEmulated::AllocateAndAcquireLock() {
100 if (memory_)
101 return true;
102
103 memory_.reset(new uint8[bytes_]);
104 return false;
105 }
106
107 void DiscardableMemoryEmulated::Purge() {
108 memory_.reset();
109 }
110
111 } // namespace internal
112 } // namespace base
OLDNEW
« no previous file with comments | « base/memory/discardable_memory_emulated.h ('k') | base/memory/discardable_memory_linux.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698