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 virtual 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 virtual 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 virtual 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 virtual int readBytes(uint64_t address,
76 uint8_t *buf) const override = 0;
78 /// getPointer - Ensures that the requested data is in memory, and returns
79 /// A pointer to it. More efficient than using readBytes if the
80 /// data is already in memory.
81 /// May block until (address - base + size) bytes have been read
82 /// @param address - address of the byte, in the same space as getBase()
83 /// @param size - amount of data that must be available on return
84 /// @result - valid pointer to the requested data
85 virtual const uint8_t *getPointer(uint64_t address, uint64_t size) const = 0;
87 /// isValidAddress - Returns true if the address is within the object
88 /// (i.e. between base and base + extent - 1 inclusive)
89 /// May block until (address - base) bytes have been read
90 /// @param address - address of the byte, in the same space as getBase()
91 /// @result - true if the address may be read with readByte()
92 virtual bool isValidAddress(uint64_t address) const = 0;
94 /// isObjectEnd - Returns true if the address is one past the end of the
95 /// object (i.e. if it is equal to base + extent)
96 /// May block until (address - base) bytes have been read
97 /// @param address - address of the byte, in the same space as getBase()
98 /// @result - true if the address is equal to base + extent
99 virtual bool isObjectEnd(uint64_t address) const = 0;
102 /// StreamingMemoryObject - interface to data which is actually streamed from
103 /// a DataStreamer. In addition to inherited members, it has the
104 /// dropLeadingBytes and setKnownObjectSize methods which are not applicable
105 /// to non-streamed objects.
106 class StreamingMemoryObject : public StreamableMemoryObject {
108 StreamingMemoryObject(DataStreamer *streamer);
109 virtual uint64_t getBase() const override { return 0; }
110 virtual uint64_t getExtent() const override;
111 virtual int readByte(uint64_t address, uint8_t *ptr) const override;
112 virtual int readBytes(uint64_t address,
114 uint8_t *buf) const override;
115 virtual const uint8_t *getPointer(uint64_t address,
116 uint64_t size) const override {
117 // This could be fixed by ensuring the bytes are fetched and making a copy,
118 // requiring that the bitcode size be known, or otherwise ensuring that
119 // the memory doesn't go away/get reallocated, but it's
120 // not currently necessary. Users that need the pointer don't stream.
121 assert(0 && "getPointer in streaming memory objects not allowed");
124 virtual bool isValidAddress(uint64_t address) const override;
125 virtual bool isObjectEnd(uint64_t address) const override;
127 /// Drop s bytes from the front of the stream, pushing the positions of the
128 /// remaining bytes down by s. This is used to skip past the bitcode header,
129 /// since we don't know a priori if it's present, and we can't put bytes
130 /// back into the stream once we've read them.
131 bool dropLeadingBytes(size_t s);
133 /// If the data object size is known in advance, many of the operations can
134 /// be made more efficient, so this method should be called before reading
135 /// starts (although it can be called anytime).
136 void setKnownObjectSize(size_t size);
139 const static uint32_t kChunkSize = 4096 * 4;
140 mutable std::vector<unsigned char> Bytes;
141 std::unique_ptr<DataStreamer> Streamer;
142 mutable size_t BytesRead; // Bytes read from stream
143 size_t BytesSkipped;// Bytes skipped at start of stream (e.g. wrapper/header)
144 mutable size_t ObjectSize; // 0 if unknown, set if wrapper seen or EOF reached
145 mutable bool EOFReached;
147 // Fetch enough bytes such that Pos can be read or EOF is reached
148 // (i.e. BytesRead > Pos). Return true if Pos can be read.
149 // Unlike most of the functions in BitcodeReader, returns true on success.
150 // Most of the requests will be small, but we fetch at kChunkSize bytes
151 // at a time to avoid making too many potentially expensive GetBytes calls
152 bool fetchToPos(size_t Pos) const {
153 if (EOFReached) return Pos < ObjectSize;
154 while (Pos >= BytesRead) {
155 Bytes.resize(BytesRead + BytesSkipped + kChunkSize);
156 size_t bytes = Streamer->GetBytes(&Bytes[BytesRead + BytesSkipped],
159 if (bytes < kChunkSize) {
160 if (ObjectSize && BytesRead < Pos)
161 assert(0 && "Unexpected short read fetching bitcode");
162 if (BytesRead <= Pos) { // reached EOF/ran out of bytes
163 ObjectSize = BytesRead;
172 StreamingMemoryObject(const StreamingMemoryObject&) LLVM_DELETED_FUNCTION;
173 void operator=(const StreamingMemoryObject&) LLVM_DELETED_FUNCTION;
176 StreamableMemoryObject *getNonStreamedMemoryObject(
177 const unsigned char *Start, const unsigned char *End);
180 #endif // STREAMABLEMEMORYOBJECT_H_