OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 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. | |
4 | |
5 #include "base/memory/shared_memory_handle.h" | |
6 | |
7 #include <unistd.h> | |
8 | |
9 #include "base/logging.h" | |
10 #include "base/posix/eintr_wrapper.h" | |
11 | |
12 namespace base { | |
13 | |
14 SharedMemoryHandle::SharedMemoryHandle() = default; | |
15 | |
16 SharedMemoryHandle::SharedMemoryHandle( | |
17 const base::FileDescriptor& file_descriptor) | |
18 : file_descriptor_(file_descriptor) {} | |
19 | |
20 // static | |
21 SharedMemoryHandle SharedMemoryHandle::ImportHandle(int fd) { | |
22 SharedMemoryHandle handle; | |
23 handle.file_descriptor_.fd = fd; | |
24 handle.file_descriptor_.auto_close = false; | |
25 return handle; | |
26 } | |
27 | |
28 int SharedMemoryHandle::GetHandle() const { | |
29 return file_descriptor_.fd; | |
30 } | |
31 | |
32 void SharedMemoryHandle::SetHandle(int handle) { | |
33 file_descriptor_.fd = handle; | |
34 } | |
35 | |
36 bool SharedMemoryHandle::IsValid() const { | |
37 return file_descriptor_.fd >= 0; | |
38 } | |
39 | |
40 void SharedMemoryHandle::Close() const { | |
41 if (IGNORE_EINTR(close(file_descriptor_.fd)) < 0) | |
42 PLOG(ERROR) << "close"; | |
43 } | |
44 | |
45 int SharedMemoryHandle::Release() { | |
46 int old_fd = file_descriptor_.fd; | |
47 file_descriptor_.fd = -1; | |
48 return old_fd; | |
49 } | |
50 | |
51 SharedMemoryHandle SharedMemoryHandle::Duplicate() const { | |
52 if (!IsValid()) | |
53 return SharedMemoryHandle(); | |
54 | |
55 int duped_handle = HANDLE_EINTR(dup(file_descriptor_.fd)); | |
56 if (duped_handle < 0) | |
57 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
| |
58 return SharedMemoryHandle(FileDescriptor(duped_handle, true)); | |
59 } | |
60 | |
61 void SharedMemoryHandle::SetOwnershipPassesToIPC(bool ownership_passes) { | |
62 file_descriptor_.auto_close = ownership_passes; | |
63 } | |
64 | |
65 bool SharedMemoryHandle::OwnershipPassesToIPC() const { | |
66 return file_descriptor_.auto_close; | |
67 } | |
68 | |
69 } // namespace base | |
OLD | NEW |