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"
11 #include "llvm/Support/Compiler.h"
21 class RawMemoryObject : public StreamableMemoryObject {
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 getBase() const override { return 0; }
29 uint64_t getExtent() const override {
30 return LastChar - FirstChar;
32 int readByte(uint64_t address, uint8_t* ptr) const override;
33 int readBytes(uint64_t address, uint64_t size,
34 uint8_t *buf) const override;
35 const uint8_t *getPointer(uint64_t address, uint64_t size) const override;
36 bool isValidAddress(uint64_t address) const override {
37 return validAddress(address);
39 bool isObjectEnd(uint64_t address) const override {
40 return objectEnd(address);
44 const uint8_t* const FirstChar;
45 const uint8_t* const LastChar;
47 // These are implemented as inline functions here to avoid multiple virtual
48 // calls per public function
49 bool validAddress(uint64_t address) const {
50 return static_cast<std::ptrdiff_t>(address) < LastChar - FirstChar;
52 bool objectEnd(uint64_t address) const {
53 return static_cast<std::ptrdiff_t>(address) == LastChar - FirstChar;
56 RawMemoryObject(const RawMemoryObject&) LLVM_DELETED_FUNCTION;
57 void operator=(const RawMemoryObject&) LLVM_DELETED_FUNCTION;
60 int RawMemoryObject::readByte(uint64_t address, uint8_t* ptr) const {
61 if (!validAddress(address)) return -1;
62 *ptr = *((uint8_t *)(uintptr_t)(address + FirstChar));
66 int RawMemoryObject::readBytes(uint64_t address,
69 if (!validAddress(address) || !validAddress(address + size - 1)) return -1;
70 memcpy(buf, (uint8_t *)(uintptr_t)(address + FirstChar), size);
74 const uint8_t *RawMemoryObject::getPointer(uint64_t address,
75 uint64_t size) const {
76 return FirstChar + address;
78 } // anonymous namespace
81 // If the bitcode has a header, then its size is known, and we don't have to
82 // block until we actually want to read it.
83 bool StreamingMemoryObject::isValidAddress(uint64_t address) const {
84 if (ObjectSize && address < ObjectSize) return true;
85 return fetchToPos(address);
88 bool StreamingMemoryObject::isObjectEnd(uint64_t address) const {
89 if (ObjectSize) return address == ObjectSize;
91 return address == ObjectSize && address != 0;
94 uint64_t StreamingMemoryObject::getExtent() const {
95 if (ObjectSize) return ObjectSize;
96 size_t pos = BytesRead + kChunkSize;
97 // keep fetching until we run out of bytes
98 while (fetchToPos(pos)) pos += kChunkSize;
102 int StreamingMemoryObject::readByte(uint64_t address, uint8_t* ptr) const {
103 if (!fetchToPos(address)) return -1;
104 *ptr = Bytes[address + BytesSkipped];
108 int StreamingMemoryObject::readBytes(uint64_t address,
110 uint8_t *buf) const {
111 if (!fetchToPos(address + size - 1)) return -1;
112 memcpy(buf, &Bytes[address + BytesSkipped], size);
116 bool StreamingMemoryObject::dropLeadingBytes(size_t s) {
117 if (BytesRead < s) return true;
123 void StreamingMemoryObject::setKnownObjectSize(size_t size) {
128 StreamableMemoryObject *getNonStreamedMemoryObject(
129 const unsigned char *Start, const unsigned char *End) {
130 return new RawMemoryObject(Start, End);
133 StreamableMemoryObject::~StreamableMemoryObject() { }
135 StreamingMemoryObject::StreamingMemoryObject(DataStreamer *streamer) :
136 Bytes(kChunkSize), Streamer(streamer), BytesRead(0), BytesSkipped(0),
137 ObjectSize(0), EOFReached(false) {
138 BytesRead = streamer->GetBytes(&Bytes[0], kChunkSize);