| 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 | 17 |
| 17 namespace { | 18 namespace { |
| 18 | 19 |
| 19 // Errors that can occur during Shared Memory construction. | 20 // Errors that can occur during Shared Memory construction. |
| 20 // These match tools/metrics/histograms/histograms.xml. | 21 // These match tools/metrics/histograms/histograms.xml. |
| 21 // This enum is append-only. | 22 // This enum is append-only. |
| (...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 320 | 321 |
| 321 if (external_section_ && !IsSectionSafeToMap(mapped_file_.Get())) | 322 if (external_section_ && !IsSectionSafeToMap(mapped_file_.Get())) |
| 322 return false; | 323 return false; |
| 323 | 324 |
| 324 memory_ = MapViewOfFile( | 325 memory_ = MapViewOfFile( |
| 325 mapped_file_.Get(), | 326 mapped_file_.Get(), |
| 326 read_only_ ? FILE_MAP_READ : FILE_MAP_READ | FILE_MAP_WRITE, | 327 read_only_ ? FILE_MAP_READ : FILE_MAP_READ | FILE_MAP_WRITE, |
| 327 static_cast<uint64_t>(offset) >> 32, static_cast<DWORD>(offset), bytes); | 328 static_cast<uint64_t>(offset) >> 32, static_cast<DWORD>(offset), bytes); |
| 328 if (memory_ != NULL) { | 329 if (memory_ != NULL) { |
| 329 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) & | 330 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) & |
| 330 (SharedMemory::MAP_MINIMUM_ALIGNMENT - 1)); | 331 (SharedMemory::MAP_MINIMUM_ALIGNMENT - 1)); |
| 331 mapped_size_ = GetMemorySectionSize(memory_); | 332 mapped_size_ = GetMemorySectionSize(memory_); |
| 333 SharedMemoryTracker::GetInstance()->IncrementMemoryUsage(*this); |
| 332 return true; | 334 return true; |
| 333 } | 335 } |
| 334 return false; | 336 return false; |
| 335 } | 337 } |
| 336 | 338 |
| 337 bool SharedMemory::Unmap() { | 339 bool SharedMemory::Unmap() { |
| 338 if (memory_ == NULL) | 340 if (memory_ == NULL) |
| 339 return false; | 341 return false; |
| 340 | 342 |
| 341 UnmapViewOfFile(memory_); | 343 UnmapViewOfFile(memory_); |
| 344 SharedMemoryTracker::GetInstance()->DecrementMemoryUsage(*this); |
| 342 memory_ = NULL; | 345 memory_ = NULL; |
| 343 return true; | 346 return true; |
| 344 } | 347 } |
| 345 | 348 |
| 346 bool SharedMemory::ShareToProcessCommon(ProcessHandle process, | 349 bool SharedMemory::ShareToProcessCommon(ProcessHandle process, |
| 347 SharedMemoryHandle* new_handle, | 350 SharedMemoryHandle* new_handle, |
| 348 bool close_self, | 351 bool close_self, |
| 349 ShareMode share_mode) { | 352 ShareMode share_mode) { |
| 350 *new_handle = SharedMemoryHandle(); | 353 *new_handle = SharedMemoryHandle(); |
| 351 DWORD access = FILE_MAP_READ | SECTION_QUERY; | 354 DWORD access = FILE_MAP_READ | SECTION_QUERY; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 387 | 390 |
| 388 SharedMemoryHandle SharedMemory::TakeHandle() { | 391 SharedMemoryHandle SharedMemory::TakeHandle() { |
| 389 SharedMemoryHandle handle(mapped_file_.Take(), base::GetCurrentProcId()); | 392 SharedMemoryHandle handle(mapped_file_.Take(), base::GetCurrentProcId()); |
| 390 handle.SetOwnershipPassesToIPC(true); | 393 handle.SetOwnershipPassesToIPC(true); |
| 391 memory_ = nullptr; | 394 memory_ = nullptr; |
| 392 mapped_size_ = 0; | 395 mapped_size_ = 0; |
| 393 return handle; | 396 return handle; |
| 394 } | 397 } |
| 395 | 398 |
| 396 } // namespace base | 399 } // namespace base |
| OLD | NEW |