Delete chrome.mediaGalleriesPrivate because the functionality unique to it has since...
[chromium-blink-merge.git] / base / memory / discardable_shared_memory.cc
blobe04377ee3337a1732d491043481723ffa9cc2afb
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
3 // found in the LICENSE file.
5 #include "base/memory/discardable_shared_memory.h"
7 #if defined(OS_POSIX)
8 #include <unistd.h>
9 #endif
11 #include <algorithm>
13 #include "base/atomicops.h"
14 #include "base/logging.h"
15 #include "base/numerics/safe_math.h"
16 #include "base/process/process_metrics.h"
18 #if defined(OS_ANDROID)
19 #include "third_party/ashmem/ashmem.h"
20 #endif
22 namespace base {
23 namespace {
25 // Use a machine-sized pointer as atomic type. It will use the Atomic32 or
26 // Atomic64 routines, depending on the architecture.
27 typedef intptr_t AtomicType;
28 typedef uintptr_t UAtomicType;
30 // Template specialization for timestamp serialization/deserialization. This
31 // is used to serialize timestamps using Unix time on systems where AtomicType
32 // does not have enough precision to contain a timestamp in the standard
33 // serialized format.
34 template <int>
35 Time TimeFromWireFormat(int64 value);
36 template <int>
37 int64 TimeToWireFormat(Time time);
39 // Serialize to Unix time when using 4-byte wire format.
40 // Note: 19 January 2038, this will cease to work.
41 template <>
42 Time ALLOW_UNUSED_TYPE TimeFromWireFormat<4>(int64 value) {
43 return value ? Time::UnixEpoch() + TimeDelta::FromSeconds(value) : Time();
45 template <>
46 int64 ALLOW_UNUSED_TYPE TimeToWireFormat<4>(Time time) {
47 return time > Time::UnixEpoch() ? (time - Time::UnixEpoch()).InSeconds() : 0;
50 // Standard serialization format when using 8-byte wire format.
51 template <>
52 Time ALLOW_UNUSED_TYPE TimeFromWireFormat<8>(int64 value) {
53 return Time::FromInternalValue(value);
55 template <>
56 int64 ALLOW_UNUSED_TYPE TimeToWireFormat<8>(Time time) {
57 return time.ToInternalValue();
60 struct SharedState {
61 enum LockState { UNLOCKED = 0, LOCKED = 1 };
63 explicit SharedState(AtomicType ivalue) { value.i = ivalue; }
64 SharedState(LockState lock_state, Time timestamp) {
65 int64 wire_timestamp = TimeToWireFormat<sizeof(AtomicType)>(timestamp);
66 DCHECK_GE(wire_timestamp, 0);
67 DCHECK_EQ(lock_state & ~1, 0);
68 value.u = (static_cast<UAtomicType>(wire_timestamp) << 1) | lock_state;
71 LockState GetLockState() const { return static_cast<LockState>(value.u & 1); }
73 Time GetTimestamp() const {
74 return TimeFromWireFormat<sizeof(AtomicType)>(value.u >> 1);
77 // Bit 1: Lock state. Bit is set when locked.
78 // Bit 2..sizeof(AtomicType)*8: Usage timestamp. NULL time when locked or
79 // purged.
80 union {
81 AtomicType i;
82 UAtomicType u;
83 } value;
86 // Shared state is stored at offset 0 in shared memory segments.
87 SharedState* SharedStateFromSharedMemory(const SharedMemory& shared_memory) {
88 DCHECK(shared_memory.memory());
89 return static_cast<SharedState*>(shared_memory.memory());
92 // Round up |size| to a multiple of alignment, which must be a power of two.
93 size_t Align(size_t alignment, size_t size) {
94 DCHECK_EQ(alignment & (alignment - 1), 0u);
95 return (size + alignment - 1) & ~(alignment - 1);
98 // Round up |size| to a multiple of page size.
99 size_t AlignToPageSize(size_t size) {
100 return Align(base::GetPageSize(), size);
103 } // namespace
105 DiscardableSharedMemory::DiscardableSharedMemory()
106 : mapped_size_(0), locked_page_count_(0) {
109 DiscardableSharedMemory::DiscardableSharedMemory(
110 SharedMemoryHandle shared_memory_handle)
111 : shared_memory_(shared_memory_handle, false),
112 mapped_size_(0),
113 locked_page_count_(0) {
116 DiscardableSharedMemory::~DiscardableSharedMemory() {
119 bool DiscardableSharedMemory::CreateAndMap(size_t size) {
120 CheckedNumeric<size_t> checked_size = size;
121 checked_size += AlignToPageSize(sizeof(SharedState));
122 if (!checked_size.IsValid())
123 return false;
125 if (!shared_memory_.CreateAndMapAnonymous(checked_size.ValueOrDie()))
126 return false;
128 mapped_size_ =
129 shared_memory_.mapped_size() - AlignToPageSize(sizeof(SharedState));
131 locked_page_count_ = AlignToPageSize(mapped_size_) / base::GetPageSize();
132 #if DCHECK_IS_ON()
133 for (size_t page = 0; page < locked_page_count_; ++page)
134 locked_pages_.insert(page);
135 #endif
137 DCHECK(last_known_usage_.is_null());
138 SharedState new_state(SharedState::LOCKED, Time());
139 subtle::Release_Store(&SharedStateFromSharedMemory(shared_memory_)->value.i,
140 new_state.value.i);
141 return true;
144 bool DiscardableSharedMemory::Map(size_t size) {
145 if (!shared_memory_.Map(AlignToPageSize(sizeof(SharedState)) + size))
146 return false;
148 mapped_size_ =
149 shared_memory_.mapped_size() - AlignToPageSize(sizeof(SharedState));
151 locked_page_count_ = AlignToPageSize(mapped_size_) / base::GetPageSize();
152 #if DCHECK_IS_ON()
153 for (size_t page = 0; page < locked_page_count_; ++page)
154 locked_pages_.insert(page);
155 #endif
157 return true;
160 bool DiscardableSharedMemory::Lock(size_t offset, size_t length) {
161 DCHECK_EQ(AlignToPageSize(offset), offset);
162 DCHECK_EQ(AlignToPageSize(length), length);
164 // Calls to this function must synchronized properly.
165 DFAKE_SCOPED_LOCK(thread_collision_warner_);
167 // Return false when instance has been purged or not initialized properly by
168 // checking if |last_known_usage_| is NULL.
169 if (last_known_usage_.is_null())
170 return false;
172 DCHECK(shared_memory_.memory());
174 // We need to successfully acquire the platform independent lock before
175 // individual pages can be locked.
176 if (!locked_page_count_) {
177 SharedState old_state(SharedState::UNLOCKED, last_known_usage_);
178 SharedState new_state(SharedState::LOCKED, Time());
179 SharedState result(subtle::Acquire_CompareAndSwap(
180 &SharedStateFromSharedMemory(shared_memory_)->value.i,
181 old_state.value.i,
182 new_state.value.i));
183 if (result.value.u != old_state.value.u) {
184 // Update |last_known_usage_| in case the above CAS failed because of
185 // an incorrect timestamp.
186 last_known_usage_ = result.GetTimestamp();
187 return false;
191 // Zero for length means "everything onward".
192 if (!length)
193 length = AlignToPageSize(mapped_size_) - offset;
195 size_t start = offset / base::GetPageSize();
196 size_t end = start + length / base::GetPageSize();
197 DCHECK_LT(start, end);
198 DCHECK_LE(end, AlignToPageSize(mapped_size_) / base::GetPageSize());
200 // Add pages to |locked_page_count_|.
201 // Note: Locking a page that is already locked is an error.
202 locked_page_count_ += end - start;
203 #if DCHECK_IS_ON()
204 // Detect incorrect usage by keeping track of exactly what pages are locked.
205 for (auto page = start; page < end; ++page) {
206 auto result = locked_pages_.insert(page);
207 DCHECK(result.second);
209 DCHECK_EQ(locked_pages_.size(), locked_page_count_);
210 #endif
212 #if defined(OS_ANDROID)
213 SharedMemoryHandle handle = shared_memory_.handle();
214 DCHECK(SharedMemory::IsHandleValid(handle));
215 if (ashmem_pin_region(
216 handle.fd, AlignToPageSize(sizeof(SharedState)) + offset, length)) {
217 return false;
219 #endif
221 return true;
224 void DiscardableSharedMemory::Unlock(size_t offset, size_t length) {
225 DCHECK_EQ(AlignToPageSize(offset), offset);
226 DCHECK_EQ(AlignToPageSize(length), length);
228 // Calls to this function must synchronized properly.
229 DFAKE_SCOPED_LOCK(thread_collision_warner_);
231 // Zero for length means "everything onward".
232 if (!length)
233 length = AlignToPageSize(mapped_size_) - offset;
235 DCHECK(shared_memory_.memory());
237 #if defined(OS_ANDROID)
238 SharedMemoryHandle handle = shared_memory_.handle();
239 DCHECK(SharedMemory::IsHandleValid(handle));
240 if (ashmem_unpin_region(
241 handle.fd, AlignToPageSize(sizeof(SharedState)) + offset, length)) {
242 DPLOG(ERROR) << "ashmem_unpin_region() failed";
244 #endif
246 size_t start = offset / base::GetPageSize();
247 size_t end = start + length / base::GetPageSize();
248 DCHECK_LT(start, end);
249 DCHECK_LE(end, AlignToPageSize(mapped_size_) / base::GetPageSize());
251 // Remove pages from |locked_page_count_|.
252 // Note: Unlocking a page that is not locked is an error.
253 DCHECK_GE(locked_page_count_, end - start);
254 locked_page_count_ -= end - start;
255 #if DCHECK_IS_ON()
256 // Detect incorrect usage by keeping track of exactly what pages are locked.
257 for (auto page = start; page < end; ++page) {
258 auto erased_count = locked_pages_.erase(page);
259 DCHECK_EQ(1u, erased_count);
261 DCHECK_EQ(locked_pages_.size(), locked_page_count_);
262 #endif
264 // Early out and avoid releasing the platform independent lock if some pages
265 // are still locked.
266 if (locked_page_count_)
267 return;
269 Time current_time = Now();
270 DCHECK(!current_time.is_null());
272 SharedState old_state(SharedState::LOCKED, Time());
273 SharedState new_state(SharedState::UNLOCKED, current_time);
274 // Note: timestamp cannot be NULL as that is a unique value used when
275 // locked or purged.
276 DCHECK(!new_state.GetTimestamp().is_null());
277 // Timestamp precision should at least be accurate to the second.
278 DCHECK_EQ((new_state.GetTimestamp() - Time::UnixEpoch()).InSeconds(),
279 (current_time - Time::UnixEpoch()).InSeconds());
280 SharedState result(subtle::Release_CompareAndSwap(
281 &SharedStateFromSharedMemory(shared_memory_)->value.i,
282 old_state.value.i,
283 new_state.value.i));
285 DCHECK_EQ(old_state.value.u, result.value.u);
287 last_known_usage_ = current_time;
290 void* DiscardableSharedMemory::memory() const {
291 return reinterpret_cast<uint8*>(shared_memory_.memory()) +
292 AlignToPageSize(sizeof(SharedState));
295 bool DiscardableSharedMemory::Purge(Time current_time) {
296 // Calls to this function must synchronized properly.
297 DFAKE_SCOPED_LOCK(thread_collision_warner_);
299 // Early out if not mapped. This can happen if the segment was previously
300 // unmapped using a call to Close().
301 if (!shared_memory_.memory())
302 return true;
304 SharedState old_state(SharedState::UNLOCKED, last_known_usage_);
305 SharedState new_state(SharedState::UNLOCKED, Time());
306 SharedState result(subtle::Acquire_CompareAndSwap(
307 &SharedStateFromSharedMemory(shared_memory_)->value.i,
308 old_state.value.i,
309 new_state.value.i));
311 // Update |last_known_usage_| to |current_time| if the memory is locked. This
312 // allows the caller to determine if purging failed because last known usage
313 // was incorrect or memory was locked. In the second case, the caller should
314 // most likely wait for some amount of time before attempting to purge the
315 // the memory again.
316 if (result.value.u != old_state.value.u) {
317 last_known_usage_ = result.GetLockState() == SharedState::LOCKED
318 ? current_time
319 : result.GetTimestamp();
320 return false;
323 last_known_usage_ = Time();
324 return true;
327 bool DiscardableSharedMemory::PurgeAndTruncate(Time current_time) {
328 if (!Purge(current_time))
329 return false;
331 #if defined(OS_POSIX)
332 // Truncate shared memory to size of SharedState.
333 SharedMemoryHandle handle = shared_memory_.handle();
334 if (SharedMemory::IsHandleValid(handle)) {
335 if (HANDLE_EINTR(ftruncate(handle.fd, sizeof(SharedState))) != 0)
336 DPLOG(ERROR) << "ftruncate() failed";
338 #endif
340 return true;
343 bool DiscardableSharedMemory::IsMemoryResident() const {
344 DCHECK(shared_memory_.memory());
346 SharedState result(subtle::NoBarrier_Load(
347 &SharedStateFromSharedMemory(shared_memory_)->value.i));
349 return result.GetLockState() == SharedState::LOCKED ||
350 !result.GetTimestamp().is_null();
353 void DiscardableSharedMemory::Close() {
354 shared_memory_.Unmap();
355 shared_memory_.Close();
356 mapped_size_ = 0;
359 Time DiscardableSharedMemory::Now() const {
360 return Time::Now();
363 } // namespace base