Chromium Code Reviews| 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..610e1acfdff3286d18d2a81a15020f88af464179 |
| --- /dev/null |
| +++ b/base/memory/shared_memory_handle_posix.cc |
| @@ -0,0 +1,55 @@ |
| +// 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) {} |
| + |
|
Nico
2017/04/27 16:24:44
do you want to call Close() in the dtor if auto_cl
erikchen
2017/04/27 17:08:10
No, that's done in ipc_message_utils.cc.
This CL
|
| +// static |
| +SharedMemoryHandle SharedMemoryHandle::ImportHandle(int fd) { |
| + SharedMemoryHandle handle; |
| + handle.file_descriptor.fd = fd; |
| + handle.file_descriptor.auto_close = true; |
| + return handle; |
| +} |
| + |
| +int SharedMemoryHandle::GetHandle() const { |
| + return file_descriptor.fd; |
| +} |
| + |
| +bool SharedMemoryHandle::IsValid() const { |
| + return file_descriptor.fd >= 0; |
| +} |
| + |
| +void SharedMemoryHandle::Close() const { |
| + if (IGNORE_EINTR(close(file_descriptor.fd)) < 0) |
| + PLOG(ERROR) << "close"; |
|
Nico
2017/04/27 16:24:44
Should this then call Invalidate()? Closing but ke
erikchen
2017/04/27 17:08:10
It should, but it never has on any platform. I've
|
| +} |
| + |
| +void SharedMemoryHandle::Invalidate() { |
| + file_descriptor.fd = -1; |
| +} |
| + |
| +SharedMemoryHandle SharedMemoryHandle::Duplicate() const { |
| + if (!IsValid()) |
| + return SharedMemoryHandle(); |
| + |
| + int duped_handle = HANDLE_EINTR(dup(file_descriptor.fd)); |
| + if (duped_handle < 0) |
| + return SharedMemoryHandle(); |
| + return SharedMemoryHandle(FileDescriptor(duped_handle, true)); |
| +} |
| + |
| +} // namespace base |