Nick Lewycky | 44ebf8b | 2012-02-06 22:41:47 +0000 | [diff] [blame] | 1 | //===--- llvm/Support/DataStream.cpp - Lazy streamed data -----------------===// |
Derek Schuff | 2ea9387 | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 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" |
Derek Schuff | 2ea9387 | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 18 | #include "llvm/Support/DataStream.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/Statistic.h" |
Derek Schuff | eb44651 | 2012-02-07 00:28:46 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Program.h" |
Derek Schuff | 2ea9387 | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 21 | #include "llvm/Support/system_error.h" |
Derek Schuff | 2ea9387 | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 22 | #include <cerrno> |
| 23 | #include <cstdio> |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 24 | #include <string> |
Derek Schuff | 2ea9387 | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 25 | #if !defined(_MSC_VER) && !defined(__MINGW32__) |
| 26 | #include <unistd.h> |
| 27 | #else |
| 28 | #include <io.h> |
| 29 | #endif |
| 30 | #include <fcntl.h> |
| 31 | using 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 | |
| 44 | STATISTIC(NumStreamFetches, "Number of calls to Data stream fetch"); |
| 45 | |
| 46 | namespace llvm { |
| 47 | DataStreamer::~DataStreamer() {} |
| 48 | } |
| 49 | |
| 50 | namespace { |
| 51 | |
Derek Schuff | 2ea9387 | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 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 { |
| 55 | int Fd; |
| 56 | public: |
| 57 | DataFileStreamer() : Fd(0) {} |
| 58 | virtual ~DataFileStreamer() { |
| 59 | close(Fd); |
| 60 | } |
Craig Topper | a96a182 | 2012-09-23 02:12:10 +0000 | [diff] [blame] | 61 | virtual size_t GetBytes(unsigned char *buf, size_t len) LLVM_OVERRIDE { |
Derek Schuff | 2ea9387 | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 62 | NumStreamFetches++; |
| 63 | return read(Fd, buf, len); |
| 64 | } |
| 65 | |
| 66 | error_code OpenFile(const std::string &Filename) { |
David Blaikie | 4eebfb8 | 2012-02-09 00:29:19 +0000 | [diff] [blame] | 67 | if (Filename == "-") { |
| 68 | Fd = 0; |
Rafael Espindola | 9f1d9fd | 2013-06-12 20:58:35 +0000 | [diff] [blame] | 69 | sys::ChangeStdinToBinary(); |
David Blaikie | 58604cd | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 70 | return error_code::success(); |
David Blaikie | 4eebfb8 | 2012-02-09 00:29:19 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Derek Schuff | 2ea9387 | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 73 | int OpenFlags = O_RDONLY; |
| 74 | #ifdef O_BINARY |
| 75 | OpenFlags |= O_BINARY; // Open input file in binary mode on win32. |
| 76 | #endif |
David Blaikie | 4eebfb8 | 2012-02-09 00:29:19 +0000 | [diff] [blame] | 77 | Fd = ::open(Filename.c_str(), OpenFlags); |
| 78 | if (Fd == -1) |
| 79 | return error_code(errno, posix_category()); |
David Blaikie | 58604cd | 2012-02-09 19:24:12 +0000 | [diff] [blame] | 80 | return error_code::success(); |
Derek Schuff | 2ea9387 | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 81 | } |
| 82 | }; |
| 83 | |
| 84 | } |
| 85 | |
| 86 | namespace llvm { |
| 87 | DataStreamer *getDataFileStreamer(const std::string &Filename, |
| 88 | std::string *StrError) { |
| 89 | DataFileStreamer *s = new DataFileStreamer(); |
David Blaikie | 4eebfb8 | 2012-02-09 00:29:19 +0000 | [diff] [blame] | 90 | if (error_code e = s->OpenFile(Filename)) { |
Derek Schuff | 2ea9387 | 2012-02-06 22:30:29 +0000 | [diff] [blame] | 91 | *StrError = std::string("Could not open ") + Filename + ": " + |
| 92 | e.message() + "\n"; |
| 93 | return NULL; |
| 94 | } |
| 95 | return s; |
| 96 | } |
| 97 | |
| 98 | } |