| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/shared_memory.h" | 5 #include "base/memory/shared_memory.h" |
| 6 | 6 |
| 7 #include <aclapi.h> | 7 #include <aclapi.h> |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/memory/shared_memory_tracker.h" |
| 12 #include "base/metrics/histogram_macros.h" | 13 #include "base/metrics/histogram_macros.h" |
| 13 #include "base/rand_util.h" | 14 #include "base/rand_util.h" |
| 14 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
| 15 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
| 16 #include "base/unguessable_token.h" | 17 #include "base/unguessable_token.h" |
| 17 | 18 |
| 18 namespace { | 19 namespace { |
| 19 | 20 |
| 20 // Errors that can occur during Shared Memory construction. | 21 // Errors that can occur during Shared Memory construction. |
| 21 // These match tools/metrics/histograms/histograms.xml. | 22 // These match tools/metrics/histograms/histograms.xml. |
| (...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 302 return false; | 303 return false; |
| 303 | 304 |
| 304 memory_ = MapViewOfFile( | 305 memory_ = MapViewOfFile( |
| 305 shm_.GetHandle(), | 306 shm_.GetHandle(), |
| 306 read_only_ ? FILE_MAP_READ : FILE_MAP_READ | FILE_MAP_WRITE, | 307 read_only_ ? FILE_MAP_READ : FILE_MAP_READ | FILE_MAP_WRITE, |
| 307 static_cast<uint64_t>(offset) >> 32, static_cast<DWORD>(offset), bytes); | 308 static_cast<uint64_t>(offset) >> 32, static_cast<DWORD>(offset), bytes); |
| 308 if (memory_ != NULL) { | 309 if (memory_ != NULL) { |
| 309 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) & | 310 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) & |
| 310 (SharedMemory::MAP_MINIMUM_ALIGNMENT - 1)); | 311 (SharedMemory::MAP_MINIMUM_ALIGNMENT - 1)); |
| 311 mapped_size_ = GetMemorySectionSize(memory_); | 312 mapped_size_ = GetMemorySectionSize(memory_); |
| 313 SharedMemoryTracker::GetInstance()->IncrementMemoryUsage(*this); |
| 312 return true; | 314 return true; |
| 313 } | 315 } |
| 314 return false; | 316 return false; |
| 315 } | 317 } |
| 316 | 318 |
| 317 bool SharedMemory::Unmap() { | 319 bool SharedMemory::Unmap() { |
| 318 if (memory_ == NULL) | 320 if (memory_ == NULL) |
| 319 return false; | 321 return false; |
| 320 | 322 |
| 323 SharedMemoryTracker::GetInstance()->DecrementMemoryUsage(*this); |
| 321 UnmapViewOfFile(memory_); | 324 UnmapViewOfFile(memory_); |
| 322 memory_ = NULL; | 325 memory_ = NULL; |
| 323 return true; | 326 return true; |
| 324 } | 327 } |
| 325 | 328 |
| 326 SharedMemoryHandle SharedMemory::GetReadOnlyHandle() { | 329 SharedMemoryHandle SharedMemory::GetReadOnlyHandle() { |
| 327 HANDLE result; | 330 HANDLE result; |
| 328 ProcessHandle process = GetCurrentProcess(); | 331 ProcessHandle process = GetCurrentProcess(); |
| 329 if (!::DuplicateHandle(process, shm_.GetHandle(), process, &result, | 332 if (!::DuplicateHandle(process, shm_.GetHandle(), process, &result, |
| 330 FILE_MAP_READ | SECTION_QUERY, FALSE, 0)) { | 333 FILE_MAP_READ | SECTION_QUERY, FALSE, 0)) { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 350 SharedMemoryHandle SharedMemory::TakeHandle() { | 353 SharedMemoryHandle SharedMemory::TakeHandle() { |
| 351 SharedMemoryHandle handle(shm_); | 354 SharedMemoryHandle handle(shm_); |
| 352 handle.SetOwnershipPassesToIPC(true); | 355 handle.SetOwnershipPassesToIPC(true); |
| 353 shm_ = SharedMemoryHandle(); | 356 shm_ = SharedMemoryHandle(); |
| 354 memory_ = nullptr; | 357 memory_ = nullptr; |
| 355 mapped_size_ = 0; | 358 mapped_size_ = 0; |
| 356 return handle; | 359 return handle; |
| 357 } | 360 } |
| 358 | 361 |
| 359 } // namespace base | 362 } // namespace base |
| OLD | NEW |