OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 | 8 |
9 #include <stdio.h> | |
10 | |
9 namespace base { | 11 namespace base { |
10 | 12 |
11 DiscardableMemory::DiscardableMemory() | 13 DiscardableMemory::DiscardableMemory() |
14 #if defined(OS_ANDROID) || defined(OS_MACOSX) | |
12 : memory_(NULL), | 15 : memory_(NULL), |
13 size_(0), | 16 size_(0), |
14 is_locked_(false) | 17 is_locked_(false) |
15 #if defined(OS_ANDROID) | 18 #if defined(OS_ANDROID) |
16 , fd_(-1) | 19 , fd_(-1) |
17 #endif // OS_ANDROID | 20 #endif // OS_ANDROID |
21 #endif // OS_ANDROID || OS_MACOSX | |
18 { | 22 { |
19 DCHECK(Supported()); | 23 DCHECK(Supported()); |
20 } | 24 } |
21 | 25 |
26 // Stub implementations for platforms that don't support discardable memory. | |
27 | |
28 #if !defined(OS_ANDROID) && !defined(OS_MACOSX) | |
29 | |
30 namespace { | |
31 static DiscardableMemoryProvider* s_provider = NULL; | |
jonathan.backer
2013/05/29 14:02:54
Who owns the s_provider? I see it being set, but
Ian Vollick
2013/06/06 00:21:47
The render thread impl. It now resets it in its de
| |
32 } // namespace | |
33 | |
34 DiscardableMemory::~DiscardableMemory() { | |
35 if (s_provider) | |
36 s_provider->Unregister(this); | |
37 else | |
38 NOTREACHED(); | |
39 } | |
40 | |
41 // static | |
42 bool DiscardableMemory::Supported() { | |
43 return s_provider; | |
44 } | |
45 | |
46 // static | |
47 void DiscardableMemory::SetProvider(DiscardableMemoryProvider* provider) { | |
48 s_provider = provider; | |
49 } | |
50 | |
51 bool DiscardableMemory::InitializeAndLock(size_t size) { | |
52 if (s_provider) | |
53 return s_provider->Register(this, size); | |
54 NOTREACHED(); | |
55 return false; | |
56 } | |
57 | |
58 LockDiscardableMemoryStatus DiscardableMemory::Lock() { | |
59 if (s_provider) | |
60 return s_provider->Lock(this); | |
61 NOTREACHED(); | |
62 return DISCARDABLE_MEMORY_FAILED; | |
63 } | |
64 | |
65 void DiscardableMemory::Unlock() { | |
66 if (s_provider) | |
67 s_provider->Unlock(this); | |
68 else | |
69 NOTREACHED(); | |
70 } | |
71 | |
72 void* DiscardableMemory::Memory() const { | |
73 if (s_provider) | |
74 return s_provider->Memory(this); | |
75 NOTREACHED(); | |
76 return NULL; | |
77 } | |
78 | |
79 // static | |
80 bool DiscardableMemory::PurgeForTestingSupported() { | |
81 if (s_provider) | |
82 return s_provider->PurgeForTestingSupported(); | |
83 NOTREACHED(); | |
84 return false; | |
85 } | |
86 | |
87 // static | |
88 void DiscardableMemory::PurgeForTesting() { | |
89 if (s_provider) | |
90 s_provider->PurgeForTesting(); | |
91 else | |
92 NOTREACHED(); | |
93 } | |
94 | |
95 #else | |
96 | |
22 void* DiscardableMemory::Memory() const { | 97 void* DiscardableMemory::Memory() const { |
23 DCHECK(is_locked_); | 98 DCHECK(is_locked_); |
24 return memory_; | 99 return memory_; |
25 } | 100 } |
26 | 101 |
27 // Stub implementations for platforms that don't support discardable memory. | |
28 | |
29 #if !defined(OS_ANDROID) && !defined(OS_MACOSX) | |
30 | |
31 DiscardableMemory::~DiscardableMemory() { | |
32 NOTIMPLEMENTED(); | |
33 } | |
34 | |
35 // static | |
36 bool DiscardableMemory::Supported() { | |
37 return false; | |
38 } | |
39 | |
40 bool DiscardableMemory::InitializeAndLock(size_t size) { | |
41 NOTIMPLEMENTED(); | |
42 return false; | |
43 } | |
44 | |
45 LockDiscardableMemoryStatus DiscardableMemory::Lock() { | |
46 NOTIMPLEMENTED(); | |
47 return DISCARDABLE_MEMORY_FAILED; | |
48 } | |
49 | |
50 void DiscardableMemory::Unlock() { | |
51 NOTIMPLEMENTED(); | |
52 } | |
53 | |
54 // static | |
55 bool DiscardableMemory::PurgeForTestingSupported() { | |
56 return false; | |
57 } | |
58 | |
59 // static | |
60 void DiscardableMemory::PurgeForTesting() { | |
61 NOTIMPLEMENTED(); | |
62 } | |
63 | |
64 #endif // OS_* | 102 #endif // OS_* |
65 | 103 |
66 } // namespace base | 104 } // namespace base |
OLD | NEW |