| 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 <mach/mach_vm.h> | 8 #include <mach/mach_vm.h> |
| 9 #include <stddef.h> | 9 #include <stddef.h> |
| 10 #include <sys/mman.h> | 10 #include <sys/mman.h> |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 } | 121 } |
| 122 | 122 |
| 123 bool SharedMemory::CreateAndMapAnonymous(size_t size) { | 123 bool SharedMemory::CreateAndMapAnonymous(size_t size) { |
| 124 return CreateAnonymous(size) && Map(size); | 124 return CreateAnonymous(size) && Map(size); |
| 125 } | 125 } |
| 126 | 126 |
| 127 // Chromium mostly only uses the unique/private shmem as specified by | 127 // Chromium mostly only uses the unique/private shmem as specified by |
| 128 // "name == L"". The exception is in the StatsTable. | 128 // "name == L"". The exception is in the StatsTable. |
| 129 bool SharedMemory::Create(const SharedMemoryCreateOptions& options) { | 129 bool SharedMemory::Create(const SharedMemoryCreateOptions& options) { |
| 130 DCHECK(!shm_.IsValid()); | 130 DCHECK(!shm_.IsValid()); |
| 131 if (options.size == 0) { | 131 if (options.size == 0) |
| 132 last_error_ = SharedMemoryError::BAD_PARAMS; | |
| 133 return false; | 132 return false; |
| 134 } | |
| 135 | 133 |
| 136 if (options.size > static_cast<size_t>(std::numeric_limits<int>::max())) { | 134 if (options.size > static_cast<size_t>(std::numeric_limits<int>::max())) |
| 137 last_error_ = SharedMemoryError::BAD_PARAMS; | |
| 138 return false; | 135 return false; |
| 139 } | |
| 140 | 136 |
| 141 if (options.type == SharedMemoryHandle::MACH) { | 137 if (options.type == SharedMemoryHandle::MACH) { |
| 142 shm_ = SharedMemoryHandle(options.size, UnguessableToken::Create()); | 138 shm_ = SharedMemoryHandle(options.size, UnguessableToken::Create()); |
| 143 requested_size_ = options.size; | 139 requested_size_ = options.size; |
| 144 return shm_.IsValid(); | 140 return shm_.IsValid(); |
| 145 } | 141 } |
| 146 | 142 |
| 147 // This function theoretically can block on the disk. Both profiling of real | 143 // This function theoretically can block on the disk. Both profiling of real |
| 148 // users and local instrumentation shows that this is a real problem. | 144 // users and local instrumentation shows that this is a real problem. |
| 149 // https://code.google.com/p/chromium/issues/detail?id=466437 | 145 // https://code.google.com/p/chromium/issues/detail?id=466437 |
| 150 base::ThreadRestrictions::ScopedAllowIO allow_io; | 146 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 151 | 147 |
| 152 ScopedFILE fp; | 148 ScopedFILE fp; |
| 153 ScopedFD readonly_fd; | 149 ScopedFD readonly_fd; |
| 154 | 150 |
| 155 FilePath path; | 151 FilePath path; |
| 156 bool result = CreateAnonymousSharedMemory(options, &fp, &readonly_fd, &path, | 152 bool result = CreateAnonymousSharedMemory(options, &fp, &readonly_fd, &path); |
| 157 &last_error_); | |
| 158 if (!result) | 153 if (!result) |
| 159 return false; | 154 return false; |
| 160 DCHECK(fp); // Should be guaranteed by CreateAnonymousSharedMemory(). | 155 DCHECK(fp); // Should be guaranteed by CreateAnonymousSharedMemory(). |
| 161 | 156 |
| 162 // Get current size. | 157 // Get current size. |
| 163 struct stat stat; | 158 struct stat stat; |
| 164 if (fstat(fileno(fp.get()), &stat) != 0) { | 159 if (fstat(fileno(fp.get()), &stat) != 0) |
| 165 last_error_ = SharedMemoryError::STAT_FAILED; | |
| 166 return false; | 160 return false; |
| 167 } | |
| 168 const size_t current_size = stat.st_size; | 161 const size_t current_size = stat.st_size; |
| 169 if (current_size != options.size) { | 162 if (current_size != options.size) { |
| 170 if (HANDLE_EINTR(ftruncate(fileno(fp.get()), options.size)) != 0) { | 163 if (HANDLE_EINTR(ftruncate(fileno(fp.get()), options.size)) != 0) |
| 171 last_error_ = SharedMemoryError::TRUNCATE_FAILED; | |
| 172 return false; | 164 return false; |
| 173 } | |
| 174 } | 165 } |
| 175 requested_size_ = options.size; | 166 requested_size_ = options.size; |
| 176 | 167 |
| 177 int mapped_file = -1; | 168 int mapped_file = -1; |
| 178 int readonly_mapped_file = -1; | 169 int readonly_mapped_file = -1; |
| 179 result = PrepareMapFile(std::move(fp), std::move(readonly_fd), &mapped_file, | 170 result = PrepareMapFile(std::move(fp), std::move(readonly_fd), &mapped_file, |
| 180 &readonly_mapped_file, &last_error_); | 171 &readonly_mapped_file); |
| 181 shm_ = SharedMemoryHandle(FileDescriptor(mapped_file, false), options.size, | 172 shm_ = SharedMemoryHandle(FileDescriptor(mapped_file, false), options.size, |
| 182 UnguessableToken::Create()); | 173 UnguessableToken::Create()); |
| 183 readonly_shm_ = | 174 readonly_shm_ = |
| 184 SharedMemoryHandle(FileDescriptor(readonly_mapped_file, false), | 175 SharedMemoryHandle(FileDescriptor(readonly_mapped_file, false), |
| 185 options.size, shm_.GetGUID()); | 176 options.size, shm_.GetGUID()); |
| 186 return result; | 177 return result; |
| 187 } | 178 } |
| 188 | 179 |
| 189 bool SharedMemory::MapAt(off_t offset, size_t bytes) { | 180 bool SharedMemory::MapAt(off_t offset, size_t bytes) { |
| 190 if (!shm_.IsValid()) { | 181 if (!shm_.IsValid()) |
| 191 last_error_ = SharedMemoryError::BAD_PARAMS; | |
| 192 return false; | 182 return false; |
| 193 } | 183 if (bytes > static_cast<size_t>(std::numeric_limits<int>::max())) |
| 194 if (bytes > static_cast<size_t>(std::numeric_limits<int>::max())) { | |
| 195 last_error_ = SharedMemoryError::BAD_PARAMS; | |
| 196 return false; | 184 return false; |
| 197 } | 185 if (memory_) |
| 198 if (memory_) { | |
| 199 last_error_ = SharedMemoryError::BAD_PARAMS; | |
| 200 return false; | 186 return false; |
| 201 } | |
| 202 | 187 |
| 203 bool success = shm_.MapAt(offset, bytes, &memory_, read_only_); | 188 bool success = shm_.MapAt(offset, bytes, &memory_, read_only_); |
| 204 if (success) { | 189 if (success) { |
| 205 mapped_size_ = bytes; | 190 mapped_size_ = bytes; |
| 206 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) & | 191 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) & |
| 207 (SharedMemory::MAP_MINIMUM_ALIGNMENT - 1)); | 192 (SharedMemory::MAP_MINIMUM_ALIGNMENT - 1)); |
| 208 mapped_memory_mechanism_ = shm_.type_; | 193 mapped_memory_mechanism_ = shm_.type_; |
| 209 SharedMemoryTracker::GetInstance()->IncrementMemoryUsage(*this); | 194 SharedMemoryTracker::GetInstance()->IncrementMemoryUsage(*this); |
| 210 } else { | 195 } else { |
| 211 last_error_ = SharedMemoryError::MMAP_FAILED; | |
| 212 memory_ = NULL; | 196 memory_ = NULL; |
| 213 } | 197 } |
| 214 | 198 |
| 215 return success; | 199 return success; |
| 216 } | 200 } |
| 217 | 201 |
| 218 bool SharedMemory::Unmap() { | 202 bool SharedMemory::Unmap() { |
| 219 if (memory_ == NULL) | 203 if (memory_ == NULL) |
| 220 return false; | 204 return false; |
| 221 | 205 |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 266 | 250 |
| 267 DCHECK(shm_.IsValid()); | 251 DCHECK(shm_.IsValid()); |
| 268 base::SharedMemoryHandle new_handle; | 252 base::SharedMemoryHandle new_handle; |
| 269 bool success = MakeMachSharedMemoryHandleReadOnly(&new_handle, shm_, memory_); | 253 bool success = MakeMachSharedMemoryHandleReadOnly(&new_handle, shm_, memory_); |
| 270 if (success) | 254 if (success) |
| 271 new_handle.SetOwnershipPassesToIPC(true); | 255 new_handle.SetOwnershipPassesToIPC(true); |
| 272 return new_handle; | 256 return new_handle; |
| 273 } | 257 } |
| 274 | 258 |
| 275 } // namespace base | 259 } // namespace base |
| OLD | NEW |