 Chromium Code Reviews
 Chromium Code Reviews Issue 1016493002:
  Add a DiscardableMemoryShmemeAllocator.  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master
    
  
    Issue 1016493002:
  Add a DiscardableMemoryShmemeAllocator.  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master| OLD | NEW | 
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "mojo/services/html_viewer/discardable_memory_allocator.h" | |
| 6 | |
| 7 #include "base/memory/discardable_memory.h" | |
| 8 #include "base/memory/weak_ptr.h" | |
| 9 #include "base/stl_util.h" | |
| 10 | |
| 11 namespace html_viewer { | |
| 12 | |
| 13 // Represents an actual memory chunk. This is an object owned by | |
| 14 // DiscardableMemoryAllocator. DiscardableMemoryChunkImpl are passed to the | |
| 15 // rest of the program, and access this memory through a weak ptr. | |
| 16 class DiscardableMemoryAllocator::OwnedMemoryChunk { | |
| 17 public: | |
| 18 explicit OwnedMemoryChunk(size_t size) | |
| 19 : is_locked_(true), | |
| 20 size_(size), | |
| 21 memory_(new uint8_t[size]), | |
| 22 allocator_(nullptr), | |
| 
jamesr
2015/03/18 22:40:14
does it every make sense for allocator_ to be null
 | |
| 23 weak_factory_(this) {} | |
| 24 ~OwnedMemoryChunk() {} | |
| 25 | |
| 26 void Lock() { | |
| 27 DCHECK(!is_locked_); | |
| 28 is_locked_ = true; | |
| 29 | |
| 30 // While locked, we also move ourselves to the back of the list. | |
| 31 allocator_->MoveToBack(it_); | |
| 32 } | |
| 33 | |
| 34 void Unlock() { | |
| 35 DCHECK(is_locked_); | |
| 36 is_locked_ = false; | |
| 37 } | |
| 38 | |
| 39 bool locked() const { return is_locked_; } | |
| 40 size_t size() const { return size_; } | |
| 41 void* Memory() const { | |
| 42 DCHECK(is_locked_); | |
| 43 return memory_.get(); | |
| 44 } | |
| 45 | |
| 46 base::WeakPtr<OwnedMemoryChunk> GetWeakPtr() { | |
| 47 return weak_factory_.GetWeakPtr(); | |
| 48 } | |
| 49 | |
| 50 void SetAllocatorAndPosition(std::list<OwnedMemoryChunk*>::iterator it, | |
| 51 DiscardableMemoryAllocator* allocator) { | |
| 52 it_ = it; | |
| 53 allocator_ = allocator; | |
| 54 } | |
| 55 | |
| 56 private: | |
| 57 bool is_locked_; | |
| 58 size_t size_; | |
| 59 scoped_ptr<uint8_t[]> memory_; | |
| 60 | |
| 61 std::list<OwnedMemoryChunk*>::iterator it_; | |
| 62 DiscardableMemoryAllocator* allocator_; | |
| 63 | |
| 64 base::WeakPtrFactory<OwnedMemoryChunk> weak_factory_; | |
| 65 | |
| 66 DISALLOW_IMPLICIT_CONSTRUCTORS(OwnedMemoryChunk); | |
| 67 }; | |
| 68 | |
| 69 namespace { | |
| 70 | |
| 71 // Interface to the rest of the program. These objects are owned outside of the | |
| 72 // allocator and wrap a weak ptr. | |
| 73 class DiscardableMemoryChunkImpl : public base::DiscardableMemory { | |
| 74 public: | |
| 75 explicit DiscardableMemoryChunkImpl( | |
| 76 base::WeakPtr<DiscardableMemoryAllocator::OwnedMemoryChunk> chunk) | |
| 77 : memory_chunk_(chunk) {} | |
| 78 ~DiscardableMemoryChunkImpl() override {} | |
| 79 | |
| 80 // Overridden from DiscardableMemoryChunk: | |
| 81 bool Lock() override { | |
| 82 if (!memory_chunk_) | |
| 83 return false; | |
| 84 | |
| 85 memory_chunk_->Lock(); | |
| 86 return true; | |
| 87 } | |
| 88 | |
| 89 void Unlock() override { | |
| 90 if (memory_chunk_) | |
| 
jamesr
2015/03/18 22:40:14
seems like this should be a DCHECK since it's not
 | |
| 91 memory_chunk_->Unlock(); | |
| 92 } | |
| 93 | |
| 94 void* Memory() const override { | |
| 95 if (memory_chunk_) | |
| 96 return memory_chunk_->Memory(); | |
| 97 return nullptr; | |
| 98 } | |
| 99 | |
| 100 private: | |
| 101 base::WeakPtr<DiscardableMemoryAllocator::OwnedMemoryChunk> memory_chunk_; | |
| 102 | |
| 103 DISALLOW_IMPLICIT_CONSTRUCTORS(DiscardableMemoryChunkImpl); | |
| 104 }; | |
| 105 | |
| 106 } // namespace | |
| 107 | |
| 108 DiscardableMemoryAllocator::DiscardableMemoryAllocator(size_t max_memory) | |
| 109 : max_memory_(max_memory), | |
| 110 total_live_memory_(0) { | |
| 
jamesr
2015/03/18 22:40:14
0u
 | |
| 111 } | |
| 112 | |
| 113 DiscardableMemoryAllocator::~DiscardableMemoryAllocator() { | |
| 114 STLDeleteElements(&live_chunks_); | |
| 115 } | |
| 116 | |
| 117 scoped_ptr<base::DiscardableMemory> | |
| 118 DiscardableMemoryAllocator::AllocateLockedDiscardableMemory(size_t size) { | |
| 119 OwnedMemoryChunk* chunk = new OwnedMemoryChunk(size); | |
| 120 auto it = live_chunks_.insert(live_chunks_.end(), chunk); | |
| 121 chunk->SetAllocatorAndPosition(it, this); | |
| 122 total_live_memory_ += size; | |
| 123 | |
| 124 // Purge unlocked chunks from the cache until |total_live_memory_| is under | |
| 
jamesr
2015/03/18 22:40:14
what if the size of all currently locked chunks ex
 | |
| 125 // the desired maximum size. | |
| 126 it = live_chunks_.begin(); | |
| 127 while (total_live_memory_ > max_memory_ && it != live_chunks_.end()) { | |
| 
jamesr
2015/03/18 22:40:14
the second clause here looks like we'll break even
 
Elliot Glaysher
2015/03/18 23:54:44
I'm not sure what you're asking for here. I get th
 | |
| 128 if (!(*it)->locked()) { | |
| 129 total_live_memory_ -= (*it)->size(); | |
| 130 delete *it; | |
| 131 it = live_chunks_.erase(it); | |
| 132 } else { | |
| 133 it++; | |
| 134 } | |
| 135 } | |
| 136 | |
| 137 return make_scoped_ptr(new DiscardableMemoryChunkImpl(chunk->GetWeakPtr())); | |
| 138 } | |
| 139 | |
| 140 void DiscardableMemoryAllocator::MoveToBack( | |
| 141 std::list<OwnedMemoryChunk*>::iterator it) { | |
| 142 OwnedMemoryChunk* chunk = *it; | |
| 143 live_chunks_.erase(it); | |
| 144 it = live_chunks_.insert(live_chunks_.end(), chunk); | |
| 145 chunk->SetAllocatorAndPosition(it, this); | |
| 146 } | |
| 147 | |
| 148 } // namespace html_viewer | |
| OLD | NEW |