DataStream.cpp revision 263508
1284345Ssjg//===--- llvm/Support/DataStream.cpp - Lazy streamed data -----------------===// 2284345Ssjg// 3284345Ssjg// The LLVM Compiler Infrastructure 4284345Ssjg// 5291563Sbdrewery// This file is distributed under the University of Illinois Open Source 6291563Sbdrewery// License. See LICENSE.TXT for details. 7291563Sbdrewery// 8284345Ssjg//===----------------------------------------------------------------------===// 9284345Ssjg// 10284345Ssjg// This file implements DataStreamer, which fetches bytes of Data from 11284345Ssjg// a stream source. It provides support for streaming (lazy reading) of 12284345Ssjg// bitcode. An example implementation of streaming from a file or stdin 13284345Ssjg// is included. 14284345Ssjg// 15//===----------------------------------------------------------------------===// 16 17#define DEBUG_TYPE "Data-stream" 18#include "llvm/Support/DataStream.h" 19#include "llvm/ADT/Statistic.h" 20#include "llvm/Support/FileSystem.h" 21#include "llvm/Support/Program.h" 22#include "llvm/Support/system_error.h" 23#include <cerrno> 24#include <cstdio> 25#include <string> 26#if !defined(_MSC_VER) && !defined(__MINGW32__) 27#include <unistd.h> 28#else 29#include <io.h> 30#endif 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 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 { 55 int Fd; 56public: 57 DataFileStreamer() : Fd(0) {} 58 virtual ~DataFileStreamer() { 59 close(Fd); 60 } 61 virtual size_t GetBytes(unsigned char *buf, size_t len) LLVM_OVERRIDE { 62 NumStreamFetches++; 63 return read(Fd, buf, len); 64 } 65 66 error_code OpenFile(const std::string &Filename) { 67 if (Filename == "-") { 68 Fd = 0; 69 sys::ChangeStdinToBinary(); 70 return error_code::success(); 71 } 72 73 return sys::fs::openFileForRead(Filename, Fd); 74 } 75}; 76 77} 78 79namespace llvm { 80DataStreamer *getDataFileStreamer(const std::string &Filename, 81 std::string *StrError) { 82 DataFileStreamer *s = new DataFileStreamer(); 83 if (error_code e = s->OpenFile(Filename)) { 84 *StrError = std::string("Could not open ") + Filename + ": " + 85 e.message() + "\n"; 86 return NULL; 87 } 88 return s; 89} 90 91} 92