xref: /aosp_15_r20/external/llvm/lib/Support/DataStream.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===--- llvm/Support/DataStream.cpp - Lazy streamed data -----------------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file implements DataStreamer, which fetches bytes of Data from
11*9880d681SAndroid Build Coastguard Worker // a stream source. It provides support for streaming (lazy reading) of
12*9880d681SAndroid Build Coastguard Worker // bitcode. An example implementation of streaming from a file or stdin
13*9880d681SAndroid Build Coastguard Worker // is included.
14*9880d681SAndroid Build Coastguard Worker //
15*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
16*9880d681SAndroid Build Coastguard Worker 
17*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/DataStream.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/Statistic.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/STLExtras.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/FileSystem.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Program.h"
22*9880d681SAndroid Build Coastguard Worker #include <string>
23*9880d681SAndroid Build Coastguard Worker #include <system_error>
24*9880d681SAndroid Build Coastguard Worker #if !defined(_MSC_VER) && !defined(__MINGW32__)
25*9880d681SAndroid Build Coastguard Worker #include <unistd.h>
26*9880d681SAndroid Build Coastguard Worker #else
27*9880d681SAndroid Build Coastguard Worker #include <io.h>
28*9880d681SAndroid Build Coastguard Worker #endif
29*9880d681SAndroid Build Coastguard Worker using namespace llvm;
30*9880d681SAndroid Build Coastguard Worker 
31*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "Data-stream"
32*9880d681SAndroid Build Coastguard Worker 
33*9880d681SAndroid Build Coastguard Worker // Interface goals:
34*9880d681SAndroid Build Coastguard Worker // * StreamingMemoryObject doesn't care about complexities like using
35*9880d681SAndroid Build Coastguard Worker //   threads/async callbacks to actually overlap download+compile
36*9880d681SAndroid Build Coastguard Worker // * Don't want to duplicate Data in memory
37*9880d681SAndroid Build Coastguard Worker // * Don't need to know total Data len in advance
38*9880d681SAndroid Build Coastguard Worker // Non-goals:
39*9880d681SAndroid Build Coastguard Worker // StreamingMemoryObject already has random access so this interface only does
40*9880d681SAndroid Build Coastguard Worker // in-order streaming (no arbitrary seeking, else we'd have to buffer all the
41*9880d681SAndroid Build Coastguard Worker // Data here in addition to MemoryObject).  This also means that if we want
42*9880d681SAndroid Build Coastguard Worker // to be able to to free Data, BitstreamBytes/BitcodeReader will implement it
43*9880d681SAndroid Build Coastguard Worker 
44*9880d681SAndroid Build Coastguard Worker STATISTIC(NumStreamFetches, "Number of calls to Data stream fetch");
45*9880d681SAndroid Build Coastguard Worker 
46*9880d681SAndroid Build Coastguard Worker namespace llvm {
~DataStreamer()47*9880d681SAndroid Build Coastguard Worker DataStreamer::~DataStreamer() {}
48*9880d681SAndroid Build Coastguard Worker }
49*9880d681SAndroid Build Coastguard Worker 
50*9880d681SAndroid Build Coastguard Worker namespace {
51*9880d681SAndroid Build Coastguard Worker 
52*9880d681SAndroid Build Coastguard Worker // Very simple stream backed by a file. Mostly useful for stdin and debugging;
53*9880d681SAndroid Build Coastguard Worker // actual file access is probably still best done with mmap.
54*9880d681SAndroid Build Coastguard Worker class DataFileStreamer : public DataStreamer {
55*9880d681SAndroid Build Coastguard Worker  int Fd;
56*9880d681SAndroid Build Coastguard Worker public:
DataFileStreamer()57*9880d681SAndroid Build Coastguard Worker   DataFileStreamer() : Fd(0) {}
~DataFileStreamer()58*9880d681SAndroid Build Coastguard Worker   ~DataFileStreamer() override { close(Fd); }
GetBytes(unsigned char * buf,size_t len)59*9880d681SAndroid Build Coastguard Worker   size_t GetBytes(unsigned char *buf, size_t len) override {
60*9880d681SAndroid Build Coastguard Worker     NumStreamFetches++;
61*9880d681SAndroid Build Coastguard Worker     return read(Fd, buf, len);
62*9880d681SAndroid Build Coastguard Worker   }
63*9880d681SAndroid Build Coastguard Worker 
OpenFile(const std::string & Filename)64*9880d681SAndroid Build Coastguard Worker   std::error_code OpenFile(const std::string &Filename) {
65*9880d681SAndroid Build Coastguard Worker     if (Filename == "-") {
66*9880d681SAndroid Build Coastguard Worker       Fd = 0;
67*9880d681SAndroid Build Coastguard Worker       sys::ChangeStdinToBinary();
68*9880d681SAndroid Build Coastguard Worker       return std::error_code();
69*9880d681SAndroid Build Coastguard Worker     }
70*9880d681SAndroid Build Coastguard Worker 
71*9880d681SAndroid Build Coastguard Worker     return sys::fs::openFileForRead(Filename, Fd);
72*9880d681SAndroid Build Coastguard Worker   }
73*9880d681SAndroid Build Coastguard Worker };
74*9880d681SAndroid Build Coastguard Worker 
75*9880d681SAndroid Build Coastguard Worker }
76*9880d681SAndroid Build Coastguard Worker 
77*9880d681SAndroid Build Coastguard Worker std::unique_ptr<DataStreamer>
getDataFileStreamer(const std::string & Filename,std::string * StrError)78*9880d681SAndroid Build Coastguard Worker llvm::getDataFileStreamer(const std::string &Filename, std::string *StrError) {
79*9880d681SAndroid Build Coastguard Worker   std::unique_ptr<DataFileStreamer> s = make_unique<DataFileStreamer>();
80*9880d681SAndroid Build Coastguard Worker   if (std::error_code e = s->OpenFile(Filename)) {
81*9880d681SAndroid Build Coastguard Worker     *StrError = std::string("Could not open ") + Filename + ": " +
82*9880d681SAndroid Build Coastguard Worker         e.message() + "\n";
83*9880d681SAndroid Build Coastguard Worker     return nullptr;
84*9880d681SAndroid Build Coastguard Worker   }
85*9880d681SAndroid Build Coastguard Worker   return std::move(s);
86*9880d681SAndroid Build Coastguard Worker }
87