xref: /netbsd-src/external/apache2/llvm/dist/llvm/include/llvm/ExecutionEngine/Orc/Shared/FDRawByteChannel.h (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 //===- FDRawByteChannel.h - File descriptor based byte-channel -*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // File descriptor based RawByteChannel.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_EXECUTIONENGINE_ORC_SHARED_FDRAWBYTECHANNEL_H
14 #define LLVM_EXECUTIONENGINE_ORC_SHARED_FDRAWBYTECHANNEL_H
15 
16 #include "llvm/ExecutionEngine/Orc/Shared/RawByteChannel.h"
17 
18 #if !defined(_MSC_VER) && !defined(__MINGW32__)
19 #include <unistd.h>
20 #else
21 #include <io.h>
22 #endif
23 
24 namespace llvm {
25 namespace orc {
26 namespace shared {
27 
28 /// Serialization channel that reads from and writes from file descriptors.
29 class FDRawByteChannel final : public RawByteChannel {
30 public:
FDRawByteChannel(int InFD,int OutFD)31   FDRawByteChannel(int InFD, int OutFD) : InFD(InFD), OutFD(OutFD) {}
32 
readBytes(char * Dst,unsigned Size)33   llvm::Error readBytes(char *Dst, unsigned Size) override {
34     assert(Dst && "Attempt to read into null.");
35     ssize_t Completed = 0;
36     while (Completed < static_cast<ssize_t>(Size)) {
37       ssize_t Read = ::read(InFD, Dst + Completed, Size - Completed);
38       if (Read <= 0) {
39         auto ErrNo = errno;
40         if (ErrNo == EAGAIN || ErrNo == EINTR)
41           continue;
42         else
43           return llvm::errorCodeToError(
44               std::error_code(errno, std::generic_category()));
45       }
46       Completed += Read;
47     }
48     return llvm::Error::success();
49   }
50 
appendBytes(const char * Src,unsigned Size)51   llvm::Error appendBytes(const char *Src, unsigned Size) override {
52     assert(Src && "Attempt to append from null.");
53     ssize_t Completed = 0;
54     while (Completed < static_cast<ssize_t>(Size)) {
55       ssize_t Written = ::write(OutFD, Src + Completed, Size - Completed);
56       if (Written < 0) {
57         auto ErrNo = errno;
58         if (ErrNo == EAGAIN || ErrNo == EINTR)
59           continue;
60         else
61           return llvm::errorCodeToError(
62               std::error_code(errno, std::generic_category()));
63       }
64       Completed += Written;
65     }
66     return llvm::Error::success();
67   }
68 
send()69   llvm::Error send() override { return llvm::Error::success(); }
70 
71 private:
72   int InFD, OutFD;
73 };
74 
75 } // namespace shared
76 } // namespace orc
77 } // namespace llvm
78 
79 #endif // LLVM_EXECUTIONENGINE_ORC_SHARED_FDRAWBYTECHANNEL_H
80