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 16using namespace llvm; 17 18namespace { 19 20class RawMemoryObject : public StreamableMemoryObject { 21public: 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 27 virtual uint64_t getBase() const LLVM_OVERRIDE { return 0; } 28 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, 35 uint64_t* copied) const LLVM_OVERRIDE; 36 virtual const uint8_t *getPointer(uint64_t address, 37 uint64_t size) const LLVM_OVERRIDE; 38 virtual bool isValidAddress(uint64_t address) const LLVM_OVERRIDE { 39 return validAddress(address); 40 } 41 virtual bool isObjectEnd(uint64_t address) const LLVM_OVERRIDE { 42 return objectEnd(address); 43 } 44 45private: 46 const uint8_t* const FirstChar; 47 const uint8_t* const LastChar; 48 49 // These are implemented as inline functions here to avoid multiple virtual 50 // calls per public function 51 bool validAddress(uint64_t address) const { 52 return static_cast<ptrdiff_t>(address) < LastChar - FirstChar; 53 } 54 bool objectEnd(uint64_t address) const { 55 return static_cast<ptrdiff_t>(address) == LastChar - FirstChar; 56 } 57 58 RawMemoryObject(const RawMemoryObject&) LLVM_DELETED_FUNCTION; 59 void operator=(const RawMemoryObject&) LLVM_DELETED_FUNCTION; 60}; 61 62int RawMemoryObject::readByte(uint64_t address, uint8_t* ptr) const { 63 if (!validAddress(address)) return -1; 64 *ptr = *((uint8_t *)(uintptr_t)(address + FirstChar)); 65 return 0; 66} 67 68int RawMemoryObject::readBytes(uint64_t address, 69 uint64_t size, 70 uint8_t* buf, 71 uint64_t* copied) const { 72 if (!validAddress(address) || !validAddress(address + size - 1)) return -1; 73 memcpy(buf, (uint8_t *)(uintptr_t)(address + FirstChar), size); 74 if (copied) *copied = size; 75 return size; 76} 77 78const uint8_t *RawMemoryObject::getPointer(uint64_t address, 79 uint64_t size) const { 80 return FirstChar + address; 81} 82} // anonymous namespace 83 84namespace llvm { 85// If the bitcode has a header, then its size is known, and we don't have to 86// block until we actually want to read it. 87bool StreamingMemoryObject::isValidAddress(uint64_t address) const { 88 if (ObjectSize && address < ObjectSize) return true; 89 return fetchToPos(address); 90} 91 92bool StreamingMemoryObject::isObjectEnd(uint64_t address) const { 93 if (ObjectSize) return address == ObjectSize; 94 fetchToPos(address); 95 return address == ObjectSize && address != 0; 96} 97 98uint64_t StreamingMemoryObject::getExtent() const { 99 if (ObjectSize) return ObjectSize; 100 size_t pos = BytesRead + kChunkSize; 101 // keep fetching until we run out of bytes 102 while (fetchToPos(pos)) pos += kChunkSize; 103 return ObjectSize; 104} 105 106int StreamingMemoryObject::readByte(uint64_t address, uint8_t* ptr) const { 107 if (!fetchToPos(address)) return -1; 108 *ptr = Bytes[address + BytesSkipped]; 109 return 0; 110} 111 112int StreamingMemoryObject::readBytes(uint64_t address, 113 uint64_t size, 114 uint8_t* buf, 115 uint64_t* copied) const { 116 if (!fetchToPos(address + size - 1)) return -1; 117 memcpy(buf, &Bytes[address + BytesSkipped], size); 118 if (copied) *copied = size; 119 return 0; 120} 121 122bool StreamingMemoryObject::dropLeadingBytes(size_t s) { 123 if (BytesRead < s) return true; 124 BytesSkipped = s; 125 BytesRead -= s; 126 return false; 127} 128 129void StreamingMemoryObject::setKnownObjectSize(size_t size) { 130 ObjectSize = size; 131 Bytes.reserve(size); 132} 133 134StreamableMemoryObject *getNonStreamedMemoryObject( 135 const unsigned char *Start, const unsigned char *End) { 136 return new RawMemoryObject(Start, End); 137} 138 139StreamableMemoryObject::~StreamableMemoryObject() { } 140 141StreamingMemoryObject::StreamingMemoryObject(DataStreamer *streamer) : 142 Bytes(kChunkSize), Streamer(streamer), BytesRead(0), BytesSkipped(0), 143 ObjectSize(0), EOFReached(false) { 144 BytesRead = streamer->GetBytes(&Bytes[0], kChunkSize); 145} 146} 147