a44b95804c7ae595c120a610a37e26ae6b447217
[oota-llvm.git] / lib / Support / DataStream.cpp
1 //===--- llvm/Support/DataStream.cpp - Lazy streamed data -----------------===//
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 // This file implements DataStreamer, which fetches bytes of Data from
11 // a stream source. It provides support for streaming (lazy reading) of
12 // bitcode. An example implementation of streaming from a file or stdin
13 // is included.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #include "llvm/Support/DataStream.h"
18 #include "llvm/ADT/Statistic.h"
19 #include "llvm/Support/FileSystem.h"
20 #include "llvm/Support/Program.h"
21 #include <string>
22 #include <system_error>
23 #if !defined(_MSC_VER) && !defined(__MINGW32__)
24 #include <unistd.h>
25 #else
26 #include <io.h>
27 #endif
28 using namespace llvm;
29
30 #define DEBUG_TYPE "Data-stream"
31
32 // Interface goals:
33 // * StreamingMemoryObject doesn't care about complexities like using
34 //   threads/async callbacks to actually overlap download+compile
35 // * Don't want to duplicate Data in memory
36 // * Don't need to know total Data len in advance
37 // Non-goals:
38 // StreamingMemoryObject already has random access so this interface only does
39 // in-order streaming (no arbitrary seeking, else we'd have to buffer all the
40 // Data here in addition to MemoryObject).  This also means that if we want
41 // to be able to to free Data, BitstreamBytes/BitcodeReader will implement it
42
43 STATISTIC(NumStreamFetches, "Number of calls to Data stream fetch");
44
45 namespace llvm {
46 DataStreamer::~DataStreamer() {}
47 }
48
49 namespace {
50
51 // Very simple stream backed by a file. Mostly useful for stdin and debugging;
52 // actual file access is probably still best done with mmap.
53 class DataFileStreamer : public DataStreamer {
54  int Fd;
55 public:
56   DataFileStreamer() : Fd(0) {}
57   virtual ~DataFileStreamer() {
58     close(Fd);
59   }
60   size_t GetBytes(unsigned char *buf, size_t len) override {
61     NumStreamFetches++;
62     return read(Fd, buf, len);
63   }
64
65   std::error_code OpenFile(const std::string &Filename) {
66     if (Filename == "-") {
67       Fd = 0;
68       sys::ChangeStdinToBinary();
69       return std::error_code();
70     }
71
72     return sys::fs::openFileForRead(Filename, Fd);
73   }
74 };
75
76 }
77
78 namespace llvm {
79 DataStreamer *getDataFileStreamer(const std::string &Filename,
80                                   std::string *StrError) {
81   DataFileStreamer *s = new DataFileStreamer();
82   if (std::error_code e = s->OpenFile(Filename)) {
83     *StrError = std::string("Could not open ") + Filename + ": " +
84         e.message() + "\n";
85     return nullptr;
86   }
87   return s;
88 }
89
90 }