blob: 1472935d86880870921cb76ff12a739097187d73 [file] [log] [blame]
Jim Stichnothd722ff22016-12-26 20:10:07 -08001//===--- 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"
Jim Stichnothd722ff22016-12-26 20:10:07 -080018#include "llvm/ADT/STLExtras.h"
Antonio Maioranoca8a16e2020-11-10 16:56:20 -050019#include "llvm/ADT/Statistic.h"
Jim Stichnothd722ff22016-12-26 20:10:07 -080020#include "llvm/Support/FileSystem.h"
21#include "llvm/Support/Program.h"
22#include <string>
23#include <system_error>
24#if !defined(_MSC_VER) && !defined(__MINGW32__)
25#include <unistd.h>
26#else
27#include <io.h>
28#endif
29using namespace llvm;
30
31#define DEBUG_TYPE "Data-stream"
32
33// Interface goals:
34// * StreamingMemoryObject 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
38// Non-goals:
39// StreamingMemoryObject 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
43
44STATISTIC(NumStreamFetches, "Number of calls to Data stream fetch");
45
46namespace llvm {
47DataStreamer::~DataStreamer() {}
Antonio Maioranoca8a16e2020-11-10 16:56:20 -050048} // namespace llvm
Jim Stichnothd722ff22016-12-26 20:10:07 -080049
50namespace {
51
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.
54class DataFileStreamer : public DataStreamer {
Antonio Maioranoca8a16e2020-11-10 16:56:20 -050055 int Fd;
56
Jim Stichnothd722ff22016-12-26 20:10:07 -080057public:
58 DataFileStreamer() : Fd(0) {}
59 ~DataFileStreamer() override { close(Fd); }
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
Antonio Maioranoca8a16e2020-11-10 16:56:20 -050076} // namespace
Jim Stichnothd722ff22016-12-26 20:10:07 -080077
78std::unique_ptr<DataStreamer>
79llvm::getDataFileStreamer(const std::string &Filename, std::string *StrError) {
80 std::unique_ptr<DataFileStreamer> s = make_unique<DataFileStreamer>();
81 if (std::error_code e = s->OpenFile(Filename)) {
Antonio Maioranoca8a16e2020-11-10 16:56:20 -050082 *StrError =
83 std::string("Could not open ") + Filename + ": " + e.message() + "\n";
Jim Stichnothd722ff22016-12-26 20:10:07 -080084 return nullptr;
85 }
86 return std::move(s);
87}