[Orc] Fix the MSVC bots by using LLVM_EXPLICIT rather than explicit.
[oota-llvm.git] / lib / Support / StreamingMemoryObject.cpp
1 //===- StreamingMemoryObject.cpp - Streamable data interface -------------===//
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 #include "llvm/Support/StreamingMemoryObject.h"
11 #include "llvm/Support/Compiler.h"
12 #include <cassert>
13 #include <cstddef>
14 #include <cstring>
15
16
17 using namespace llvm;
18
19 namespace {
20
21 class RawMemoryObject : public MemoryObject {
22 public:
23   RawMemoryObject(const unsigned char *Start, const unsigned char *End) :
24     FirstChar(Start), LastChar(End) {
25     assert(LastChar >= FirstChar && "Invalid start/end range");
26   }
27
28   uint64_t getExtent() const override {
29     return LastChar - FirstChar;
30   }
31   uint64_t readBytes(uint8_t *Buf, uint64_t Size,
32                      uint64_t Address) const override;
33   const uint8_t *getPointer(uint64_t address, uint64_t size) const override;
34   bool isValidAddress(uint64_t address) const override {
35     return validAddress(address);
36   }
37
38 private:
39   const uint8_t* const FirstChar;
40   const uint8_t* const LastChar;
41
42   // These are implemented as inline functions here to avoid multiple virtual
43   // calls per public function
44   bool validAddress(uint64_t address) const {
45     return static_cast<std::ptrdiff_t>(address) < LastChar - FirstChar;
46   }
47
48   RawMemoryObject(const RawMemoryObject&) LLVM_DELETED_FUNCTION;
49   void operator=(const RawMemoryObject&) LLVM_DELETED_FUNCTION;
50 };
51
52 uint64_t RawMemoryObject::readBytes(uint8_t *Buf, uint64_t Size,
53                                     uint64_t Address) const {
54   uint64_t BufferSize = LastChar - FirstChar;
55   if (Address >= BufferSize)
56     return 0;
57
58   uint64_t End = Address + Size;
59   if (End > BufferSize)
60     End = BufferSize;
61
62   assert(static_cast<int64_t>(End - Address) >= 0);
63   Size = End - Address;
64   memcpy(Buf, Address + FirstChar, Size);
65   return Size;
66 }
67
68 const uint8_t *RawMemoryObject::getPointer(uint64_t address,
69                                            uint64_t size) const {
70   return FirstChar + address;
71 }
72 } // anonymous namespace
73
74 namespace llvm {
75 // If the bitcode has a header, then its size is known, and we don't have to
76 // block until we actually want to read it.
77 bool StreamingMemoryObject::isValidAddress(uint64_t address) const {
78   if (ObjectSize && address < ObjectSize) return true;
79     return fetchToPos(address);
80 }
81
82 uint64_t StreamingMemoryObject::getExtent() const {
83   if (ObjectSize) return ObjectSize;
84   size_t pos = BytesRead + kChunkSize;
85   // keep fetching until we run out of bytes
86   while (fetchToPos(pos)) pos += kChunkSize;
87   return ObjectSize;
88 }
89
90 uint64_t StreamingMemoryObject::readBytes(uint8_t *Buf, uint64_t Size,
91                                           uint64_t Address) const {
92   fetchToPos(Address + Size - 1);
93   if (Address >= BytesRead)
94     return 0;
95
96   uint64_t End = Address + Size;
97   if (End > BytesRead)
98     End = BytesRead;
99   assert(static_cast<int64_t>(End - Address) >= 0);
100   Size = End - Address;
101   memcpy(Buf, &Bytes[Address + BytesSkipped], Size);
102   return Size;
103 }
104
105 bool StreamingMemoryObject::dropLeadingBytes(size_t s) {
106   if (BytesRead < s) return true;
107   BytesSkipped = s;
108   BytesRead -= s;
109   return false;
110 }
111
112 void StreamingMemoryObject::setKnownObjectSize(size_t size) {
113   ObjectSize = size;
114   Bytes.reserve(size);
115 }
116
117 MemoryObject *getNonStreamedMemoryObject(const unsigned char *Start,
118                                          const unsigned char *End) {
119   return new RawMemoryObject(Start, End);
120 }
121
122 StreamingMemoryObject::StreamingMemoryObject(DataStreamer *streamer) :
123   Bytes(kChunkSize), Streamer(streamer), BytesRead(0), BytesSkipped(0),
124   ObjectSize(0), EOFReached(false) {
125   BytesRead = streamer->GetBytes(&Bytes[0], kChunkSize);
126 }
127 }