//===----------------------------------------------------------------------===//
#include "llvm/Support/StreamingMemoryObject.h"
-#include "llvm/Support/Compiler.h"
#include <cassert>
#include <cstddef>
#include <cstring>
-
-
using namespace llvm;
namespace {
return static_cast<std::ptrdiff_t>(address) < LastChar - FirstChar;
}
- RawMemoryObject(const RawMemoryObject&) LLVM_DELETED_FUNCTION;
- void operator=(const RawMemoryObject&) LLVM_DELETED_FUNCTION;
+ RawMemoryObject(const RawMemoryObject&) = delete;
+ void operator=(const RawMemoryObject&) = delete;
};
uint64_t RawMemoryObject::readBytes(uint8_t *Buf, uint64_t Size,
if (End > BufferSize)
End = BufferSize;
+ assert(static_cast<int64_t>(End - Address) >= 0);
Size = End - Address;
- assert(Size >= 0);
- memcpy(Buf, (uint8_t *)(Address + FirstChar), Size);
+ memcpy(Buf, Address + FirstChar, Size);
return Size;
}
// block until we actually want to read it.
bool StreamingMemoryObject::isValidAddress(uint64_t address) const {
if (ObjectSize && address < ObjectSize) return true;
- return fetchToPos(address);
+ return fetchToPos(address);
}
uint64_t StreamingMemoryObject::getExtent() const {
uint64_t StreamingMemoryObject::readBytes(uint8_t *Buf, uint64_t Size,
uint64_t Address) const {
fetchToPos(Address + Size - 1);
- uint64_t BufferSize = Bytes.size() - BytesSkipped;
- if (Address >= BufferSize)
+ // Note: For wrapped bitcode files will set ObjectSize after the
+ // first call to fetchToPos. In such cases, ObjectSize can be
+ // smaller than BytesRead.
+ size_t MaxAddress =
+ (ObjectSize && ObjectSize < BytesRead) ? ObjectSize : BytesRead;
+ if (Address >= MaxAddress)
return 0;
uint64_t End = Address + Size;
- if (End > BufferSize)
- End = BufferSize;
+ if (End > MaxAddress)
+ End = MaxAddress;
+ assert(End >= Address);
Size = End - Address;
- assert(Size >= 0);
memcpy(Buf, &Bytes[Address + BytesSkipped], Size);
return Size;
}
void StreamingMemoryObject::setKnownObjectSize(size_t size) {
ObjectSize = size;
Bytes.reserve(size);
+ if (ObjectSize <= BytesRead)
+ EOFReached = true;
}
MemoryObject *getNonStreamedMemoryObject(const unsigned char *Start,
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);
+StreamingMemoryObject::StreamingMemoryObject(
+ std::unique_ptr<DataStreamer> Streamer)
+ : Bytes(kChunkSize), Streamer(std::move(Streamer)), BytesRead(0),
+ BytesSkipped(0), ObjectSize(0), EOFReached(false) {
+ BytesRead = this->Streamer->GetBytes(&Bytes[0], kChunkSize);
}
}