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