1 //===- StreamingMemoryObject.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/StreamingMemoryObject.h"
11 #include "llvm/Support/Compiler.h"
21 class RawMemoryObject : public MemoryObject {
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 getExtent() const override {
29 return LastChar - FirstChar;
31 int readBytes(uint64_t address, uint64_t size,
32 uint8_t *buf) const override;
33 const uint8_t *getPointer(uint64_t address, uint64_t size) const override;
34 bool isValidAddress(uint64_t address) const override {
35 return validAddress(address);
37 bool isObjectEnd(uint64_t address) const override {
38 return objectEnd(address);
42 const uint8_t* const FirstChar;
43 const uint8_t* const LastChar;
45 // These are implemented as inline functions here to avoid multiple virtual
46 // calls per public function
47 bool validAddress(uint64_t address) const {
48 return static_cast<std::ptrdiff_t>(address) < LastChar - FirstChar;
50 bool objectEnd(uint64_t address) const {
51 return static_cast<std::ptrdiff_t>(address) == LastChar - FirstChar;
54 RawMemoryObject(const RawMemoryObject&) LLVM_DELETED_FUNCTION;
55 void operator=(const RawMemoryObject&) LLVM_DELETED_FUNCTION;
58 int RawMemoryObject::readBytes(uint64_t address,
61 if (!validAddress(address) || !validAddress(address + size - 1)) return -1;
62 memcpy(buf, (uint8_t *)(uintptr_t)(address + FirstChar), size);
66 const uint8_t *RawMemoryObject::getPointer(uint64_t address,
67 uint64_t size) const {
68 return FirstChar + address;
70 } // anonymous namespace
73 // If the bitcode has a header, then its size is known, and we don't have to
74 // block until we actually want to read it.
75 bool StreamingMemoryObject::isValidAddress(uint64_t address) const {
76 if (ObjectSize && address < ObjectSize) return true;
77 return fetchToPos(address);
80 bool StreamingMemoryObject::isObjectEnd(uint64_t address) const {
81 if (ObjectSize) return address == ObjectSize;
83 return address == ObjectSize && address != 0;
86 uint64_t StreamingMemoryObject::getExtent() const {
87 if (ObjectSize) return ObjectSize;
88 size_t pos = BytesRead + kChunkSize;
89 // keep fetching until we run out of bytes
90 while (fetchToPos(pos)) pos += kChunkSize;
94 int StreamingMemoryObject::readBytes(uint64_t address,
97 if (!fetchToPos(address + size - 1)) return -1;
98 memcpy(buf, &Bytes[address + BytesSkipped], size);
102 bool StreamingMemoryObject::dropLeadingBytes(size_t s) {
103 if (BytesRead < s) return true;
109 void StreamingMemoryObject::setKnownObjectSize(size_t size) {
114 MemoryObject *getNonStreamedMemoryObject(const unsigned char *Start,
115 const unsigned char *End) {
116 return new RawMemoryObject(Start, End);
119 StreamingMemoryObject::StreamingMemoryObject(DataStreamer *streamer) :
120 Bytes(kChunkSize), Streamer(streamer), BytesRead(0), BytesSkipped(0),
121 ObjectSize(0), EOFReached(false) {
122 BytesRead = streamer->GetBytes(&Bytes[0], kChunkSize);