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 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 // Shares the discardable memory segment to another process. Attempts to | 111 // Shares the discardable memory segment to another process. Attempts to |
118 // create a platform-specific |new_handle| which can be used in a remote | 112 // 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 | 113 // process to access the discardable memory segment. |new_handle| is an |
120 // output parameter to receive the handle for use in the remote process. | 114 // output parameter to receive the handle for use in the remote process. |
121 // Returns true on success, false otherwise. | 115 // Returns true on success, false otherwise. |
122 bool ShareToProcess(ProcessHandle process_handle, | 116 bool ShareToProcess(ProcessHandle process_handle, |
123 SharedMemoryHandle* new_handle) { | 117 SharedMemoryHandle* new_handle) { |
124 return shared_memory_.ShareToProcess(process_handle, new_handle); | 118 return shared_memory_.ShareToProcess(process_handle, new_handle); |
125 } | 119 } |
126 | 120 |
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: | 121 private: |
134 // Virtual for tests. | 122 // Virtual for tests. |
135 virtual Time Now() const; | 123 virtual Time Now() const; |
136 | 124 |
137 SharedMemory shared_memory_; | 125 SharedMemory shared_memory_; |
138 size_t mapped_size_; | 126 size_t mapped_size_; |
139 size_t locked_page_count_; | 127 size_t locked_page_count_; |
140 #if DCHECK_IS_ON() | 128 #if DCHECK_IS_ON() |
141 std::set<size_t> locked_pages_; | 129 std::set<size_t> locked_pages_; |
142 #endif | 130 #endif |
143 // Implementation is not thread-safe but still usable if clients are | 131 // Implementation is not thread-safe but still usable if clients are |
144 // synchronized somehow. Use a collision warner to detect incorrect usage. | 132 // synchronized somehow. Use a collision warner to detect incorrect usage. |
145 DFAKE_MUTEX(thread_collision_warner_); | 133 DFAKE_MUTEX(thread_collision_warner_); |
146 Time last_known_usage_; | 134 Time last_known_usage_; |
147 | 135 |
148 DISALLOW_COPY_AND_ASSIGN(DiscardableSharedMemory); | 136 DISALLOW_COPY_AND_ASSIGN(DiscardableSharedMemory); |
149 }; | 137 }; |
150 | 138 |
151 } // namespace base | 139 } // namespace base |
152 | 140 |
153 #endif // BASE_MEMORY_DISCARDABLE_SHARED_MEMORY_H_ | 141 #endif // BASE_MEMORY_DISCARDABLE_SHARED_MEMORY_H_ |
OLD | NEW |