1 //===- StreamableMemoryObject.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/StreamableMemoryObject.h"
19 class RawMemoryObject : public StreamableMemoryObject {
21 RawMemoryObject(const unsigned char *Start, const unsigned char *End) :
22 FirstChar(Start), LastChar(End) {
23 assert(LastChar > FirstChar && "Invalid start/end range");
26 virtual uint64_t getBase() const { return 0; }
27 virtual uint64_t getExtent() { return LastChar - FirstChar; }
28 virtual int readByte(uint64_t address, uint8_t* ptr);
29 virtual int readBytes(uint64_t address,
33 virtual const uint8_t *getPointer(uint64_t address, uint64_t size);
34 virtual bool isValidAddress(uint64_t address) {return validAddress(address);}
35 virtual bool isObjectEnd(uint64_t address) {return objectEnd(address);}
38 const uint8_t* const FirstChar;
39 const uint8_t* const LastChar;
41 // These are implemented as inline functions here to avoid multiple virtual
42 // calls per public function
43 bool validAddress(uint64_t address) {
44 return static_cast<ptrdiff_t>(address) < LastChar - FirstChar;
46 bool objectEnd(uint64_t address) {
47 return static_cast<ptrdiff_t>(address) == LastChar - FirstChar;
50 RawMemoryObject(const RawMemoryObject&); // DO NOT IMPLEMENT
51 void operator=(const RawMemoryObject&); // DO NOT IMPLEMENT
54 int RawMemoryObject::readByte(uint64_t address, uint8_t* ptr) {
55 if (!validAddress(address)) return -1;
56 *ptr = *((uint8_t *)(uintptr_t)(address + FirstChar));
60 int RawMemoryObject::readBytes(uint64_t address,
64 if (!validAddress(address) || !validAddress(address + size - 1)) return -1;
65 memcpy(buf, (uint8_t *)(uintptr_t)(address + FirstChar), size);
66 if (copied) *copied = size;
70 const uint8_t *RawMemoryObject::getPointer(uint64_t address, uint64_t size) {
71 return FirstChar + address;
73 } // anonymous namespace
76 // If the bitcode has a header, then its size is known, and we don't have to
77 // block until we actually want to read it.
78 bool StreamingMemoryObject::isValidAddress(uint64_t address) {
79 if (ObjectSize && address < ObjectSize) return true;
80 return fetchToPos(address);
83 bool StreamingMemoryObject::isObjectEnd(uint64_t address) {
84 if (ObjectSize) return address == ObjectSize;
86 return address == ObjectSize && address != 0;
89 uint64_t StreamingMemoryObject::getExtent() {
90 if (ObjectSize) return ObjectSize;
91 size_t pos = BytesRead + kChunkSize;
92 // keep fetching until we run out of bytes
93 while (fetchToPos(pos)) pos += kChunkSize;
97 int StreamingMemoryObject::readByte(uint64_t address, uint8_t* ptr) {
98 if (!fetchToPos(address)) return -1;
99 *ptr = Bytes[address + BytesSkipped];
103 int StreamingMemoryObject::readBytes(uint64_t address,
107 if (!fetchToPos(address + size - 1)) return -1;
108 memcpy(buf, &Bytes[address + BytesSkipped], size);
109 if (copied) *copied = size;
113 bool StreamingMemoryObject::dropLeadingBytes(size_t s) {
114 if (BytesRead < s) return true;
120 void StreamingMemoryObject::setKnownObjectSize(size_t size) {
125 StreamableMemoryObject *getNonStreamedMemoryObject(
126 const unsigned char *Start, const unsigned char *End) {
127 return new RawMemoryObject(Start, End);
130 StreamableMemoryObject::~StreamableMemoryObject() { }
132 StreamingMemoryObject::StreamingMemoryObject(DataStreamer *streamer) :
133 Bytes(kChunkSize), Streamer(streamer), BytesRead(0), BytesSkipped(0),
134 ObjectSize(0), EOFReached(false) {
135 BytesRead = streamer->GetBytes(&Bytes[0], kChunkSize);