OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/shared_memory.h" | 5 #include "base/shared_memory.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <fcntl.h> | 8 #include <fcntl.h> |
9 #include <sys/mman.h> | 9 #include <sys/mman.h> |
10 | 10 |
(...skipping 11 matching lines...) Expand all Loading... |
22 } | 22 } |
23 | 23 |
24 SharedMemory::SharedMemory() | 24 SharedMemory::SharedMemory() |
25 : mapped_file_(-1), | 25 : mapped_file_(-1), |
26 memory_(NULL), | 26 memory_(NULL), |
27 read_only_(false), | 27 read_only_(false), |
28 max_size_(0) { | 28 max_size_(0) { |
29 } | 29 } |
30 | 30 |
31 SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only) | 31 SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only) |
32 : mapped_file_(handle), | 32 : mapped_file_(handle.fd), |
33 memory_(NULL), | 33 memory_(NULL), |
34 read_only_(read_only), | 34 read_only_(read_only), |
35 max_size_(0) { | 35 max_size_(0) { |
36 } | 36 } |
37 | 37 |
38 SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only, | 38 SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only, |
39 ProcessHandle process) | 39 ProcessHandle process) |
40 : mapped_file_(handle), | 40 : mapped_file_(handle.fd), |
41 memory_(NULL), | 41 memory_(NULL), |
42 read_only_(read_only), | 42 read_only_(read_only), |
43 max_size_(0) { | 43 max_size_(0) { |
44 // We don't handle this case yet (note the ignored parameter); let's die if | 44 // We don't handle this case yet (note the ignored parameter); let's die if |
45 // someone comes calling. | 45 // someone comes calling. |
46 NOTREACHED(); | 46 NOTREACHED(); |
47 } | 47 } |
48 | 48 |
49 SharedMemory::~SharedMemory() { | 49 SharedMemory::~SharedMemory() { |
50 Close(); | 50 Close(); |
51 } | 51 } |
52 | 52 |
| 53 // static |
| 54 bool SharedMemory::IsHandleValid(const SharedMemoryHandle& handle) { |
| 55 return handle.fd >= 0; |
| 56 } |
| 57 |
53 bool SharedMemory::Create(const std::wstring &name, bool read_only, | 58 bool SharedMemory::Create(const std::wstring &name, bool read_only, |
54 bool open_existing, size_t size) { | 59 bool open_existing, size_t size) { |
55 read_only_ = read_only; | 60 read_only_ = read_only; |
56 | 61 |
57 int posix_flags = 0; | 62 int posix_flags = 0; |
58 posix_flags |= read_only ? O_RDONLY : O_RDWR; | 63 posix_flags |= read_only ? O_RDONLY : O_RDWR; |
59 if (!open_existing || mapped_file_ <= 0) | 64 if (!open_existing || mapped_file_ <= 0) |
60 posix_flags |= O_CREAT; | 65 posix_flags |= O_CREAT; |
61 | 66 |
62 if (!CreateOrOpen(name, posix_flags, size)) | 67 if (!CreateOrOpen(name, posix_flags, size)) |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 | 230 |
226 munmap(memory_, max_size_); | 231 munmap(memory_, max_size_); |
227 memory_ = NULL; | 232 memory_ = NULL; |
228 max_size_ = 0; | 233 max_size_ = 0; |
229 return true; | 234 return true; |
230 } | 235 } |
231 | 236 |
232 bool SharedMemory::ShareToProcessCommon(ProcessHandle process, | 237 bool SharedMemory::ShareToProcessCommon(ProcessHandle process, |
233 SharedMemoryHandle *new_handle, | 238 SharedMemoryHandle *new_handle, |
234 bool close_self) { | 239 bool close_self) { |
235 *new_handle = 0; | 240 const int new_fd = dup(mapped_file_); |
236 // TODO(awalker): figure out if we need this, and do the appropriate | 241 DCHECK(new_fd >= -1); |
237 // VM magic if so. | 242 new_handle->fd = new_fd; |
238 return false; | 243 new_handle->auto_close = true; |
| 244 |
| 245 if (close_self) |
| 246 Close(); |
| 247 |
| 248 return true; |
239 } | 249 } |
240 | 250 |
241 | 251 |
242 void SharedMemory::Close() { | 252 void SharedMemory::Close() { |
243 | 253 |
244 Unmap(); | 254 Unmap(); |
245 | 255 |
246 std::string posix_name(WideToUTF8(name_)); | 256 std::string posix_name(WideToUTF8(name_)); |
247 if (mapped_file_ > 0) { | 257 if (mapped_file_ > 0) { |
248 close(mapped_file_); | 258 close(mapped_file_); |
(...skipping 21 matching lines...) Expand all Loading... |
270 } | 280 } |
271 | 281 |
272 void SharedMemory::Lock() { | 282 void SharedMemory::Lock() { |
273 LockOrUnlockCommon(F_LOCK); | 283 LockOrUnlockCommon(F_LOCK); |
274 } | 284 } |
275 | 285 |
276 void SharedMemory::Unlock() { | 286 void SharedMemory::Unlock() { |
277 LockOrUnlockCommon(F_ULOCK); | 287 LockOrUnlockCommon(F_ULOCK); |
278 } | 288 } |
279 | 289 |
| 290 SharedMemoryHandle SharedMemory::handle() const { |
| 291 return FileDescriptor(mapped_file_, false); |
| 292 } |
| 293 |
280 } // namespace base | 294 } // namespace base |
OLD | NEW |