blob: 68874e24d95b1948251c72c98ce3158b09ec1d60 [file] [log] [blame]
Jim Stichnothd722ff22016-12-26 20:10:07 -08001//===- StreamingMemoryObject.cpp - Streamable data interface -------------===//
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#include "llvm/Support/StreamingMemoryObject.h"
11#include <cassert>
12#include <cstddef>
13#include <cstring>
14using namespace llvm;
15
16namespace {
17
18class RawMemoryObject : public MemoryObject {
19public:
Antonio Maioranoca8a16e2020-11-10 16:56:20 -050020 RawMemoryObject(const unsigned char *Start, const unsigned char *End)
21 : FirstChar(Start), LastChar(End) {
Jim Stichnothd722ff22016-12-26 20:10:07 -080022 assert(LastChar >= FirstChar && "Invalid start/end range");
23 }
24
Antonio Maioranoca8a16e2020-11-10 16:56:20 -050025 uint64_t getExtent() const override { return LastChar - FirstChar; }
Jim Stichnothd722ff22016-12-26 20:10:07 -080026 uint64_t readBytes(uint8_t *Buf, uint64_t Size,
27 uint64_t Address) const override;
28 const uint8_t *getPointer(uint64_t address, uint64_t size) const override;
29 bool isValidAddress(uint64_t address) const override {
30 return validAddress(address);
31 }
32
33private:
Antonio Maioranoca8a16e2020-11-10 16:56:20 -050034 const uint8_t *const FirstChar;
35 const uint8_t *const LastChar;
Jim Stichnothd722ff22016-12-26 20:10:07 -080036
37 // These are implemented as inline functions here to avoid multiple virtual
38 // calls per public function
39 bool validAddress(uint64_t address) const {
40 return static_cast<std::ptrdiff_t>(address) < LastChar - FirstChar;
41 }
42
Antonio Maioranoca8a16e2020-11-10 16:56:20 -050043 RawMemoryObject(const RawMemoryObject &) = delete;
44 void operator=(const RawMemoryObject &) = delete;
Jim Stichnothd722ff22016-12-26 20:10:07 -080045};
46
47uint64_t RawMemoryObject::readBytes(uint8_t *Buf, uint64_t Size,
48 uint64_t Address) const {
49 uint64_t BufferSize = LastChar - FirstChar;
50 if (Address >= BufferSize)
51 return 0;
52
53 uint64_t End = Address + Size;
54 if (End > BufferSize)
55 End = BufferSize;
56
57 assert(static_cast<int64_t>(End - Address) >= 0);
58 Size = End - Address;
59 memcpy(Buf, Address + FirstChar, Size);
60 return Size;
61}
62
63const uint8_t *RawMemoryObject::getPointer(uint64_t address,
64 uint64_t size) const {
65 return FirstChar + address;
66}
67} // anonymous namespace
68
69namespace llvm {
70// If the bitcode has a header, then its size is known, and we don't have to
71// block until we actually want to read it.
72bool StreamingMemoryObject::isValidAddress(uint64_t address) const {
Antonio Maioranoca8a16e2020-11-10 16:56:20 -050073 if (ObjectSize && address < ObjectSize)
74 return true;
Jim Stichnothd722ff22016-12-26 20:10:07 -080075 return fetchToPos(address);
76}
77
78uint64_t StreamingMemoryObject::getExtent() const {
Antonio Maioranoca8a16e2020-11-10 16:56:20 -050079 if (ObjectSize)
80 return ObjectSize;
Jim Stichnothd722ff22016-12-26 20:10:07 -080081 size_t pos = BytesRead + kChunkSize;
82 // keep fetching until we run out of bytes
Antonio Maioranoca8a16e2020-11-10 16:56:20 -050083 while (fetchToPos(pos))
84 pos += kChunkSize;
Jim Stichnothd722ff22016-12-26 20:10:07 -080085 return ObjectSize;
86}
87
88uint64_t StreamingMemoryObject::readBytes(uint8_t *Buf, uint64_t Size,
89 uint64_t Address) const {
90 fetchToPos(Address + Size - 1);
91 // Note: For wrapped bitcode files will set ObjectSize after the
92 // first call to fetchToPos. In such cases, ObjectSize can be
93 // smaller than BytesRead.
94 size_t MaxAddress =
95 (ObjectSize && ObjectSize < BytesRead) ? ObjectSize : BytesRead;
96 if (Address >= MaxAddress)
97 return 0;
98
99 uint64_t End = Address + Size;
100 if (End > MaxAddress)
101 End = MaxAddress;
102 assert(End >= Address);
103 Size = End - Address;
104 memcpy(Buf, &Bytes[Address + BytesSkipped], Size);
105 return Size;
106}
107
108const uint8_t *StreamingMemoryObject::getPointer(uint64_t Address,
109 uint64_t Size) const {
110 fetchToPos(Address + Size - 1);
111 return &Bytes[Address + BytesSkipped];
112}
113
114bool StreamingMemoryObject::dropLeadingBytes(size_t s) {
Antonio Maioranoca8a16e2020-11-10 16:56:20 -0500115 if (BytesRead < s)
116 return true;
Jim Stichnothd722ff22016-12-26 20:10:07 -0800117 BytesSkipped = s;
118 BytesRead -= s;
119 return false;
120}
121
122void StreamingMemoryObject::setKnownObjectSize(size_t size) {
123 ObjectSize = size;
124 Bytes.reserve(size);
125 if (ObjectSize <= BytesRead)
126 EOFReached = true;
127}
128
129MemoryObject *getNonStreamedMemoryObject(const unsigned char *Start,
130 const unsigned char *End) {
131 return new RawMemoryObject(Start, End);
132}
133
134StreamingMemoryObject::StreamingMemoryObject(
135 std::unique_ptr<DataStreamer> Streamer)
136 : Bytes(kChunkSize), Streamer(std::move(Streamer)), BytesRead(0),
137 BytesSkipped(0), ObjectSize(0), EOFReached(false) {
138 BytesRead = this->Streamer->GetBytes(&Bytes[0], kChunkSize);
139}
Antonio Maioranoca8a16e2020-11-10 16:56:20 -0500140} // namespace llvm