Add ICU message format support
[chromium-blink-merge.git] / base / memory / shared_memory_handle_mac.cc
blobeb4f4654a938f2025b2008081e6bea50117c8b10
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"
7 #include <unistd.h>
9 #include "base/posix/eintr_wrapper.h"
11 #if defined(OS_MACOSX) && !defined(OS_IOS)
12 namespace base {
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) {
37 if (this == &handle)
38 return *this;
40 type_ = handle.type_;
41 file_descriptor_ = handle.file_descriptor_;
42 return *this;
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;
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 {
58 return type_;
61 bool SharedMemoryHandle::IsValid() const {
62 switch (type_) {
63 case POSIX:
64 return file_descriptor_.fd >= 0;
65 case MACH:
66 return false;
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));
84 if (duped_handle < 0)
85 return SharedMemoryHandle();
86 return SharedMemoryHandle(duped_handle, true);
89 } // namespace base
90 #endif // defined(OS_MACOSX) && !defined(OS_IOS)