1 //===--- llvm/Support/DataStream.cpp - Lazy streamed data -----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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
15 //===----------------------------------------------------------------------===//
17 #include "llvm/Support/DataStream.h"
18 #include "llvm/ADT/Statistic.h"
19 #include "llvm/Support/FileSystem.h"
20 #include "llvm/Support/Program.h"
24 #include <system_error>
25 #if !defined(_MSC_VER) && !defined(__MINGW32__)
32 #define DEBUG_TYPE "Data-stream"
35 // * StreamingMemoryObject 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
40 // StreamingMemoryObject 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
45 STATISTIC(NumStreamFetches, "Number of calls to Data stream fetch");
48 DataStreamer::~DataStreamer() {}
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 {
58 DataFileStreamer() : Fd(0) {}
59 virtual ~DataFileStreamer() {
62 size_t GetBytes(unsigned char *buf, size_t len) override {
64 return read(Fd, buf, len);
67 std::error_code OpenFile(const std::string &Filename) {
68 if (Filename == "-") {
70 sys::ChangeStdinToBinary();
71 return std::error_code();
74 return sys::fs::openFileForRead(Filename, Fd);
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 + ": " +