OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_SHARED_MEMORY_H_ | 5 #ifndef BASE_MEMORY_DISCARDABLE_SHARED_MEMORY_H_ |
6 #define BASE_MEMORY_DISCARDABLE_SHARED_MEMORY_H_ | 6 #define BASE_MEMORY_DISCARDABLE_SHARED_MEMORY_H_ |
7 | 7 |
8 #include "base/base_export.h" | 8 #include "base/base_export.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/shared_memory.h" | 10 #include "base/memory/shared_memory.h" |
11 #include "base/threading/thread_collision_warner.h" | 11 #include "base/threading/thread_collision_warner.h" |
12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
13 | 13 |
14 #if DCHECK_IS_ON() | 14 #if DCHECK_IS_ON() |
15 #include <set> | 15 #include <set> |
16 #endif | 16 #endif |
17 | 17 |
18 // Define DISCARDABLE_SHARED_MEMORY_SHRINKING if platform supports shrinking | |
19 // of discardable shared memory segments. | |
20 #if defined(OS_POSIX) && !defined(OS_ANDROID) | |
21 #define DISCARDABLE_SHARED_MEMORY_SHRINKING | |
22 #endif | |
23 | |
24 namespace base { | 18 namespace base { |
25 | 19 |
26 // Platform abstraction for discardable shared memory. | 20 // Platform abstraction for discardable shared memory. |
27 // | 21 // |
28 // This class is not thread-safe. Clients are responsible for synchronizing | 22 // This class is not thread-safe. Clients are responsible for synchronizing |
29 // access to an instance of this class. | 23 // access to an instance of this class. |
30 class BASE_EXPORT DiscardableSharedMemory { | 24 class BASE_EXPORT DiscardableSharedMemory { |
31 public: | 25 public: |
32 enum LockResult { SUCCESS, PURGED, FAILED }; | 26 enum LockResult { SUCCESS, PURGED, FAILED }; |
33 | 27 |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 // if locked or the actual last usage timestamp if unlocked. It is often | 86 // if locked or the actual last usage timestamp if unlocked. It is often |
93 // necessary to call this function twice for the object to successfully be | 87 // necessary to call this function twice for the object to successfully be |
94 // purged. First call, updates |last_known_usage_|. Second call, successfully | 88 // purged. First call, updates |last_known_usage_|. Second call, successfully |
95 // purges the object using the updated |last_known_usage_|. | 89 // purges the object using the updated |last_known_usage_|. |
96 // Note: there is no guarantee that multiple calls to this function will | 90 // Note: there is no guarantee that multiple calls to this function will |
97 // successfully purge object. DiscardableSharedMemory object might be locked | 91 // successfully purge object. DiscardableSharedMemory object might be locked |
98 // or another thread/process might be able to lock and unlock it in between | 92 // or another thread/process might be able to lock and unlock it in between |
99 // each call. | 93 // each call. |
100 bool Purge(Time current_time); | 94 bool Purge(Time current_time); |
101 | 95 |
| 96 // Purge and release as much memory as possible to the OS. |
| 97 // Note: The amount of memory that can be released to the OS is platform |
| 98 // specific. Best case, all but one page is released. Worst case, nothing |
| 99 // is released. |
| 100 bool PurgeAndTruncate(Time current_time); |
| 101 |
102 // Returns true if memory is still resident. | 102 // Returns true if memory is still resident. |
103 bool IsMemoryResident() const; | 103 bool IsMemoryResident() const; |
104 | 104 |
105 // Closes the open discardable memory segment. | 105 // Closes the open discardable memory segment. |
106 // It is safe to call Close repeatedly. | 106 // It is safe to call Close repeatedly. |
107 void Close(); | 107 void Close(); |
108 | 108 |
109 // Shares the discardable memory segment to another process. Attempts to | 109 // Shares the discardable memory segment to another process. Attempts to |
110 // create a platform-specific |new_handle| which can be used in a remote | 110 // create a platform-specific |new_handle| which can be used in a remote |
111 // process to access the discardable memory segment. |new_handle| is an | 111 // process to access the discardable memory segment. |new_handle| is an |
112 // output parameter to receive the handle for use in the remote process. | 112 // output parameter to receive the handle for use in the remote process. |
113 // Returns true on success, false otherwise. | 113 // Returns true on success, false otherwise. |
114 bool ShareToProcess(ProcessHandle process_handle, | 114 bool ShareToProcess(ProcessHandle process_handle, |
115 SharedMemoryHandle* new_handle) { | 115 SharedMemoryHandle* new_handle) { |
116 return shared_memory_.ShareToProcess(process_handle, new_handle); | 116 return shared_memory_.ShareToProcess(process_handle, new_handle); |
117 } | 117 } |
118 | 118 |
119 #if defined(DISCARDABLE_SHARED_MEMORY_SHRINKING) | |
120 // Release as much memory as possible to the OS. The change in size will | |
121 // be reflected by the return value of mapped_size(). | |
122 void Shrink(); | |
123 #endif | |
124 | |
125 private: | 119 private: |
126 // Virtual for tests. | 120 // Virtual for tests. |
127 virtual Time Now() const; | 121 virtual Time Now() const; |
128 | 122 |
129 SharedMemory shared_memory_; | 123 SharedMemory shared_memory_; |
130 size_t mapped_size_; | 124 size_t mapped_size_; |
131 size_t locked_page_count_; | 125 size_t locked_page_count_; |
132 #if DCHECK_IS_ON() | 126 #if DCHECK_IS_ON() |
133 std::set<size_t> locked_pages_; | 127 std::set<size_t> locked_pages_; |
134 #endif | 128 #endif |
135 // Implementation is not thread-safe but still usable if clients are | 129 // Implementation is not thread-safe but still usable if clients are |
136 // synchronized somehow. Use a collision warner to detect incorrect usage. | 130 // synchronized somehow. Use a collision warner to detect incorrect usage. |
137 DFAKE_MUTEX(thread_collision_warner_); | 131 DFAKE_MUTEX(thread_collision_warner_); |
138 Time last_known_usage_; | 132 Time last_known_usage_; |
139 | 133 |
140 DISALLOW_COPY_AND_ASSIGN(DiscardableSharedMemory); | 134 DISALLOW_COPY_AND_ASSIGN(DiscardableSharedMemory); |
141 }; | 135 }; |
142 | 136 |
143 } // namespace base | 137 } // namespace base |
144 | 138 |
145 #endif // BASE_MEMORY_DISCARDABLE_SHARED_MEMORY_H_ | 139 #endif // BASE_MEMORY_DISCARDABLE_SHARED_MEMORY_H_ |
OLD | NEW |