OLD | NEW |
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 namespace internal { |
60 bool DiscardableMemory::SupportedNatively() { | 20 |
61 return false; | 21 DiscardableMemoryEmulated::DiscardableMemoryEmulated(size_t size) |
| 22 : is_locked_(false) { |
| 23 g_provider.Pointer()->Register(this, size); |
| 24 } |
| 25 |
| 26 DiscardableMemoryEmulated::~DiscardableMemoryEmulated() { |
| 27 if (is_locked_) |
| 28 Unlock(); |
| 29 g_provider.Pointer()->Unregister(this); |
| 30 } |
| 31 |
| 32 bool DiscardableMemoryEmulated::Initialize() { |
| 33 return Lock() == DISCARDABLE_MEMORY_PURGED; |
| 34 } |
| 35 |
| 36 LockDiscardableMemoryStatus DiscardableMemoryEmulated::Lock() { |
| 37 DCHECK(!is_locked_); |
| 38 |
| 39 bool purged = false; |
| 40 memory_ = g_provider.Pointer()->Acquire(this, &purged); |
| 41 if (!memory_) |
| 42 return DISCARDABLE_MEMORY_FAILED; |
| 43 |
| 44 is_locked_ = true; |
| 45 return purged ? DISCARDABLE_MEMORY_PURGED : DISCARDABLE_MEMORY_SUCCESS; |
| 46 } |
| 47 |
| 48 void DiscardableMemoryEmulated::Unlock() { |
| 49 DCHECK(is_locked_); |
| 50 g_provider.Pointer()->Release(this, memory_.Pass()); |
| 51 is_locked_ = false; |
| 52 } |
| 53 |
| 54 void* DiscardableMemoryEmulated::Memory() const { |
| 55 DCHECK(memory_); |
| 56 return memory_.get(); |
62 } | 57 } |
63 | 58 |
64 // static | 59 // static |
65 scoped_ptr<DiscardableMemory> DiscardableMemory::CreateLockedMemory( | 60 void DiscardableMemoryEmulated::PurgeForTesting() { |
66 size_t size) { | 61 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 } | 62 } |
74 | 63 |
75 // static | 64 } // namespace internal |
76 bool DiscardableMemory::PurgeForTestingSupported() { | |
77 return true; | |
78 } | |
79 | |
80 // static | |
81 void DiscardableMemory::PurgeForTesting() { | |
82 DiscardableMemoryProvider::GetInstance()->PurgeAll(); | |
83 } | |
84 | |
85 } // namespace base | 65 } // namespace base |
OLD | NEW |