1 //===- StreamingMemoryObject.cpp - Streamable data interface -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Support/StreamingMemoryObject.h"
18 class RawMemoryObject : public MemoryObject {
20 RawMemoryObject(const unsigned char *Start, const unsigned char *End) :
21 FirstChar(Start), LastChar(End) {
22 assert(LastChar >= FirstChar && "Invalid start/end range");
25 uint64_t getExtent() const override {
26 return LastChar - FirstChar;
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 return validAddress(address);
36 const uint8_t* const FirstChar;
37 const uint8_t* const LastChar;
39 // These are implemented as inline functions here to avoid multiple virtual
40 // calls per public function
41 bool validAddress(uint64_t address) const {
42 return static_cast<std::ptrdiff_t>(address) < LastChar - FirstChar;
45 RawMemoryObject(const RawMemoryObject&) = delete;
46 void operator=(const RawMemoryObject&) = delete;
49 uint64_t RawMemoryObject::readBytes(uint8_t *Buf, uint64_t Size,
50 uint64_t Address) const {
51 uint64_t BufferSize = LastChar - FirstChar;
52 if (Address >= BufferSize)
55 uint64_t End = Address + Size;
59 assert(static_cast<int64_t>(End - Address) >= 0);
61 memcpy(Buf, Address + FirstChar, Size);
65 const uint8_t *RawMemoryObject::getPointer(uint64_t address,
66 uint64_t size) const {
67 return FirstChar + address;
69 } // anonymous namespace
72 // If the bitcode has a header, then its size is known, and we don't have to
73 // block until we actually want to read it.
74 bool StreamingMemoryObject::isValidAddress(uint64_t address) const {
75 if (ObjectSize && address < ObjectSize) return true;
76 return fetchToPos(address);
79 uint64_t StreamingMemoryObject::getExtent() const {
80 if (ObjectSize) return ObjectSize;
81 size_t pos = BytesRead + kChunkSize;
82 // keep fetching until we run out of bytes
83 while (fetchToPos(pos)) pos += kChunkSize;
87 uint64_t StreamingMemoryObject::readBytes(uint8_t *Buf, uint64_t Size,
88 uint64_t Address) const {
89 fetchToPos(Address + Size - 1);
90 // Note: For wrapped bitcode files will set ObjectSize after the
91 // first call to fetchToPos. In such cases, ObjectSize can be
92 // smaller than BytesRead.
94 (ObjectSize && ObjectSize < BytesRead) ? ObjectSize : BytesRead;
95 if (Address >= MaxAddress)
98 uint64_t End = Address + Size;
101 assert(End >= Address);
102 Size = End - Address;
103 memcpy(Buf, &Bytes[Address + BytesSkipped], Size);
107 bool StreamingMemoryObject::dropLeadingBytes(size_t s) {
108 if (BytesRead < s) return true;
114 void StreamingMemoryObject::setKnownObjectSize(size_t size) {
117 if (ObjectSize <= BytesRead)
121 MemoryObject *getNonStreamedMemoryObject(const unsigned char *Start,
122 const unsigned char *End) {
123 return new RawMemoryObject(Start, End);
126 StreamingMemoryObject::StreamingMemoryObject(
127 std::unique_ptr<DataStreamer> Streamer)
128 : Bytes(kChunkSize), Streamer(std::move(Streamer)), BytesRead(0),
129 BytesSkipped(0), ObjectSize(0), EOFReached(false) {
130 BytesRead = this->Streamer->GetBytes(&Bytes[0], kChunkSize);