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 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
67 FALSE, 0); | 67 FALSE, 0); |
68 } | 68 } |
69 | 69 |
70 SharedMemory::~SharedMemory() { | 70 SharedMemory::~SharedMemory() { |
71 Unmap(); | 71 Unmap(); |
72 Close(); | 72 Close(); |
73 } | 73 } |
74 | 74 |
75 // static | 75 // static |
76 bool SharedMemory::IsHandleValid(const SharedMemoryHandle& handle) { | 76 bool SharedMemory::IsHandleValid(const SharedMemoryHandle& handle) { |
77 return handle != NULL; | 77 return handle != NULL && handle != INVALID_HANDLE_VALUE; |
rvargas (doing something else)
2015/09/18 22:36:25
<rant>
I know I'm grumpy, but this pattern doesn't
| |
78 } | 78 } |
79 | 79 |
80 // static | 80 // static |
81 SharedMemoryHandle SharedMemory::NULLHandle() { | 81 SharedMemoryHandle SharedMemory::NULLHandle() { |
82 return NULL; | 82 return NULL; |
83 } | 83 } |
84 | 84 |
85 // static | 85 // static |
86 void SharedMemory::CloseHandle(const SharedMemoryHandle& handle) { | 86 void SharedMemory::CloseHandle(const SharedMemoryHandle& handle) { |
87 DCHECK(handle != NULL); | 87 DCHECK(IsHandleValid(handle)); |
rvargas (doing something else)
2015/09/18 22:36:25
ScopedHandle closer(handle);
<rant> receiving con
| |
88 ::CloseHandle(handle); | 88 ::CloseHandle(handle); |
89 } | 89 } |
90 | 90 |
91 // static | 91 // static |
92 size_t SharedMemory::GetHandleLimit() { | 92 size_t SharedMemory::GetHandleLimit() { |
93 // Rounded down from value reported here: | 93 // Rounded down from value reported here: |
94 // http://blogs.technet.com/b/markrussinovich/archive/2009/09/29/3283844.aspx | 94 // http://blogs.technet.com/b/markrussinovich/archive/2009/09/29/3283844.aspx |
95 return static_cast<size_t>(1 << 23); | 95 return static_cast<size_t>(1 << 23); |
96 } | 96 } |
97 | 97 |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
171 | 171 |
172 return true; | 172 return true; |
173 } | 173 } |
174 | 174 |
175 bool SharedMemory::Delete(const std::string& name) { | 175 bool SharedMemory::Delete(const std::string& name) { |
176 // intentionally empty -- there is nothing for us to do on Windows. | 176 // intentionally empty -- there is nothing for us to do on Windows. |
177 return true; | 177 return true; |
178 } | 178 } |
179 | 179 |
180 bool SharedMemory::Open(const std::string& name, bool read_only) { | 180 bool SharedMemory::Open(const std::string& name, bool read_only) { |
181 DCHECK(!mapped_file_); | 181 DCHECK(!IsHandleValid(mapped_file_)); |
rvargas (doing something else)
2015/09/18 22:36:25
DCHECK(mapped_file_.Isvalid());
(+ making the mem
| |
182 | 182 |
183 name_ = ASCIIToUTF16(name); | 183 name_ = ASCIIToUTF16(name); |
184 read_only_ = read_only; | 184 read_only_ = read_only; |
185 mapped_file_ = OpenFileMapping( | 185 mapped_file_ = OpenFileMapping( |
186 read_only_ ? FILE_MAP_READ : FILE_MAP_READ | FILE_MAP_WRITE, | 186 read_only_ ? FILE_MAP_READ : FILE_MAP_READ | FILE_MAP_WRITE, |
187 false, name_.empty() ? NULL : name_.c_str()); | 187 false, name_.empty() ? NULL : name_.c_str()); |
188 if (mapped_file_ != NULL) { | 188 if (IsHandleValid(mapped_file_)) { |
189 // Note: size_ is not set in this case. | 189 // Note: size_ is not set in this case. |
190 return true; | 190 return true; |
191 } | 191 } |
192 return false; | 192 return false; |
193 } | 193 } |
194 | 194 |
195 bool SharedMemory::MapAt(off_t offset, size_t bytes) { | 195 bool SharedMemory::MapAt(off_t offset, size_t bytes) { |
196 if (mapped_file_ == NULL) | 196 if (!IsHandleValid(mapped_file_)) |
197 return false; | 197 return false; |
198 | 198 |
199 if (bytes > static_cast<size_t>(std::numeric_limits<int>::max())) | 199 if (bytes > static_cast<size_t>(std::numeric_limits<int>::max())) |
200 return false; | 200 return false; |
201 | 201 |
202 if (memory_) | 202 if (memory_) |
203 return false; | 203 return false; |
204 | 204 |
205 memory_ = MapViewOfFile(mapped_file_, | 205 memory_ = MapViewOfFile(mapped_file_, |
206 read_only_ ? FILE_MAP_READ : FILE_MAP_READ | | 206 read_only_ ? FILE_MAP_READ : FILE_MAP_READ | |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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 = result; |
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 (IsHandleValid(mapped_file_)) { |
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 mapped_file_; |
270 } | 270 } |
271 | 271 |
272 } // namespace base | 272 } // namespace base |
OLD | NEW |