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 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
319 | 320 |
320 if (external_section_ && !IsSectionSafeToMap(mapped_file_.Get())) | 321 if (external_section_ && !IsSectionSafeToMap(mapped_file_.Get())) |
321 return false; | 322 return false; |
322 | 323 |
323 memory_ = MapViewOfFile( | 324 memory_ = MapViewOfFile( |
324 mapped_file_.Get(), | 325 mapped_file_.Get(), |
325 read_only_ ? FILE_MAP_READ : FILE_MAP_READ | FILE_MAP_WRITE, | 326 read_only_ ? FILE_MAP_READ : FILE_MAP_READ | FILE_MAP_WRITE, |
326 static_cast<uint64_t>(offset) >> 32, static_cast<DWORD>(offset), bytes); | 327 static_cast<uint64_t>(offset) >> 32, static_cast<DWORD>(offset), bytes); |
327 if (memory_ != NULL) { | 328 if (memory_ != NULL) { |
328 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) & | 329 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) & |
329 (SharedMemory::MAP_MINIMUM_ALIGNMENT - 1)); | 330 (SharedMemory::MAP_MINIMUM_ALIGNMENT - 1)); |
330 mapped_size_ = GetMemorySectionSize(memory_); | 331 mapped_size_ = GetMemorySectionSize(memory_); |
332 SharedMemoryTracker::GetInstance()->IncrementMemoryUsage(*this); | |
331 return true; | 333 return true; |
332 } | 334 } |
333 return false; | 335 return false; |
334 } | 336 } |
335 | 337 |
336 bool SharedMemory::Unmap() { | 338 bool SharedMemory::Unmap() { |
337 if (memory_ == NULL) | 339 if (memory_ == NULL) |
338 return false; | 340 return false; |
339 | 341 |
340 UnmapViewOfFile(memory_); | 342 UnmapViewOfFile(memory_); |
343 SharedMemoryTracker::GetInstance()->DecrementMemoryUsage(*this); | |
341 memory_ = NULL; | 344 memory_ = NULL; |
342 return true; | 345 return true; |
343 } | 346 } |
344 | 347 |
345 bool SharedMemory::ShareToProcessCommon(ProcessHandle process, | 348 bool SharedMemory::ShareToProcessCommon(ProcessHandle process, |
346 SharedMemoryHandle* new_handle, | 349 SharedMemoryHandle* new_handle, |
347 bool close_self, | 350 bool close_self, |
348 ShareMode share_mode) { | 351 ShareMode share_mode) { |
349 *new_handle = SharedMemoryHandle(); | 352 *new_handle = SharedMemoryHandle(); |
350 DWORD access = FILE_MAP_READ | SECTION_QUERY; | 353 DWORD access = FILE_MAP_READ | SECTION_QUERY; |
(...skipping 19 matching lines...) Expand all Loading... | |
370 access, FALSE, options)) { | 373 access, FALSE, options)) { |
371 return false; | 374 return false; |
372 } | 375 } |
373 *new_handle = SharedMemoryHandle(result, base::GetProcId(process)); | 376 *new_handle = SharedMemoryHandle(result, base::GetProcId(process)); |
374 new_handle->SetOwnershipPassesToIPC(true); | 377 new_handle->SetOwnershipPassesToIPC(true); |
375 return true; | 378 return true; |
376 } | 379 } |
377 | 380 |
378 | 381 |
379 void SharedMemory::Close() { | 382 void SharedMemory::Close() { |
383 SharedMemoryTracker::GetInstance()->DecrementMemoryUsage(*this); | |
Primiano Tucci (use gerrit)
2017/01/20 16:34:52
see my prev comment about close vs unmap. I think
hajimehoshi
2017/01/23 11:59:09
Done.
| |
380 mapped_file_.Close(); | 384 mapped_file_.Close(); |
381 } | 385 } |
382 | 386 |
383 SharedMemoryHandle SharedMemory::handle() const { | 387 SharedMemoryHandle SharedMemory::handle() const { |
384 return SharedMemoryHandle(mapped_file_.Get(), base::GetCurrentProcId()); | 388 return SharedMemoryHandle(mapped_file_.Get(), base::GetCurrentProcId()); |
385 } | 389 } |
386 | 390 |
387 SharedMemoryHandle SharedMemory::TakeHandle() { | 391 SharedMemoryHandle SharedMemory::TakeHandle() { |
388 SharedMemoryHandle handle(mapped_file_.Take(), base::GetCurrentProcId()); | 392 SharedMemoryHandle handle(mapped_file_.Take(), base::GetCurrentProcId()); |
389 handle.SetOwnershipPassesToIPC(true); | 393 handle.SetOwnershipPassesToIPC(true); |
390 memory_ = nullptr; | 394 memory_ = nullptr; |
391 mapped_size_ = 0; | 395 mapped_size_ = 0; |
392 return handle; | 396 return handle; |
393 } | 397 } |
394 | 398 |
395 } // namespace base | 399 } // namespace base |
OLD | NEW |