]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Host/common/Pipe.cpp
Merge ^/head r275685 through r275714.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Host / common / Pipe.cpp
1 //===-- Pipe.cpp ------------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "lldb/Host/Pipe.h"
11
12 #if defined(_WIN32)
13 #include <io.h>
14 #include <fcntl.h>
15 #else
16 #include <unistd.h>
17 #endif
18
19 using namespace lldb_private;
20
21 int Pipe::kInvalidDescriptor = -1;
22
23 enum PIPES { READ, WRITE }; // Constants 0 and 1 for READ and WRITE
24
25 Pipe::Pipe()
26 {
27     m_fds[READ] = Pipe::kInvalidDescriptor;
28     m_fds[WRITE] = Pipe::kInvalidDescriptor;
29 }
30
31 Pipe::~Pipe()
32 {
33     Close();
34 }
35
36 bool
37 Pipe::Open()
38 {
39     if (IsValid())
40         return true;
41
42 #ifdef _WIN32
43     if (::_pipe(m_fds, 256, O_BINARY) == 0)
44         return true;
45 #else
46     if (::pipe(m_fds) == 0)
47         return true;
48 #endif
49     m_fds[READ] = Pipe::kInvalidDescriptor;
50     m_fds[WRITE] = Pipe::kInvalidDescriptor;
51     return false;
52 }
53
54 int
55 Pipe::GetReadFileDescriptor() const
56 {
57     return m_fds[READ];
58 }
59
60 int
61 Pipe::GetWriteFileDescriptor() const
62 {
63     return m_fds[WRITE];
64 }
65
66 int
67 Pipe::ReleaseReadFileDescriptor()
68 {
69     const int fd = m_fds[READ];
70     m_fds[READ] = Pipe::kInvalidDescriptor;
71     return fd;
72 }
73
74 int
75 Pipe::ReleaseWriteFileDescriptor()
76 {
77     const int fd = m_fds[WRITE];
78     m_fds[WRITE] = Pipe::kInvalidDescriptor;
79     return fd;
80 }
81
82 void
83 Pipe::Close()
84 {
85     CloseReadFileDescriptor();
86     CloseWriteFileDescriptor();
87 }
88
89 bool
90 Pipe::ReadDescriptorIsValid() const
91 {
92     return m_fds[READ] != Pipe::kInvalidDescriptor;
93 }
94
95 bool
96 Pipe::WriteDescriptorIsValid() const
97 {
98     return m_fds[WRITE] != Pipe::kInvalidDescriptor;
99 }
100
101 bool
102 Pipe::IsValid() const
103 {
104     return ReadDescriptorIsValid() && WriteDescriptorIsValid();
105 }
106
107 bool
108 Pipe::CloseReadFileDescriptor()
109 {
110     if (ReadDescriptorIsValid())
111     {
112         int err;
113 #ifdef _WIN32
114         err = _close(m_fds[READ]);
115 #else
116         err = close(m_fds[READ]);
117 #endif
118         m_fds[READ] = Pipe::kInvalidDescriptor;
119         return err == 0;
120     }
121     return true;
122 }
123
124 bool
125 Pipe::CloseWriteFileDescriptor()
126 {
127     if (WriteDescriptorIsValid())
128     {
129         int err;
130 #ifdef _WIN32
131         err = _close(m_fds[WRITE]);
132 #else
133         err = close(m_fds[WRITE]);
134 #endif
135         m_fds[WRITE] = Pipe::kInvalidDescriptor;
136         return err == 0;
137     }
138     return true;
139 }
140
141
142 size_t
143 Pipe::Read (void *buf, size_t num_bytes)
144 {
145     if (ReadDescriptorIsValid())
146     {
147         const int fd = GetReadFileDescriptor();
148 #ifdef _WIN32
149         return _read (fd, (char *)buf, num_bytes);
150 #else
151         return read (fd, buf, num_bytes);
152 #endif
153     }
154     return 0; // Return 0 since errno won't be set if we didn't call read
155 }
156
157 size_t
158 Pipe::Write (const void *buf, size_t num_bytes)
159 {
160     if (WriteDescriptorIsValid())
161     {
162         const int fd = GetWriteFileDescriptor();
163 #ifdef _WIN32
164         return _write (fd, (char *)buf, num_bytes);
165 #else
166         return write (fd, buf, num_bytes);
167 #endif
168     }
169     return 0; // Return 0 since errno won't be set if we didn't call write
170 }
171