Purge unused includes throughout libSupport.
[oota-llvm.git] / include / llvm / Support / StreamingMemoryObject.h
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(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     // This could be fixed by ensuring the bytes are fetched and making a copy,
33     // requiring that the bitcode size be known, or otherwise ensuring that
34     // the memory doesn't go away/get reallocated, but it's
35     // not currently necessary. Users that need the pointer don't stream.
36     llvm_unreachable("getPointer in streaming memory objects not allowed");
37     return nullptr;
38   }
39   bool isValidAddress(uint64_t address) const override;
40
41   /// Drop s bytes from the front of the stream, pushing the positions of the
42   /// remaining bytes down by s. This is used to skip past the bitcode header,
43   /// since we don't know a priori if it's present, and we can't put bytes
44   /// back into the stream once we've read them.
45   bool dropLeadingBytes(size_t s);
46
47   /// If the data object size is known in advance, many of the operations can
48   /// be made more efficient, so this method should be called before reading
49   /// starts (although it can be called anytime).
50   void setKnownObjectSize(size_t size);
51
52 private:
53   const static uint32_t kChunkSize = 4096 * 4;
54   mutable std::vector<unsigned char> Bytes;
55   std::unique_ptr<DataStreamer> Streamer;
56   mutable size_t BytesRead;   // Bytes read from stream
57   size_t BytesSkipped;// Bytes skipped at start of stream (e.g. wrapper/header)
58   mutable size_t ObjectSize; // 0 if unknown, set if wrapper seen or EOF reached
59   mutable bool EOFReached;
60
61   // Fetch enough bytes such that Pos can be read or EOF is reached
62   // (i.e. BytesRead > Pos). Return true if Pos can be read.
63   // Unlike most of the functions in BitcodeReader, returns true on success.
64   // Most of the requests will be small, but we fetch at kChunkSize bytes
65   // at a time to avoid making too many potentially expensive GetBytes calls
66   bool fetchToPos(size_t Pos) const {
67     if (EOFReached)
68       return Pos < ObjectSize;
69     while (Pos >= BytesRead) {
70       Bytes.resize(BytesRead + BytesSkipped + kChunkSize);
71       size_t bytes = Streamer->GetBytes(&Bytes[BytesRead + BytesSkipped],
72                                         kChunkSize);
73       BytesRead += bytes;
74       if (bytes != kChunkSize) { // reached EOF/ran out of bytes
75         ObjectSize = BytesRead;
76         EOFReached = true;
77         break;
78       }
79     }
80     return Pos < BytesRead;
81   }
82
83   StreamingMemoryObject(const StreamingMemoryObject&) = delete;
84   void operator=(const StreamingMemoryObject&) = delete;
85 };
86
87 MemoryObject *getNonStreamedMemoryObject(
88     const unsigned char *Start, const unsigned char *End);
89
90 }
91 #endif  // STREAMINGMEMORYOBJECT_H_