| 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 <limits> | 14 #include <limits> |
| 15 | 15 |
| 16 #include "base/logging.h" | 16 #include "base/logging.h" |
| 17 #include "base/memory/shared_memory_tracker.h" |
| 17 | 18 |
| 18 namespace base { | 19 namespace base { |
| 19 | 20 |
| 20 SharedMemory::SharedMemory() | 21 SharedMemory::SharedMemory() |
| 21 : mapped_size_(0), memory_(NULL), read_only_(false), requested_size_(0) {} | 22 : mapped_size_(0), memory_(NULL), read_only_(false), requested_size_(0) {} |
| 22 | 23 |
| 23 SharedMemory::SharedMemory(const SharedMemoryHandle& handle, bool read_only) | 24 SharedMemory::SharedMemory(const SharedMemoryHandle& handle, bool read_only) |
| 24 : shm_(handle), | 25 : shm_(handle), |
| 25 mapped_size_(0), | 26 mapped_size_(0), |
| 26 memory_(NULL), | 27 memory_(NULL), |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 return false; | 79 return false; |
| 79 | 80 |
| 80 memory_ = mmap(NULL, bytes, PROT_READ | (read_only_ ? 0 : PROT_WRITE), | 81 memory_ = mmap(NULL, bytes, PROT_READ | (read_only_ ? 0 : PROT_WRITE), |
| 81 MAP_SHARED, shm_.GetHandle(), offset); | 82 MAP_SHARED, shm_.GetHandle(), offset); |
| 82 | 83 |
| 83 bool mmap_succeeded = memory_ != MAP_FAILED && memory_ != NULL; | 84 bool mmap_succeeded = memory_ != MAP_FAILED && memory_ != NULL; |
| 84 if (mmap_succeeded) { | 85 if (mmap_succeeded) { |
| 85 mapped_size_ = bytes; | 86 mapped_size_ = bytes; |
| 86 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) & | 87 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) & |
| 87 (SharedMemory::MAP_MINIMUM_ALIGNMENT - 1)); | 88 (SharedMemory::MAP_MINIMUM_ALIGNMENT - 1)); |
| 89 SharedMemoryTracker::GetInstance()->IncrementMemoryUsage(*this); |
| 88 } else { | 90 } else { |
| 89 memory_ = NULL; | 91 memory_ = NULL; |
| 90 } | 92 } |
| 91 | 93 |
| 92 return mmap_succeeded; | 94 return mmap_succeeded; |
| 93 } | 95 } |
| 94 | 96 |
| 95 bool SharedMemory::Unmap() { | 97 bool SharedMemory::Unmap() { |
| 96 if (memory_ == NULL) | 98 if (memory_ == NULL) |
| 97 return false; | 99 return false; |
| 98 | 100 |
| 101 SharedMemoryTracker::GetInstance()->DecrementMemoryUsage(*this); |
| 99 if (munmap(memory_, mapped_size_) < 0) | 102 if (munmap(memory_, mapped_size_) < 0) |
| 100 DPLOG(ERROR) << "munmap"; | 103 DPLOG(ERROR) << "munmap"; |
| 101 memory_ = NULL; | 104 memory_ = NULL; |
| 102 mapped_size_ = 0; | 105 mapped_size_ = 0; |
| 103 return true; | 106 return true; |
| 104 } | 107 } |
| 105 | 108 |
| 106 SharedMemoryHandle SharedMemory::handle() const { | 109 SharedMemoryHandle SharedMemory::handle() const { |
| 107 SharedMemoryHandle handle_copy = shm_; | 110 SharedMemoryHandle handle_copy = shm_; |
| 108 handle_copy.SetOwnershipPassesToIPC(false); | 111 handle_copy.SetOwnershipPassesToIPC(false); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 125 } | 128 } |
| 126 } | 129 } |
| 127 | 130 |
| 128 SharedMemoryHandle SharedMemory::GetReadOnlyHandle() { | 131 SharedMemoryHandle SharedMemory::GetReadOnlyHandle() { |
| 129 // Untrusted code can't create descriptors or handles, which is needed to | 132 // Untrusted code can't create descriptors or handles, which is needed to |
| 130 // drop permissions. | 133 // drop permissions. |
| 131 return SharedMemoryHandle(); | 134 return SharedMemoryHandle(); |
| 132 } | 135 } |
| 133 | 136 |
| 134 } // namespace base | 137 } // namespace base |
| OLD | NEW |