1 //===- StreamableMemoryObject.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 //===----------------------------------------------------------------------===//
11 #ifndef LLVM_SUPPORT_STREAMABLEMEMORYOBJECT_H
12 #define LLVM_SUPPORT_STREAMABLEMEMORYOBJECT_H
14 #include "llvm/Support/Compiler.h"
15 #include "llvm/Support/DataStream.h"
16 #include "llvm/Support/ErrorHandling.h"
17 #include "llvm/Support/MemoryObject.h"
24 /// StreamableMemoryObject - Interface to data which might be streamed.
25 /// Streamability has 2 important implications/restrictions. First, the data
26 /// might not yet exist in memory when the request is made. This just means
27 /// that readByte/readBytes might have to block or do some work to get it.
28 /// More significantly, the exact size of the object might not be known until
29 /// it has all been fetched. This means that to return the right result,
30 /// getExtent must also wait for all the data to arrive; therefore it should
31 /// not be called on objects which are actually streamed (this would defeat
32 /// the purpose of streaming). Instead, isValidAddress and isObjectEnd can be
33 /// used to test addresses without knowing the exact size of the stream.
34 /// Finally, getPointer can be used instead of readBytes to avoid extra copying.
35 class StreamableMemoryObject : public MemoryObject {
37 /// Destructor - Override as necessary.
38 virtual ~StreamableMemoryObject();
40 /// getPointer - Ensures that the requested data is in memory, and returns
41 /// A pointer to it. More efficient than using readBytes if the
42 /// data is already in memory.
43 /// May block until (address - base + size) bytes have been read
44 /// @param address - address of the byte, in the same space as getBase()
45 /// @param size - amount of data that must be available on return
46 /// @result - valid pointer to the requested data
47 virtual const uint8_t *getPointer(uint64_t address, uint64_t size) const = 0;
49 /// isValidAddress - Returns true if the address is within the object
50 /// (i.e. between base and base + extent - 1 inclusive)
51 /// May block until (address - base) bytes have been read
52 /// @param address - address of the byte, in the same space as getBase()
53 /// @result - true if the address may be read with readByte()
54 virtual bool isValidAddress(uint64_t address) const = 0;
56 /// isObjectEnd - Returns true if the address is one past the end of the
57 /// object (i.e. if it is equal to base + extent)
58 /// May block until (address - base) bytes have been read
59 /// @param address - address of the byte, in the same space as getBase()
60 /// @result - true if the address is equal to base + extent
61 virtual bool isObjectEnd(uint64_t address) const = 0;
64 /// StreamingMemoryObject - interface to data which is actually streamed from
65 /// a DataStreamer. In addition to inherited members, it has the
66 /// dropLeadingBytes and setKnownObjectSize methods which are not applicable
67 /// to non-streamed objects.
68 class StreamingMemoryObject : public StreamableMemoryObject {
70 StreamingMemoryObject(DataStreamer *streamer);
71 uint64_t getExtent() const override;
72 int readBytes(uint64_t address, uint64_t size,
73 uint8_t *buf) const override;
74 const uint8_t *getPointer(uint64_t address, uint64_t size) const override {
75 // This could be fixed by ensuring the bytes are fetched and making a copy,
76 // requiring that the bitcode size be known, or otherwise ensuring that
77 // the memory doesn't go away/get reallocated, but it's
78 // not currently necessary. Users that need the pointer don't stream.
79 llvm_unreachable("getPointer in streaming memory objects not allowed");
82 bool isValidAddress(uint64_t address) const override;
83 bool isObjectEnd(uint64_t address) const override;
85 /// Drop s bytes from the front of the stream, pushing the positions of the
86 /// remaining bytes down by s. This is used to skip past the bitcode header,
87 /// since we don't know a priori if it's present, and we can't put bytes
88 /// back into the stream once we've read them.
89 bool dropLeadingBytes(size_t s);
91 /// If the data object size is known in advance, many of the operations can
92 /// be made more efficient, so this method should be called before reading
93 /// starts (although it can be called anytime).
94 void setKnownObjectSize(size_t size);
97 const static uint32_t kChunkSize = 4096 * 4;
98 mutable std::vector<unsigned char> Bytes;
99 std::unique_ptr<DataStreamer> Streamer;
100 mutable size_t BytesRead; // Bytes read from stream
101 size_t BytesSkipped;// Bytes skipped at start of stream (e.g. wrapper/header)
102 mutable size_t ObjectSize; // 0 if unknown, set if wrapper seen or EOF reached
103 mutable bool EOFReached;
105 // Fetch enough bytes such that Pos can be read or EOF is reached
106 // (i.e. BytesRead > Pos). Return true if Pos can be read.
107 // Unlike most of the functions in BitcodeReader, returns true on success.
108 // Most of the requests will be small, but we fetch at kChunkSize bytes
109 // at a time to avoid making too many potentially expensive GetBytes calls
110 bool fetchToPos(size_t Pos) const {
111 if (EOFReached) return Pos < ObjectSize;
112 while (Pos >= BytesRead) {
113 Bytes.resize(BytesRead + BytesSkipped + kChunkSize);
114 size_t bytes = Streamer->GetBytes(&Bytes[BytesRead + BytesSkipped],
117 if (bytes < kChunkSize) {
118 assert((!ObjectSize || BytesRead >= Pos) &&
119 "Unexpected short read fetching bitcode");
120 if (BytesRead <= Pos) { // reached EOF/ran out of bytes
121 ObjectSize = BytesRead;
130 StreamingMemoryObject(const StreamingMemoryObject&) LLVM_DELETED_FUNCTION;
131 void operator=(const StreamingMemoryObject&) LLVM_DELETED_FUNCTION;
134 StreamableMemoryObject *getNonStreamedMemoryObject(
135 const unsigned char *Start, const unsigned char *End);
138 #endif // STREAMABLEMEMORYOBJECT_H_