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/MemoryObject.h"
23 /// StreamableMemoryObject - Interface to data which might be streamed.
24 /// Streamability has 2 important implications/restrictions. First, the data
25 /// might not yet exist in memory when the request is made. This just means
26 /// that readByte/readBytes might have to block or do some work to get it.
27 /// More significantly, the exact size of the object might not be known until
28 /// it has all been fetched. This means that to return the right result,
29 /// getExtent must also wait for all the data to arrive; therefore it should
30 /// not be called on objects which are actually streamed (this would defeat
31 /// the purpose of streaming). Instead, isValidAddress and isObjectEnd can be
32 /// used to test addresses without knowing the exact size of the stream.
33 /// Finally, getPointer can be used instead of readBytes to avoid extra copying.
34 class StreamableMemoryObject : public MemoryObject {
36 /// Destructor - Override as necessary.
37 virtual ~StreamableMemoryObject();
39 /// getBase - Returns the lowest valid address in the region.
41 /// @result - The lowest valid address.
42 uint64_t getBase() const override = 0;
44 /// getExtent - Returns the size of the region in bytes. (The region is
45 /// contiguous, so the highest valid address of the region
46 /// is getBase() + getExtent() - 1).
47 /// May block until all bytes in the stream have been read
49 /// @result - The size of the region.
50 uint64_t getExtent() const override = 0;
52 /// readByte - Tries to read a single byte from the region.
53 /// May block until (address - base) bytes have been read
54 /// @param address - The address of the byte, in the same space as getBase().
55 /// @param ptr - A pointer to a byte to be filled in. Must be non-NULL.
56 /// @result - 0 if successful; -1 if not. Failure may be due to a
57 /// bounds violation or an implementation-specific error.
58 int readByte(uint64_t address, uint8_t *ptr) const override = 0;
60 /// readBytes - Tries to read a contiguous range of bytes from the
61 /// region, up to the end of the region.
62 /// May block until (address - base + size) bytes have
63 /// been read. Additionally, StreamableMemoryObjects will
64 /// not do partial reads - if size bytes cannot be read,
65 /// readBytes will fail.
67 /// @param address - The address of the first byte, in the same space as
69 /// @param size - The number of bytes to copy.
70 /// @param buf - A pointer to a buffer to be filled in. Must be non-NULL
71 /// and large enough to hold size bytes.
72 /// @result - 0 if successful; -1 if not. Failure may be due to a
73 /// bounds violation or an implementation-specific error.
74 int readBytes(uint64_t address, uint64_t size,
75 uint8_t *buf) const override = 0;
77 /// getPointer - Ensures that the requested data is in memory, and returns
78 /// A pointer to it. More efficient than using readBytes if the
79 /// data is already in memory.
80 /// May block until (address - base + size) bytes have been read
81 /// @param address - address of the byte, in the same space as getBase()
82 /// @param size - amount of data that must be available on return
83 /// @result - valid pointer to the requested data
84 virtual const uint8_t *getPointer(uint64_t address, uint64_t size) const = 0;
86 /// isValidAddress - Returns true if the address is within the object
87 /// (i.e. between base and base + extent - 1 inclusive)
88 /// May block until (address - base) bytes have been read
89 /// @param address - address of the byte, in the same space as getBase()
90 /// @result - true if the address may be read with readByte()
91 virtual bool isValidAddress(uint64_t address) const = 0;
93 /// isObjectEnd - Returns true if the address is one past the end of the
94 /// object (i.e. if it is equal to base + extent)
95 /// May block until (address - base) bytes have been read
96 /// @param address - address of the byte, in the same space as getBase()
97 /// @result - true if the address is equal to base + extent
98 virtual bool isObjectEnd(uint64_t address) const = 0;
101 /// StreamingMemoryObject - interface to data which is actually streamed from
102 /// a DataStreamer. In addition to inherited members, it has the
103 /// dropLeadingBytes and setKnownObjectSize methods which are not applicable
104 /// to non-streamed objects.
105 class StreamingMemoryObject : public StreamableMemoryObject {
107 StreamingMemoryObject(DataStreamer *streamer);
108 uint64_t getBase() const override { return 0; }
109 uint64_t getExtent() const override;
110 int readByte(uint64_t address, uint8_t *ptr) const override;
111 int readBytes(uint64_t address, uint64_t size,
112 uint8_t *buf) const override;
113 const uint8_t *getPointer(uint64_t address, uint64_t size) const override {
114 // This could be fixed by ensuring the bytes are fetched and making a copy,
115 // requiring that the bitcode size be known, or otherwise ensuring that
116 // the memory doesn't go away/get reallocated, but it's
117 // not currently necessary. Users that need the pointer don't stream.
118 assert(0 && "getPointer in streaming memory objects not allowed");
121 bool isValidAddress(uint64_t address) const override;
122 bool isObjectEnd(uint64_t address) const override;
124 /// Drop s bytes from the front of the stream, pushing the positions of the
125 /// remaining bytes down by s. This is used to skip past the bitcode header,
126 /// since we don't know a priori if it's present, and we can't put bytes
127 /// back into the stream once we've read them.
128 bool dropLeadingBytes(size_t s);
130 /// If the data object size is known in advance, many of the operations can
131 /// be made more efficient, so this method should be called before reading
132 /// starts (although it can be called anytime).
133 void setKnownObjectSize(size_t size);
136 const static uint32_t kChunkSize = 4096 * 4;
137 mutable std::vector<unsigned char> Bytes;
138 std::unique_ptr<DataStreamer> Streamer;
139 mutable size_t BytesRead; // Bytes read from stream
140 size_t BytesSkipped;// Bytes skipped at start of stream (e.g. wrapper/header)
141 mutable size_t ObjectSize; // 0 if unknown, set if wrapper seen or EOF reached
142 mutable bool EOFReached;
144 // Fetch enough bytes such that Pos can be read or EOF is reached
145 // (i.e. BytesRead > Pos). Return true if Pos can be read.
146 // Unlike most of the functions in BitcodeReader, returns true on success.
147 // Most of the requests will be small, but we fetch at kChunkSize bytes
148 // at a time to avoid making too many potentially expensive GetBytes calls
149 bool fetchToPos(size_t Pos) const {
150 if (EOFReached) return Pos < ObjectSize;
151 while (Pos >= BytesRead) {
152 Bytes.resize(BytesRead + BytesSkipped + kChunkSize);
153 size_t bytes = Streamer->GetBytes(&Bytes[BytesRead + BytesSkipped],
156 if (bytes < kChunkSize) {
157 if (ObjectSize && BytesRead < Pos)
158 assert(0 && "Unexpected short read fetching bitcode");
159 if (BytesRead <= Pos) { // reached EOF/ran out of bytes
160 ObjectSize = BytesRead;
169 StreamingMemoryObject(const StreamingMemoryObject&) LLVM_DELETED_FUNCTION;
170 void operator=(const StreamingMemoryObject&) LLVM_DELETED_FUNCTION;
173 StreamableMemoryObject *getNonStreamedMemoryObject(
174 const unsigned char *Start, const unsigned char *End);
177 #endif // STREAMABLEMEMORYOBJECT_H_