StreamingMemoryObject.h [plain text]
#ifndef LLVM_SUPPORT_STREAMINGMEMORYOBJECT_H
#define LLVM_SUPPORT_STREAMINGMEMORYOBJECT_H
#include "llvm/Support/Compiler.h"
#include "llvm/Support/DataStream.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/MemoryObject.h"
#include <memory>
#include <vector>
namespace llvm {
class StreamingMemoryObject : public MemoryObject {
public:
StreamingMemoryObject(std::unique_ptr<DataStreamer> Streamer);
uint64_t getExtent() const override;
uint64_t readBytes(uint8_t *Buf, uint64_t Size,
uint64_t Address) const override;
const uint8_t *getPointer(uint64_t Address, uint64_t Size) const override;
bool isValidAddress(uint64_t address) const override;
bool dropLeadingBytes(size_t s);
void setKnownObjectSize(size_t size);
static const uint32_t kChunkSize = 4096 * 4;
private:
mutable std::vector<unsigned char> Bytes;
std::unique_ptr<DataStreamer> Streamer;
mutable size_t BytesRead; size_t BytesSkipped; mutable size_t ObjectSize; mutable bool EOFReached;
bool fetchToPos(size_t Pos) const {
while (Pos >= BytesRead) {
if (EOFReached)
return false;
Bytes.resize(BytesRead + BytesSkipped + kChunkSize);
size_t bytes = Streamer->GetBytes(&Bytes[BytesRead + BytesSkipped],
kChunkSize);
BytesRead += bytes;
if (bytes == 0) { if (ObjectSize == 0)
ObjectSize = BytesRead;
EOFReached = true;
}
}
return !ObjectSize || Pos < ObjectSize;
}
StreamingMemoryObject(const StreamingMemoryObject&) = delete;
void operator=(const StreamingMemoryObject&) = delete;
};
MemoryObject *getNonStreamedMemoryObject(
const unsigned char *Start, const unsigned char *End);
}
#endif // STREAMINGMEMORYOBJECT_H_