DataStream.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //===--- 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. #include "llvm/Support/DataStream.h"
  17. #include "llvm/ADT/Statistic.h"
  18. #include "llvm/ADT/STLExtras.h"
  19. #include "llvm/Support/FileSystem.h"
  20. #include "llvm/Support/Program.h"
  21. #include <string>
  22. #include <system_error>
  23. #if !defined(_MSC_VER) && !defined(__MINGW32__)
  24. #include <unistd.h>
  25. #else
  26. #include <io.h>
  27. #endif
  28. using namespace llvm;
  29. #define DEBUG_TYPE "Data-stream"
  30. // Interface goals:
  31. // * StreamingMemoryObject doesn't care about complexities like using
  32. // threads/async callbacks to actually overlap download+compile
  33. // * Don't want to duplicate Data in memory
  34. // * Don't need to know total Data len in advance
  35. // Non-goals:
  36. // StreamingMemoryObject already has random access so this interface only does
  37. // in-order streaming (no arbitrary seeking, else we'd have to buffer all the
  38. // Data here in addition to MemoryObject). This also means that if we want
  39. // to be able to to free Data, BitstreamBytes/BitcodeReader will implement it
  40. STATISTIC(NumStreamFetches, "Number of calls to Data stream fetch");
  41. namespace llvm {
  42. DataStreamer::~DataStreamer() {}
  43. }
  44. namespace {
  45. // Very simple stream backed by a file. Mostly useful for stdin and debugging;
  46. // actual file access is probably still best done with mmap.
  47. class DataFileStreamer : public DataStreamer {
  48. int Fd;
  49. public:
  50. DataFileStreamer() : Fd(0) {}
  51. virtual ~DataFileStreamer() {
  52. llvm::sys::fs::msf_close(Fd); // HLSL Change - use msf_close
  53. }
  54. size_t GetBytes(unsigned char *buf, size_t len) override {
  55. NumStreamFetches++;
  56. return llvm::sys::fs::msf_read(Fd, buf, len);
  57. }
  58. std::error_code OpenFile(const std::string &Filename) {
  59. if (Filename == "-") {
  60. Fd = 0;
  61. sys::ChangeStdinToBinary();
  62. return std::error_code();
  63. }
  64. return sys::fs::openFileForRead(Filename, Fd);
  65. }
  66. };
  67. }
  68. std::unique_ptr<DataStreamer>
  69. llvm::getDataFileStreamer(const std::string &Filename, std::string *StrError) {
  70. std::unique_ptr<DataFileStreamer> s = make_unique<DataFileStreamer>();
  71. if (std::error_code e = s->OpenFile(Filename)) {
  72. *StrError = std::string("Could not open ") + Filename + ": " +
  73. e.message() + "\n";
  74. return nullptr;
  75. }
  76. return std::move(s);
  77. }