1 //===- StreamingMemoryObject.h - Streamable data interface -----*- C++ -*-===//
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 #ifndef LLVM_SUPPORT_STREAMINGMEMORYOBJECT_H
11 #define LLVM_SUPPORT_STREAMINGMEMORYOBJECT_H
13 #include "llvm/Support/Compiler.h"
14 #include "llvm/Support/DataStream.h"
15 #include "llvm/Support/ErrorHandling.h"
16 #include "llvm/Support/MemoryObject.h"
23 /// Interface to data which is actually streamed from a DataStreamer. In
24 /// addition to inherited members, it has the dropLeadingBytes and
25 /// setKnownObjectSize methods which are not applicable to non-streamed objects.
26 class StreamingMemoryObject : public MemoryObject {
28 StreamingMemoryObject(DataStreamer *streamer);
29 uint64_t getExtent() const override;
30 int readBytes(uint64_t address, uint64_t size,
31 uint8_t *buf) const override;
32 const uint8_t *getPointer(uint64_t address, uint64_t size) const override {
33 // This could be fixed by ensuring the bytes are fetched and making a copy,
34 // requiring that the bitcode size be known, or otherwise ensuring that
35 // the memory doesn't go away/get reallocated, but it's
36 // not currently necessary. Users that need the pointer don't stream.
37 llvm_unreachable("getPointer in streaming memory objects not allowed");
40 bool isValidAddress(uint64_t address) const override;
41 bool isObjectEnd(uint64_t address) const override;
43 /// Drop s bytes from the front of the stream, pushing the positions of the
44 /// remaining bytes down by s. This is used to skip past the bitcode header,
45 /// since we don't know a priori if it's present, and we can't put bytes
46 /// back into the stream once we've read them.
47 bool dropLeadingBytes(size_t s);
49 /// If the data object size is known in advance, many of the operations can
50 /// be made more efficient, so this method should be called before reading
51 /// starts (although it can be called anytime).
52 void setKnownObjectSize(size_t size);
55 const static uint32_t kChunkSize = 4096 * 4;
56 mutable std::vector<unsigned char> Bytes;
57 std::unique_ptr<DataStreamer> Streamer;
58 mutable size_t BytesRead; // Bytes read from stream
59 size_t BytesSkipped;// Bytes skipped at start of stream (e.g. wrapper/header)
60 mutable size_t ObjectSize; // 0 if unknown, set if wrapper seen or EOF reached
61 mutable bool EOFReached;
63 // Fetch enough bytes such that Pos can be read or EOF is reached
64 // (i.e. BytesRead > Pos). Return true if Pos can be read.
65 // Unlike most of the functions in BitcodeReader, returns true on success.
66 // Most of the requests will be small, but we fetch at kChunkSize bytes
67 // at a time to avoid making too many potentially expensive GetBytes calls
68 bool fetchToPos(size_t Pos) const {
69 if (EOFReached) return Pos < ObjectSize;
70 while (Pos >= BytesRead) {
71 Bytes.resize(BytesRead + BytesSkipped + kChunkSize);
72 size_t bytes = Streamer->GetBytes(&Bytes[BytesRead + BytesSkipped],
75 if (bytes < kChunkSize) {
76 assert((!ObjectSize || BytesRead >= Pos) &&
77 "Unexpected short read fetching bitcode");
78 if (BytesRead <= Pos) { // reached EOF/ran out of bytes
79 ObjectSize = BytesRead;
88 StreamingMemoryObject(const StreamingMemoryObject&) LLVM_DELETED_FUNCTION;
89 void operator=(const StreamingMemoryObject&) LLVM_DELETED_FUNCTION;
92 MemoryObject *getNonStreamedMemoryObject(
93 const unsigned char *Start, const unsigned char *End);
96 #endif // STREAMINGMEMORYOBJECT_H_