2014-11-12 11:55:46 +08:00
|
|
|
//===- StreamingMemoryObject.cpp - Streamable data interface -------------===//
|
2012-02-07 06:30:29 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-11-12 11:55:46 +08:00
|
|
|
#include "llvm/Support/StreamingMemoryObject.h"
|
2012-02-07 06:30:29 +08:00
|
|
|
#include <cassert>
|
2014-03-06 14:13:54 +08:00
|
|
|
#include <cstddef>
|
2012-02-07 06:30:29 +08:00
|
|
|
#include <cstring>
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2014-11-12 11:55:46 +08:00
|
|
|
class RawMemoryObject : public MemoryObject {
|
2012-02-07 06:30:29 +08:00
|
|
|
public:
|
|
|
|
RawMemoryObject(const unsigned char *Start, const unsigned char *End) :
|
|
|
|
FirstChar(Start), LastChar(End) {
|
2012-06-20 08:16:40 +08:00
|
|
|
assert(LastChar >= FirstChar && "Invalid start/end range");
|
2012-02-07 06:30:29 +08:00
|
|
|
}
|
|
|
|
|
2014-03-10 11:53:12 +08:00
|
|
|
uint64_t getExtent() const override {
|
2012-09-23 10:12:10 +08:00
|
|
|
return LastChar - FirstChar;
|
|
|
|
}
|
2014-11-13 01:11:16 +08:00
|
|
|
uint64_t readBytes(uint8_t *Buf, uint64_t Size,
|
|
|
|
uint64_t Address) const override;
|
2014-03-10 11:53:12 +08:00
|
|
|
const uint8_t *getPointer(uint64_t address, uint64_t size) const override;
|
|
|
|
bool isValidAddress(uint64_t address) const override {
|
2012-02-29 09:09:06 +08:00
|
|
|
return validAddress(address);
|
|
|
|
}
|
2012-02-07 06:30:29 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
const uint8_t* const FirstChar;
|
|
|
|
const uint8_t* const LastChar;
|
|
|
|
|
|
|
|
// These are implemented as inline functions here to avoid multiple virtual
|
|
|
|
// calls per public function
|
2012-02-29 09:09:06 +08:00
|
|
|
bool validAddress(uint64_t address) const {
|
2014-03-06 14:05:26 +08:00
|
|
|
return static_cast<std::ptrdiff_t>(address) < LastChar - FirstChar;
|
2012-02-07 06:30:29 +08:00
|
|
|
}
|
|
|
|
|
2015-02-16 06:54:22 +08:00
|
|
|
RawMemoryObject(const RawMemoryObject&) = delete;
|
|
|
|
void operator=(const RawMemoryObject&) = delete;
|
2012-02-07 06:30:29 +08:00
|
|
|
};
|
|
|
|
|
2014-11-13 01:11:16 +08:00
|
|
|
uint64_t RawMemoryObject::readBytes(uint8_t *Buf, uint64_t Size,
|
|
|
|
uint64_t Address) const {
|
|
|
|
uint64_t BufferSize = LastChar - FirstChar;
|
|
|
|
if (Address >= BufferSize)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
uint64_t End = Address + Size;
|
|
|
|
if (End > BufferSize)
|
|
|
|
End = BufferSize;
|
|
|
|
|
2014-11-13 21:55:13 +08:00
|
|
|
assert(static_cast<int64_t>(End - Address) >= 0);
|
2014-11-13 01:11:16 +08:00
|
|
|
Size = End - Address;
|
2014-11-13 21:55:13 +08:00
|
|
|
memcpy(Buf, Address + FirstChar, Size);
|
2014-11-13 01:11:16 +08:00
|
|
|
return Size;
|
2012-02-07 06:30:29 +08:00
|
|
|
}
|
|
|
|
|
2012-02-29 09:09:06 +08:00
|
|
|
const uint8_t *RawMemoryObject::getPointer(uint64_t address,
|
|
|
|
uint64_t size) const {
|
2012-02-07 06:30:29 +08:00
|
|
|
return FirstChar + address;
|
|
|
|
}
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
// If the bitcode has a header, then its size is known, and we don't have to
|
|
|
|
// block until we actually want to read it.
|
2012-02-29 09:09:06 +08:00
|
|
|
bool StreamingMemoryObject::isValidAddress(uint64_t address) const {
|
2012-02-07 06:30:29 +08:00
|
|
|
if (ObjectSize && address < ObjectSize) return true;
|
|
|
|
return fetchToPos(address);
|
|
|
|
}
|
|
|
|
|
2012-02-29 09:09:06 +08:00
|
|
|
uint64_t StreamingMemoryObject::getExtent() const {
|
2012-02-07 06:30:29 +08:00
|
|
|
if (ObjectSize) return ObjectSize;
|
|
|
|
size_t pos = BytesRead + kChunkSize;
|
|
|
|
// keep fetching until we run out of bytes
|
|
|
|
while (fetchToPos(pos)) pos += kChunkSize;
|
|
|
|
return ObjectSize;
|
|
|
|
}
|
|
|
|
|
2014-11-13 01:11:16 +08:00
|
|
|
uint64_t StreamingMemoryObject::readBytes(uint8_t *Buf, uint64_t Size,
|
|
|
|
uint64_t Address) const {
|
|
|
|
fetchToPos(Address + Size - 1);
|
2014-11-13 15:23:22 +08:00
|
|
|
if (Address >= BytesRead)
|
2014-11-13 01:11:16 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
uint64_t End = Address + Size;
|
2014-11-13 15:23:22 +08:00
|
|
|
if (End > BytesRead)
|
|
|
|
End = BytesRead;
|
2014-11-13 21:55:13 +08:00
|
|
|
assert(static_cast<int64_t>(End - Address) >= 0);
|
2014-11-13 01:11:16 +08:00
|
|
|
Size = End - Address;
|
|
|
|
memcpy(Buf, &Bytes[Address + BytesSkipped], Size);
|
|
|
|
return Size;
|
2012-02-07 06:30:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool StreamingMemoryObject::dropLeadingBytes(size_t s) {
|
|
|
|
if (BytesRead < s) return true;
|
|
|
|
BytesSkipped = s;
|
|
|
|
BytesRead -= s;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void StreamingMemoryObject::setKnownObjectSize(size_t size) {
|
|
|
|
ObjectSize = size;
|
|
|
|
Bytes.reserve(size);
|
|
|
|
}
|
|
|
|
|
2014-11-12 11:55:46 +08:00
|
|
|
MemoryObject *getNonStreamedMemoryObject(const unsigned char *Start,
|
|
|
|
const unsigned char *End) {
|
2012-02-07 06:30:29 +08:00
|
|
|
return new RawMemoryObject(Start, End);
|
|
|
|
}
|
|
|
|
|
|
|
|
StreamingMemoryObject::StreamingMemoryObject(DataStreamer *streamer) :
|
|
|
|
Bytes(kChunkSize), Streamer(streamer), BytesRead(0), BytesSkipped(0),
|
|
|
|
ObjectSize(0), EOFReached(false) {
|
|
|
|
BytesRead = streamer->GetBytes(&Bytes[0], kChunkSize);
|
|
|
|
}
|
|
|
|
}
|