blob: 5cb0680522d379d5bc0754e2f3efa9cfe742d737 [file] [log] [blame]
Nick Lewycky44ebf8b2012-02-06 22:41:47 +00001//===- StreamableMemoryObject.cpp - Streamable data interface -------------===//
Derek Schuff2ea93872012-02-06 22:30:29 +00002//
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"
Craig Topper86a1c322012-09-15 17:09:36 +000011#include "llvm/Support/Compiler.h"
Derek Schuff2ea93872012-02-06 22:30:29 +000012#include <cassert>
Stephen Hines36b56882014-04-23 16:57:46 -070013#include <cstddef>
Derek Schuff2ea93872012-02-06 22:30:29 +000014#include <cstring>
15
16
17using namespace llvm;
18
19namespace {
20
21class RawMemoryObject : public StreamableMemoryObject {
22public:
23 RawMemoryObject(const unsigned char *Start, const unsigned char *End) :
24 FirstChar(Start), LastChar(End) {
Kaelyn Uhrain44b2c822012-06-20 00:16:40 +000025 assert(LastChar >= FirstChar && "Invalid start/end range");
Derek Schuff2ea93872012-02-06 22:30:29 +000026 }
27
Stephen Hines36b56882014-04-23 16:57:46 -070028 uint64_t getBase() const override { return 0; }
29 uint64_t getExtent() const override {
Craig Toppera96a1822012-09-23 02:12:10 +000030 return LastChar - FirstChar;
31 }
Stephen Hines36b56882014-04-23 16:57:46 -070032 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 {
Derek Schuffadef06a2012-02-29 01:09:06 +000037 return validAddress(address);
38 }
Stephen Hines36b56882014-04-23 16:57:46 -070039 bool isObjectEnd(uint64_t address) const override {
Craig Toppera96a1822012-09-23 02:12:10 +000040 return objectEnd(address);
41 }
Derek Schuff2ea93872012-02-06 22:30:29 +000042
43private:
44 const uint8_t* const FirstChar;
45 const uint8_t* const LastChar;
46
47 // These are implemented as inline functions here to avoid multiple virtual
48 // calls per public function
Derek Schuffadef06a2012-02-29 01:09:06 +000049 bool validAddress(uint64_t address) const {
Stephen Hines36b56882014-04-23 16:57:46 -070050 return static_cast<std::ptrdiff_t>(address) < LastChar - FirstChar;
Derek Schuff2ea93872012-02-06 22:30:29 +000051 }
Derek Schuffadef06a2012-02-29 01:09:06 +000052 bool objectEnd(uint64_t address) const {
Stephen Hines36b56882014-04-23 16:57:46 -070053 return static_cast<std::ptrdiff_t>(address) == LastChar - FirstChar;
Derek Schuff2ea93872012-02-06 22:30:29 +000054 }
55
Craig Topper86a1c322012-09-15 17:09:36 +000056 RawMemoryObject(const RawMemoryObject&) LLVM_DELETED_FUNCTION;
57 void operator=(const RawMemoryObject&) LLVM_DELETED_FUNCTION;
Derek Schuff2ea93872012-02-06 22:30:29 +000058};
59
Derek Schuffadef06a2012-02-29 01:09:06 +000060int RawMemoryObject::readByte(uint64_t address, uint8_t* ptr) const {
Derek Schuff2ea93872012-02-06 22:30:29 +000061 if (!validAddress(address)) return -1;
62 *ptr = *((uint8_t *)(uintptr_t)(address + FirstChar));
63 return 0;
64}
65
66int RawMemoryObject::readBytes(uint64_t address,
67 uint64_t size,
Benjamin Kramer49a6a8d2013-05-24 10:54:58 +000068 uint8_t *buf) const {
Derek Schuff2ea93872012-02-06 22:30:29 +000069 if (!validAddress(address) || !validAddress(address + size - 1)) return -1;
70 memcpy(buf, (uint8_t *)(uintptr_t)(address + FirstChar), size);
Derek Schuff2ea93872012-02-06 22:30:29 +000071 return size;
72}
73
Derek Schuffadef06a2012-02-29 01:09:06 +000074const uint8_t *RawMemoryObject::getPointer(uint64_t address,
75 uint64_t size) const {
Derek Schuff2ea93872012-02-06 22:30:29 +000076 return FirstChar + address;
77}
78} // anonymous namespace
79
80namespace llvm {
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.
Derek Schuffadef06a2012-02-29 01:09:06 +000083bool StreamingMemoryObject::isValidAddress(uint64_t address) const {
Derek Schuff2ea93872012-02-06 22:30:29 +000084 if (ObjectSize && address < ObjectSize) return true;
85 return fetchToPos(address);
86}
87
Derek Schuffadef06a2012-02-29 01:09:06 +000088bool StreamingMemoryObject::isObjectEnd(uint64_t address) const {
Derek Schuff2ea93872012-02-06 22:30:29 +000089 if (ObjectSize) return address == ObjectSize;
90 fetchToPos(address);
91 return address == ObjectSize && address != 0;
92}
93
Derek Schuffadef06a2012-02-29 01:09:06 +000094uint64_t StreamingMemoryObject::getExtent() const {
Derek Schuff2ea93872012-02-06 22:30:29 +000095 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;
99 return ObjectSize;
100}
101
Derek Schuffadef06a2012-02-29 01:09:06 +0000102int StreamingMemoryObject::readByte(uint64_t address, uint8_t* ptr) const {
Derek Schuff2ea93872012-02-06 22:30:29 +0000103 if (!fetchToPos(address)) return -1;
104 *ptr = Bytes[address + BytesSkipped];
105 return 0;
106}
107
108int StreamingMemoryObject::readBytes(uint64_t address,
109 uint64_t size,
Benjamin Kramer49a6a8d2013-05-24 10:54:58 +0000110 uint8_t *buf) const {
Derek Schuff2ea93872012-02-06 22:30:29 +0000111 if (!fetchToPos(address + size - 1)) return -1;
112 memcpy(buf, &Bytes[address + BytesSkipped], size);
Derek Schuff2ea93872012-02-06 22:30:29 +0000113 return 0;
114}
115
116bool StreamingMemoryObject::dropLeadingBytes(size_t s) {
117 if (BytesRead < s) return true;
118 BytesSkipped = s;
119 BytesRead -= s;
120 return false;
121}
122
123void StreamingMemoryObject::setKnownObjectSize(size_t size) {
124 ObjectSize = size;
125 Bytes.reserve(size);
126}
127
128StreamableMemoryObject *getNonStreamedMemoryObject(
129 const unsigned char *Start, const unsigned char *End) {
130 return new RawMemoryObject(Start, End);
131}
132
133StreamableMemoryObject::~StreamableMemoryObject() { }
134
135StreamingMemoryObject::StreamingMemoryObject(DataStreamer *streamer) :
136 Bytes(kChunkSize), Streamer(streamer), BytesRead(0), BytesSkipped(0),
137 ObjectSize(0), EOFReached(false) {
138 BytesRead = streamer->GetBytes(&Bytes[0], kChunkSize);
139}
140}