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 "build/build_config.h" | 29 #include "build/build_config.h" |
29 | 30 |
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
205 return false; | 206 return false; |
206 if (memory_) | 207 if (memory_) |
207 return false; | 208 return false; |
208 | 209 |
209 bool success = shm_.MapAt(offset, bytes, &memory_, read_only_); | 210 bool success = shm_.MapAt(offset, bytes, &memory_, read_only_); |
210 if (success) { | 211 if (success) { |
211 mapped_size_ = bytes; | 212 mapped_size_ = bytes; |
212 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) & | 213 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) & |
213 (SharedMemory::MAP_MINIMUM_ALIGNMENT - 1)); | 214 (SharedMemory::MAP_MINIMUM_ALIGNMENT - 1)); |
214 mapped_memory_mechanism_ = shm_.type_; | 215 mapped_memory_mechanism_ = shm_.type_; |
216 SharedMemoryTracker::GetInstance()->IncrementMemoryUsage(*this); | |
215 } else { | 217 } else { |
216 memory_ = NULL; | 218 memory_ = NULL; |
217 } | 219 } |
218 | 220 |
219 return success; | 221 return success; |
220 } | 222 } |
221 | 223 |
222 bool SharedMemory::Unmap() { | 224 bool SharedMemory::Unmap() { |
223 if (memory_ == NULL) | 225 if (memory_ == NULL) |
224 return false; | 226 return false; |
225 | 227 |
226 switch (mapped_memory_mechanism_) { | 228 switch (mapped_memory_mechanism_) { |
227 case SharedMemoryHandle::POSIX: | 229 case SharedMemoryHandle::POSIX: |
228 munmap(memory_, mapped_size_); | 230 munmap(memory_, mapped_size_); |
229 break; | 231 break; |
230 case SharedMemoryHandle::MACH: | 232 case SharedMemoryHandle::MACH: |
231 mach_vm_deallocate(mach_task_self(), | 233 mach_vm_deallocate(mach_task_self(), |
232 reinterpret_cast<mach_vm_address_t>(memory_), | 234 reinterpret_cast<mach_vm_address_t>(memory_), |
233 mapped_size_); | 235 mapped_size_); |
234 break; | 236 break; |
235 } | 237 } |
236 | 238 SharedMemoryTracker::GetInstance()->DecrementMemoryUsage(*this); |
237 memory_ = NULL; | 239 memory_ = NULL; |
238 mapped_size_ = 0; | 240 mapped_size_ = 0; |
239 return true; | 241 return true; |
240 } | 242 } |
241 | 243 |
242 SharedMemoryHandle SharedMemory::handle() const { | 244 SharedMemoryHandle SharedMemory::handle() const { |
243 switch (shm_.type_) { | 245 switch (shm_.type_) { |
244 case SharedMemoryHandle::POSIX: | 246 case SharedMemoryHandle::POSIX: |
245 return SharedMemoryHandle( | 247 return SharedMemoryHandle( |
246 FileDescriptor(shm_.file_descriptor_.fd, false)); | 248 FileDescriptor(shm_.file_descriptor_.fd, false)); |
247 case SharedMemoryHandle::MACH: | 249 case SharedMemoryHandle::MACH: |
248 return shm_; | 250 return shm_; |
249 } | 251 } |
250 } | 252 } |
251 | 253 |
252 SharedMemoryHandle SharedMemory::TakeHandle() { | 254 SharedMemoryHandle SharedMemory::TakeHandle() { |
253 SharedMemoryHandle dup = DuplicateHandle(handle()); | 255 SharedMemoryHandle dup = DuplicateHandle(handle()); |
254 Close(); | 256 Close(); |
255 return dup; | 257 return dup; |
256 } | 258 } |
257 | 259 |
258 void SharedMemory::Close() { | 260 void SharedMemory::Close() { |
261 SharedMemoryTracker::GetInstance()->DecrementMemoryUsage(*this); | |
Primiano Tucci (use gerrit)
2017/01/20 16:34:52
I think this might not be the right point.
The dec
hajimehoshi
2017/01/23 11:59:09
I was wondering if the file descriptor's is still
| |
259 shm_.Close(); | 262 shm_.Close(); |
260 shm_ = SharedMemoryHandle(); | 263 shm_ = SharedMemoryHandle(); |
261 if (shm_.type_ == SharedMemoryHandle::POSIX) { | 264 if (shm_.type_ == SharedMemoryHandle::POSIX) { |
262 if (readonly_mapped_file_ > 0) { | 265 if (readonly_mapped_file_ > 0) { |
263 if (IGNORE_EINTR(close(readonly_mapped_file_)) < 0) | 266 if (IGNORE_EINTR(close(readonly_mapped_file_)) < 0) |
264 PLOG(ERROR) << "close"; | 267 PLOG(ERROR) << "close"; |
265 readonly_mapped_file_ = -1; | 268 readonly_mapped_file_ = -1; |
266 } | 269 } |
267 } | 270 } |
268 } | 271 } |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
319 ShareMode share_mode) { | 322 ShareMode share_mode) { |
320 bool success = Share(new_handle, share_mode); | 323 bool success = Share(new_handle, share_mode); |
321 if (close_self) { | 324 if (close_self) { |
322 Unmap(); | 325 Unmap(); |
323 Close(); | 326 Close(); |
324 } | 327 } |
325 return success; | 328 return success; |
326 } | 329 } |
327 | 330 |
328 } // namespace base | 331 } // namespace base |
OLD | NEW |