blob: 32653de5194383549de9bb6b88b427383cca9f1f [file] [log] [blame]
Nick Lewycky44ebf8b2012-02-06 22:41:47 +00001//===--- llvm/Support/DataStream.cpp - Lazy streamed data -----------------===//
Derek Schuff2ea93872012-02-06 22:30:29 +00002//
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
Derek Schuff2ea93872012-02-06 22:30:29 +000017#include "llvm/Support/DataStream.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000018#include "llvm/ADT/Statistic.h"
Rafael Espindolac1b49b52013-07-16 19:44:17 +000019#include "llvm/Support/FileSystem.h"
Derek Schuffeb446512012-02-07 00:28:46 +000020#include "llvm/Support/Program.h"
Derek Schuff2ea93872012-02-06 22:30:29 +000021#include <cerrno>
22#include <cstdio>
Chandler Carruthd04a8d42012-12-03 16:50:05 +000023#include <string>
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -070024#include <system_error>
Derek Schuff2ea93872012-02-06 22:30:29 +000025#if !defined(_MSC_VER) && !defined(__MINGW32__)
26#include <unistd.h>
27#else
28#include <io.h>
29#endif
Derek Schuff2ea93872012-02-06 22:30:29 +000030using namespace llvm;
31
Stephen Hinesdce4a402014-05-29 02:49:00 -070032#define DEBUG_TYPE "Data-stream"
33
Derek Schuff2ea93872012-02-06 22:30:29 +000034// 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
45STATISTIC(NumStreamFetches, "Number of calls to Data stream fetch");
46
47namespace llvm {
48DataStreamer::~DataStreamer() {}
49}
50
51namespace {
52
Derek Schuff2ea93872012-02-06 22:30:29 +000053// Very simple stream backed by a file. Mostly useful for stdin and debugging;
54// actual file access is probably still best done with mmap.
55class DataFileStreamer : public DataStreamer {
56 int Fd;
57public:
58 DataFileStreamer() : Fd(0) {}
59 virtual ~DataFileStreamer() {
60 close(Fd);
61 }
Stephen Hines36b56882014-04-23 16:57:46 -070062 size_t GetBytes(unsigned char *buf, size_t len) override {
Derek Schuff2ea93872012-02-06 22:30:29 +000063 NumStreamFetches++;
64 return read(Fd, buf, len);
65 }
66
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -070067 std::error_code OpenFile(const std::string &Filename) {
David Blaikie4eebfb82012-02-09 00:29:19 +000068 if (Filename == "-") {
69 Fd = 0;
Rafael Espindola9f1d9fd2013-06-12 20:58:35 +000070 sys::ChangeStdinToBinary();
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -070071 return std::error_code();
David Blaikie4eebfb82012-02-09 00:29:19 +000072 }
Rafael Espindolac1b49b52013-07-16 19:44:17 +000073
74 return sys::fs::openFileForRead(Filename, Fd);
Derek Schuff2ea93872012-02-06 22:30:29 +000075 }
76};
77
78}
79
80namespace llvm {
81DataStreamer *getDataFileStreamer(const std::string &Filename,
82 std::string *StrError) {
83 DataFileStreamer *s = new DataFileStreamer();
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -070084 if (std::error_code e = s->OpenFile(Filename)) {
Derek Schuff2ea93872012-02-06 22:30:29 +000085 *StrError = std::string("Could not open ") + Filename + ": " +
86 e.message() + "\n";
Stephen Hinesdce4a402014-05-29 02:49:00 -070087 return nullptr;
Derek Schuff2ea93872012-02-06 22:30:29 +000088 }
89 return s;
90}
91
92}