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);
39 const uint8_t* const FirstChar;
40 const uint8_t* const LastChar;
42 // These are implemented as inline functions here to avoid multiple virtual
43 // calls per public function
44 bool validAddress(uint64_t address) const {
45 return static_cast<std::ptrdiff_t>(address) < LastChar - FirstChar;
48 RawMemoryObject(const RawMemoryObject&) = delete;
49 void operator=(const RawMemoryObject&) = delete;
52 uint64_t RawMemoryObject::readBytes(uint8_t *Buf, uint64_t Size,
53 uint64_t Address) const {
54 uint64_t BufferSize = LastChar - FirstChar;
55 if (Address >= BufferSize)
58 uint64_t End = Address + Size;
62 assert(static_cast<int64_t>(End - Address) >= 0);
64 memcpy(Buf, Address + FirstChar, Size);
68 const uint8_t *RawMemoryObject::getPointer(uint64_t address,
69 uint64_t size) const {
70 return FirstChar + address;
72 } // anonymous namespace
75 // If the bitcode has a header, then its size is known, and we don't have to
76 // block until we actually want to read it.
77 bool StreamingMemoryObject::isValidAddress(uint64_t address) const {
78 if (ObjectSize && address < ObjectSize) return true;
79 return fetchToPos(address);
82 uint64_t StreamingMemoryObject::getExtent() const {
83 if (ObjectSize) return ObjectSize;
84 size_t pos = BytesRead + kChunkSize;
85 // keep fetching until we run out of bytes
86 while (fetchToPos(pos)) pos += kChunkSize;
90 uint64_t StreamingMemoryObject::readBytes(uint8_t *Buf, uint64_t Size,
91 uint64_t Address) const {
92 fetchToPos(Address + Size - 1);
93 if (Address >= BytesRead)
96 uint64_t End = Address + Size;
99 assert(static_cast<int64_t>(End - Address) >= 0);
100 Size = End - Address;
101 memcpy(Buf, &Bytes[Address + BytesSkipped], Size);
105 bool StreamingMemoryObject::dropLeadingBytes(size_t s) {
106 if (BytesRead < s) return true;
112 void StreamingMemoryObject::setKnownObjectSize(size_t size) {
117 MemoryObject *getNonStreamedMemoryObject(const unsigned char *Start,
118 const unsigned char *End) {
119 return new RawMemoryObject(Start, End);
122 StreamingMemoryObject::StreamingMemoryObject(DataStreamer *streamer) :
123 Bytes(kChunkSize), Streamer(streamer), BytesRead(0), BytesSkipped(0),
124 ObjectSize(0), EOFReached(false) {
125 BytesRead = streamer->GetBytes(&Bytes[0], kChunkSize);