OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <aclapi.h> | 7 #include <aclapi.h> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 27 matching lines...) Expand all Loading... |
38 SharedMemory::SharedMemory(const std::wstring& name) | 38 SharedMemory::SharedMemory(const std::wstring& name) |
39 : name_(name), | 39 : name_(name), |
40 mapped_file_(NULL), | 40 mapped_file_(NULL), |
41 mapped_size_(0), | 41 mapped_size_(0), |
42 memory_(NULL), | 42 memory_(NULL), |
43 read_only_(false), | 43 read_only_(false), |
44 requested_size_(0) { | 44 requested_size_(0) { |
45 } | 45 } |
46 | 46 |
47 SharedMemory::SharedMemory(const SharedMemoryHandle& handle, bool read_only) | 47 SharedMemory::SharedMemory(const SharedMemoryHandle& handle, bool read_only) |
48 : mapped_file_(handle), | 48 : mapped_file_(handle.GetHandle()), |
49 mapped_size_(0), | 49 mapped_size_(0), |
50 memory_(NULL), | 50 memory_(NULL), |
51 read_only_(read_only), | 51 read_only_(read_only), |
52 requested_size_(0) { | 52 requested_size_(0) { |
| 53 DCHECK(!handle.IsValid() || handle.BelongsToCurrentProcess()); |
53 } | 54 } |
54 | 55 |
55 SharedMemory::SharedMemory(const SharedMemoryHandle& handle, | 56 SharedMemory::SharedMemory(const SharedMemoryHandle& handle, |
56 bool read_only, | 57 bool read_only, |
57 ProcessHandle process) | 58 ProcessHandle process) |
58 : mapped_file_(NULL), | 59 : mapped_file_(NULL), |
59 mapped_size_(0), | 60 mapped_size_(0), |
60 memory_(NULL), | 61 memory_(NULL), |
61 read_only_(read_only), | 62 read_only_(read_only), |
62 requested_size_(0) { | 63 requested_size_(0) { |
63 ::DuplicateHandle(process, handle, | 64 ::DuplicateHandle( |
64 GetCurrentProcess(), &mapped_file_, | 65 process, handle.GetHandle(), GetCurrentProcess(), &mapped_file_, |
65 read_only_ ? FILE_MAP_READ : FILE_MAP_READ | | 66 read_only_ ? FILE_MAP_READ : FILE_MAP_READ | FILE_MAP_WRITE, FALSE, 0); |
66 FILE_MAP_WRITE, | |
67 FALSE, 0); | |
68 } | 67 } |
69 | 68 |
70 SharedMemory::~SharedMemory() { | 69 SharedMemory::~SharedMemory() { |
71 Unmap(); | 70 Unmap(); |
72 Close(); | 71 Close(); |
73 } | 72 } |
74 | 73 |
75 // static | 74 // static |
76 bool SharedMemory::IsHandleValid(const SharedMemoryHandle& handle) { | 75 bool SharedMemory::IsHandleValid(const SharedMemoryHandle& handle) { |
77 return handle != NULL; | 76 return handle.IsValid(); |
78 } | 77 } |
79 | 78 |
80 // static | 79 // static |
81 SharedMemoryHandle SharedMemory::NULLHandle() { | 80 SharedMemoryHandle SharedMemory::NULLHandle() { |
82 return NULL; | 81 return SharedMemoryHandle(); |
83 } | 82 } |
84 | 83 |
85 // static | 84 // static |
86 void SharedMemory::CloseHandle(const SharedMemoryHandle& handle) { | 85 void SharedMemory::CloseHandle(const SharedMemoryHandle& handle) { |
87 DCHECK(handle != NULL); | 86 handle.Close(); |
88 ::CloseHandle(handle); | |
89 } | 87 } |
90 | 88 |
91 // static | 89 // static |
92 size_t SharedMemory::GetHandleLimit() { | 90 size_t SharedMemory::GetHandleLimit() { |
93 // Rounded down from value reported here: | 91 // Rounded down from value reported here: |
94 // http://blogs.technet.com/b/markrussinovich/archive/2009/09/29/3283844.aspx | 92 // http://blogs.technet.com/b/markrussinovich/archive/2009/09/29/3283844.aspx |
95 return static_cast<size_t>(1 << 23); | 93 return static_cast<size_t>(1 << 23); |
96 } | 94 } |
97 | 95 |
98 // static | 96 // static |
99 SharedMemoryHandle SharedMemory::DuplicateHandle( | 97 SharedMemoryHandle SharedMemory::DuplicateHandle( |
100 const SharedMemoryHandle& handle) { | 98 const SharedMemoryHandle& handle) { |
| 99 DCHECK(handle.BelongsToCurrentProcess()); |
| 100 HANDLE duped_handle; |
101 ProcessHandle process = GetCurrentProcess(); | 101 ProcessHandle process = GetCurrentProcess(); |
102 SharedMemoryHandle duped_handle; | 102 BOOL success = |
103 BOOL success = ::DuplicateHandle(process, handle, process, &duped_handle, 0, | 103 ::DuplicateHandle(process, handle.GetHandle(), process, &duped_handle, 0, |
104 FALSE, DUPLICATE_SAME_ACCESS); | 104 FALSE, DUPLICATE_SAME_ACCESS); |
105 if (success) | 105 if (success) |
106 return duped_handle; | 106 return SharedMemoryHandle(duped_handle, GetCurrentProcId()); |
107 return NULLHandle(); | 107 return SharedMemoryHandle(); |
108 } | 108 } |
109 | 109 |
110 bool SharedMemory::CreateAndMapAnonymous(size_t size) { | 110 bool SharedMemory::CreateAndMapAnonymous(size_t size) { |
111 return CreateAnonymous(size) && Map(size); | 111 return CreateAnonymous(size) && Map(size); |
112 } | 112 } |
113 | 113 |
114 bool SharedMemory::Create(const SharedMemoryCreateOptions& options) { | 114 bool SharedMemory::Create(const SharedMemoryCreateOptions& options) { |
115 // TODO(bsy,sehr): crbug.com/210609 NaCl forces us to round up 64k here, | 115 // TODO(bsy,sehr): crbug.com/210609 NaCl forces us to round up 64k here, |
116 // wasting 32k per mapping on average. | 116 // wasting 32k per mapping on average. |
117 static const size_t kSectionMask = 65536 - 1; | 117 static const size_t kSectionMask = 65536 - 1; |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
223 | 223 |
224 UnmapViewOfFile(memory_); | 224 UnmapViewOfFile(memory_); |
225 memory_ = NULL; | 225 memory_ = NULL; |
226 return true; | 226 return true; |
227 } | 227 } |
228 | 228 |
229 bool SharedMemory::ShareToProcessCommon(ProcessHandle process, | 229 bool SharedMemory::ShareToProcessCommon(ProcessHandle process, |
230 SharedMemoryHandle* new_handle, | 230 SharedMemoryHandle* new_handle, |
231 bool close_self, | 231 bool close_self, |
232 ShareMode share_mode) { | 232 ShareMode share_mode) { |
233 *new_handle = 0; | 233 *new_handle = SharedMemoryHandle(); |
234 DWORD access = FILE_MAP_READ; | 234 DWORD access = FILE_MAP_READ; |
235 DWORD options = 0; | 235 DWORD options = 0; |
236 HANDLE mapped_file = mapped_file_; | 236 HANDLE mapped_file = mapped_file_; |
237 HANDLE result; | 237 HANDLE result; |
238 if (share_mode == SHARE_CURRENT_MODE && !read_only_) | 238 if (share_mode == SHARE_CURRENT_MODE && !read_only_) |
239 access |= FILE_MAP_WRITE; | 239 access |= FILE_MAP_WRITE; |
240 if (close_self) { | 240 if (close_self) { |
241 // DUPLICATE_CLOSE_SOURCE causes DuplicateHandle to close mapped_file. | 241 // DUPLICATE_CLOSE_SOURCE causes DuplicateHandle to close mapped_file. |
242 options = DUPLICATE_CLOSE_SOURCE; | 242 options = DUPLICATE_CLOSE_SOURCE; |
243 mapped_file_ = NULL; | 243 mapped_file_ = NULL; |
244 Unmap(); | 244 Unmap(); |
245 } | 245 } |
246 | 246 |
247 if (process == GetCurrentProcess() && close_self) { | 247 if (process == GetCurrentProcess() && close_self) { |
248 *new_handle = mapped_file; | 248 *new_handle = SharedMemoryHandle(mapped_file, base::GetCurrentProcId()); |
249 return true; | 249 return true; |
250 } | 250 } |
251 | 251 |
252 if (!::DuplicateHandle(GetCurrentProcess(), mapped_file, process, &result, | 252 if (!::DuplicateHandle(GetCurrentProcess(), mapped_file, process, &result, |
253 access, FALSE, options)) { | 253 access, FALSE, options)) { |
254 return false; | 254 return false; |
255 } | 255 } |
256 *new_handle = result; | 256 *new_handle = SharedMemoryHandle(result, base::GetProcId(process)); |
257 return true; | 257 return true; |
258 } | 258 } |
259 | 259 |
260 | 260 |
261 void SharedMemory::Close() { | 261 void SharedMemory::Close() { |
262 if (mapped_file_ != NULL) { | 262 if (mapped_file_ != NULL) { |
263 CloseHandle(mapped_file_); | 263 ::CloseHandle(mapped_file_); |
264 mapped_file_ = NULL; | 264 mapped_file_ = NULL; |
265 } | 265 } |
266 } | 266 } |
267 | 267 |
268 SharedMemoryHandle SharedMemory::handle() const { | 268 SharedMemoryHandle SharedMemory::handle() const { |
269 return mapped_file_; | 269 return SharedMemoryHandle(mapped_file_, base::GetCurrentProcId()); |
270 } | 270 } |
271 | 271 |
272 } // namespace base | 272 } // namespace base |
OLD | NEW |