1 //===- StreamingMemoryObject.h - Streamable data interface -----*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef LLVM_SUPPORT_STREAMINGMEMORYOBJECT_H 11 #define LLVM_SUPPORT_STREAMINGMEMORYOBJECT_H 12 13 #include "llvm/Support/Compiler.h" 14 #include "llvm/Support/DataStream.h" 15 #include "llvm/Support/ErrorHandling.h" 16 #include "llvm/Support/MemoryObject.h" 17 #include <memory> 18 #include <vector> 19 20 namespace llvm { 21 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 { 26 public: 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; 33 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); 39 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); 44 45 /// The number of bytes read at a time from the data streamer. 46 static const uint32_t kChunkSize = 4096 * 4; 47 48 private: 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; 55 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 61 // calls. fetchToPos(size_t Pos)62 bool fetchToPos(size_t Pos) const { 63 while (Pos >= BytesRead) { 64 if (EOFReached) 65 return false; 66 Bytes.resize(BytesRead + BytesSkipped + kChunkSize); 67 size_t bytes = Streamer->GetBytes(&Bytes[BytesRead + BytesSkipped], 68 kChunkSize); 69 BytesRead += bytes; 70 if (bytes == 0) { // reached EOF/ran out of bytes 71 if (ObjectSize == 0) 72 ObjectSize = BytesRead; 73 EOFReached = true; 74 } 75 } 76 return !ObjectSize || Pos < ObjectSize; 77 } 78 79 StreamingMemoryObject(const StreamingMemoryObject&) = delete; 80 void operator=(const StreamingMemoryObject&) = delete; 81 }; 82 83 MemoryObject *getNonStreamedMemoryObject( 84 const unsigned char *Start, const unsigned char *End); 85 86 } 87 #endif // STREAMINGMEMORYOBJECT_H_ 88