Replace OwningPtr<T> with std::unique_ptr<T>.
[oota-llvm.git] / include / llvm / Support / StreamableMemoryObject.h
1 //===- StreamableMemoryObject.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
11 #ifndef LLVM_SUPPORT_STREAMABLEMEMORYOBJECT_H
12 #define LLVM_SUPPORT_STREAMABLEMEMORYOBJECT_H
13
14 #include "llvm/Support/Compiler.h"
15 #include "llvm/Support/DataStream.h"
16 #include "llvm/Support/MemoryObject.h"
17 #include <cassert>
18 #include <memory>
19 #include <vector>
20
21 namespace llvm {
22
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 {
35  public:
36   /// Destructor      - Override as necessary.
37   virtual ~StreamableMemoryObject();
38
39   /// getBase         - Returns the lowest valid address in the region.
40   ///
41   /// @result         - The lowest valid address.
42   virtual uint64_t getBase() const override = 0;
43
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
48   ///
49   /// @result         - The size of the region.
50   virtual uint64_t getExtent() const override = 0;
51
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;
59
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.
66   ///
67   /// @param address  - The address of the first byte, in the same space as
68   ///                   getBase().
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,
75                         uint64_t size,
76                         uint8_t *buf) const override = 0;
77
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;
86
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;
93
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;
100 };
101
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 {
107 public:
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,
113                         uint64_t size,
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");
122     return NULL;
123   }
124   virtual bool isValidAddress(uint64_t address) const override;
125   virtual bool isObjectEnd(uint64_t address) const override;
126
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);
132
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);
137
138 private:
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;
146
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],
157                                         kChunkSize);
158       BytesRead += bytes;
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;
164           EOFReached = true;
165           return false;
166         }
167       }
168     }
169     return true;
170   }
171
172   StreamingMemoryObject(const StreamingMemoryObject&) LLVM_DELETED_FUNCTION;
173   void operator=(const StreamingMemoryObject&) LLVM_DELETED_FUNCTION;
174 };
175
176 StreamableMemoryObject *getNonStreamedMemoryObject(
177     const unsigned char *Start, const unsigned char *End);
178
179 }
180 #endif  // STREAMABLEMEMORYOBJECT_H_