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 #define DEBUG_TYPE "Data-stream"
18 #include "llvm/Support/DataStream.h"
19 #include "llvm/ADT/Statistic.h"
20 #include "llvm/Support/Program.h"
21 #include "llvm/Support/system_error.h"
25 #if !defined(_MSC_VER) && !defined(__MINGW32__)
34 // * StreamableMemoryObject doesn't care about complexities like using
35 // threads/async callbacks to actually overlap download+compile
36 // * Don't want to duplicate Data in memory
37 // * Don't need to know total Data len in advance
39 // StreamableMemoryObject already has random access so this interface only does
40 // in-order streaming (no arbitrary seeking, else we'd have to buffer all the
41 // Data here in addition to MemoryObject). This also means that if we want
42 // to be able to to free Data, BitstreamBytes/BitcodeReader will implement it
44 STATISTIC(NumStreamFetches, "Number of calls to Data stream fetch");
47 DataStreamer::~DataStreamer() {}
52 // Very simple stream backed by a file. Mostly useful for stdin and debugging;
53 // actual file access is probably still best done with mmap.
54 class DataFileStreamer : public DataStreamer {
57 DataFileStreamer() : Fd(0) {}
58 virtual ~DataFileStreamer() {
61 virtual size_t GetBytes(unsigned char *buf, size_t len) LLVM_OVERRIDE {
63 return read(Fd, buf, len);
66 error_code OpenFile(const std::string &Filename) {
67 if (Filename == "-") {
69 sys::Program::ChangeStdinToBinary();
70 return error_code::success();
73 int OpenFlags = O_RDONLY;
75 OpenFlags |= O_BINARY; // Open input file in binary mode on win32.
77 Fd = ::open(Filename.c_str(), OpenFlags);
79 return error_code(errno, posix_category());
80 return error_code::success();
87 DataStreamer *getDataFileStreamer(const std::string &Filename,
88 std::string *StrError) {
89 DataFileStreamer *s = new DataFileStreamer();
90 if (error_code e = s->OpenFile(Filename)) {
91 *StrError = std::string("Could not open ") + Filename + ": " +