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"
22 /// Interface to data which is actually streamed from a DataStreamer. In
23 /// addition to inherited members, it has the dropLeadingBytes and
24 /// setKnownObjectSize methods which are not applicable to non-streamed objects.
25 class StreamingMemoryObject : public MemoryObject {
27 StreamingMemoryObject(std::unique_ptr<DataStreamer> Streamer);
28 uint64_t getExtent() const override;
29 uint64_t readBytes(uint8_t *Buf, uint64_t Size,
30 uint64_t Address) const override;
31 const uint8_t *getPointer(uint64_t Address, uint64_t Size) const override;
32 bool isValidAddress(uint64_t address) const override;
34 /// Drop s bytes from the front of the stream, pushing the positions of the
35 /// remaining bytes down by s. This is used to skip past the bitcode header,
36 /// since we don't know a priori if it's present, and we can't put bytes
37 /// back into the stream once we've read them.
38 bool dropLeadingBytes(size_t s);
40 /// If the data object size is known in advance, many of the operations can
41 /// be made more efficient, so this method should be called before reading
42 /// starts (although it can be called anytime).
43 void setKnownObjectSize(size_t size);
45 /// The number of bytes read at a time from the data streamer.
46 static const uint32_t kChunkSize = 4096 * 4;
49 mutable std::vector<unsigned char> Bytes;
50 std::unique_ptr<DataStreamer> Streamer;
51 mutable size_t BytesRead; // Bytes read from stream
52 size_t BytesSkipped;// Bytes skipped at start of stream (e.g. wrapper/header)
53 mutable size_t ObjectSize; // 0 if unknown, set if wrapper seen or EOF reached
54 mutable bool EOFReached;
56 // Fetch enough bytes such that Pos can be read (i.e. BytesRead >
57 // Pos). Returns true if Pos can be read. Unlike most of the
58 // functions in BitcodeReader, returns true on success. Most of the
59 // requests will be small, but we fetch at kChunkSize bytes at a
60 // time to avoid making too many potentially expensive GetBytes
62 bool fetchToPos(size_t Pos) const {
63 while (Pos >= BytesRead) {
66 Bytes.resize(BytesRead + BytesSkipped + kChunkSize);
67 size_t bytes = Streamer->GetBytes(&Bytes[BytesRead + BytesSkipped],
70 if (bytes == 0) { // reached EOF/ran out of bytes
72 ObjectSize = BytesRead;
76 return !ObjectSize || Pos < ObjectSize;
79 StreamingMemoryObject(const StreamingMemoryObject&) = delete;
80 void operator=(const StreamingMemoryObject&) = delete;
83 MemoryObject *getNonStreamedMemoryObject(
84 const unsigned char *Start, const unsigned char *End);
87 #endif // STREAMINGMEMORYOBJECT_H_