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 |
18 namespace base { | 24 namespace base { |
19 | 25 |
20 // Platform abstraction for discardable shared memory. | 26 // Platform abstraction for discardable shared memory. |
21 // | 27 // |
22 // This class is not thread-safe. Clients are responsible for synchronizing | 28 // This class is not thread-safe. Clients are responsible for synchronizing |
23 // access to an instance of this class. | 29 // access to an instance of this class. |
24 class BASE_EXPORT DiscardableSharedMemory { | 30 class BASE_EXPORT DiscardableSharedMemory { |
25 public: | 31 public: |
26 enum LockResult { SUCCESS, PURGED, FAILED }; | 32 enum LockResult { SUCCESS, PURGED, FAILED }; |
27 | 33 |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 // if locked or the actual last usage timestamp if unlocked. It is often | 92 // if locked or the actual last usage timestamp if unlocked. It is often |
87 // necessary to call this function twice for the object to successfully be | 93 // necessary to call this function twice for the object to successfully be |
88 // purged. First call, updates |last_known_usage_|. Second call, successfully | 94 // purged. First call, updates |last_known_usage_|. Second call, successfully |
89 // purges the object using the updated |last_known_usage_|. | 95 // purges the object using the updated |last_known_usage_|. |
90 // Note: there is no guarantee that multiple calls to this function will | 96 // Note: there is no guarantee that multiple calls to this function will |
91 // successfully purge object. DiscardableSharedMemory object might be locked | 97 // successfully purge object. DiscardableSharedMemory object might be locked |
92 // or another thread/process might be able to lock and unlock it in between | 98 // or another thread/process might be able to lock and unlock it in between |
93 // each call. | 99 // each call. |
94 bool Purge(Time current_time); | 100 bool Purge(Time current_time); |
95 | 101 |
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 |
119 private: | 125 private: |
120 // Virtual for tests. | 126 // Virtual for tests. |
121 virtual Time Now() const; | 127 virtual Time Now() const; |
122 | 128 |
123 SharedMemory shared_memory_; | 129 SharedMemory shared_memory_; |
124 size_t mapped_size_; | 130 size_t mapped_size_; |
125 size_t locked_page_count_; | 131 size_t locked_page_count_; |
126 #if DCHECK_IS_ON() | 132 #if DCHECK_IS_ON() |
127 std::set<size_t> locked_pages_; | 133 std::set<size_t> locked_pages_; |
128 #endif | 134 #endif |
129 // Implementation is not thread-safe but still usable if clients are | 135 // Implementation is not thread-safe but still usable if clients are |
130 // synchronized somehow. Use a collision warner to detect incorrect usage. | 136 // synchronized somehow. Use a collision warner to detect incorrect usage. |
131 DFAKE_MUTEX(thread_collision_warner_); | 137 DFAKE_MUTEX(thread_collision_warner_); |
132 Time last_known_usage_; | 138 Time last_known_usage_; |
133 | 139 |
134 DISALLOW_COPY_AND_ASSIGN(DiscardableSharedMemory); | 140 DISALLOW_COPY_AND_ASSIGN(DiscardableSharedMemory); |
135 }; | 141 }; |
136 | 142 |
137 } // namespace base | 143 } // namespace base |
138 | 144 |
139 #endif // BASE_MEMORY_DISCARDABLE_SHARED_MEMORY_H_ | 145 #endif // BASE_MEMORY_DISCARDABLE_SHARED_MEMORY_H_ |
OLD | NEW |