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

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 RenderProcessDiscardableMemoryProvider::
10 RenderProcessDiscardableMemoryProvider()
11 : visible_(false) {
12 }
13
14 RenderProcessDiscardableMemoryProvider::
15 ~RenderProcessDiscardableMemoryProvider() {
16 AllocationMap::iterator it = allocations_.begin();
17 for (; it != allocations_.end(); ++it)
18 if (it->second.memory != NULL)
19 free(it->second.memory);
20 }
21
22 void RenderProcessDiscardableMemoryProvider::RenderProcessVisibilityChanged(
23 bool visible) {
24 visible_ = visible;
25 EnforcePolicy();
26 }
27
28 bool RenderProcessDiscardableMemoryProvider::Register(
29 const base::DiscardableMemory* discardable, size_t size) {
30 Unregister(discardable);
31 AllocatedMemory allocation = { NULL, false, size };
32 allocation.memory = malloc(size * sizeof(char));
33 if (allocation.memory == NULL)
34 return false;
35 allocations_[discardable] = allocation;
36 return true;
37 }
38
39 void RenderProcessDiscardableMemoryProvider::Unregister(
40 const base::DiscardableMemory* discardable) {
41 AllocationMap::iterator it = allocations_.find(discardable);
42 if (it == allocations_.end())
43 return;
jonathan.backer 2013/05/29 14:02:54 DCHECK(!it->second.locked)?
Ian Vollick 2013/06/06 00:21:47 Done.
44 if (it->second.memory != NULL)
45 free(it->second.memory);
46 allocations_.erase(it);
47 }
48
49 base::LockDiscardableMemoryStatus RenderProcessDiscardableMemoryProvider::Lock(
50 const base::DiscardableMemory* discardable) {
51 AllocationMap::iterator it = allocations_.find(discardable);
52 if (it == allocations_.end())
53 return base::DISCARDABLE_MEMORY_FAILED;
54
55 it->second.locked = true;
56 if (it->second.memory != NULL)
57 return base::DISCARDABLE_MEMORY_SUCCESS;
58
59 it->second.memory = malloc(it->second.size * sizeof(char));
60 return base::DISCARDABLE_MEMORY_PURGED;
61 }
62
63 void RenderProcessDiscardableMemoryProvider::Unlock(
64 const base::DiscardableMemory* discardable) {
65 AllocationMap::iterator it = allocations_.find(discardable);
66 if (it == allocations_.end())
67 return;
68 it->second.locked = false;
69 }
70
71 void* RenderProcessDiscardableMemoryProvider::Memory(
72 const base::DiscardableMemory* discardable) const {
73 AllocationMap::const_iterator it = allocations_.find(discardable);
74 if (it == allocations_.end())
75 return NULL;
jonathan.backer 2013/05/29 14:02:54 DCHECK(it->second.locked)?
Ian Vollick 2013/06/06 00:21:47 Done.
76 return it->second.memory;
77 }
78
79 bool RenderProcessDiscardableMemoryProvider::PurgeForTestingSupported() const {
80 return true;
81 }
82
83 void RenderProcessDiscardableMemoryProvider::PurgeForTesting() {
84 Purge();
85 }
86
87 void RenderProcessDiscardableMemoryProvider::Purge() {
88 AllocationMap::iterator it = allocations_.begin();
89 for (; it != allocations_.end(); ++it) {
90 if (it->second.memory != NULL && !it->second.locked) {
91 free(it->second.memory);
92 it->second.memory = NULL;
93 }
94 }
95 }
96
97 // We could be smarter here and use a MRU scheme and memory limits. Instead,
98 // we'll just purge everything when the process is no longer visible.
99 void RenderProcessDiscardableMemoryProvider::EnforcePolicy() {
100 if (!visible_)
101 Purge();
102 }
103
104 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698