| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <errno.h> | 7 #include <errno.h> |
| 8 #include <fcntl.h> | 8 #include <fcntl.h> |
| 9 #include <stddef.h> | 9 #include <stddef.h> |
| 10 #include <sys/mman.h> | 10 #include <sys/mman.h> |
| 11 #include <sys/stat.h> | 11 #include <sys/stat.h> |
| 12 #include <unistd.h> | 12 #include <unistd.h> |
| 13 | 13 |
| 14 #include "base/files/file_util.h" | 14 #include "base/files/file_util.h" |
| 15 #include "base/files/scoped_file.h" | 15 #include "base/files/scoped_file.h" |
| 16 #include "base/logging.h" | 16 #include "base/logging.h" |
| 17 #include "base/memory/shared_memory_helper.h" | 17 #include "base/memory/shared_memory_helper.h" |
| 18 #include "base/memory/shared_memory_tracker.h" |
| 18 #include "base/posix/eintr_wrapper.h" | 19 #include "base/posix/eintr_wrapper.h" |
| 19 #include "base/posix/safe_strerror.h" | 20 #include "base/posix/safe_strerror.h" |
| 20 #include "base/process/process_metrics.h" | 21 #include "base/process/process_metrics.h" |
| 21 #include "base/scoped_generic.h" | 22 #include "base/scoped_generic.h" |
| 22 #include "base/strings/utf_string_conversions.h" | 23 #include "base/strings/utf_string_conversions.h" |
| 23 #include "base/threading/thread_restrictions.h" | 24 #include "base/threading/thread_restrictions.h" |
| 24 #include "build/build_config.h" | 25 #include "build/build_config.h" |
| 25 | 26 |
| 26 #if defined(OS_ANDROID) | 27 #if defined(OS_ANDROID) |
| 27 #include "base/os_compat_android.h" | 28 #include "base/os_compat_android.h" |
| (...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 277 } | 278 } |
| 278 #endif | 279 #endif |
| 279 | 280 |
| 280 memory_ = mmap(NULL, bytes, PROT_READ | (read_only_ ? 0 : PROT_WRITE), | 281 memory_ = mmap(NULL, bytes, PROT_READ | (read_only_ ? 0 : PROT_WRITE), |
| 281 MAP_SHARED, mapped_file_, offset); | 282 MAP_SHARED, mapped_file_, offset); |
| 282 | 283 |
| 283 bool mmap_succeeded = memory_ != (void*)-1 && memory_ != NULL; | 284 bool mmap_succeeded = memory_ != (void*)-1 && memory_ != NULL; |
| 284 if (mmap_succeeded) { | 285 if (mmap_succeeded) { |
| 285 mapped_size_ = bytes; | 286 mapped_size_ = bytes; |
| 286 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) & | 287 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) & |
| 287 (SharedMemory::MAP_MINIMUM_ALIGNMENT - 1)); | 288 (SharedMemory::MAP_MINIMUM_ALIGNMENT - 1)); |
| 289 if (!SharedMemoryTracker::GetInstance()->IncrementMemoryUsage(*this)) |
| 290 return false; |
| 288 } else { | 291 } else { |
| 289 memory_ = NULL; | 292 memory_ = NULL; |
| 290 } | 293 } |
| 291 | 294 |
| 292 return mmap_succeeded; | 295 return mmap_succeeded; |
| 293 } | 296 } |
| 294 | 297 |
| 295 bool SharedMemory::Unmap() { | 298 bool SharedMemory::Unmap() { |
| 296 if (memory_ == NULL) | 299 if (memory_ == NULL) |
| 297 return false; | 300 return false; |
| 298 | 301 |
| 299 munmap(memory_, mapped_size_); | 302 munmap(memory_, mapped_size_); |
| 303 SharedMemoryTracker::GetInstance()->DecrementMemoryUsage(*this); |
| 300 memory_ = NULL; | 304 memory_ = NULL; |
| 301 mapped_size_ = 0; | 305 mapped_size_ = 0; |
| 302 return true; | 306 return true; |
| 303 } | 307 } |
| 304 | 308 |
| 305 SharedMemoryHandle SharedMemory::handle() const { | 309 SharedMemoryHandle SharedMemory::handle() const { |
| 306 return FileDescriptor(mapped_file_, false); | 310 return FileDescriptor(mapped_file_, false); |
| 307 } | 311 } |
| 308 | 312 |
| 309 SharedMemoryHandle SharedMemory::TakeHandle() { | 313 SharedMemoryHandle SharedMemory::TakeHandle() { |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 384 | 388 |
| 385 if (close_self) { | 389 if (close_self) { |
| 386 Unmap(); | 390 Unmap(); |
| 387 Close(); | 391 Close(); |
| 388 } | 392 } |
| 389 | 393 |
| 390 return true; | 394 return true; |
| 391 } | 395 } |
| 392 | 396 |
| 393 } // namespace base | 397 } // namespace base |
| OLD | NEW |