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