llvm.org GIT mirror llvm / 4ac35e8
Support: Remove MemoryObject and DataStreamer interfaces. These interfaces are no longer used. Differential Revision: https://reviews.llvm.org/D26222 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@285774 91177308-0d34-0410-b5e6-96231b3b80d8 Peter Collingbourne 2 years ago
13 changed file(s) with 0 addition(s) and 507 deletion(s). Raw diff Collapse all Expand all
+0
-38
include/llvm/Support/DataStream.h less more
None //===---- llvm/Support/DataStream.h - Lazy bitcode streaming ----*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This header defines DataStreamer, which fetches bytes of data from
10 // a stream source. It provides support for streaming (lazy reading) of
11 // data, e.g. bitcode
12 //
13 //===----------------------------------------------------------------------===//
14
15
16 #ifndef LLVM_SUPPORT_DATASTREAM_H
17 #define LLVM_SUPPORT_DATASTREAM_H
18
19 #include
20 #include
21
22 namespace llvm {
23
24 class DataStreamer {
25 public:
26 /// Fetch bytes [start-end) from the stream, and write them to the
27 /// buffer pointed to by buf. Returns the number of bytes actually written.
28 virtual size_t GetBytes(unsigned char *buf, size_t len) = 0;
29
30 virtual ~DataStreamer();
31 };
32
33 std::unique_ptr getDataFileStreamer(const std::string &Filename,
34 std::string *Err);
35 }
36
37 #endif // LLVM_SUPPORT_DATASTREAM_H_
+0
-68
include/llvm/Support/MemoryObject.h less more
None //===- MemoryObject.h - Abstract memory interface ---------------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #ifndef LLVM_SUPPORT_MEMORYOBJECT_H
10 #define LLVM_SUPPORT_MEMORYOBJECT_H
11
12 #include "llvm/Support/DataTypes.h"
13
14 namespace llvm {
15
16 /// Interface to data which might be streamed. Streamability has 2 important
17 /// implications/restrictions. First, the data might not yet exist in memory
18 /// when the request is made. This just means that readByte/readBytes might have
19 /// to block or do some work to get it. More significantly, the exact size of
20 /// the object might not be known until it has all been fetched. This means that
21 /// to return the right result, getExtent must also wait for all the data to
22 /// arrive; therefore it should not be called on objects which are actually
23 /// streamed (this would defeat the purpose of streaming). Instead,
24 /// isValidAddress can be used to test addresses without knowing the exact size
25 /// of the stream. Finally, getPointer can be used instead of readBytes to avoid
26 /// extra copying.
27 class MemoryObject {
28 public:
29 virtual ~MemoryObject();
30
31 /// Returns the size of the region in bytes. (The region is contiguous, so
32 /// the highest valid address of the region is getExtent() - 1).
33 ///
34 /// @result - The size of the region.
35 virtual uint64_t getExtent() const = 0;
36
37 /// Tries to read a contiguous range of bytes from the region, up to the end
38 /// of the region.
39 ///
40 /// @param Buf - A pointer to a buffer to be filled in. Must be non-NULL
41 /// and large enough to hold size bytes.
42 /// @param Size - The number of bytes to copy.
43 /// @param Address - The address of the first byte, in the same space as
44 /// getBase().
45 /// @result - The number of bytes read.
46 virtual uint64_t readBytes(uint8_t *Buf, uint64_t Size,
47 uint64_t Address) const = 0;
48
49 /// Ensures that the requested data is in memory, and returns a pointer to it.
50 /// More efficient than using readBytes if the data is already in memory. May
51 /// block until (address - base + size) bytes have been read
52 /// @param address - address of the byte, in the same space as getBase()
53 /// @param size - amount of data that must be available on return
54 /// @result - valid pointer to the requested data
55 virtual const uint8_t *getPointer(uint64_t address, uint64_t size) const = 0;
56
57 /// Returns true if the address is within the object (i.e. between base and
58 /// base + extent - 1 inclusive). May block until (address - base) bytes have
59 /// been read
60 /// @param address - address of the byte, in the same space as getBase()
61 /// @result - true if the address may be read with readByte()
62 virtual bool isValidAddress(uint64_t address) const = 0;
63 };
64
65 }
66
67 #endif
+0
-87
include/llvm/Support/StreamingMemoryObject.h less more
None //===- StreamingMemoryObject.h - Streamable data interface -----*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #ifndef LLVM_SUPPORT_STREAMINGMEMORYOBJECT_H
10 #define LLVM_SUPPORT_STREAMINGMEMORYOBJECT_H
11
12 #include "llvm/Support/Compiler.h"
13 #include "llvm/Support/DataStream.h"
14 #include "llvm/Support/ErrorHandling.h"
15 #include "llvm/Support/MemoryObject.h"
16 #include
17 #include
18
19 namespace llvm {
20
21 /// Interface to data which is actually streamed from a DataStreamer. In
22 /// addition to inherited members, it has the dropLeadingBytes and
23 /// setKnownObjectSize methods which are not applicable to non-streamed objects.
24 class StreamingMemoryObject : public MemoryObject {
25 public:
26 StreamingMemoryObject(std::unique_ptr Streamer);
27 uint64_t getExtent() const override;
28 uint64_t readBytes(uint8_t *Buf, uint64_t Size,
29 uint64_t Address) const override;
30 const uint8_t *getPointer(uint64_t Address, uint64_t Size) const override;
31 bool isValidAddress(uint64_t address) const override;
32
33 /// Drop s bytes from the front of the stream, pushing the positions of the
34 /// remaining bytes down by s. This is used to skip past the bitcode header,
35 /// since we don't know a priori if it's present, and we can't put bytes
36 /// back into the stream once we've read them.
37 bool dropLeadingBytes(size_t s);
38
39 /// If the data object size is known in advance, many of the operations can
40 /// be made more efficient, so this method should be called before reading
41 /// starts (although it can be called anytime).
42 void setKnownObjectSize(size_t size);
43
44 /// The number of bytes read at a time from the data streamer.
45 static const uint32_t kChunkSize = 4096 * 4;
46
47 private:
48 mutable std::vector Bytes;
49 std::unique_ptr Streamer;
50 mutable size_t BytesRead; // Bytes read from stream
51 size_t BytesSkipped;// Bytes skipped at start of stream (e.g. wrapper/header)
52 mutable size_t ObjectSize; // 0 if unknown, set if wrapper seen or EOF reached
53 mutable bool EOFReached;
54
55 // Fetch enough bytes such that Pos can be read (i.e. BytesRead >
56 // Pos). Returns true if Pos can be read. Unlike most of the
57 // functions in BitcodeReader, returns true on success. Most of the
58 // requests will be small, but we fetch at kChunkSize bytes at a
59 // time to avoid making too many potentially expensive GetBytes
60 // calls.
61 bool fetchToPos(size_t Pos) const {
62 while (Pos >= BytesRead) {
63 if (EOFReached)
64 return false;
65 Bytes.resize(BytesRead + BytesSkipped + kChunkSize);
66 size_t bytes = Streamer->GetBytes(&Bytes[BytesRead + BytesSkipped],
67 kChunkSize);
68 BytesRead += bytes;
69 if (bytes == 0) { // reached EOF/ran out of bytes
70 if (ObjectSize == 0)
71 ObjectSize = BytesRead;
72 EOFReached = true;
73 }
74 }
75 return !ObjectSize || Pos < ObjectSize;
76 }
77
78 StreamingMemoryObject(const StreamingMemoryObject&) = delete;
79 void operator=(const StreamingMemoryObject&) = delete;
80 };
81
82 MemoryObject *getNonStreamedMemoryObject(
83 const unsigned char *Start, const unsigned char *End);
84
85 }
86 #endif // STREAMINGMEMORYOBJECT_H_
1616 #include "llvm/Support/Debug.h"
1717 #include "llvm/Support/FileSystem.h"
1818 #include "llvm/Support/Format.h"
19 #include "llvm/Support/MemoryObject.h"
2019 #include "llvm/Support/Path.h"
2120 #include "llvm/Support/raw_ostream.h"
2221 #include
4444 ConvertUTFWrapper.cpp
4545 CrashRecoveryContext.cpp
4646 DataExtractor.cpp
47 DataStream.cpp
4847 Debug.cpp
4948 DeltaAlgorithm.cpp
5049 DAGDeltaAlgorithm.cpp
6867 ManagedStatic.cpp
6968 MathExtras.cpp
7069 MemoryBuffer.cpp
71 MemoryObject.cpp
7270 MD5.cpp
7371 NativeFormatting.cpp
7472 Options.cpp
8482 SourceMgr.cpp
8583 SpecialCaseList.cpp
8684 Statistic.cpp
87 StreamingMemoryObject.cpp
8885 StringExtras.cpp
8986 StringMap.cpp
9087 StringPool.cpp
+0
-86
lib/Support/DataStream.cpp less more
None //===--- llvm/Support/DataStream.cpp - Lazy streamed data -----------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements DataStreamer, which fetches bytes of Data from
10 // a stream source. It provides support for streaming (lazy reading) of
11 // bitcode. An example implementation of streaming from a file or stdin
12 // is included.
13 //
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
22 #include
23 #if !defined(_MSC_VER) && !defined(__MINGW32__)
24 #include
25 #else
26 #include
27 #endif
28 using namespace llvm;
29
30 #define DEBUG_TYPE "Data-stream"
31
32 // Interface goals:
33 // * StreamingMemoryObject doesn't care about complexities like using
34 // threads/async callbacks to actually overlap download+compile
35 // * Don't want to duplicate Data in memory
36 // * Don't need to know total Data len in advance
37 // Non-goals:
38 // StreamingMemoryObject already has random access so this interface only does
39 // in-order streaming (no arbitrary seeking, else we'd have to buffer all the
40 // Data here in addition to MemoryObject). This also means that if we want
41 // to be able to to free Data, BitstreamBytes/BitcodeReader will implement it
42
43 STATISTIC(NumStreamFetches, "Number of calls to Data stream fetch");
44
45 namespace llvm {
46 DataStreamer::~DataStreamer() {}
47 }
48
49 namespace {
50
51 // Very simple stream backed by a file. Mostly useful for stdin and debugging;
52 // actual file access is probably still best done with mmap.
53 class DataFileStreamer : public DataStreamer {
54 int Fd;
55 public:
56 DataFileStreamer() : Fd(0) {}
57 ~DataFileStreamer() override { close(Fd); }
58 size_t GetBytes(unsigned char *buf, size_t len) override {
59 NumStreamFetches++;
60 return read(Fd, buf, len);
61 }
62
63 std::error_code OpenFile(const std::string &Filename) {
64 if (Filename == "-") {
65 Fd = 0;
66 sys::ChangeStdinToBinary();
67 return std::error_code();
68 }
69
70 return sys::fs::openFileForRead(Filename, Fd);
71 }
72 };
73
74 }
75
76 std::unique_ptr
77 llvm::getDataFileStreamer(const std::string &Filename, std::string *StrError) {
78 std::unique_ptr s = make_unique();
79 if (std::error_code e = s->OpenFile(Filename)) {
80 *StrError = std::string("Could not open ") + Filename + ": " +
81 e.message() + "\n";
82 return nullptr;
83 }
84 return std::move(s);
85 }
+0
-14
lib/Support/MemoryObject.cpp less more
None //===- MemoryObject.cpp - Abstract memory interface -----------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/Support/MemoryObject.h"
10 using namespace llvm;
11
12 MemoryObject::~MemoryObject() {
13 }
+0
-138
lib/Support/StreamingMemoryObject.cpp less more
None //===- StreamingMemoryObject.cpp - Streamable data interface -------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/Support/StreamingMemoryObject.h"
10 #include
11 #include
12 #include
13 using namespace llvm;
14
15 namespace {
16
17 class RawMemoryObject : public MemoryObject {
18 public:
19 RawMemoryObject(const unsigned char *Start, const unsigned char *End) :
20 FirstChar(Start), LastChar(End) {
21 assert(LastChar >= FirstChar && "Invalid start/end range");
22 }
23
24 uint64_t getExtent() const override {
25 return LastChar - FirstChar;
26 }
27 uint64_t readBytes(uint8_t *Buf, uint64_t Size,
28 uint64_t Address) const override;
29 const uint8_t *getPointer(uint64_t address, uint64_t size) const override;
30 bool isValidAddress(uint64_t address) const override {
31 return validAddress(address);
32 }
33
34 private:
35 const uint8_t* const FirstChar;
36 const uint8_t* const LastChar;
37
38 // These are implemented as inline functions here to avoid multiple virtual
39 // calls per public function
40 bool validAddress(uint64_t address) const {
41 return static_cast(address) < LastChar - FirstChar;
42 }
43
44 RawMemoryObject(const RawMemoryObject&) = delete;
45 void operator=(const RawMemoryObject&) = delete;
46 };
47
48 uint64_t RawMemoryObject::readBytes(uint8_t *Buf, uint64_t Size,
49 uint64_t Address) const {
50 uint64_t BufferSize = LastChar - FirstChar;
51 if (Address >= BufferSize)
52 return 0;
53
54 uint64_t End = Address + Size;
55 if (End > BufferSize)
56 End = BufferSize;
57
58 assert(static_cast(End - Address) >= 0);
59 Size = End - Address;
60 memcpy(Buf, Address + FirstChar, Size);
61 return Size;
62 }
63
64 const uint8_t *RawMemoryObject::getPointer(uint64_t address,
65 uint64_t size) const {
66 return FirstChar + address;
67 }
68 } // anonymous namespace
69
70 namespace llvm {
71 // If the bitcode has a header, then its size is known, and we don't have to
72 // block until we actually want to read it.
73 bool StreamingMemoryObject::isValidAddress(uint64_t address) const {
74 if (ObjectSize && address < ObjectSize) return true;
75 return fetchToPos(address);
76 }
77
78 uint64_t StreamingMemoryObject::getExtent() const {
79 if (ObjectSize) return ObjectSize;
80 size_t pos = BytesRead + kChunkSize;
81 // keep fetching until we run out of bytes
82 while (fetchToPos(pos)) pos += kChunkSize;
83 return ObjectSize;
84 }
85
86 uint64_t StreamingMemoryObject::readBytes(uint8_t *Buf, uint64_t Size,
87 uint64_t Address) const {
88 fetchToPos(Address + Size - 1);
89 // Note: For wrapped bitcode files will set ObjectSize after the
90 // first call to fetchToPos. In such cases, ObjectSize can be
91 // smaller than BytesRead.
92 size_t MaxAddress =
93 (ObjectSize && ObjectSize < BytesRead) ? ObjectSize : BytesRead;
94 if (Address >= MaxAddress)
95 return 0;
96
97 uint64_t End = Address + Size;
98 if (End > MaxAddress)
99 End = MaxAddress;
100 assert(End >= Address);
101 Size = End - Address;
102 memcpy(Buf, &Bytes[Address + BytesSkipped], Size);
103 return Size;
104 }
105
106 const uint8_t *StreamingMemoryObject::getPointer(uint64_t Address,
107 uint64_t Size) const {
108 fetchToPos(Address + Size - 1);
109 return &Bytes[Address + BytesSkipped];
110 }
111
112 bool StreamingMemoryObject::dropLeadingBytes(size_t s) {
113 if (BytesRead < s) return true;
114 BytesSkipped = s;
115 BytesRead -= s;
116 return false;
117 }
118
119 void StreamingMemoryObject::setKnownObjectSize(size_t size) {
120 ObjectSize = size;
121 Bytes.reserve(size);
122 if (ObjectSize <= BytesRead)
123 EOFReached = true;
124 }
125
126 MemoryObject *getNonStreamedMemoryObject(const unsigned char *Start,
127 const unsigned char *End) {
128 return new RawMemoryObject(Start, End);
129 }
130
131 StreamingMemoryObject::StreamingMemoryObject(
132 std::unique_ptr Streamer)
133 : Bytes(kChunkSize), Streamer(std::move(Streamer)), BytesRead(0),
134 BytesSkipped(0), ObjectSize(0), EOFReached(false) {
135 BytesRead = this->Streamer->GetBytes(&Bytes[0], kChunkSize);
136 }
137 }
1717 namespace llvm {
1818
1919 class MCInst;
20 class MemoryObject;
2120 class raw_ostream;
2221
2322 class AArch64Disassembler : public MCDisassembler {
2626 #include "llvm/Support/Debug.h"
2727 #include "llvm/Support/ErrorHandling.h"
2828 #include "llvm/Support/LEB128.h"
29 #include "llvm/Support/MemoryObject.h"
3029 #include "llvm/Support/raw_ostream.h"
3130 #include "llvm/Support/TargetRegistry.h"
3231
1818 #include "llvm/MC/MCInst.h"
1919 #include "llvm/MC/MCSubtargetInfo.h"
2020 #include "llvm/Support/MathExtras.h"
21 #include "llvm/Support/MemoryObject.h"
2221 #include "llvm/Support/TargetRegistry.h"
2322
2423 using namespace llvm;
3737 ScaledNumberTest.cpp
3838 SourceMgrTest.cpp
3939 SpecialCaseListTest.cpp
40 StreamingMemoryObjectTest.cpp
4140 StringPool.cpp
4241 SwapByteOrderTest.cpp
4342 TargetParserTest.cpp
+0
-68
unittests/Support/StreamingMemoryObjectTest.cpp less more
None //===- unittests/Support/StreamingMemoryObjectTest.cpp --------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/ADT/STLExtras.h"
10 #include "llvm/ADT/StringRef.h"
11 #include "llvm/Support/StreamingMemoryObject.h"
12 #include "gtest/gtest.h"
13 #include
14
15 using namespace llvm;
16
17 namespace {
18
19 class NullDataStreamer : public DataStreamer {
20 size_t GetBytes(unsigned char *Buffer, size_t Length) override {
21 memset(Buffer, 0, Length);
22 return Length;
23 }
24 };
25
26 class BufferStreamer : public DataStreamer {
27 StringRef Buffer;
28
29 public:
30 BufferStreamer(StringRef Buffer) : Buffer(Buffer) {}
31 size_t GetBytes(unsigned char *OutBuffer, size_t Length) override {
32 if (Length >= Buffer.size())
33 Length = Buffer.size();
34
35 std::copy(Buffer.begin(), Buffer.begin() + Length, OutBuffer);
36 Buffer = Buffer.drop_front(Length);
37 return Length;
38 }
39 };
40
41 TEST(StreamingMemoryObjectTest, isValidAddress) {
42 auto DS = make_unique();
43 StreamingMemoryObject O(std::move(DS));
44 EXPECT_TRUE(O.isValidAddress(32 * 1024));
45 }
46
47 TEST(StreamingMemoryObjectTest, setKnownObjectSize) {
48 auto DS = make_unique();
49 StreamingMemoryObject O(std::move(DS));
50 uint8_t Buf[32];
51 EXPECT_EQ(16u, O.readBytes(Buf, 16, 0));
52 O.setKnownObjectSize(24);
53 EXPECT_EQ(8u, O.readBytes(Buf, 16, 16));
54 }
55
56 TEST(StreamingMemoryObjectTest, getPointer) {
57 uint8_t InputBuffer[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
58 StreamingMemoryObject O(make_unique(StringRef(
59 reinterpret_cast(InputBuffer), sizeof(InputBuffer))));
60
61 EXPECT_TRUE(std::equal(InputBuffer + 1, InputBuffer + 2, O.getPointer(1, 2)));
62 EXPECT_TRUE(std::equal(InputBuffer + 3, InputBuffer + 7, O.getPointer(3, 4)));
63 EXPECT_TRUE(std::equal(InputBuffer + 4, InputBuffer + 8, O.getPointer(4, 5)));
64 EXPECT_TRUE(std::equal(InputBuffer, InputBuffer + 8, O.getPointer(0, 20)));
65 }
66
67 } // end namespace