OLD | NEW |
| (Empty) |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/memory/shared_memory_handle.h" | |
6 | |
7 #include <unistd.h> | |
8 | |
9 #include "base/posix/eintr_wrapper.h" | |
10 | |
11 #if defined(OS_MACOSX) && !defined(OS_IOS) | |
12 namespace base { | |
13 | |
14 static_assert(sizeof(SharedMemoryHandle::Type) <= | |
15 sizeof(SharedMemoryHandle::TypeWireFormat), | |
16 "Size of enum SharedMemoryHandle::Type exceeds size of type " | |
17 "transmitted over wire."); | |
18 | |
19 SharedMemoryHandle::SharedMemoryHandle() : type_(POSIX), file_descriptor_() { | |
20 } | |
21 | |
22 SharedMemoryHandle::SharedMemoryHandle( | |
23 const base::FileDescriptor& file_descriptor) | |
24 : type_(POSIX), file_descriptor_(file_descriptor) { | |
25 } | |
26 | |
27 SharedMemoryHandle::SharedMemoryHandle(int fd, bool auto_close) | |
28 : type_(POSIX), file_descriptor_(fd, auto_close) { | |
29 } | |
30 | |
31 SharedMemoryHandle::SharedMemoryHandle(const SharedMemoryHandle& handle) | |
32 : type_(handle.type_), file_descriptor_(handle.file_descriptor_) { | |
33 } | |
34 | |
35 SharedMemoryHandle& SharedMemoryHandle::operator=( | |
36 const SharedMemoryHandle& handle) { | |
37 if (this == &handle) | |
38 return *this; | |
39 | |
40 type_ = handle.type_; | |
41 file_descriptor_ = handle.file_descriptor_; | |
42 return *this; | |
43 } | |
44 | |
45 bool SharedMemoryHandle::operator==(const SharedMemoryHandle& handle) const { | |
46 // Invalid handles are always equal, even if they have different types. | |
47 if (!IsValid() && !handle.IsValid()) | |
48 return true; | |
49 | |
50 return type_ == handle.type_ && file_descriptor_ == handle.file_descriptor_; | |
51 } | |
52 | |
53 bool SharedMemoryHandle::operator!=(const SharedMemoryHandle& handle) const { | |
54 return !(*this == handle); | |
55 } | |
56 | |
57 SharedMemoryHandle::Type SharedMemoryHandle::GetType() const { | |
58 return type_; | |
59 } | |
60 | |
61 bool SharedMemoryHandle::IsValid() const { | |
62 switch (type_) { | |
63 case POSIX: | |
64 return file_descriptor_.fd >= 0; | |
65 case MACH: | |
66 return false; | |
67 } | |
68 } | |
69 | |
70 void SharedMemoryHandle::SetFileHandle(int fd, bool auto_close) { | |
71 DCHECK_EQ(type_, POSIX); | |
72 file_descriptor_.fd = fd; | |
73 file_descriptor_.auto_close = auto_close; | |
74 } | |
75 | |
76 const FileDescriptor SharedMemoryHandle::GetFileDescriptor() const { | |
77 DCHECK_EQ(type_, POSIX); | |
78 return file_descriptor_; | |
79 } | |
80 | |
81 SharedMemoryHandle SharedMemoryHandle::Duplicate() const { | |
82 DCHECK_EQ(type_, POSIX); | |
83 int duped_handle = HANDLE_EINTR(dup(file_descriptor_.fd)); | |
84 if (duped_handle < 0) | |
85 return SharedMemoryHandle(); | |
86 return SharedMemoryHandle(duped_handle, true); | |
87 } | |
88 | |
89 } // namespace base | |
90 #endif // defined(OS_MACOSX) && !defined(OS_IOS) | |
OLD | NEW |