blob: b2dd979e7f1169e90f3b5d450ef57c527737e90f [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
17#define DEBUG_TYPE "Data-stream"
18#include "llvm/ADT/Statistic.h"
19#include "llvm/Support/DataStream.h"
Derek Schuffeb446512012-02-07 00:28:46 +000020#include "llvm/Support/Program.h"
Derek Schuff2ea93872012-02-06 22:30:29 +000021#include "llvm/Support/system_error.h"
22#include <string>
23#include <cerrno>
24#include <cstdio>
25#if !defined(_MSC_VER) && !defined(__MINGW32__)
26#include <unistd.h>
27#else
28#include <io.h>
29#endif
30#include <fcntl.h>
31using namespace llvm;
32
33// Interface goals:
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
38// Non-goals:
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
43
44STATISTIC(NumStreamFetches, "Number of calls to Data stream fetch");
45
46namespace llvm {
47DataStreamer::~DataStreamer() {}
48}
49
50namespace {
51
52const static error_code success;
53
54// Very simple stream backed by a file. Mostly useful for stdin and debugging;
55// actual file access is probably still best done with mmap.
56class DataFileStreamer : public DataStreamer {
57 int Fd;
58public:
59 DataFileStreamer() : Fd(0) {}
60 virtual ~DataFileStreamer() {
61 close(Fd);
62 }
63 virtual size_t GetBytes(unsigned char *buf, size_t len) {
64 NumStreamFetches++;
65 return read(Fd, buf, len);
66 }
67
68 error_code OpenFile(const std::string &Filename) {
69 int OpenFlags = O_RDONLY;
70#ifdef O_BINARY
71 OpenFlags |= O_BINARY; // Open input file in binary mode on win32.
72#endif
Derek Schuffeb446512012-02-07 00:28:46 +000073 if (Filename == "-") {
Derek Schuff2ea93872012-02-06 22:30:29 +000074 Fd = 0;
Derek Schuffeb446512012-02-07 00:28:46 +000075 sys::Program::ChangeStdinToBinary();
76 }
Derek Schuff2ea93872012-02-06 22:30:29 +000077 else
78 Fd = ::open(Filename.c_str(), OpenFlags);
79 if (Fd == -1) return error_code(errno, posix_category());
80 return success;
81 }
82};
83
84}
85
86namespace llvm {
87DataStreamer *getDataFileStreamer(const std::string &Filename,
88 std::string *StrError) {
89 DataFileStreamer *s = new DataFileStreamer();
90 error_code e = s->OpenFile(Filename);
91 if (e != success) {
92 *StrError = std::string("Could not open ") + Filename + ": " +
93 e.message() + "\n";
94 return NULL;
95 }
96 return s;
97}
98
99}