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> |
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/mac/mac_util.h" | 17 #include "base/mac/mac_util.h" |
18 #include "base/mac/scoped_mach_vm.h" | 18 #include "base/mac/scoped_mach_vm.h" |
19 #include "base/memory/shared_memory_helper.h" | 19 #include "base/memory/shared_memory_helper.h" |
| 20 #include "base/memory/shared_memory_tracker.h" |
20 #include "base/metrics/field_trial.h" | 21 #include "base/metrics/field_trial.h" |
21 #include "base/metrics/histogram_macros.h" | 22 #include "base/metrics/histogram_macros.h" |
22 #include "base/posix/eintr_wrapper.h" | 23 #include "base/posix/eintr_wrapper.h" |
23 #include "base/posix/safe_strerror.h" | 24 #include "base/posix/safe_strerror.h" |
24 #include "base/process/process_metrics.h" | 25 #include "base/process/process_metrics.h" |
25 #include "base/scoped_generic.h" | 26 #include "base/scoped_generic.h" |
26 #include "base/strings/utf_string_conversions.h" | 27 #include "base/strings/utf_string_conversions.h" |
27 #include "base/threading/thread_restrictions.h" | 28 #include "base/threading/thread_restrictions.h" |
28 #include "base/unguessable_token.h" | 29 #include "base/unguessable_token.h" |
29 #include "build/build_config.h" | 30 #include "build/build_config.h" |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
198 last_error_ = SharedMemoryError::BAD_PARAMS; | 199 last_error_ = SharedMemoryError::BAD_PARAMS; |
199 return false; | 200 return false; |
200 } | 201 } |
201 | 202 |
202 bool success = shm_.MapAt(offset, bytes, &memory_, read_only_); | 203 bool success = shm_.MapAt(offset, bytes, &memory_, read_only_); |
203 if (success) { | 204 if (success) { |
204 mapped_size_ = bytes; | 205 mapped_size_ = bytes; |
205 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) & | 206 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) & |
206 (SharedMemory::MAP_MINIMUM_ALIGNMENT - 1)); | 207 (SharedMemory::MAP_MINIMUM_ALIGNMENT - 1)); |
207 mapped_memory_mechanism_ = shm_.type_; | 208 mapped_memory_mechanism_ = shm_.type_; |
| 209 SharedMemoryTracker::GetInstance()->IncrementMemoryUsage(*this); |
208 } else { | 210 } else { |
209 last_error_ = SharedMemoryError::MMAP_FAILED; | 211 last_error_ = SharedMemoryError::MMAP_FAILED; |
210 memory_ = NULL; | 212 memory_ = NULL; |
211 } | 213 } |
212 | 214 |
213 return success; | 215 return success; |
214 } | 216 } |
215 | 217 |
216 bool SharedMemory::Unmap() { | 218 bool SharedMemory::Unmap() { |
217 if (memory_ == NULL) | 219 if (memory_ == NULL) |
218 return false; | 220 return false; |
219 | 221 |
220 switch (mapped_memory_mechanism_) { | 222 switch (mapped_memory_mechanism_) { |
221 case SharedMemoryHandle::POSIX: | 223 case SharedMemoryHandle::POSIX: |
222 munmap(memory_, mapped_size_); | 224 munmap(memory_, mapped_size_); |
223 break; | 225 break; |
224 case SharedMemoryHandle::MACH: | 226 case SharedMemoryHandle::MACH: |
225 mach_vm_deallocate(mach_task_self(), | 227 mach_vm_deallocate(mach_task_self(), |
226 reinterpret_cast<mach_vm_address_t>(memory_), | 228 reinterpret_cast<mach_vm_address_t>(memory_), |
227 mapped_size_); | 229 mapped_size_); |
228 break; | 230 break; |
229 } | 231 } |
230 | 232 SharedMemoryTracker::GetInstance()->DecrementMemoryUsage(*this); |
231 memory_ = NULL; | 233 memory_ = NULL; |
232 mapped_size_ = 0; | 234 mapped_size_ = 0; |
233 return true; | 235 return true; |
234 } | 236 } |
235 | 237 |
236 SharedMemoryHandle SharedMemory::handle() const { | 238 SharedMemoryHandle SharedMemory::handle() const { |
237 return shm_; | 239 return shm_; |
238 } | 240 } |
239 | 241 |
240 SharedMemoryHandle SharedMemory::TakeHandle() { | 242 SharedMemoryHandle SharedMemory::TakeHandle() { |
(...skipping 23 matching lines...) Expand all Loading... |
264 | 266 |
265 DCHECK(shm_.IsValid()); | 267 DCHECK(shm_.IsValid()); |
266 base::SharedMemoryHandle new_handle; | 268 base::SharedMemoryHandle new_handle; |
267 bool success = MakeMachSharedMemoryHandleReadOnly(&new_handle, shm_, memory_); | 269 bool success = MakeMachSharedMemoryHandleReadOnly(&new_handle, shm_, memory_); |
268 if (success) | 270 if (success) |
269 new_handle.SetOwnershipPassesToIPC(true); | 271 new_handle.SetOwnershipPassesToIPC(true); |
270 return new_handle; | 272 return new_handle; |
271 } | 273 } |
272 | 274 |
273 } // namespace base | 275 } // namespace base |
OLD | NEW |