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

Unified Diff: base/memory/discardable_memory_ashmem.cc

Issue 195863005: Use DiscardableMemoryManager on Android. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix base.gypi Created 6 years, 8 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: base/memory/discardable_memory_ashmem.cc
diff --git a/base/memory/discardable_memory_ashmem.cc b/base/memory/discardable_memory_ashmem.cc
new file mode 100644
index 0000000000000000000000000000000000000000..d1cbca94fbd84bbdf2b1dcbe93a560df7ef6a4f9
--- /dev/null
+++ b/base/memory/discardable_memory_ashmem.cc
@@ -0,0 +1,70 @@
+// Copyright 2014 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 "base/memory/discardable_memory_ashmem.h"
+
+#include "base/memory/discardable_memory_allocator_android.h"
+
+namespace base {
+namespace internal {
+
+DiscardableMemoryAshmem::DiscardableMemoryAshmem(
+ size_t size,
+ DiscardableMemoryAllocator* allocator,
+ DiscardableMemoryManager* manager)
+ : allocator_(allocator),
+ manager_(manager) {
+ manager_->Register(this, size);
+}
+
+DiscardableMemoryAshmem::~DiscardableMemoryAshmem() {
+ // Note that this will call Purge() below while the manager's mutex is
+ // acquired which allows the ashmem allocator not to have any mutex.
+ manager_->Unregister(this);
+ DCHECK(!ashmem_chunk_);
+}
+
+bool DiscardableMemoryAshmem::Initialize() {
+ return Lock() == DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS;
+}
+
+DiscardableMemoryLockStatus DiscardableMemoryAshmem::Lock() {
+ bool purged = false;
+ if (!manager_->AcquireLock(this, &purged))
+ return DISCARDABLE_MEMORY_LOCK_STATUS_FAILED;
+
+ return purged ? DISCARDABLE_MEMORY_LOCK_STATUS_PURGED
+ : DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS;
+}
+
+void DiscardableMemoryAshmem::Unlock() {
+ manager_->ReleaseLock(this);
+}
+
+void* DiscardableMemoryAshmem::Memory() const {
+ DCHECK(ashmem_chunk_);
+ return ashmem_chunk_->Memory();
+}
+
+// Note that the methods below are called by the manager while its internal
+// mutex is acquired which means that they don't need extra synchronization.
+
+bool DiscardableMemoryAshmem::AllocateAndAcquireLock(size_t bytes) {
+ if (ashmem_chunk_)
+ return ashmem_chunk_->Lock();
+
+ ashmem_chunk_ = allocator_->Allocate(bytes);
+ return !!ashmem_chunk_;
+}
+
+void DiscardableMemoryAshmem::ReleaseLock() {
+ ashmem_chunk_->Unlock();
+}
+
+void DiscardableMemoryAshmem::Purge() {
+ ashmem_chunk_.reset();
+}
+
+} // namespace internal
+} // namespace base

Powered by Google App Engine
This is Rietveld 408576698