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 #ifndef BASE_MEMORY_DISCARDABLE_MEMORY_H_ | 5 #ifndef BASE_MEMORY_DISCARDABLE_MEMORY_H_ |
6 #define BASE_MEMORY_DISCARDABLE_MEMORY_H_ | 6 #define BASE_MEMORY_DISCARDABLE_MEMORY_H_ |
7 | 7 |
8 #include "base/base_export.h" | 8 #include "base/base_export.h" |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
11 | 11 |
12 namespace base { | 12 namespace base { |
13 | 13 |
14 enum LockDiscardableMemoryStatus { | 14 enum LockDiscardableMemoryStatus { |
15 DISCARDABLE_MEMORY_FAILED = -1, | 15 DISCARDABLE_MEMORY_FAILED = -1, |
16 DISCARDABLE_MEMORY_PURGED = 0, | 16 DISCARDABLE_MEMORY_PURGED = 0, |
17 DISCARDABLE_MEMORY_SUCCESS = 1 | 17 DISCARDABLE_MEMORY_SUCCESS = 1 |
18 }; | 18 }; |
19 | 19 |
20 #if defined(OS_MACOSX) || defined(OS_ANDROID) | |
21 #define DISCARDABLE_MEMORY_SUPPORTED_NATIVELY | |
22 #endif | |
23 | |
20 // Platform abstraction for discardable memory. DiscardableMemory is used to | 24 // Platform abstraction for discardable memory. DiscardableMemory is used to |
21 // cache large objects without worrying about blowing out memory, both on mobile | 25 // cache large objects without worrying about blowing out memory, both on mobile |
22 // devices where there is no swap, and desktop devices where unused free memory | 26 // devices where there is no swap, and desktop devices where unused free memory |
23 // should be used to help the user experience. This is preferable to releasing | 27 // should be used to help the user experience. This is preferable to releasing |
24 // memory in response to an OOM signal because it is simpler, though it has less | 28 // memory in response to an OOM signal because it is simpler, though it has less |
25 // flexibility as to which objects get discarded. | 29 // flexibility as to which objects get discarded. |
26 // | 30 // |
27 // Discardable memory has two states: locked and unlocked. While the memory is | 31 // Discardable memory has two states: locked and unlocked. While the memory is |
28 // locked, it will not be discarded. Unlocking the memory allows the OS to | 32 // locked, it will not be discarded. Unlocking the memory allows the OS to |
29 // reclaim it if needed. Locks do not nest. | 33 // reclaim it if needed. Locks do not nest. |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
65 // DISCARDABLE_MEMORY_FAILED then this object should be discarded and | 69 // DISCARDABLE_MEMORY_FAILED then this object should be discarded and |
66 // a new one should be created. If the return value is | 70 // a new one should be created. If the return value is |
67 // DISCARDABLE_MEMORY_PURGED then the memory is present but any data that | 71 // DISCARDABLE_MEMORY_PURGED then the memory is present but any data that |
68 // was in it is gone. | 72 // was in it is gone. |
69 LockDiscardableMemoryStatus Lock() WARN_UNUSED_RESULT; | 73 LockDiscardableMemoryStatus Lock() WARN_UNUSED_RESULT; |
70 | 74 |
71 // Unlock the memory so that it can be purged by the system. Must be called | 75 // Unlock the memory so that it can be purged by the system. Must be called |
72 // after every successful lock call. | 76 // after every successful lock call. |
73 void Unlock(); | 77 void Unlock(); |
74 | 78 |
79 bool is_locked() const { return is_locked_; } | |
80 | |
75 // Return the memory address held by this object. The object must be locked | 81 // Return the memory address held by this object. The object must be locked |
76 // before calling this. Otherwise, this will cause a DCHECK error. | 82 // before calling this. Otherwise, this will cause a DCHECK error. |
77 void* Memory() const; | 83 void* Memory() const; |
78 | 84 |
79 // Testing utility calls. | 85 // Testing utility calls. |
80 | 86 |
81 // Check whether a purge of all discardable memory in the system is supported. | 87 // Check whether a purge of all discardable memory in the system is supported. |
82 // Use only for testing! | 88 // Use only for testing! |
83 static bool PurgeForTestingSupported(); | 89 static bool PurgeForTestingSupported(); |
84 | 90 |
85 // Purge all discardable memory in the system. This call has global effects | 91 // Purge all discardable memory in the system. This call has global effects |
86 // across all running processes, so it should only be used for testing! | 92 // across all running processes, so it should only be used for testing! |
87 static void PurgeForTesting(); | 93 static void PurgeForTesting(); |
88 | 94 |
89 private: | 95 private: |
96 #if !defined(DISCARDABLE_MEMORY_SUPPORTED_NATIVELY) | |
Avi (use Gerrit)
2013/06/18 00:00:46
How will this work on a platform like Win32 where
| |
97 friend class DiscardableMemoryProvider; | |
98 friend class DiscardableMemoryProviderTest; | |
99 | |
100 // Used by the DiscardableMemoryProvider to allocate and free memory. | |
101 bool Allocate(); | |
102 void Deallocate(); | |
103 #endif | |
104 | |
90 #if defined(OS_ANDROID) | 105 #if defined(OS_ANDROID) |
91 // Maps the discardable memory into the caller's address space. | 106 // Maps the discardable memory into the caller's address space. |
92 // Returns true on success, false otherwise. | 107 // Returns true on success, false otherwise. |
93 bool Map(); | 108 bool Map(); |
94 | 109 |
95 // Unmaps the discardable memory from the caller's address space. | 110 // Unmaps the discardable memory from the caller's address space. |
96 void Unmap(); | 111 void Unmap(); |
97 | 112 |
98 // Reserve a file descriptor. When reaching the fd limit, this call returns | 113 // Reserve a file descriptor. When reaching the fd limit, this call returns |
99 // false and initialization should fail. | 114 // false and initialization should fail. |
100 bool ReserveFileDescriptor(); | 115 bool ReserveFileDescriptor(); |
101 | 116 |
102 // Release a file descriptor so that others can reserve it. | 117 // Release a file descriptor so that others can reserve it. |
103 void ReleaseFileDescriptor(); | 118 void ReleaseFileDescriptor(); |
104 #endif // OS_ANDROID | 119 #endif // OS_ANDROID |
105 | 120 |
106 void* memory_; | 121 void* memory_; |
107 size_t size_; | 122 size_t size_; |
108 bool is_locked_; | 123 bool is_locked_; |
109 #if defined(OS_ANDROID) | 124 #if defined(OS_ANDROID) |
110 int fd_; | 125 int fd_; |
111 #endif // OS_ANDROID | 126 #endif // OS_ANDROID |
112 | 127 |
113 DISALLOW_COPY_AND_ASSIGN(DiscardableMemory); | 128 DISALLOW_COPY_AND_ASSIGN(DiscardableMemory); |
114 }; | 129 }; |
115 | 130 |
116 } // namespace base | 131 } // namespace base |
117 | 132 |
118 #endif // BASE_MEMORY_DISCARDABLE_MEMORY_H_ | 133 #endif // BASE_MEMORY_DISCARDABLE_MEMORY_H_ |
OLD | NEW |