1 //===- 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> 14 using namespace llvm; 15 16 namespace { 17 18 class RawMemoryObject : public MemoryObject { 19 public: 20 RawMemoryObject(const unsigned char *Start, const unsigned char *End) : 21 FirstChar(Start), LastChar(End) { 22 assert(LastChar >= FirstChar && "Invalid start/end range"); 23 } 24 25 uint64_t getExtent() const override { 26 return LastChar - FirstChar; 27 } 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); 33 } 34 35 private: 36 const uint8_t* const FirstChar; 37 const uint8_t* const LastChar; 38 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; 43 } 44 45 RawMemoryObject(const RawMemoryObject&) = delete; 46 void operator=(const RawMemoryObject&) = delete; 47 }; 48 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) 53 return 0; 54 55 uint64_t End = Address + Size; 56 if (End > BufferSize) 57 End = BufferSize; 58 59 assert(static_cast<int64_t>(End - Address) >= 0); 60 Size = End - Address; 61 memcpy(Buf, Address + FirstChar, Size); 62 return Size; 63 } 64 65 const uint8_t *RawMemoryObject::getPointer(uint64_t address, 66 uint64_t size) const { 67 return FirstChar + address; 68 } 69 } // anonymous namespace 70 71 namespace llvm { 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); 77 } 78 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; 84 return ObjectSize; 85 } 86 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) 91 return 0; 92 93 uint64_t End = Address + Size; 94 if (End > BytesRead) 95 End = BytesRead; 96 assert(static_cast<int64_t>(End - Address) >= 0); 97 Size = End - Address; 98 memcpy(Buf, &Bytes[Address + BytesSkipped], Size); 99 return Size; 100 } 101 102 bool StreamingMemoryObject::dropLeadingBytes(size_t s) { 103 if (BytesRead < s) return true; 104 BytesSkipped = s; 105 BytesRead -= s; 106 return false; 107 } 108 109 void StreamingMemoryObject::setKnownObjectSize(size_t size) { 110 ObjectSize = size; 111 Bytes.reserve(size); 112 } 113 114 MemoryObject *getNonStreamedMemoryObject(const unsigned char *Start, 115 const unsigned char *End) { 116 return new RawMemoryObject(Start, End); 117 } 118 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); 123 } 124 } 125