blob: fa8edc729c95c2384478f0d0f03e442ae8dd6cfc [file] [log] [blame]
Derek Schuff2ea93872012-02-06 22:30:29 +00001//===--- 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#define DEBUG_TYPE "Data-stream"
18#include "llvm/ADT/Statistic.h"
19#include "llvm/Support/DataStream.h"
20#include "llvm/Support/system_error.h"
21#include <string>
22#include <cerrno>
23#include <cstdio>
24#if !defined(_MSC_VER) && !defined(__MINGW32__)
25#include <unistd.h>
26#else
27#include <io.h>
28#endif
29#include <fcntl.h>
30using namespace llvm;
31
32// Interface goals:
33// * StreamableMemoryObject doesn't care about complexities like using
34// threads/async callbacks to actually overlap download+compile
35// * Don't want to duplicate Data in memory
36// * Don't need to know total Data len in advance
37// Non-goals:
38// StreamableMemoryObject already has random access so this interface only does
39// in-order streaming (no arbitrary seeking, else we'd have to buffer all the
40// Data here in addition to MemoryObject). This also means that if we want
41// to be able to to free Data, BitstreamBytes/BitcodeReader will implement it
42
43STATISTIC(NumStreamFetches, "Number of calls to Data stream fetch");
44
45namespace llvm {
46DataStreamer::~DataStreamer() {}
47}
48
49namespace {
50
51const static error_code success;
52
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.
55class DataFileStreamer : public DataStreamer {
56 int Fd;
57public:
58 DataFileStreamer() : Fd(0) {}
59 virtual ~DataFileStreamer() {
60 close(Fd);
61 }
62 virtual size_t GetBytes(unsigned char *buf, size_t len) {
63 NumStreamFetches++;
64 return read(Fd, buf, len);
65 }
66
67 error_code OpenFile(const std::string &Filename) {
68 int OpenFlags = O_RDONLY;
69#ifdef O_BINARY
70 OpenFlags |= O_BINARY; // Open input file in binary mode on win32.
71#endif
72 if (Filename == "-")
73 Fd = 0;
74 else
75 Fd = ::open(Filename.c_str(), OpenFlags);
76 if (Fd == -1) return error_code(errno, posix_category());
77 return success;
78 }
79};
80
81}
82
83namespace llvm {
84DataStreamer *getDataFileStreamer(const std::string &Filename,
85 std::string *StrError) {
86 DataFileStreamer *s = new DataFileStreamer();
87 error_code e = s->OpenFile(Filename);
88 if (e != success) {
89 *StrError = std::string("Could not open ") + Filename + ": " +
90 e.message() + "\n";
91 return NULL;
92 }
93 return s;
94}
95
96}