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