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 #include "base/memory/discardable_shared_memory.h" | 5 #include "base/memory/discardable_shared_memory.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 | 10 |
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
217 DCHECK_EQ(locked_pages_.size(), locked_page_count_); | 217 DCHECK_EQ(locked_pages_.size(), locked_page_count_); |
218 #endif | 218 #endif |
219 | 219 |
220 // Always behave as if memory was purged when trying to lock a 0 byte segment. | 220 // Always behave as if memory was purged when trying to lock a 0 byte segment. |
221 if (!length) | 221 if (!length) |
222 return PURGED; | 222 return PURGED; |
223 | 223 |
224 // Pin pages if supported. | 224 // Pin pages if supported. |
225 #if defined(OS_ANDROID) | 225 #if defined(OS_ANDROID) |
226 SharedMemoryHandle handle = shared_memory_.handle(); | 226 SharedMemoryHandle handle = shared_memory_.handle(); |
227 if (SharedMemory::IsHandleValid(handle)) { | 227 if (handle.IsValid()) { |
228 if (ashmem_pin_region( | 228 if (ashmem_pin_region(handle.GetHandle(), |
229 handle.fd, AlignToPageSize(sizeof(SharedState)) + offset, length)) { | 229 AlignToPageSize(sizeof(SharedState)) + offset, |
| 230 length)) { |
230 return PURGED; | 231 return PURGED; |
231 } | 232 } |
232 } | 233 } |
233 #endif | 234 #endif |
234 | 235 |
235 return SUCCESS; | 236 return SUCCESS; |
236 } | 237 } |
237 | 238 |
238 void DiscardableSharedMemory::Unlock(size_t offset, size_t length) { | 239 void DiscardableSharedMemory::Unlock(size_t offset, size_t length) { |
239 DCHECK_EQ(AlignToPageSize(offset), offset); | 240 DCHECK_EQ(AlignToPageSize(offset), offset); |
240 DCHECK_EQ(AlignToPageSize(length), length); | 241 DCHECK_EQ(AlignToPageSize(length), length); |
241 | 242 |
242 // Calls to this function must be synchronized properly. | 243 // Calls to this function must be synchronized properly. |
243 DFAKE_SCOPED_LOCK(thread_collision_warner_); | 244 DFAKE_SCOPED_LOCK(thread_collision_warner_); |
244 | 245 |
245 // Zero for length means "everything onward". | 246 // Zero for length means "everything onward". |
246 if (!length) | 247 if (!length) |
247 length = AlignToPageSize(mapped_size_) - offset; | 248 length = AlignToPageSize(mapped_size_) - offset; |
248 | 249 |
249 DCHECK(shared_memory_.memory()); | 250 DCHECK(shared_memory_.memory()); |
250 | 251 |
251 // Unpin pages if supported. | 252 // Unpin pages if supported. |
252 #if defined(OS_ANDROID) | 253 #if defined(OS_ANDROID) |
253 SharedMemoryHandle handle = shared_memory_.handle(); | 254 SharedMemoryHandle handle = shared_memory_.handle(); |
254 if (SharedMemory::IsHandleValid(handle)) { | 255 if (handle.IsValid()) { |
255 if (ashmem_unpin_region( | 256 if (ashmem_unpin_region(handle.GetHandle(), |
256 handle.fd, AlignToPageSize(sizeof(SharedState)) + offset, length)) { | 257 AlignToPageSize(sizeof(SharedState)) + offset, |
| 258 length)) { |
257 DPLOG(ERROR) << "ashmem_unpin_region() failed"; | 259 DPLOG(ERROR) << "ashmem_unpin_region() failed"; |
258 } | 260 } |
259 } | 261 } |
260 #endif | 262 #endif |
261 | 263 |
262 size_t start = offset / base::GetPageSize(); | 264 size_t start = offset / base::GetPageSize(); |
263 size_t end = start + length / base::GetPageSize(); | 265 size_t end = start + length / base::GetPageSize(); |
264 DCHECK_LE(start, end); | 266 DCHECK_LE(start, end); |
265 DCHECK_LE(end, AlignToPageSize(mapped_size_) / base::GetPageSize()); | 267 DCHECK_LE(end, AlignToPageSize(mapped_size_) / base::GetPageSize()); |
266 | 268 |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
395 | 397 |
396 void DiscardableSharedMemory::Close() { | 398 void DiscardableSharedMemory::Close() { |
397 shared_memory_.Close(); | 399 shared_memory_.Close(); |
398 } | 400 } |
399 | 401 |
400 Time DiscardableSharedMemory::Now() const { | 402 Time DiscardableSharedMemory::Now() const { |
401 return Time::Now(); | 403 return Time::Now(); |
402 } | 404 } |
403 | 405 |
404 } // namespace base | 406 } // namespace base |
OLD | NEW |