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

Side by Side Diff: content/renderer/render_process_discardable_memory_provider.cc

Issue 15650016: [Not for review] Discardable memory emulation (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 7 years, 6 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 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 "content/renderer/render_process_discardable_memory_provider.h"
6
7 namespace content {
8
9 namespace {
10
11 class EmulatedDiscardableMemory : public base::DiscardableMemory {
12 public:
13 EmulatedDiscardableMemory(RenderProcessDiscardableMemoryProvider* provider)
14 : provider_(provider) {
15 }
16
17 virtual ~EmulatedDiscardableMemory() {
18 }
19
20 virtual bool InitializeAndLock(size_t size) OVERRIDE {
21 if (provider_->Register(this, size))
22 return Lock() != base::DISCARDABLE_MEMORY_FAILED;
23 return false;
24 }
25
26 virtual base::LockDiscardableMemoryStatus Lock() OVERRIDE {
27 return provider_->Lock(this);
28 }
29
30 virtual void Unlock() OVERRIDE {
31 provider_->Unlock(this);
32 }
33
34 virtual void* Memory() const OVERRIDE {
35 return provider_->Memory(this);
36 }
37
38 private:
39 RenderProcessDiscardableMemoryProvider* provider_;
40
41 DISALLOW_COPY_AND_ASSIGN(EmulatedDiscardableMemory);
42 };
43
44 } // namespace
45
46 RenderProcessDiscardableMemoryProvider::
47 RenderProcessDiscardableMemoryProvider()
48 : visible_(false) {
49 }
50
51 RenderProcessDiscardableMemoryProvider::
52 ~RenderProcessDiscardableMemoryProvider() {
53 AllocationMap::iterator it = allocations_.begin();
54 for (; it != allocations_.end(); ++it)
55 if (it->second.memory != NULL)
56 free(it->second.memory);
57 }
58
59 void RenderProcessDiscardableMemoryProvider::RenderProcessVisibilityChanged(
60 bool visible) {
61 visible_ = visible;
62 EnforcePolicy();
63 }
64
65 bool RenderProcessDiscardableMemoryProvider::Register(
66 const base::DiscardableMemory* discardable, size_t size) {
67 Unregister(discardable);
68 AllocatedMemory allocation = { NULL, false, size };
69 allocation.memory = malloc(size * sizeof(char));
70 if (allocation.memory == NULL)
71 return false;
72 allocations_[discardable] = allocation;
73 return true;
74 }
75
76 void RenderProcessDiscardableMemoryProvider::Unregister(
77 const base::DiscardableMemory* discardable) {
78 AllocationMap::iterator it = allocations_.find(discardable);
79 if (it == allocations_.end())
80 return;
81 DCHECK(!it->second.locked);
82 if (it->second.memory != NULL)
83 free(it->second.memory);
84 allocations_.erase(it);
85 }
86
87 base::LockDiscardableMemoryStatus RenderProcessDiscardableMemoryProvider::Lock(
88 const base::DiscardableMemory* discardable) {
89 AllocationMap::iterator it = allocations_.find(discardable);
90 if (it == allocations_.end())
91 return base::DISCARDABLE_MEMORY_FAILED;
92
93 it->second.locked = true;
94 if (it->second.memory != NULL)
95 return base::DISCARDABLE_MEMORY_SUCCESS;
96
97 it->second.memory = malloc(it->second.size * sizeof(char));
98 return base::DISCARDABLE_MEMORY_PURGED;
99 }
100
101 void RenderProcessDiscardableMemoryProvider::Unlock(
102 const base::DiscardableMemory* discardable) {
103 AllocationMap::iterator it = allocations_.find(discardable);
104 if (it == allocations_.end())
105 return;
106 it->second.locked = false;
107 }
108
109 void* RenderProcessDiscardableMemoryProvider::Memory(
110 const base::DiscardableMemory* discardable) const {
111 AllocationMap::const_iterator it = allocations_.find(discardable);
112 DCHECK(it->second.locked);
113 if (it == allocations_.end())
114 return NULL;
115 return it->second.memory;
116 }
117
118 base::DiscardableMemory* RenderProcessDiscardableMemoryProvider::
119 CreateDiscardableMemory() {
120 return new EmulatedDiscardableMemory(this);
121 }
122
123 bool RenderProcessDiscardableMemoryProvider::PurgeForTestingSupported() const {
124 return true;
125 }
126
127 void RenderProcessDiscardableMemoryProvider::PurgeForTesting() {
128 Purge();
129 }
130
131 void RenderProcessDiscardableMemoryProvider::Purge() {
132 AllocationMap::iterator it = allocations_.begin();
133 for (; it != allocations_.end(); ++it) {
134 if (it->second.memory != NULL && !it->second.locked) {
135 free(it->second.memory);
136 it->second.memory = NULL;
137 }
138 }
139 }
140
141 // We could be smarter here and use a MRU scheme and memory limits. Instead,
142 // we'll just purge everything when the process is no longer visible.
143 void RenderProcessDiscardableMemoryProvider::EnforcePolicy() {
144 if (!visible_)
145 Purge();
146 }
147
148 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698