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_file_(-1), | 22 : mapped_file_(-1), |
22 mapped_size_(0), | 23 mapped_size_(0), |
23 memory_(NULL), | 24 memory_(NULL), |
24 read_only_(false), | 25 read_only_(false), |
25 requested_size_(0) { | 26 requested_size_(0) { |
26 } | 27 } |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 if (memory_) | 93 if (memory_) |
93 return false; | 94 return false; |
94 | 95 |
95 memory_ = mmap(NULL, bytes, PROT_READ | (read_only_ ? 0 : PROT_WRITE), | 96 memory_ = mmap(NULL, bytes, PROT_READ | (read_only_ ? 0 : PROT_WRITE), |
96 MAP_SHARED, mapped_file_, offset); | 97 MAP_SHARED, mapped_file_, offset); |
97 | 98 |
98 bool mmap_succeeded = memory_ != MAP_FAILED && memory_ != NULL; | 99 bool mmap_succeeded = memory_ != MAP_FAILED && memory_ != NULL; |
99 if (mmap_succeeded) { | 100 if (mmap_succeeded) { |
100 mapped_size_ = bytes; | 101 mapped_size_ = bytes; |
101 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) & | 102 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) & |
102 (SharedMemory::MAP_MINIMUM_ALIGNMENT - 1)); | 103 (SharedMemory::MAP_MINIMUM_ALIGNMENT - 1)); |
| 104 SharedMemoryTracker::GetInstance()->IncrementMemoryUsage(*this); |
103 } else { | 105 } else { |
104 memory_ = NULL; | 106 memory_ = NULL; |
105 } | 107 } |
106 | 108 |
107 return mmap_succeeded; | 109 return mmap_succeeded; |
108 } | 110 } |
109 | 111 |
110 bool SharedMemory::Unmap() { | 112 bool SharedMemory::Unmap() { |
111 if (memory_ == NULL) | 113 if (memory_ == NULL) |
112 return false; | 114 return false; |
113 | 115 |
114 if (munmap(memory_, mapped_size_) < 0) | 116 if (munmap(memory_, mapped_size_) < 0) |
115 DPLOG(ERROR) << "munmap"; | 117 DPLOG(ERROR) << "munmap"; |
| 118 SharedMemoryTracker::GetInstance()->DecrementMemoryUsage(*this); |
116 memory_ = NULL; | 119 memory_ = NULL; |
117 mapped_size_ = 0; | 120 mapped_size_ = 0; |
118 return true; | 121 return true; |
119 } | 122 } |
120 | 123 |
121 SharedMemoryHandle SharedMemory::handle() const { | 124 SharedMemoryHandle SharedMemory::handle() const { |
122 return FileDescriptor(mapped_file_, false); | 125 return FileDescriptor(mapped_file_, false); |
123 } | 126 } |
124 | 127 |
125 SharedMemoryHandle SharedMemory::TakeHandle() { | 128 SharedMemoryHandle SharedMemory::TakeHandle() { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 new_handle->auto_close = true; | 160 new_handle->auto_close = true; |
158 | 161 |
159 if (close_self) { | 162 if (close_self) { |
160 Unmap(); | 163 Unmap(); |
161 Close(); | 164 Close(); |
162 } | 165 } |
163 return true; | 166 return true; |
164 } | 167 } |
165 | 168 |
166 } // namespace base | 169 } // namespace base |
OLD | NEW |