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

Unified Diff: base/memory/shared_memory_handle_posix.cc

Issue 2843113002: make base::SharedMemoryHandle a class on POSIX. (Closed)
Patch Set: Fix test error. Created 3 years, 8 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
Index: base/memory/shared_memory_handle_posix.cc
diff --git a/base/memory/shared_memory_handle_posix.cc b/base/memory/shared_memory_handle_posix.cc
new file mode 100644
index 0000000000000000000000000000000000000000..f11b35b1f8218437635047dba8a01e0dd9cca24e
--- /dev/null
+++ b/base/memory/shared_memory_handle_posix.cc
@@ -0,0 +1,69 @@
+// Copyright 2017 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/logging.h"
+#include "base/posix/eintr_wrapper.h"
+
+namespace base {
+
+SharedMemoryHandle::SharedMemoryHandle() = default;
+
+SharedMemoryHandle::SharedMemoryHandle(
+ const base::FileDescriptor& file_descriptor)
+ : file_descriptor_(file_descriptor) {}
+
+// static
+SharedMemoryHandle SharedMemoryHandle::ImportHandle(int fd) {
+ SharedMemoryHandle handle;
+ handle.file_descriptor_.fd = fd;
+ handle.file_descriptor_.auto_close = false;
+ return handle;
+}
+
+int SharedMemoryHandle::GetHandle() const {
+ return file_descriptor_.fd;
+}
+
+void SharedMemoryHandle::SetHandle(int handle) {
+ file_descriptor_.fd = handle;
+}
+
+bool SharedMemoryHandle::IsValid() const {
+ return file_descriptor_.fd >= 0;
+}
+
+void SharedMemoryHandle::Close() const {
+ if (IGNORE_EINTR(close(file_descriptor_.fd)) < 0)
+ PLOG(ERROR) << "close";
+}
+
+int SharedMemoryHandle::Release() {
+ int old_fd = file_descriptor_.fd;
+ file_descriptor_.fd = -1;
+ return old_fd;
+}
+
+SharedMemoryHandle SharedMemoryHandle::Duplicate() const {
+ if (!IsValid())
+ return SharedMemoryHandle();
+
+ int duped_handle = HANDLE_EINTR(dup(file_descriptor_.fd));
+ if (duped_handle < 0)
+ return SharedMemoryHandle();
Robert Sesek 2017/04/28 16:19:13 Deliberately dropping the PLOG that was in ShareTo
erikchen 2017/04/28 17:03:38 I'll add it back in a followup CL. https://bugs.ch
+ return SharedMemoryHandle(FileDescriptor(duped_handle, true));
+}
+
+void SharedMemoryHandle::SetOwnershipPassesToIPC(bool ownership_passes) {
+ file_descriptor_.auto_close = ownership_passes;
+}
+
+bool SharedMemoryHandle::OwnershipPassesToIPC() const {
+ return file_descriptor_.auto_close;
+}
+
+} // namespace base

Powered by Google App Engine
This is Rietveld 408576698