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"
11 #include "llvm/Support/Compiler.h"
21 class RawMemoryObject : public MemoryObject {
23 RawMemoryObject(const unsigned char *Start, const unsigned char *End) :
24 FirstChar(Start), LastChar(End) {
25 assert(LastChar >= FirstChar && "Invalid start/end range");
28 uint64_t getExtent() const override {
29 return LastChar - FirstChar;
31 uint64_t readBytes(uint8_t *Buf, uint64_t Size,
32 uint64_t Address) const override;
33 const uint8_t *getPointer(uint64_t address, uint64_t size) const override;
34 bool isValidAddress(uint64_t address) const override {
35 return validAddress(address);
37 bool isObjectEnd(uint64_t address) const override {
38 return objectEnd(address);
42 const uint8_t* const FirstChar;
43 const uint8_t* const LastChar;
45 // These are implemented as inline functions here to avoid multiple virtual
46 // calls per public function
47 bool validAddress(uint64_t address) const {
48 return static_cast<std::ptrdiff_t>(address) < LastChar - FirstChar;
50 bool objectEnd(uint64_t address) const {
51 return static_cast<std::ptrdiff_t>(address) == LastChar - FirstChar;
54 RawMemoryObject(const RawMemoryObject&) LLVM_DELETED_FUNCTION;
55 void operator=(const RawMemoryObject&) LLVM_DELETED_FUNCTION;
58 uint64_t RawMemoryObject::readBytes(uint8_t *Buf, uint64_t Size,
59 uint64_t Address) const {
60 uint64_t BufferSize = LastChar - FirstChar;
61 if (Address >= BufferSize)
64 uint64_t End = Address + Size;
70 memcpy(Buf, (uint8_t *)(Address + FirstChar), Size);
74 const uint8_t *RawMemoryObject::getPointer(uint64_t address,
75 uint64_t size) const {
76 return FirstChar + address;
78 } // anonymous namespace
81 // If the bitcode has a header, then its size is known, and we don't have to
82 // block until we actually want to read it.
83 bool StreamingMemoryObject::isValidAddress(uint64_t address) const {
84 if (ObjectSize && address < ObjectSize) return true;
85 return fetchToPos(address);
88 bool StreamingMemoryObject::isObjectEnd(uint64_t address) const {
89 if (ObjectSize) return address == ObjectSize;
91 return address == ObjectSize && address != 0;
94 uint64_t StreamingMemoryObject::getExtent() const {
95 if (ObjectSize) return ObjectSize;
96 size_t pos = BytesRead + kChunkSize;
97 // keep fetching until we run out of bytes
98 while (fetchToPos(pos)) pos += kChunkSize;
102 uint64_t StreamingMemoryObject::readBytes(uint8_t *Buf, uint64_t Size,
103 uint64_t Address) const {
104 fetchToPos(Address + Size - 1);
105 uint64_t BufferSize = Bytes.size() - BytesSkipped;
106 if (Address >= BufferSize)
109 uint64_t End = Address + Size;
110 if (End > BufferSize)
112 Size = End - Address;
114 memcpy(Buf, &Bytes[Address + BytesSkipped], Size);
118 bool StreamingMemoryObject::dropLeadingBytes(size_t s) {
119 if (BytesRead < s) return true;
125 void StreamingMemoryObject::setKnownObjectSize(size_t size) {
130 MemoryObject *getNonStreamedMemoryObject(const unsigned char *Start,
131 const unsigned char *End) {
132 return new RawMemoryObject(Start, End);
135 StreamingMemoryObject::StreamingMemoryObject(DataStreamer *streamer) :
136 Bytes(kChunkSize), Streamer(streamer), BytesRead(0), BytesSkipped(0),
137 ObjectSize(0), EOFReached(false) {
138 BytesRead = streamer->GetBytes(&Bytes[0], kChunkSize);