Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(79)

Side by Side Diff: base/memory/discardable_shared_memory.h

Issue 1409743002: Re-land: base: Use MADV_REMOVE instead of ftruncate to purge discardable memory segments. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: update comments Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 the behavior of using madvise() to synchronsouly purge memory reliably
20 #if defined(OS_POSIX) && !defined(OS_ANDROID) 20 // results in zero-filled-on-demand pages. madvise() will still be used on
21 #define DISCARDABLE_SHARED_MEMORY_SHRINKING 21 // other POSIX compatible platforms but doesn't provide the zero-filled pages
22 // guarantee.
23 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_NACL)
danakj 2015/10/21 18:34:25 Thanks for the extra comments, I'm finally wrappin
reveman 2015/10/21 19:28:53 Changed things a bit in latest patch, including a
24 #define DISCARDABLE_SHARED_MEMORY_REMOVE
22 #endif 25 #endif
23 26
24 namespace base { 27 namespace base {
25 28
26 // Platform abstraction for discardable shared memory. 29 // Platform abstraction for discardable shared memory.
27 // 30 //
28 // This class is not thread-safe. Clients are responsible for synchronizing 31 // This class is not thread-safe. Clients are responsible for synchronizing
29 // access to an instance of this class. 32 // access to an instance of this class.
30 class BASE_EXPORT DiscardableSharedMemory { 33 class BASE_EXPORT DiscardableSharedMemory {
31 public: 34 public:
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
117 // Shares the discardable memory segment to another process. Attempts to 120 // Shares the discardable memory segment to another process. Attempts to
118 // create a platform-specific |new_handle| which can be used in a remote 121 // 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 122 // process to access the discardable memory segment. |new_handle| is an
120 // output parameter to receive the handle for use in the remote process. 123 // output parameter to receive the handle for use in the remote process.
121 // Returns true on success, false otherwise. 124 // Returns true on success, false otherwise.
122 bool ShareToProcess(ProcessHandle process_handle, 125 bool ShareToProcess(ProcessHandle process_handle,
123 SharedMemoryHandle* new_handle) { 126 SharedMemoryHandle* new_handle) {
124 return shared_memory_.ShareToProcess(process_handle, new_handle); 127 return shared_memory_.ShareToProcess(process_handle, new_handle);
125 } 128 }
126 129
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: 130 private:
134 // Virtual for tests. 131 // Virtual for tests.
135 virtual Time Now() const; 132 virtual Time Now() const;
136 133
137 SharedMemory shared_memory_; 134 SharedMemory shared_memory_;
138 size_t mapped_size_; 135 size_t mapped_size_;
139 size_t locked_page_count_; 136 size_t locked_page_count_;
140 #if DCHECK_IS_ON() 137 #if DCHECK_IS_ON()
141 std::set<size_t> locked_pages_; 138 std::set<size_t> locked_pages_;
142 #endif 139 #endif
143 // Implementation is not thread-safe but still usable if clients are 140 // Implementation is not thread-safe but still usable if clients are
144 // synchronized somehow. Use a collision warner to detect incorrect usage. 141 // synchronized somehow. Use a collision warner to detect incorrect usage.
145 DFAKE_MUTEX(thread_collision_warner_); 142 DFAKE_MUTEX(thread_collision_warner_);
146 Time last_known_usage_; 143 Time last_known_usage_;
147 144
148 DISALLOW_COPY_AND_ASSIGN(DiscardableSharedMemory); 145 DISALLOW_COPY_AND_ASSIGN(DiscardableSharedMemory);
149 }; 146 };
150 147
151 } // namespace base 148 } // namespace base
152 149
153 #endif // BASE_MEMORY_DISCARDABLE_SHARED_MEMORY_H_ 150 #endif // BASE_MEMORY_DISCARDABLE_SHARED_MEMORY_H_
OLDNEW
« no previous file with comments | « no previous file | base/memory/discardable_shared_memory.cc » ('j') | base/memory/discardable_shared_memory.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698