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

Side by Side 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: 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/memory/discardable_memory.h" 5 #include "base/memory/discardable_memory_emulated.h"
6 6
7 #include "base/lazy_instance.h"
7 #include "base/memory/discardable_memory_provider.h" 8 #include "base/memory/discardable_memory_provider.h"
8 9
9 using base::internal::DiscardableMemoryProvider; 10 namespace base {
10 11
11 namespace base {
12 namespace { 12 namespace {
13 13
14 class DiscardableMemoryEmulated : public DiscardableMemory { 14 base::LazyInstance<internal::DiscardableMemoryProvider>::Leaky g_provider =
15 public: 15 LAZY_INSTANCE_INITIALIZER;
16 explicit DiscardableMemoryEmulated(size_t size) : is_locked_(false) {
17 DiscardableMemoryProvider::GetInstance()->Register(this, size);
18 }
19
20 virtual ~DiscardableMemoryEmulated() {
21 if (is_locked_)
22 Unlock();
23 DiscardableMemoryProvider::GetInstance()->Unregister(this);
24 }
25
26 // DiscardableMemory:
27 virtual LockDiscardableMemoryStatus Lock() OVERRIDE {
28 DCHECK(!is_locked_);
29
30 bool purged = false;
31 memory_ = DiscardableMemoryProvider::GetInstance()->Acquire(this, &purged);
32 if (!memory_)
33 return DISCARDABLE_MEMORY_FAILED;
34
35 is_locked_ = true;
36 return purged ? DISCARDABLE_MEMORY_PURGED : DISCARDABLE_MEMORY_SUCCESS;
37 }
38
39 virtual void Unlock() OVERRIDE {
40 DCHECK(is_locked_);
41 DiscardableMemoryProvider::GetInstance()->Release(this, memory_.Pass());
42 is_locked_ = false;
43 }
44
45 virtual void* Memory() const OVERRIDE {
46 DCHECK(memory_);
47 return memory_.get();
48 }
49
50 private:
51 scoped_ptr<uint8, FreeDeleter> memory_;
52 bool is_locked_;
53
54 DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryEmulated);
55 };
56 16
57 } // namespace 17 } // namespace
58 18
59 // static 19 DiscardableMemoryEmulated::DiscardableMemoryEmulated(size_t size)
60 bool DiscardableMemory::SupportedNatively() { 20 : is_locked_(false) {
61 return false; 21 g_provider.Pointer()->Register(this, size);
22 }
23
24 DiscardableMemoryEmulated::~DiscardableMemoryEmulated() {
25 if (is_locked_)
26 Unlock();
27 g_provider.Pointer()->Unregister(this);
28 }
29
30 bool DiscardableMemoryEmulated::Initialize() {
31 return Lock() == DISCARDABLE_MEMORY_PURGED;
32 }
33
34 LockDiscardableMemoryStatus DiscardableMemoryEmulated::Lock() {
35 DCHECK(!is_locked_);
36
37 bool purged = false;
38 memory_ = g_provider.Pointer()->Acquire(this, &purged);
39 if (!memory_)
40 return DISCARDABLE_MEMORY_FAILED;
41
42 is_locked_ = true;
43 return purged ? DISCARDABLE_MEMORY_PURGED : DISCARDABLE_MEMORY_SUCCESS;
44 }
45
46 void DiscardableMemoryEmulated::Unlock() {
47 DCHECK(is_locked_);
48 g_provider.Pointer()->Release(this, memory_.Pass());
49 is_locked_ = false;
50 }
51
52 void* DiscardableMemoryEmulated::Memory() const {
53 DCHECK(memory_);
54 return memory_.get();
62 } 55 }
63 56
64 // static 57 // static
65 scoped_ptr<DiscardableMemory> DiscardableMemory::CreateLockedMemory( 58 void DiscardableMemoryEmulated::PurgeForTesting() {
66 size_t size) { 59 g_provider.Pointer()->PurgeAll();
67 scoped_ptr<DiscardableMemory> memory(new DiscardableMemoryEmulated(size));
68 if (!memory)
69 return scoped_ptr<DiscardableMemory>();
70 if (memory->Lock() != DISCARDABLE_MEMORY_PURGED)
71 return scoped_ptr<DiscardableMemory>();
72 return memory.Pass();
73 }
74
75 // static
76 bool DiscardableMemory::PurgeForTestingSupported() {
77 return true;
78 }
79
80 // static
81 void DiscardableMemory::PurgeForTesting() {
82 DiscardableMemoryProvider::GetInstance()->PurgeAll();
83 } 60 }
84 61
85 } // namespace base 62 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698