Chromium Code Reviews| 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 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 227 | 223 |
| 228 // Pin pages if supported. | 224 // Pin pages if supported. |
| 229 #if defined(OS_ANDROID) | 225 #if defined(OS_ANDROID) |
| 230 SharedMemoryHandle handle = shared_memory_.handle(); | 226 SharedMemoryHandle handle = shared_memory_.handle(); |
| 231 if (SharedMemory::IsHandleValid(handle)) { | 227 if (SharedMemory::IsHandleValid(handle)) { |
| 232 if (ashmem_pin_region( | 228 if (ashmem_pin_region( |
| 233 handle.fd, AlignToPageSize(sizeof(SharedState)) + offset, length)) { | 229 handle.fd, AlignToPageSize(sizeof(SharedState)) + offset, length)) { |
| 234 return PURGED; | 230 return PURGED; |
| 235 } | 231 } |
| 236 } | 232 } |
| 237 #elif defined(OS_WIN) | |
| 238 if (base::win::GetVersion() >= base::win::VERSION_WIN8) { | |
| 239 if (!VirtualAlloc(reinterpret_cast<char*>(shared_memory_.memory()) + | |
| 240 AlignToPageSize(sizeof(SharedState)) + offset, | |
| 241 length, MEM_RESET_UNDO, PAGE_READWRITE)) { | |
|
danakj
2016/03/25 23:49:39
I'm confused by this code heh.
https://msdn.micro
reveman
2016/03/26 07:01:10
Discardable memory is initially locked. The only t
| |
| 242 return PURGED; | |
| 243 } | |
| 244 } | |
| 245 #endif | 233 #endif |
| 246 | 234 |
| 247 return SUCCESS; | 235 return SUCCESS; |
| 248 } | 236 } |
| 249 | 237 |
| 250 void DiscardableSharedMemory::Unlock(size_t offset, size_t length) { | 238 void DiscardableSharedMemory::Unlock(size_t offset, size_t length) { |
| 251 DCHECK_EQ(AlignToPageSize(offset), offset); | 239 DCHECK_EQ(AlignToPageSize(offset), offset); |
| 252 DCHECK_EQ(AlignToPageSize(length), length); | 240 DCHECK_EQ(AlignToPageSize(length), length); |
| 253 | 241 |
| 254 // Calls to this function must be synchronized properly. | 242 // Calls to this function must be synchronized properly. |
| 255 DFAKE_SCOPED_LOCK(thread_collision_warner_); | 243 DFAKE_SCOPED_LOCK(thread_collision_warner_); |
| 256 | 244 |
| 257 // Zero for length means "everything onward". | 245 // Zero for length means "everything onward". |
| 258 if (!length) | 246 if (!length) |
| 259 length = AlignToPageSize(mapped_size_) - offset; | 247 length = AlignToPageSize(mapped_size_) - offset; |
| 260 | 248 |
| 261 DCHECK(shared_memory_.memory()); | 249 DCHECK(shared_memory_.memory()); |
| 262 | 250 |
| 263 // Unpin pages if supported. | 251 // Unpin pages if supported. |
| 264 #if defined(OS_ANDROID) | 252 #if defined(OS_ANDROID) |
| 265 SharedMemoryHandle handle = shared_memory_.handle(); | 253 SharedMemoryHandle handle = shared_memory_.handle(); |
| 266 if (SharedMemory::IsHandleValid(handle)) { | 254 if (SharedMemory::IsHandleValid(handle)) { |
| 267 if (ashmem_unpin_region( | 255 if (ashmem_unpin_region( |
| 268 handle.fd, AlignToPageSize(sizeof(SharedState)) + offset, length)) { | 256 handle.fd, AlignToPageSize(sizeof(SharedState)) + offset, length)) { |
| 269 DPLOG(ERROR) << "ashmem_unpin_region() failed"; | 257 DPLOG(ERROR) << "ashmem_unpin_region() failed"; |
| 270 } | 258 } |
| 271 } | 259 } |
| 272 #elif defined(OS_WIN) | |
| 273 if (base::win::GetVersion() >= base::win::VERSION_WIN8) { | |
| 274 // Note: MEM_RESET is not technically gated on Win8. However, this Unlock | |
| 275 // function needs to match the Lock behaviour (MEM_RESET_UNDO) to properly | |
| 276 // implement memory pinning. It needs to bias towards preserving the | |
| 277 // contents of memory between an Unlock and next Lock. | |
| 278 if (!VirtualAlloc(reinterpret_cast<char*>(shared_memory_.memory()) + | |
| 279 AlignToPageSize(sizeof(SharedState)) + offset, | |
| 280 length, MEM_RESET, PAGE_READWRITE)) { | |
| 281 DPLOG(ERROR) << "VirtualAlloc() MEM_RESET failed in Unlock()"; | |
| 282 } | |
| 283 } | |
| 284 #endif | 260 #endif |
| 285 | 261 |
| 286 size_t start = offset / base::GetPageSize(); | 262 size_t start = offset / base::GetPageSize(); |
| 287 size_t end = start + length / base::GetPageSize(); | 263 size_t end = start + length / base::GetPageSize(); |
| 288 DCHECK_LE(start, end); | 264 DCHECK_LE(start, end); |
| 289 DCHECK_LE(end, AlignToPageSize(mapped_size_) / base::GetPageSize()); | 265 DCHECK_LE(end, AlignToPageSize(mapped_size_) / base::GetPageSize()); |
| 290 | 266 |
| 291 // Remove pages from |locked_page_count_|. | 267 // Remove pages from |locked_page_count_|. |
| 292 // Note: Unlocking a page that is not locked is an error. | 268 // Note: Unlocking a page that is not locked is an error. |
| 293 DCHECK_GE(locked_page_count_, end - start); | 269 DCHECK_GE(locked_page_count_, end - start); |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 414 | 390 |
| 415 void DiscardableSharedMemory::Close() { | 391 void DiscardableSharedMemory::Close() { |
| 416 shared_memory_.Close(); | 392 shared_memory_.Close(); |
| 417 } | 393 } |
| 418 | 394 |
| 419 Time DiscardableSharedMemory::Now() const { | 395 Time DiscardableSharedMemory::Now() const { |
| 420 return Time::Now(); | 396 return Time::Now(); |
| 421 } | 397 } |
| 422 | 398 |
| 423 } // namespace base | 399 } // namespace base |
| OLD | NEW |