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.
5 #include "base/memory/shared_memory_handle.h"
9 #include "base/posix/eintr_wrapper.h"
11 #if defined(OS_MACOSX) && !defined(OS_IOS)
14 static_assert(sizeof(SharedMemoryHandle::Type
) <=
15 sizeof(SharedMemoryHandle::TypeWireFormat
),
16 "Size of enum SharedMemoryHandle::Type exceeds size of type "
17 "transmitted over wire.");
19 SharedMemoryHandle::SharedMemoryHandle() : type_(POSIX
), file_descriptor_() {
22 SharedMemoryHandle::SharedMemoryHandle(
23 const base::FileDescriptor
& file_descriptor
)
24 : type_(POSIX
), file_descriptor_(file_descriptor
) {
27 SharedMemoryHandle::SharedMemoryHandle(int fd
, bool auto_close
)
28 : type_(POSIX
), file_descriptor_(fd
, auto_close
) {
31 SharedMemoryHandle::SharedMemoryHandle(const SharedMemoryHandle
& handle
)
32 : type_(handle
.type_
), file_descriptor_(handle
.file_descriptor_
) {
35 SharedMemoryHandle
& SharedMemoryHandle::operator=(
36 const SharedMemoryHandle
& handle
) {
41 file_descriptor_
= handle
.file_descriptor_
;
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())
50 return type_
== handle
.type_
&& file_descriptor_
== handle
.file_descriptor_
;
53 bool SharedMemoryHandle::operator!=(const SharedMemoryHandle
& handle
) const {
54 return !(*this == handle
);
57 SharedMemoryHandle::Type
SharedMemoryHandle::GetType() const {
61 bool SharedMemoryHandle::IsValid() const {
64 return file_descriptor_
.fd
>= 0;
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
;
76 const FileDescriptor
SharedMemoryHandle::GetFileDescriptor() const {
77 DCHECK_EQ(type_
, POSIX
);
78 return file_descriptor_
;
81 SharedMemoryHandle
SharedMemoryHandle::Duplicate() const {
82 DCHECK_EQ(type_
, POSIX
);
83 int duped_handle
= HANDLE_EINTR(dup(file_descriptor_
.fd
));
85 return SharedMemoryHandle();
86 return SharedMemoryHandle(duped_handle
, true);
90 #endif // defined(OS_MACOSX) && !defined(OS_IOS)