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

Unified Diff: mojo/services/html_viewer/discardable_memory_allocator.cc

Issue 1016493002: Add a DiscardableMemoryShmemeAllocator. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: And fix the unittests Created 5 years, 9 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 side-by-side diff with in-line comments
Download patch
Index: mojo/services/html_viewer/discardable_memory_allocator.cc
diff --git a/mojo/services/html_viewer/discardable_memory_allocator.cc b/mojo/services/html_viewer/discardable_memory_allocator.cc
new file mode 100644
index 0000000000000000000000000000000000000000..232005dcff87dc01a5be882620c8134440758c6f
--- /dev/null
+++ b/mojo/services/html_viewer/discardable_memory_allocator.cc
@@ -0,0 +1,148 @@
+// 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 "mojo/services/html_viewer/discardable_memory_allocator.h"
+
+#include "base/memory/discardable_memory.h"
+#include "base/memory/weak_ptr.h"
+#include "base/stl_util.h"
+
+namespace html_viewer {
+
+// Represents an actual memory chunk. This is an object owned by
+// DiscardableMemoryAllocator. DiscardableMemoryChunkImpl are passed to the
+// rest of the program, and access this memory through a weak ptr.
+class DiscardableMemoryAllocator::OwnedMemoryChunk {
+ public:
+ explicit OwnedMemoryChunk(size_t size)
+ : is_locked_(true),
+ size_(size),
+ memory_(new uint8_t[size]),
+ allocator_(nullptr),
jamesr 2015/03/18 22:40:14 does it every make sense for allocator_ to be null
+ weak_factory_(this) {}
+ ~OwnedMemoryChunk() {}
+
+ void Lock() {
+ DCHECK(!is_locked_);
+ is_locked_ = true;
+
+ // While locked, we also move ourselves to the back of the list.
+ allocator_->MoveToBack(it_);
+ }
+
+ void Unlock() {
+ DCHECK(is_locked_);
+ is_locked_ = false;
+ }
+
+ bool locked() const { return is_locked_; }
+ size_t size() const { return size_; }
+ void* Memory() const {
+ DCHECK(is_locked_);
+ return memory_.get();
+ }
+
+ base::WeakPtr<OwnedMemoryChunk> GetWeakPtr() {
+ return weak_factory_.GetWeakPtr();
+ }
+
+ void SetAllocatorAndPosition(std::list<OwnedMemoryChunk*>::iterator it,
+ DiscardableMemoryAllocator* allocator) {
+ it_ = it;
+ allocator_ = allocator;
+ }
+
+ private:
+ bool is_locked_;
+ size_t size_;
+ scoped_ptr<uint8_t[]> memory_;
+
+ std::list<OwnedMemoryChunk*>::iterator it_;
+ DiscardableMemoryAllocator* allocator_;
+
+ base::WeakPtrFactory<OwnedMemoryChunk> weak_factory_;
+
+ DISALLOW_IMPLICIT_CONSTRUCTORS(OwnedMemoryChunk);
+};
+
+namespace {
+
+// Interface to the rest of the program. These objects are owned outside of the
+// allocator and wrap a weak ptr.
+class DiscardableMemoryChunkImpl : public base::DiscardableMemory {
+ public:
+ explicit DiscardableMemoryChunkImpl(
+ base::WeakPtr<DiscardableMemoryAllocator::OwnedMemoryChunk> chunk)
+ : memory_chunk_(chunk) {}
+ ~DiscardableMemoryChunkImpl() override {}
+
+ // Overridden from DiscardableMemoryChunk:
+ bool Lock() override {
+ if (!memory_chunk_)
+ return false;
+
+ memory_chunk_->Lock();
+ return true;
+ }
+
+ void Unlock() override {
+ if (memory_chunk_)
jamesr 2015/03/18 22:40:14 seems like this should be a DCHECK since it's not
+ memory_chunk_->Unlock();
+ }
+
+ void* Memory() const override {
+ if (memory_chunk_)
+ return memory_chunk_->Memory();
+ return nullptr;
+ }
+
+ private:
+ base::WeakPtr<DiscardableMemoryAllocator::OwnedMemoryChunk> memory_chunk_;
+
+ DISALLOW_IMPLICIT_CONSTRUCTORS(DiscardableMemoryChunkImpl);
+};
+
+} // namespace
+
+DiscardableMemoryAllocator::DiscardableMemoryAllocator(size_t max_memory)
+ : max_memory_(max_memory),
+ total_live_memory_(0) {
jamesr 2015/03/18 22:40:14 0u
+}
+
+DiscardableMemoryAllocator::~DiscardableMemoryAllocator() {
+ STLDeleteElements(&live_chunks_);
+}
+
+scoped_ptr<base::DiscardableMemory>
+DiscardableMemoryAllocator::AllocateLockedDiscardableMemory(size_t size) {
+ OwnedMemoryChunk* chunk = new OwnedMemoryChunk(size);
+ auto it = live_chunks_.insert(live_chunks_.end(), chunk);
+ chunk->SetAllocatorAndPosition(it, this);
+ total_live_memory_ += size;
+
+ // 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
+ // the desired maximum size.
+ it = live_chunks_.begin();
+ 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
+ if (!(*it)->locked()) {
+ total_live_memory_ -= (*it)->size();
+ delete *it;
+ it = live_chunks_.erase(it);
+ } else {
+ it++;
+ }
+ }
+
+ return make_scoped_ptr(new DiscardableMemoryChunkImpl(chunk->GetWeakPtr()));
+}
+
+void DiscardableMemoryAllocator::MoveToBack(
+ std::list<OwnedMemoryChunk*>::iterator it) {
+ OwnedMemoryChunk* chunk = *it;
+ live_chunks_.erase(it);
+ it = live_chunks_.insert(live_chunks_.end(), chunk);
+ chunk->SetAllocatorAndPosition(it, this);
+}
+
+} // namespace html_viewer

Powered by Google App Engine
This is Rietveld 408576698