Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1191)

Unified Diff: base/memory/shared_memory_handle_mac.cc

Issue 1197853004: Revert of Revert of Make SharedMemoryHandle a class on Mac. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@shared_memory_make_class3_base
Patch Set: Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/memory/shared_memory_handle.h ('k') | base/memory/shared_memory_mac.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/memory/shared_memory_handle_mac.cc
diff --git a/base/memory/shared_memory_handle_mac.cc b/base/memory/shared_memory_handle_mac.cc
new file mode 100644
index 0000000000000000000000000000000000000000..eb4f4654a938f2025b2008081e6bea50117c8b10
--- /dev/null
+++ b/base/memory/shared_memory_handle_mac.cc
@@ -0,0 +1,90 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/memory/shared_memory_handle.h"
+
+#include <unistd.h>
+
+#include "base/posix/eintr_wrapper.h"
+
+#if defined(OS_MACOSX) && !defined(OS_IOS)
+namespace base {
+
+static_assert(sizeof(SharedMemoryHandle::Type) <=
+ sizeof(SharedMemoryHandle::TypeWireFormat),
+ "Size of enum SharedMemoryHandle::Type exceeds size of type "
+ "transmitted over wire.");
+
+SharedMemoryHandle::SharedMemoryHandle() : type_(POSIX), file_descriptor_() {
+}
+
+SharedMemoryHandle::SharedMemoryHandle(
+ const base::FileDescriptor& file_descriptor)
+ : type_(POSIX), file_descriptor_(file_descriptor) {
+}
+
+SharedMemoryHandle::SharedMemoryHandle(int fd, bool auto_close)
+ : type_(POSIX), file_descriptor_(fd, auto_close) {
+}
+
+SharedMemoryHandle::SharedMemoryHandle(const SharedMemoryHandle& handle)
+ : type_(handle.type_), file_descriptor_(handle.file_descriptor_) {
+}
+
+SharedMemoryHandle& SharedMemoryHandle::operator=(
+ const SharedMemoryHandle& handle) {
+ if (this == &handle)
+ return *this;
+
+ type_ = handle.type_;
+ file_descriptor_ = handle.file_descriptor_;
+ return *this;
+}
+
+bool SharedMemoryHandle::operator==(const SharedMemoryHandle& handle) const {
+ // Invalid handles are always equal, even if they have different types.
+ if (!IsValid() && !handle.IsValid())
+ return true;
+
+ return type_ == handle.type_ && file_descriptor_ == handle.file_descriptor_;
+}
+
+bool SharedMemoryHandle::operator!=(const SharedMemoryHandle& handle) const {
+ return !(*this == handle);
+}
+
+SharedMemoryHandle::Type SharedMemoryHandle::GetType() const {
+ return type_;
+}
+
+bool SharedMemoryHandle::IsValid() const {
+ switch (type_) {
+ case POSIX:
+ return file_descriptor_.fd >= 0;
+ case MACH:
+ return false;
+ }
+}
+
+void SharedMemoryHandle::SetFileHandle(int fd, bool auto_close) {
+ DCHECK_EQ(type_, POSIX);
+ file_descriptor_.fd = fd;
+ file_descriptor_.auto_close = auto_close;
+}
+
+const FileDescriptor SharedMemoryHandle::GetFileDescriptor() const {
+ DCHECK_EQ(type_, POSIX);
+ return file_descriptor_;
+}
+
+SharedMemoryHandle SharedMemoryHandle::Duplicate() const {
+ DCHECK_EQ(type_, POSIX);
+ int duped_handle = HANDLE_EINTR(dup(file_descriptor_.fd));
+ if (duped_handle < 0)
+ return SharedMemoryHandle();
+ return SharedMemoryHandle(duped_handle, true);
+}
+
+} // namespace base
+#endif // defined(OS_MACOSX) && !defined(OS_IOS)
« no previous file with comments | « base/memory/shared_memory_handle.h ('k') | base/memory/shared_memory_mac.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698