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 namespace base { | 18 namespace base { |
19 | 19 |
danakj
2015/03/25 17:55:00
If might be simpler to just #define DISCARDABLE_SH
reveman
2015/03/25 18:26:42
Sure, that works.
Having the define in build/ mak
| |
20 // Platform abstraction for discardable shared memory. | 20 // Platform abstraction for discardable shared memory. |
21 // | 21 // |
22 // This class is not thread-safe. Clients are responsible for synchronizing | 22 // This class is not thread-safe. Clients are responsible for synchronizing |
23 // access to an instance of this class. | 23 // access to an instance of this class. |
24 class BASE_EXPORT DiscardableSharedMemory { | 24 class BASE_EXPORT DiscardableSharedMemory { |
25 public: | 25 public: |
26 enum LockResult { SUCCESS, PURGED, FAILED }; | 26 enum LockResult { SUCCESS, PURGED, FAILED }; |
27 | 27 |
28 DiscardableSharedMemory(); | 28 DiscardableSharedMemory(); |
29 | 29 |
(...skipping 56 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 | 86 // 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 | 87 // necessary to call this function twice for the object to successfully be |
88 // purged. First call, updates |last_known_usage_|. Second call, successfully | 88 // purged. First call, updates |last_known_usage_|. Second call, successfully |
89 // purges the object using the updated |last_known_usage_|. | 89 // purges the object using the updated |last_known_usage_|. |
90 // Note: there is no guarantee that multiple calls to this function will | 90 // Note: there is no guarantee that multiple calls to this function will |
91 // successfully purge object. DiscardableSharedMemory object might be locked | 91 // successfully purge object. DiscardableSharedMemory object might be locked |
92 // or another thread/process might be able to lock and unlock it in between | 92 // or another thread/process might be able to lock and unlock it in between |
93 // each call. | 93 // each call. |
94 bool Purge(Time current_time); | 94 bool Purge(Time current_time); |
95 | 95 |
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. | 96 // Returns true if memory is still resident. |
103 bool IsMemoryResident() const; | 97 bool IsMemoryResident() const; |
104 | 98 |
105 // Closes the open discardable memory segment. | 99 // Closes the open discardable memory segment. |
106 // It is safe to call Close repeatedly. | 100 // It is safe to call Close repeatedly. |
107 void Close(); | 101 void Close(); |
108 | 102 |
109 // Shares the discardable memory segment to another process. Attempts to | 103 // Shares the discardable memory segment to another process. Attempts to |
110 // create a platform-specific |new_handle| which can be used in a remote | 104 // 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 | 105 // process to access the discardable memory segment. |new_handle| is an |
112 // output parameter to receive the handle for use in the remote process. | 106 // output parameter to receive the handle for use in the remote process. |
113 // Returns true on success, false otherwise. | 107 // Returns true on success, false otherwise. |
114 bool ShareToProcess(ProcessHandle process_handle, | 108 bool ShareToProcess(ProcessHandle process_handle, |
115 SharedMemoryHandle* new_handle) { | 109 SharedMemoryHandle* new_handle) { |
116 return shared_memory_.ShareToProcess(process_handle, new_handle); | 110 return shared_memory_.ShareToProcess(process_handle, new_handle); |
117 } | 111 } |
118 | 112 |
113 #if defined(DISCARDABLE_SHARED_MEMORY_SHRINKING) | |
114 // Release as much memory as possible to the OS. The change in size will | |
115 // be reflected by the return value of mapped_size(). | |
116 void Shrink(); | |
117 #endif | |
118 | |
119 private: | 119 private: |
120 // Virtual for tests. | 120 // Virtual for tests. |
121 virtual Time Now() const; | 121 virtual Time Now() const; |
122 | 122 |
123 SharedMemory shared_memory_; | 123 SharedMemory shared_memory_; |
124 size_t mapped_size_; | 124 size_t mapped_size_; |
125 size_t locked_page_count_; | 125 size_t locked_page_count_; |
126 #if DCHECK_IS_ON() | 126 #if DCHECK_IS_ON() |
127 std::set<size_t> locked_pages_; | 127 std::set<size_t> locked_pages_; |
128 #endif | 128 #endif |
129 // Implementation is not thread-safe but still usable if clients are | 129 // Implementation is not thread-safe but still usable if clients are |
130 // synchronized somehow. Use a collision warner to detect incorrect usage. | 130 // synchronized somehow. Use a collision warner to detect incorrect usage. |
131 DFAKE_MUTEX(thread_collision_warner_); | 131 DFAKE_MUTEX(thread_collision_warner_); |
132 Time last_known_usage_; | 132 Time last_known_usage_; |
133 | 133 |
134 DISALLOW_COPY_AND_ASSIGN(DiscardableSharedMemory); | 134 DISALLOW_COPY_AND_ASSIGN(DiscardableSharedMemory); |
135 }; | 135 }; |
136 | 136 |
137 } // namespace base | 137 } // namespace base |
138 | 138 |
139 #endif // BASE_MEMORY_DISCARDABLE_SHARED_MEMORY_H_ | 139 #endif // BASE_MEMORY_DISCARDABLE_SHARED_MEMORY_H_ |
OLD | NEW |