OLD | NEW |
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 "mojo/services/html_viewer/discardable_memory_allocator.h" | 5 #include "mojo/services/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/memory/weak_ptr.h" |
9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
10 | 10 |
11 namespace html_viewer { | 11 namespace html_viewer { |
12 | 12 |
13 // Represents an actual memory chunk. This is an object owned by | 13 // Represents an actual memory chunk. This is an object owned by |
14 // DiscardableMemoryAllocator. DiscardableMemoryChunkImpl are passed to the | 14 // DiscardableMemoryAllocator. DiscardableMemoryChunkImpl are passed to the |
15 // rest of the program, and access this memory through a weak ptr. | 15 // rest of the program, and access this memory through a weak ptr. |
16 class DiscardableMemoryAllocator::OwnedMemoryChunk { | 16 class DiscardableMemoryAllocator::OwnedMemoryChunk { |
17 public: | 17 public: |
18 OwnedMemoryChunk(size_t size, DiscardableMemoryAllocator* allocator) | 18 OwnedMemoryChunk(size_t size, DiscardableMemoryAllocator* allocator) |
19 : is_locked_(true), | 19 : is_locked_(true), |
20 size_(size), | 20 size_(size), |
21 memory_(new uint8_t[size]), | 21 data_(new uint8_t[size]), |
22 allocator_(allocator), | 22 allocator_(allocator), |
23 weak_factory_(this) {} | 23 weak_factory_(this) {} |
24 ~OwnedMemoryChunk() {} | 24 ~OwnedMemoryChunk() {} |
25 | 25 |
26 void Lock() { | 26 void Lock() { |
27 DCHECK(!is_locked_); | 27 DCHECK(!is_locked_); |
28 is_locked_ = true; | 28 is_locked_ = true; |
29 allocator_->NotifyLocked(unlocked_position_); | 29 allocator_->NotifyLocked(unlocked_position_); |
30 } | 30 } |
31 | 31 |
32 void Unlock() { | 32 void Unlock() { |
33 DCHECK(is_locked_); | 33 DCHECK(is_locked_); |
34 is_locked_ = false; | 34 is_locked_ = false; |
35 unlocked_position_ = allocator_->NotifyUnlocked(this); | 35 unlocked_position_ = allocator_->NotifyUnlocked(this); |
36 } | 36 } |
37 | 37 |
38 bool is_locked() const { return is_locked_; } | 38 bool is_locked() const { return is_locked_; } |
39 size_t size() const { return size_; } | 39 size_t size() const { return size_; } |
40 void* Memory() const { | 40 void* data() const { |
41 DCHECK(is_locked_); | 41 DCHECK(is_locked_); |
42 return memory_.get(); | 42 return data_.get(); |
43 } | 43 } |
44 | 44 |
45 base::WeakPtr<OwnedMemoryChunk> GetWeakPtr() { | 45 base::WeakPtr<OwnedMemoryChunk> GetWeakPtr() { |
46 return weak_factory_.GetWeakPtr(); | 46 return weak_factory_.GetWeakPtr(); |
47 } | 47 } |
48 | 48 |
49 private: | 49 private: |
50 bool is_locked_; | 50 bool is_locked_; |
51 size_t size_; | 51 size_t size_; |
52 scoped_ptr<uint8_t[]> memory_; | 52 scoped_ptr<uint8_t[]> data_; |
53 DiscardableMemoryAllocator* allocator_; | 53 DiscardableMemoryAllocator* allocator_; |
54 | 54 |
55 std::list<OwnedMemoryChunk*>::iterator unlocked_position_; | 55 std::list<OwnedMemoryChunk*>::iterator unlocked_position_; |
56 | 56 |
57 base::WeakPtrFactory<OwnedMemoryChunk> weak_factory_; | 57 base::WeakPtrFactory<OwnedMemoryChunk> weak_factory_; |
58 | 58 |
59 DISALLOW_IMPLICIT_CONSTRUCTORS(OwnedMemoryChunk); | 59 DISALLOW_IMPLICIT_CONSTRUCTORS(OwnedMemoryChunk); |
60 }; | 60 }; |
61 | 61 |
62 namespace { | 62 namespace { |
(...skipping 19 matching lines...) Expand all Loading... |
82 | 82 |
83 memory_chunk_->Lock(); | 83 memory_chunk_->Lock(); |
84 return true; | 84 return true; |
85 } | 85 } |
86 | 86 |
87 void Unlock() override { | 87 void Unlock() override { |
88 DCHECK(memory_chunk_); | 88 DCHECK(memory_chunk_); |
89 memory_chunk_->Unlock(); | 89 memory_chunk_->Unlock(); |
90 } | 90 } |
91 | 91 |
92 void* Memory() const override { | 92 void* data() const override { |
93 if (memory_chunk_) | 93 if (memory_chunk_) |
94 return memory_chunk_->Memory(); | 94 return memory_chunk_->data(); |
95 return nullptr; | 95 return nullptr; |
96 } | 96 } |
97 | 97 |
98 private: | 98 private: |
99 base::WeakPtr<DiscardableMemoryAllocator::OwnedMemoryChunk> memory_chunk_; | 99 base::WeakPtr<DiscardableMemoryAllocator::OwnedMemoryChunk> memory_chunk_; |
100 | 100 |
101 DISALLOW_IMPLICIT_CONSTRUCTORS(DiscardableMemoryChunkImpl); | 101 DISALLOW_IMPLICIT_CONSTRUCTORS(DiscardableMemoryChunkImpl); |
102 }; | 102 }; |
103 | 103 |
104 } // namespace | 104 } // namespace |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 return live_unlocked_chunks_.insert(live_unlocked_chunks_.end(), chunk); | 142 return live_unlocked_chunks_.insert(live_unlocked_chunks_.end(), chunk); |
143 } | 143 } |
144 | 144 |
145 void DiscardableMemoryAllocator::NotifyLocked( | 145 void DiscardableMemoryAllocator::NotifyLocked( |
146 std::list<OwnedMemoryChunk*>::iterator it) { | 146 std::list<OwnedMemoryChunk*>::iterator it) { |
147 locked_chunks_++; | 147 locked_chunks_++; |
148 live_unlocked_chunks_.erase(it); | 148 live_unlocked_chunks_.erase(it); |
149 } | 149 } |
150 | 150 |
151 } // namespace html_viewer | 151 } // namespace html_viewer |
OLD | NEW |