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 if (Address >= BytesRead)
93 uint64_t End = Address + Size;
96 assert(static_cast<int64_t>(End - Address) >= 0);
98 memcpy(Buf, &Bytes[Address + BytesSkipped], Size);
102 bool StreamingMemoryObject::dropLeadingBytes(size_t s) {
103 if (BytesRead < s) return true;
109 void StreamingMemoryObject::setKnownObjectSize(size_t size) {
114 MemoryObject *getNonStreamedMemoryObject(const unsigned char *Start,
115 const unsigned char *End) {
116 return new RawMemoryObject(Start, End);
119 StreamingMemoryObject::StreamingMemoryObject(DataStreamer *streamer) :
120 Bytes(kChunkSize), Streamer(streamer), BytesRead(0), BytesSkipped(0),
121 ObjectSize(0), EOFReached(false) {
122 BytesRead = streamer->GetBytes(&Bytes[0], kChunkSize);