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