2012-02-06 22:41:47 +00:00
|
|
|
//===--- llvm/Support/DataStream.cpp - Lazy streamed data -----------------===//
|
2012-02-06 22:30:29 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements DataStreamer, which fetches bytes of Data from
|
|
|
|
// a stream source. It provides support for streaming (lazy reading) of
|
|
|
|
// bitcode. An example implementation of streaming from a file or stdin
|
|
|
|
// is included.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Support/DataStream.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2013-07-16 19:44:17 +00:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2012-02-07 00:28:46 +00:00
|
|
|
#include "llvm/Support/Program.h"
|
2012-02-06 22:30:29 +00:00
|
|
|
#include <cerrno>
|
|
|
|
#include <cstdio>
|
2012-12-03 16:50:05 +00:00
|
|
|
#include <string>
|
2014-06-12 17:38:55 +00:00
|
|
|
#include <system_error>
|
2012-02-06 22:30:29 +00:00
|
|
|
#if !defined(_MSC_VER) && !defined(__MINGW32__)
|
|
|
|
#include <unistd.h>
|
|
|
|
#else
|
|
|
|
#include <io.h>
|
|
|
|
#endif
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 03:07:47 +00:00
|
|
|
#define DEBUG_TYPE "Data-stream"
|
|
|
|
|
2012-02-06 22:30:29 +00:00
|
|
|
// Interface goals:
|
|
|
|
// * StreamableMemoryObject doesn't care about complexities like using
|
|
|
|
// threads/async callbacks to actually overlap download+compile
|
|
|
|
// * Don't want to duplicate Data in memory
|
|
|
|
// * Don't need to know total Data len in advance
|
|
|
|
// Non-goals:
|
|
|
|
// StreamableMemoryObject already has random access so this interface only does
|
|
|
|
// in-order streaming (no arbitrary seeking, else we'd have to buffer all the
|
|
|
|
// Data here in addition to MemoryObject). This also means that if we want
|
|
|
|
// to be able to to free Data, BitstreamBytes/BitcodeReader will implement it
|
|
|
|
|
|
|
|
STATISTIC(NumStreamFetches, "Number of calls to Data stream fetch");
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
DataStreamer::~DataStreamer() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Very simple stream backed by a file. Mostly useful for stdin and debugging;
|
|
|
|
// actual file access is probably still best done with mmap.
|
|
|
|
class DataFileStreamer : public DataStreamer {
|
|
|
|
int Fd;
|
|
|
|
public:
|
|
|
|
DataFileStreamer() : Fd(0) {}
|
|
|
|
virtual ~DataFileStreamer() {
|
|
|
|
close(Fd);
|
|
|
|
}
|
2014-03-10 03:53:12 +00:00
|
|
|
size_t GetBytes(unsigned char *buf, size_t len) override {
|
2012-02-06 22:30:29 +00:00
|
|
|
NumStreamFetches++;
|
|
|
|
return read(Fd, buf, len);
|
|
|
|
}
|
|
|
|
|
2014-06-13 02:24:39 +00:00
|
|
|
std::error_code OpenFile(const std::string &Filename) {
|
2012-02-07 00:28:46 +00:00
|
|
|
if (Filename == "-") {
|
2012-02-06 22:30:29 +00:00
|
|
|
Fd = 0;
|
2013-06-12 20:58:35 +00:00
|
|
|
sys::ChangeStdinToBinary();
|
2014-06-13 02:24:39 +00:00
|
|
|
return std::error_code();
|
2012-02-07 00:28:46 +00:00
|
|
|
}
|
2013-07-16 19:44:17 +00:00
|
|
|
|
|
|
|
return sys::fs::openFileForRead(Filename, Fd);
|
2012-02-06 22:30:29 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
DataStreamer *getDataFileStreamer(const std::string &Filename,
|
|
|
|
std::string *StrError) {
|
|
|
|
DataFileStreamer *s = new DataFileStreamer();
|
2014-06-13 02:24:39 +00:00
|
|
|
if (std::error_code e = s->OpenFile(Filename)) {
|
2012-02-06 22:30:29 +00:00
|
|
|
*StrError = std::string("Could not open ") + Filename + ": " +
|
|
|
|
e.message() + "\n";
|
2014-04-07 04:17:22 +00:00
|
|
|
return nullptr;
|
2012-02-06 22:30:29 +00:00
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|