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

Side by Side Diff: components/html_viewer/discardable_memory_allocator.cc

Issue 1189833005: Revert of Support impl-side painting in Mandoline. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 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
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 "components/html_viewer/discardable_memory_allocator.h" 5 #include "components/html_viewer/discardable_memory_allocator.h"
6 6
7 #include "base/memory/discardable_memory.h" 7 #include "base/memory/discardable_memory.h"
8 #include "base/memory/weak_ptr.h"
8 #include "base/stl_util.h" 9 #include "base/stl_util.h"
9 10
10 namespace html_viewer { 11 namespace html_viewer {
11 12
12 // Interface to the rest of the program. These objects are owned outside of the 13 // Represents an actual memory chunk. This is an object owned by
13 // allocator. 14 // DiscardableMemoryAllocator. DiscardableMemoryChunkImpl are passed to the
14 class DiscardableMemoryAllocator::DiscardableMemoryChunkImpl 15 // rest of the program, and access this memory through a weak ptr.
15 : public base::DiscardableMemory { 16 class DiscardableMemoryAllocator::OwnedMemoryChunk {
16 public: 17 public:
17 DiscardableMemoryChunkImpl(size_t size, DiscardableMemoryAllocator* allocator) 18 OwnedMemoryChunk(size_t size, DiscardableMemoryAllocator* allocator)
18 : is_locked_(true), 19 : is_locked_(true),
19 size_(size), 20 size_(size),
20 data_(new uint8_t[size]), 21 data_(new uint8_t[size]),
21 allocator_(allocator) {} 22 allocator_(allocator),
23 weak_factory_(this) {}
24 ~OwnedMemoryChunk() {}
22 25
23 ~DiscardableMemoryChunkImpl() override { 26 void Lock() {
24 // Either the memory is discarded or the memory chunk is unlocked. 27 DCHECK(!is_locked_);
25 DCHECK(data_ || !is_locked_); 28 is_locked_ = true;
26 if (!is_locked_ && data_) 29 allocator_->NotifyLocked(unlocked_position_);
27 allocator_->NotifyDestructed(unlocked_position_);
28 } 30 }
29 31
30 // Overridden from DiscardableMemoryChunk: 32 void Unlock() {
31 bool Lock() override {
32 DCHECK(!is_locked_);
33 if (!data_)
34 return false;
35
36 is_locked_ = true;
37 allocator_->NotifyLocked(unlocked_position_);
38 return true;
39 }
40
41 void Unlock() override {
42 DCHECK(is_locked_); 33 DCHECK(is_locked_);
43 DCHECK(data_);
44 is_locked_ = false; 34 is_locked_ = false;
45 unlocked_position_ = allocator_->NotifyUnlocked(this); 35 unlocked_position_ = allocator_->NotifyUnlocked(this);
46 } 36 }
47 37
48 void* data() const override { 38 bool is_locked() const { return is_locked_; }
49 if (data_) { 39 size_t size() const { return size_; }
50 DCHECK(is_locked_); 40 void* data() const {
51 return data_.get(); 41 DCHECK(is_locked_);
52 } 42 return data_.get();
53 return nullptr;
54 } 43 }
55 44
56 size_t size() const { return size_; } 45 base::WeakPtr<OwnedMemoryChunk> GetWeakPtr() {
57 46 return weak_factory_.GetWeakPtr();
58 void Discard() {
59 DCHECK(!is_locked_);
60 data_.reset();
61 } 47 }
62 48
63 private: 49 private:
64 bool is_locked_; 50 bool is_locked_;
65 size_t size_; 51 size_t size_;
66 scoped_ptr<uint8_t[]> data_; 52 scoped_ptr<uint8_t[]> data_;
67 DiscardableMemoryAllocator* allocator_; 53 DiscardableMemoryAllocator* allocator_;
68 54
69 std::list<DiscardableMemoryChunkImpl*>::iterator unlocked_position_; 55 std::list<OwnedMemoryChunk*>::iterator unlocked_position_;
56
57 base::WeakPtrFactory<OwnedMemoryChunk> weak_factory_;
58
59 DISALLOW_IMPLICIT_CONSTRUCTORS(OwnedMemoryChunk);
60 };
61
62 namespace {
63
64 // Interface to the rest of the program. These objects are owned outside of the
65 // allocator and wrap a weak ptr.
66 class DiscardableMemoryChunkImpl : public base::DiscardableMemory {
67 public:
68 explicit DiscardableMemoryChunkImpl(
69 base::WeakPtr<DiscardableMemoryAllocator::OwnedMemoryChunk> chunk)
70 : memory_chunk_(chunk) {}
71 ~DiscardableMemoryChunkImpl() override {
72 // Either the memory chunk is invalid (because the backing has gone away),
73 // or the memory chunk is unlocked (because leaving the chunk locked once
74 // we deallocate means the chunk will never get cleaned up).
75 DCHECK(!memory_chunk_ || !memory_chunk_->is_locked());
76 }
77
78 // Overridden from DiscardableMemoryChunk:
79 bool Lock() override {
80 if (!memory_chunk_)
81 return false;
82
83 memory_chunk_->Lock();
84 return true;
85 }
86
87 void Unlock() override {
88 DCHECK(memory_chunk_);
89 memory_chunk_->Unlock();
90 }
91
92 void* data() const override {
93 if (memory_chunk_)
94 return memory_chunk_->data();
95 return nullptr;
96 }
97
98 private:
99 base::WeakPtr<DiscardableMemoryAllocator::OwnedMemoryChunk> memory_chunk_;
70 100
71 DISALLOW_IMPLICIT_CONSTRUCTORS(DiscardableMemoryChunkImpl); 101 DISALLOW_IMPLICIT_CONSTRUCTORS(DiscardableMemoryChunkImpl);
72 }; 102 };
73 103
104 } // namespace
105
74 DiscardableMemoryAllocator::DiscardableMemoryAllocator( 106 DiscardableMemoryAllocator::DiscardableMemoryAllocator(
75 size_t desired_max_memory) 107 size_t desired_max_memory)
76 : desired_max_memory_(desired_max_memory), 108 : desired_max_memory_(desired_max_memory),
77 total_live_memory_(0u), 109 total_live_memory_(0u),
78 locked_chunks_(0) { 110 locked_chunks_(0) {
79 } 111 }
80 112
81 DiscardableMemoryAllocator::~DiscardableMemoryAllocator() { 113 DiscardableMemoryAllocator::~DiscardableMemoryAllocator() {
82 DCHECK_EQ(0, locked_chunks_); 114 DCHECK_EQ(0, locked_chunks_);
83 STLDeleteElements(&live_unlocked_chunks_); 115 STLDeleteElements(&live_unlocked_chunks_);
84 } 116 }
85 117
86 scoped_ptr<base::DiscardableMemory> 118 scoped_ptr<base::DiscardableMemory>
87 DiscardableMemoryAllocator::AllocateLockedDiscardableMemory(size_t size) { 119 DiscardableMemoryAllocator::AllocateLockedDiscardableMemory(size_t size) {
88 base::AutoLock lock(lock_); 120 OwnedMemoryChunk* chunk = new OwnedMemoryChunk(size, this);
89 scoped_ptr<DiscardableMemoryChunkImpl> chunk(
90 new DiscardableMemoryChunkImpl(size, this));
91 total_live_memory_ += size; 121 total_live_memory_ += size;
92 locked_chunks_++; 122 locked_chunks_++;
93 123
94 // Go through the list of unlocked live chunks starting from the least 124 // Go through the list of unlocked live chunks starting from the least
95 // recently used, freeing as many as we can until we get our size under the 125 // recently used, freeing as many as we can until we get our size under the
96 // desired maximum. 126 // desired maximum.
97 auto it = live_unlocked_chunks_.begin(); 127 auto it = live_unlocked_chunks_.begin();
98 while (total_live_memory_ > desired_max_memory_ && 128 while (total_live_memory_ > desired_max_memory_ &&
99 it != live_unlocked_chunks_.end()) { 129 it != live_unlocked_chunks_.end()) {
100 total_live_memory_ -= (*it)->size(); 130 total_live_memory_ -= (*it)->size();
101 (*it)->Discard(); 131 delete *it;
102 it = live_unlocked_chunks_.erase(it); 132 it = live_unlocked_chunks_.erase(it);
103 } 133 }
104 134
105 return chunk.Pass(); 135 return make_scoped_ptr(new DiscardableMemoryChunkImpl(chunk->GetWeakPtr()));
106 } 136 }
107 137
108 std::list<DiscardableMemoryAllocator::DiscardableMemoryChunkImpl*>::iterator 138 std::list<DiscardableMemoryAllocator::OwnedMemoryChunk*>::iterator
109 DiscardableMemoryAllocator::NotifyUnlocked(DiscardableMemoryChunkImpl* chunk) { 139 DiscardableMemoryAllocator::NotifyUnlocked(
110 base::AutoLock lock(lock_); 140 DiscardableMemoryAllocator::OwnedMemoryChunk* chunk) {
111 locked_chunks_--; 141 locked_chunks_--;
112 return live_unlocked_chunks_.insert(live_unlocked_chunks_.end(), chunk); 142 return live_unlocked_chunks_.insert(live_unlocked_chunks_.end(), chunk);
113 } 143 }
114 144
115 void DiscardableMemoryAllocator::NotifyLocked( 145 void DiscardableMemoryAllocator::NotifyLocked(
116 std::list<DiscardableMemoryChunkImpl*>::iterator it) { 146 std::list<OwnedMemoryChunk*>::iterator it) {
117 base::AutoLock lock(lock_);
118 locked_chunks_++; 147 locked_chunks_++;
119 live_unlocked_chunks_.erase(it); 148 live_unlocked_chunks_.erase(it);
120 } 149 }
121 150
122 void DiscardableMemoryAllocator::NotifyDestructed(
123 std::list<DiscardableMemoryChunkImpl*>::iterator it) {
124 base::AutoLock lock(lock_);
125 live_unlocked_chunks_.erase(it);
126 }
127
128 } // namespace html_viewer 151 } // namespace html_viewer
OLDNEW
« no previous file with comments | « components/html_viewer/discardable_memory_allocator.h ('k') | components/html_viewer/discardable_memory_allocator_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698