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

Unified Diff: base/memory/discardable_memory_emulated.cc

Issue 111713008: base: Refactor DiscardableMemoryProvider to build and be tested on all platforms. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix native_discardable_memory on ios. Created 7 years 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_emulated.cc
diff --git a/base/memory/discardable_memory_emulated.cc b/base/memory/discardable_memory_emulated.cc
index 246246272d815f61330a5f4653aa895e8ec404f1..ed7c42c750f0c48be0a168e89a6a19d04a95df1a 100644
--- a/base/memory/discardable_memory_emulated.cc
+++ b/base/memory/discardable_memory_emulated.cc
@@ -2,84 +2,64 @@
// 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.h"
+#include "base/memory/discardable_memory_emulated.h"
+#include "base/lazy_instance.h"
#include "base/memory/discardable_memory_provider.h"
-using base::internal::DiscardableMemoryProvider;
-
namespace base {
+
namespace {
-class DiscardableMemoryEmulated : public DiscardableMemory {
- public:
- explicit DiscardableMemoryEmulated(size_t size) : is_locked_(false) {
- DiscardableMemoryProvider::GetInstance()->Register(this, size);
- }
-
- virtual ~DiscardableMemoryEmulated() {
- if (is_locked_)
- Unlock();
- DiscardableMemoryProvider::GetInstance()->Unregister(this);
- }
-
- // DiscardableMemory:
- virtual LockDiscardableMemoryStatus Lock() OVERRIDE {
- DCHECK(!is_locked_);
-
- bool purged = false;
- memory_ = DiscardableMemoryProvider::GetInstance()->Acquire(this, &purged);
- if (!memory_)
- return DISCARDABLE_MEMORY_FAILED;
-
- is_locked_ = true;
- return purged ? DISCARDABLE_MEMORY_PURGED : DISCARDABLE_MEMORY_SUCCESS;
- }
-
- virtual void Unlock() OVERRIDE {
- DCHECK(is_locked_);
- DiscardableMemoryProvider::GetInstance()->Release(this, memory_.Pass());
- is_locked_ = false;
- }
-
- virtual void* Memory() const OVERRIDE {
- DCHECK(memory_);
- return memory_.get();
- }
-
- private:
- scoped_ptr<uint8, FreeDeleter> memory_;
- bool is_locked_;
-
- DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryEmulated);
-};
+base::LazyInstance<internal::DiscardableMemoryProvider>::Leaky g_provider =
+ LAZY_INSTANCE_INITIALIZER;
} // namespace
-// static
-bool DiscardableMemory::SupportedNatively() {
- return false;
+namespace internal {
+
+DiscardableMemoryEmulated::DiscardableMemoryEmulated(size_t size)
+ : is_locked_(false) {
+ g_provider.Pointer()->Register(this, size);
}
-// static
-scoped_ptr<DiscardableMemory> DiscardableMemory::CreateLockedMemory(
- size_t size) {
- scoped_ptr<DiscardableMemory> memory(new DiscardableMemoryEmulated(size));
- if (!memory)
- return scoped_ptr<DiscardableMemory>();
- if (memory->Lock() != DISCARDABLE_MEMORY_PURGED)
- return scoped_ptr<DiscardableMemory>();
- return memory.Pass();
+DiscardableMemoryEmulated::~DiscardableMemoryEmulated() {
+ if (is_locked_)
+ Unlock();
+ g_provider.Pointer()->Unregister(this);
}
-// static
-bool DiscardableMemory::PurgeForTestingSupported() {
- return true;
+bool DiscardableMemoryEmulated::Initialize() {
+ return Lock() == DISCARDABLE_MEMORY_PURGED;
+}
+
+LockDiscardableMemoryStatus DiscardableMemoryEmulated::Lock() {
+ DCHECK(!is_locked_);
+
+ bool purged = false;
+ memory_ = g_provider.Pointer()->Acquire(this, &purged);
+ if (!memory_)
+ return DISCARDABLE_MEMORY_FAILED;
+
+ is_locked_ = true;
+ return purged ? DISCARDABLE_MEMORY_PURGED : DISCARDABLE_MEMORY_SUCCESS;
+}
+
+void DiscardableMemoryEmulated::Unlock() {
+ DCHECK(is_locked_);
+ g_provider.Pointer()->Release(this, memory_.Pass());
+ is_locked_ = false;
+}
+
+void* DiscardableMemoryEmulated::Memory() const {
+ DCHECK(memory_);
+ return memory_.get();
}
// static
-void DiscardableMemory::PurgeForTesting() {
- DiscardableMemoryProvider::GetInstance()->PurgeAll();
+void DiscardableMemoryEmulated::PurgeForTesting() {
+ g_provider.Pointer()->PurgeAll();
}
+} // namespace internal
} // namespace base

Powered by Google App Engine
This is Rietveld 408576698