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 // Define DISCARDABLE_SHARED_MEMORY_REMOVE if platform supports MADV_REMOVE |
19 // of discardable shared memory segments. | 19 // and can remove resources associated with discardable shared memory when |
20 #if defined(OS_POSIX) && !defined(OS_ANDROID) | 20 // purging a segment. |
21 #define DISCARDABLE_SHARED_MEMORY_SHRINKING | 21 #if defined(OS_LINUX) || defined(OS_ANDROID) |
| 22 #define DISCARDABLE_SHARED_MEMORY_REMOVE |
22 #endif | 23 #endif |
23 | 24 |
24 namespace base { | 25 namespace base { |
25 | 26 |
26 // Platform abstraction for discardable shared memory. | 27 // Platform abstraction for discardable shared memory. |
27 // | 28 // |
28 // This class is not thread-safe. Clients are responsible for synchronizing | 29 // This class is not thread-safe. Clients are responsible for synchronizing |
29 // access to an instance of this class. | 30 // access to an instance of this class. |
30 class BASE_EXPORT DiscardableSharedMemory { | 31 class BASE_EXPORT DiscardableSharedMemory { |
31 public: | 32 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 | 118 // Shares the discardable memory segment to another process. Attempts to |
118 // create a platform-specific |new_handle| which can be used in a remote | 119 // 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 | 120 // process to access the discardable memory segment. |new_handle| is an |
120 // output parameter to receive the handle for use in the remote process. | 121 // output parameter to receive the handle for use in the remote process. |
121 // Returns true on success, false otherwise. | 122 // Returns true on success, false otherwise. |
122 bool ShareToProcess(ProcessHandle process_handle, | 123 bool ShareToProcess(ProcessHandle process_handle, |
123 SharedMemoryHandle* new_handle) { | 124 SharedMemoryHandle* new_handle) { |
124 return shared_memory_.ShareToProcess(process_handle, new_handle); | 125 return shared_memory_.ShareToProcess(process_handle, new_handle); |
125 } | 126 } |
126 | 127 |
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: | 128 private: |
134 // Virtual for tests. | 129 // Virtual for tests. |
135 virtual Time Now() const; | 130 virtual Time Now() const; |
136 | 131 |
137 SharedMemory shared_memory_; | 132 SharedMemory shared_memory_; |
138 size_t mapped_size_; | 133 size_t mapped_size_; |
139 size_t locked_page_count_; | 134 size_t locked_page_count_; |
140 #if DCHECK_IS_ON() | 135 #if DCHECK_IS_ON() |
141 std::set<size_t> locked_pages_; | 136 std::set<size_t> locked_pages_; |
142 #endif | 137 #endif |
143 // Implementation is not thread-safe but still usable if clients are | 138 // Implementation is not thread-safe but still usable if clients are |
144 // synchronized somehow. Use a collision warner to detect incorrect usage. | 139 // synchronized somehow. Use a collision warner to detect incorrect usage. |
145 DFAKE_MUTEX(thread_collision_warner_); | 140 DFAKE_MUTEX(thread_collision_warner_); |
146 Time last_known_usage_; | 141 Time last_known_usage_; |
147 | 142 |
148 DISALLOW_COPY_AND_ASSIGN(DiscardableSharedMemory); | 143 DISALLOW_COPY_AND_ASSIGN(DiscardableSharedMemory); |
149 }; | 144 }; |
150 | 145 |
151 } // namespace base | 146 } // namespace base |
152 | 147 |
153 #endif // BASE_MEMORY_DISCARDABLE_SHARED_MEMORY_H_ | 148 #endif // BASE_MEMORY_DISCARDABLE_SHARED_MEMORY_H_ |
OLD | NEW |