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

Unified Diff: base/memory/discardable_memory_emulated.cc

Issue 17106004: Add discardable memory emulation for non-android/mac platforms (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix MemoryAfterUnlock test Created 7 years, 2 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
« no previous file with comments | « base/memory/discardable_memory_android.cc ('k') | base/memory/discardable_memory_mac.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/memory/discardable_memory_emulated.cc
diff --git a/base/memory/discardable_memory_emulated.cc b/base/memory/discardable_memory_emulated.cc
new file mode 100644
index 0000000000000000000000000000000000000000..246246272d815f61330a5f4653aa895e8ec404f1
--- /dev/null
+++ b/base/memory/discardable_memory_emulated.cc
@@ -0,0 +1,85 @@
+// Copyright 2013 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.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);
+};
+
+} // namespace
+
+// static
+bool DiscardableMemory::SupportedNatively() {
+ return false;
+}
+
+// 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)
Philippe 2013/11/05 12:41:39 I was playing on Linux with discardable memory (th
+ return scoped_ptr<DiscardableMemory>();
+ return memory.Pass();
+}
+
+// static
+bool DiscardableMemory::PurgeForTestingSupported() {
+ return true;
+}
+
+// static
+void DiscardableMemory::PurgeForTesting() {
+ DiscardableMemoryProvider::GetInstance()->PurgeAll();
+}
+
+} // namespace base
« no previous file with comments | « base/memory/discardable_memory_android.cc ('k') | base/memory/discardable_memory_mac.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698