| 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 |
| 11 #include "base/atomicops.h" | 11 #include "base/atomicops.h" |
| 12 #include "base/bits.h" | 12 #include "base/bits.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/numerics/safe_math.h" | 14 #include "base/numerics/safe_math.h" |
| 15 #include "base/process/process_metrics.h" | 15 #include "base/process/process_metrics.h" |
| 16 #include "build/build_config.h" | 16 #include "build/build_config.h" |
| 17 | 17 |
| 18 #if defined(OS_POSIX) && !defined(OS_NACL) | 18 #if defined(OS_POSIX) && !defined(OS_NACL) |
| 19 // For madvise() which is available on all POSIX compatible systems. | 19 // For madvise() which is available on all POSIX compatible systems. |
| 20 #include <sys/mman.h> | 20 #include <sys/mman.h> |
| 21 #endif | 21 #endif |
| 22 | 22 |
| 23 #if defined(OS_ANDROID) | 23 #if defined(OS_ANDROID) |
| 24 #include "third_party/ashmem/ashmem.h" | 24 #include "third_party/ashmem/ashmem.h" |
| 25 #endif | 25 #endif |
| 26 | 26 |
| 27 #if defined(OS_WIN) | |
| 28 #include "base/win/windows_version.h" | |
| 29 #endif | |
| 30 | |
| 31 namespace base { | 27 namespace base { |
| 32 namespace { | 28 namespace { |
| 33 | 29 |
| 34 // Use a machine-sized pointer as atomic type. It will use the Atomic32 or | 30 // Use a machine-sized pointer as atomic type. It will use the Atomic32 or |
| 35 // Atomic64 routines, depending on the architecture. | 31 // Atomic64 routines, depending on the architecture. |
| 36 typedef intptr_t AtomicType; | 32 typedef intptr_t AtomicType; |
| 37 typedef uintptr_t UAtomicType; | 33 typedef uintptr_t UAtomicType; |
| 38 | 34 |
| 39 // Template specialization for timestamp serialization/deserialization. This | 35 // Template specialization for timestamp serialization/deserialization. This |
| 40 // is used to serialize timestamps using Unix time on systems where AtomicType | 36 // is used to serialize timestamps using Unix time on systems where AtomicType |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 223 | 219 |
| 224 // Pin pages if supported. | 220 // Pin pages if supported. |
| 225 #if defined(OS_ANDROID) | 221 #if defined(OS_ANDROID) |
| 226 SharedMemoryHandle handle = shared_memory_.handle(); | 222 SharedMemoryHandle handle = shared_memory_.handle(); |
| 227 if (SharedMemory::IsHandleValid(handle)) { | 223 if (SharedMemory::IsHandleValid(handle)) { |
| 228 if (ashmem_pin_region( | 224 if (ashmem_pin_region( |
| 229 handle.fd, AlignToPageSize(sizeof(SharedState)) + offset, length)) { | 225 handle.fd, AlignToPageSize(sizeof(SharedState)) + offset, length)) { |
| 230 return PURGED; | 226 return PURGED; |
| 231 } | 227 } |
| 232 } | 228 } |
| 233 #elif defined(OS_WIN) | |
| 234 if (base::win::GetVersion() >= base::win::VERSION_WIN8) { | |
| 235 if (!VirtualAlloc(reinterpret_cast<char*>(shared_memory_.memory()) + | |
| 236 AlignToPageSize(sizeof(SharedState)) + offset, | |
| 237 length, MEM_RESET_UNDO, PAGE_READWRITE)) { | |
| 238 return PURGED; | |
| 239 } | |
| 240 } | |
| 241 #endif | 229 #endif |
| 242 | 230 |
| 243 return SUCCESS; | 231 return SUCCESS; |
| 244 } | 232 } |
| 245 | 233 |
| 246 void DiscardableSharedMemory::Unlock(size_t offset, size_t length) { | 234 void DiscardableSharedMemory::Unlock(size_t offset, size_t length) { |
| 247 DCHECK_EQ(AlignToPageSize(offset), offset); | 235 DCHECK_EQ(AlignToPageSize(offset), offset); |
| 248 DCHECK_EQ(AlignToPageSize(length), length); | 236 DCHECK_EQ(AlignToPageSize(length), length); |
| 249 | 237 |
| 250 // Calls to this function must be synchronized properly. | 238 // Calls to this function must be synchronized properly. |
| 251 DFAKE_SCOPED_LOCK(thread_collision_warner_); | 239 DFAKE_SCOPED_LOCK(thread_collision_warner_); |
| 252 | 240 |
| 253 // Zero for length means "everything onward". | 241 // Zero for length means "everything onward". |
| 254 if (!length) | 242 if (!length) |
| 255 length = AlignToPageSize(mapped_size_) - offset; | 243 length = AlignToPageSize(mapped_size_) - offset; |
| 256 | 244 |
| 257 DCHECK(shared_memory_.memory()); | 245 DCHECK(shared_memory_.memory()); |
| 258 | 246 |
| 259 // Unpin pages if supported. | 247 // Unpin pages if supported. |
| 260 #if defined(OS_ANDROID) | 248 #if defined(OS_ANDROID) |
| 261 SharedMemoryHandle handle = shared_memory_.handle(); | 249 SharedMemoryHandle handle = shared_memory_.handle(); |
| 262 if (SharedMemory::IsHandleValid(handle)) { | 250 if (SharedMemory::IsHandleValid(handle)) { |
| 263 if (ashmem_unpin_region( | 251 if (ashmem_unpin_region( |
| 264 handle.fd, AlignToPageSize(sizeof(SharedState)) + offset, length)) { | 252 handle.fd, AlignToPageSize(sizeof(SharedState)) + offset, length)) { |
| 265 DPLOG(ERROR) << "ashmem_unpin_region() failed"; | 253 DPLOG(ERROR) << "ashmem_unpin_region() failed"; |
| 266 } | 254 } |
| 267 } | 255 } |
| 268 #elif defined(OS_WIN) | |
| 269 if (base::win::GetVersion() >= base::win::VERSION_WIN8) { | |
| 270 // Note: MEM_RESET is not technically gated on Win8. However, this Unlock | |
| 271 // function needs to match the Lock behaviour (MEM_RESET_UNDO) to properly | |
| 272 // implement memory pinning. It needs to bias towards preserving the | |
| 273 // contents of memory between an Unlock and next Lock. | |
| 274 if (!VirtualAlloc(reinterpret_cast<char*>(shared_memory_.memory()) + | |
| 275 AlignToPageSize(sizeof(SharedState)) + offset, | |
| 276 length, MEM_RESET, PAGE_READWRITE)) { | |
| 277 DPLOG(ERROR) << "VirtualAlloc() MEM_RESET failed in Unlock()"; | |
| 278 } | |
| 279 } | |
| 280 #endif | 256 #endif |
| 281 | 257 |
| 282 size_t start = offset / base::GetPageSize(); | 258 size_t start = offset / base::GetPageSize(); |
| 283 size_t end = start + length / base::GetPageSize(); | 259 size_t end = start + length / base::GetPageSize(); |
| 284 DCHECK_LE(start, end); | 260 DCHECK_LE(start, end); |
| 285 DCHECK_LE(end, AlignToPageSize(mapped_size_) / base::GetPageSize()); | 261 DCHECK_LE(end, AlignToPageSize(mapped_size_) / base::GetPageSize()); |
| 286 | 262 |
| 287 // Remove pages from |locked_page_count_|. | 263 // Remove pages from |locked_page_count_|. |
| 288 // Note: Unlocking a page that is not locked is an error. | 264 // Note: Unlocking a page that is not locked is an error. |
| 289 DCHECK_GE(locked_page_count_, end - start); | 265 DCHECK_GE(locked_page_count_, end - start); |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 410 | 386 |
| 411 void DiscardableSharedMemory::Close() { | 387 void DiscardableSharedMemory::Close() { |
| 412 shared_memory_.Close(); | 388 shared_memory_.Close(); |
| 413 } | 389 } |
| 414 | 390 |
| 415 Time DiscardableSharedMemory::Now() const { | 391 Time DiscardableSharedMemory::Now() const { |
| 416 return Time::Now(); | 392 return Time::Now(); |
| 417 } | 393 } |
| 418 | 394 |
| 419 } // namespace base | 395 } // namespace base |
| OLD | NEW |