blob: 7f362239b2d5f0c4bebd6be4c01913b4f0ee06a4 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/html_viewer/discardable_memory_allocator.h"
#include "base/memory/discardable_memory.h"
#include "base/stl_util.h"
namespace html_viewer {
// Interface to the rest of the program. These objects are owned outside of the
// allocator.
class DiscardableMemoryAllocator::DiscardableMemoryChunkImpl
: public base::DiscardableMemory {
public:
DiscardableMemoryChunkImpl(size_t size, DiscardableMemoryAllocator* allocator)
: is_locked_(true),
size_(size),
data_(new uint8_t[size]),
allocator_(allocator) {}
~DiscardableMemoryChunkImpl() override {
// Either the memory is discarded or the memory chunk is unlocked.
DCHECK(data_ || !is_locked_);
if (!is_locked_ && data_)
allocator_->NotifyDestructed(unlocked_position_);
}
// Overridden from DiscardableMemoryChunk:
bool Lock() override {
DCHECK(!is_locked_);
if (!data_)
return false;
is_locked_ = true;
allocator_->NotifyLocked(unlocked_position_);
return true;
}
void Unlock() override {
DCHECK(is_locked_);
DCHECK(data_);
is_locked_ = false;
unlocked_position_ = allocator_->NotifyUnlocked(this);
}
void* data() const override {
if (data_) {
DCHECK(is_locked_);
return data_.get();
}
return nullptr;
}
size_t size() const { return size_; }
void Discard() {
DCHECK(!is_locked_);
data_.reset();
}
private:
bool is_locked_;
size_t size_;
scoped_ptr<uint8_t[]> data_;
DiscardableMemoryAllocator* allocator_;
std::list<DiscardableMemoryChunkImpl*>::iterator unlocked_position_;
DISALLOW_IMPLICIT_CONSTRUCTORS(DiscardableMemoryChunkImpl);
};
DiscardableMemoryAllocator::DiscardableMemoryAllocator(
size_t desired_max_memory)
: desired_max_memory_(desired_max_memory),
total_live_memory_(0u),
locked_chunks_(0) {
}
DiscardableMemoryAllocator::~DiscardableMemoryAllocator() {
DCHECK_EQ(0, locked_chunks_);
STLDeleteElements(&live_unlocked_chunks_);
}
scoped_ptr<base::DiscardableMemory>
DiscardableMemoryAllocator::AllocateLockedDiscardableMemory(size_t size) {
base::AutoLock lock(lock_);
scoped_ptr<DiscardableMemoryChunkImpl> chunk(
new DiscardableMemoryChunkImpl(size, this));
total_live_memory_ += size;
locked_chunks_++;
// Go through the list of unlocked live chunks starting from the least
// recently used, freeing as many as we can until we get our size under the
// desired maximum.
auto it = live_unlocked_chunks_.begin();
while (total_live_memory_ > desired_max_memory_ &&
it != live_unlocked_chunks_.end()) {
total_live_memory_ -= (*it)->size();
(*it)->Discard();
it = live_unlocked_chunks_.erase(it);
}
return chunk.Pass();
}
std::list<DiscardableMemoryAllocator::DiscardableMemoryChunkImpl*>::iterator
DiscardableMemoryAllocator::NotifyUnlocked(DiscardableMemoryChunkImpl* chunk) {
base::AutoLock lock(lock_);
locked_chunks_--;
return live_unlocked_chunks_.insert(live_unlocked_chunks_.end(), chunk);
}
void DiscardableMemoryAllocator::NotifyLocked(
std::list<DiscardableMemoryChunkImpl*>::iterator it) {
base::AutoLock lock(lock_);
locked_chunks_++;
live_unlocked_chunks_.erase(it);
}
void DiscardableMemoryAllocator::NotifyDestructed(
std::list<DiscardableMemoryChunkImpl*>::iterator it) {
base::AutoLock lock(lock_);
live_unlocked_chunks_.erase(it);
}
} // namespace html_viewer
|