1 //===- StreamableMemoryObject.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/StreamableMemoryObject.h"
11 #include "llvm/Support/Compiler.h"
12 #include <cassert>
13 #include <cstring>
14
15
16 using namespace llvm;
17
18 namespace {
19
20 class RawMemoryObject : public StreamableMemoryObject {
21 public:
RawMemoryObject(const unsigned char * Start,const unsigned char * End)22 RawMemoryObject(const unsigned char *Start, const unsigned char *End) :
23 FirstChar(Start), LastChar(End) {
24 assert(LastChar >= FirstChar && "Invalid start/end range");
25 }
26
getBase() const27 virtual uint64_t getBase() const LLVM_OVERRIDE { return 0; }
getExtent() const28 virtual uint64_t getExtent() const LLVM_OVERRIDE {
29 return LastChar - FirstChar;
30 }
31 virtual int readByte(uint64_t address, uint8_t* ptr) const LLVM_OVERRIDE;
32 virtual int readBytes(uint64_t address,
33 uint64_t size,
34 uint8_t *buf) const LLVM_OVERRIDE;
35 virtual const uint8_t *getPointer(uint64_t address,
36 uint64_t size) const LLVM_OVERRIDE;
isValidAddress(uint64_t address) const37 virtual bool isValidAddress(uint64_t address) const LLVM_OVERRIDE {
38 return validAddress(address);
39 }
isObjectEnd(uint64_t address) const40 virtual bool isObjectEnd(uint64_t address) const LLVM_OVERRIDE {
41 return objectEnd(address);
42 }
43
44 private:
45 const uint8_t* const FirstChar;
46 const uint8_t* const LastChar;
47
48 // These are implemented as inline functions here to avoid multiple virtual
49 // calls per public function
validAddress(uint64_t address) const50 bool validAddress(uint64_t address) const {
51 return static_cast<ptrdiff_t>(address) < LastChar - FirstChar;
52 }
objectEnd(uint64_t address) const53 bool objectEnd(uint64_t address) const {
54 return static_cast<ptrdiff_t>(address) == LastChar - FirstChar;
55 }
56
57 RawMemoryObject(const RawMemoryObject&) LLVM_DELETED_FUNCTION;
58 void operator=(const RawMemoryObject&) LLVM_DELETED_FUNCTION;
59 };
60
readByte(uint64_t address,uint8_t * ptr) const61 int RawMemoryObject::readByte(uint64_t address, uint8_t* ptr) const {
62 if (!validAddress(address)) return -1;
63 *ptr = *((uint8_t *)(uintptr_t)(address + FirstChar));
64 return 0;
65 }
66
readBytes(uint64_t address,uint64_t size,uint8_t * buf) const67 int RawMemoryObject::readBytes(uint64_t address,
68 uint64_t size,
69 uint8_t *buf) const {
70 if (!validAddress(address) || !validAddress(address + size - 1)) return -1;
71 memcpy(buf, (uint8_t *)(uintptr_t)(address + FirstChar), size);
72 return size;
73 }
74
getPointer(uint64_t address,uint64_t size) const75 const uint8_t *RawMemoryObject::getPointer(uint64_t address,
76 uint64_t size) const {
77 return FirstChar + address;
78 }
79 } // anonymous namespace
80
81 namespace llvm {
82 // If the bitcode has a header, then its size is known, and we don't have to
83 // block until we actually want to read it.
isValidAddress(uint64_t address) const84 bool StreamingMemoryObject::isValidAddress(uint64_t address) const {
85 if (ObjectSize && address < ObjectSize) return true;
86 return fetchToPos(address);
87 }
88
isObjectEnd(uint64_t address) const89 bool StreamingMemoryObject::isObjectEnd(uint64_t address) const {
90 if (ObjectSize) return address == ObjectSize;
91 fetchToPos(address);
92 return address == ObjectSize && address != 0;
93 }
94
getExtent() const95 uint64_t StreamingMemoryObject::getExtent() const {
96 if (ObjectSize) return ObjectSize;
97 size_t pos = BytesRead + kChunkSize;
98 // keep fetching until we run out of bytes
99 while (fetchToPos(pos)) pos += kChunkSize;
100 return ObjectSize;
101 }
102
readByte(uint64_t address,uint8_t * ptr) const103 int StreamingMemoryObject::readByte(uint64_t address, uint8_t* ptr) const {
104 if (!fetchToPos(address)) return -1;
105 *ptr = Bytes[address + BytesSkipped];
106 return 0;
107 }
108
readBytes(uint64_t address,uint64_t size,uint8_t * buf) const109 int StreamingMemoryObject::readBytes(uint64_t address,
110 uint64_t size,
111 uint8_t *buf) const {
112 if (!fetchToPos(address + size - 1)) return -1;
113 memcpy(buf, &Bytes[address + BytesSkipped], size);
114 return 0;
115 }
116
dropLeadingBytes(size_t s)117 bool StreamingMemoryObject::dropLeadingBytes(size_t s) {
118 if (BytesRead < s) return true;
119 BytesSkipped = s;
120 BytesRead -= s;
121 return false;
122 }
123
setKnownObjectSize(size_t size)124 void StreamingMemoryObject::setKnownObjectSize(size_t size) {
125 ObjectSize = size;
126 Bytes.reserve(size);
127 }
128
getNonStreamedMemoryObject(const unsigned char * Start,const unsigned char * End)129 StreamableMemoryObject *getNonStreamedMemoryObject(
130 const unsigned char *Start, const unsigned char *End) {
131 return new RawMemoryObject(Start, End);
132 }
133
~StreamableMemoryObject()134 StreamableMemoryObject::~StreamableMemoryObject() { }
135
StreamingMemoryObject(DataStreamer * streamer)136 StreamingMemoryObject::StreamingMemoryObject(DataStreamer *streamer) :
137 Bytes(kChunkSize), Streamer(streamer), BytesRead(0), BytesSkipped(0),
138 ObjectSize(0), EOFReached(false) {
139 BytesRead = streamer->GetBytes(&Bytes[0], kChunkSize);
140 }
141 }
142