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 SharedMemoryTracker::GetInstance()->IncrementMemoryUsage(*this); |
288 } else { | 290 } else { |
289 memory_ = NULL; | 291 memory_ = NULL; |
290 } | 292 } |
291 | 293 |
292 return mmap_succeeded; | 294 return mmap_succeeded; |
293 } | 295 } |
294 | 296 |
295 bool SharedMemory::Unmap() { | 297 bool SharedMemory::Unmap() { |
296 if (memory_ == NULL) | 298 if (memory_ == NULL) |
297 return false; | 299 return false; |
298 | 300 |
299 munmap(memory_, mapped_size_); | 301 munmap(memory_, mapped_size_); |
| 302 SharedMemoryTracker::GetInstance()->DecrementMemoryUsage(*this); |
300 memory_ = NULL; | 303 memory_ = NULL; |
301 mapped_size_ = 0; | 304 mapped_size_ = 0; |
302 return true; | 305 return true; |
303 } | 306 } |
304 | 307 |
305 SharedMemoryHandle SharedMemory::handle() const { | 308 SharedMemoryHandle SharedMemory::handle() const { |
306 return FileDescriptor(mapped_file_, false); | 309 return FileDescriptor(mapped_file_, false); |
307 } | 310 } |
308 | 311 |
309 SharedMemoryHandle SharedMemory::TakeHandle() { | 312 SharedMemoryHandle SharedMemory::TakeHandle() { |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
383 new_handle->auto_close = true; | 386 new_handle->auto_close = true; |
384 | 387 |
385 if (close_self) { | 388 if (close_self) { |
386 Unmap(); | 389 Unmap(); |
387 Close(); | 390 Close(); |
388 } | 391 } |
389 | 392 |
390 return true; | 393 return true; |
391 } | 394 } |
392 | 395 |
| 396 #if !defined(OS_MACOSX) || defined(OS_IOS) |
| 397 bool SharedMemory::GetUniqueId(SharedMemory::Id* id) const { |
| 398 base::ThreadRestrictions::AssertIOAllowed(); |
| 399 struct stat file_stat; |
| 400 if (fstat(static_cast<int>(handle().fd), &file_stat) != 0) |
| 401 return false; |
| 402 id->first = file_stat.st_dev; |
| 403 id->second = file_stat.st_ino; |
| 404 return true; |
| 405 } |
| 406 #endif // !defined(OS_MACOSX) || defined(OS_IOS) |
| 407 |
393 } // namespace base | 408 } // namespace base |
OLD | NEW |