Return the number of read bytes in MemoryObject::readBytes.
[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   bool isObjectEnd(uint64_t address) const override {
38     return objectEnd(address);
39   }
40
41 private:
42   const uint8_t* const FirstChar;
43   const uint8_t* const LastChar;
44
45   // These are implemented as inline functions here to avoid multiple virtual
46   // calls per public function
47   bool validAddress(uint64_t address) const {
48     return static_cast<std::ptrdiff_t>(address) < LastChar - FirstChar;
49   }
50   bool objectEnd(uint64_t address) const {
51     return static_cast<std::ptrdiff_t>(address) == LastChar - FirstChar;
52   }
53
54   RawMemoryObject(const RawMemoryObject&) LLVM_DELETED_FUNCTION;
55   void operator=(const RawMemoryObject&) LLVM_DELETED_FUNCTION;
56 };
57
58 uint64_t RawMemoryObject::readBytes(uint8_t *Buf, uint64_t Size,
59                                     uint64_t Address) const {
60   uint64_t BufferSize = LastChar - FirstChar;
61   if (Address >= BufferSize)
62     return 0;
63
64   uint64_t End = Address + Size;
65   if (End > BufferSize)
66     End = BufferSize;
67
68   Size = End - Address;
69   assert(Size >= 0);
70   memcpy(Buf, (uint8_t *)(Address + FirstChar), Size);
71   return Size;
72 }
73
74 const uint8_t *RawMemoryObject::getPointer(uint64_t address,
75                                            uint64_t size) const {
76   return FirstChar + address;
77 }
78 } // anonymous namespace
79
80 namespace llvm {
81 // If the bitcode has a header, then its size is known, and we don't have to
82 // block until we actually want to read it.
83 bool StreamingMemoryObject::isValidAddress(uint64_t address) const {
84   if (ObjectSize && address < ObjectSize) return true;
85     return fetchToPos(address);
86 }
87
88 bool StreamingMemoryObject::isObjectEnd(uint64_t address) const {
89   if (ObjectSize) return address == ObjectSize;
90   fetchToPos(address);
91   return address == ObjectSize && address != 0;
92 }
93
94 uint64_t StreamingMemoryObject::getExtent() const {
95   if (ObjectSize) return ObjectSize;
96   size_t pos = BytesRead + kChunkSize;
97   // keep fetching until we run out of bytes
98   while (fetchToPos(pos)) pos += kChunkSize;
99   return ObjectSize;
100 }
101
102 uint64_t StreamingMemoryObject::readBytes(uint8_t *Buf, uint64_t Size,
103                                           uint64_t Address) const {
104   fetchToPos(Address + Size - 1);
105   uint64_t BufferSize = Bytes.size() - BytesSkipped;
106   if (Address >= BufferSize)
107     return 0;
108
109   uint64_t End = Address + Size;
110   if (End > BufferSize)
111     End = BufferSize;
112   Size = End - Address;
113   assert(Size >= 0);
114   memcpy(Buf, &Bytes[Address + BytesSkipped], Size);
115   return Size;
116 }
117
118 bool StreamingMemoryObject::dropLeadingBytes(size_t s) {
119   if (BytesRead < s) return true;
120   BytesSkipped = s;
121   BytesRead -= s;
122   return false;
123 }
124
125 void StreamingMemoryObject::setKnownObjectSize(size_t size) {
126   ObjectSize = size;
127   Bytes.reserve(size);
128 }
129
130 MemoryObject *getNonStreamedMemoryObject(const unsigned char *Start,
131                                          const unsigned char *End) {
132   return new RawMemoryObject(Start, End);
133 }
134
135 StreamingMemoryObject::StreamingMemoryObject(DataStreamer *streamer) :
136   Bytes(kChunkSize), Streamer(streamer), BytesRead(0), BytesSkipped(0),
137   ObjectSize(0), EOFReached(false) {
138   BytesRead = streamer->GetBytes(&Bytes[0], kChunkSize);
139 }
140 }