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 // Linux (including Android) support the MADV_REMOVE argument with madvise() | 18 // Define DISCARDABLE_SHARED_MEMORY_SHRINKING if platform supports shrinking |
19 // which has the behavior of reliably causing zero-fill-on-demand pages to | 19 // of discardable shared memory segments. |
20 // be returned after a call. Here we define | 20 #if defined(OS_POSIX) && !defined(OS_ANDROID) |
21 // DISCARDABLE_SHARED_MEMORY_ZERO_FILL_ON_DEMAND_PAGES_AFTER_PURGE on Linux | 21 #define DISCARDABLE_SHARED_MEMORY_SHRINKING |
22 // and Android to indicate that this type of behavior can be expected on | |
23 // those platforms. Note that madvise() will still be used on other POSIX | |
24 // platforms but doesn't provide the zero-fill-on-demand pages guarantee. | |
25 #if defined(OS_LINUX) || defined(OS_ANDROID) | |
26 #define DISCARDABLE_SHARED_MEMORY_ZERO_FILL_ON_DEMAND_PAGES_AFTER_PURGE | |
27 #endif | 22 #endif |
28 | 23 |
29 namespace base { | 24 namespace base { |
30 | 25 |
31 // Platform abstraction for discardable shared memory. | 26 // Platform abstraction for discardable shared memory. |
32 // | 27 // |
33 // This class is not thread-safe. Clients are responsible for synchronizing | 28 // This class is not thread-safe. Clients are responsible for synchronizing |
34 // access to an instance of this class. | 29 // access to an instance of this class. |
35 class BASE_EXPORT DiscardableSharedMemory { | 30 class BASE_EXPORT DiscardableSharedMemory { |
36 public: | 31 public: |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 // Shares the discardable memory segment to another process. Attempts to | 117 // Shares the discardable memory segment to another process. Attempts to |
123 // create a platform-specific |new_handle| which can be used in a remote | 118 // create a platform-specific |new_handle| which can be used in a remote |
124 // process to access the discardable memory segment. |new_handle| is an | 119 // process to access the discardable memory segment. |new_handle| is an |
125 // output parameter to receive the handle for use in the remote process. | 120 // output parameter to receive the handle for use in the remote process. |
126 // Returns true on success, false otherwise. | 121 // Returns true on success, false otherwise. |
127 bool ShareToProcess(ProcessHandle process_handle, | 122 bool ShareToProcess(ProcessHandle process_handle, |
128 SharedMemoryHandle* new_handle) { | 123 SharedMemoryHandle* new_handle) { |
129 return shared_memory_.ShareToProcess(process_handle, new_handle); | 124 return shared_memory_.ShareToProcess(process_handle, new_handle); |
130 } | 125 } |
131 | 126 |
| 127 #if defined(DISCARDABLE_SHARED_MEMORY_SHRINKING) |
| 128 // Release as much memory as possible to the OS. The change in size will |
| 129 // be reflected by the return value of mapped_size(). |
| 130 void Shrink(); |
| 131 #endif |
| 132 |
132 private: | 133 private: |
133 // Virtual for tests. | 134 // Virtual for tests. |
134 virtual Time Now() const; | 135 virtual Time Now() const; |
135 | 136 |
136 SharedMemory shared_memory_; | 137 SharedMemory shared_memory_; |
137 size_t mapped_size_; | 138 size_t mapped_size_; |
138 size_t locked_page_count_; | 139 size_t locked_page_count_; |
139 #if DCHECK_IS_ON() | 140 #if DCHECK_IS_ON() |
140 std::set<size_t> locked_pages_; | 141 std::set<size_t> locked_pages_; |
141 #endif | 142 #endif |
142 // Implementation is not thread-safe but still usable if clients are | 143 // Implementation is not thread-safe but still usable if clients are |
143 // synchronized somehow. Use a collision warner to detect incorrect usage. | 144 // synchronized somehow. Use a collision warner to detect incorrect usage. |
144 DFAKE_MUTEX(thread_collision_warner_); | 145 DFAKE_MUTEX(thread_collision_warner_); |
145 Time last_known_usage_; | 146 Time last_known_usage_; |
146 | 147 |
147 DISALLOW_COPY_AND_ASSIGN(DiscardableSharedMemory); | 148 DISALLOW_COPY_AND_ASSIGN(DiscardableSharedMemory); |
148 }; | 149 }; |
149 | 150 |
150 } // namespace base | 151 } // namespace base |
151 | 152 |
152 #endif // BASE_MEMORY_DISCARDABLE_SHARED_MEMORY_H_ | 153 #endif // BASE_MEMORY_DISCARDABLE_SHARED_MEMORY_H_ |
OLD | NEW |