• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <cassert>
12 #include <cstddef>
13 #include <cstring>
14 using namespace llvm;
15 
16 namespace {
17 
18 class RawMemoryObject : public MemoryObject {
19 public:
RawMemoryObject(const unsigned char * Start,const unsigned char * End)20   RawMemoryObject(const unsigned char *Start, const unsigned char *End)
21       : FirstChar(Start), LastChar(End) {
22     assert(LastChar >= FirstChar && "Invalid start/end range");
23   }
24 
getExtent() const25   uint64_t getExtent() const override { return LastChar - FirstChar; }
26   uint64_t readBytes(uint8_t *Buf, uint64_t Size,
27                      uint64_t Address) const override;
28   const uint8_t *getPointer(uint64_t address, uint64_t size) const override;
isValidAddress(uint64_t address) const29   bool isValidAddress(uint64_t address) const override {
30     return validAddress(address);
31   }
32 
33 private:
34   const uint8_t *const FirstChar;
35   const uint8_t *const LastChar;
36 
37   // These are implemented as inline functions here to avoid multiple virtual
38   // calls per public function
validAddress(uint64_t address) const39   bool validAddress(uint64_t address) const {
40     return static_cast<std::ptrdiff_t>(address) < LastChar - FirstChar;
41   }
42 
43   RawMemoryObject(const RawMemoryObject &) = delete;
44   void operator=(const RawMemoryObject &) = delete;
45 };
46 
readBytes(uint8_t * Buf,uint64_t Size,uint64_t Address) const47 uint64_t RawMemoryObject::readBytes(uint8_t *Buf, uint64_t Size,
48                                     uint64_t Address) const {
49   uint64_t BufferSize = LastChar - FirstChar;
50   if (Address >= BufferSize)
51     return 0;
52 
53   uint64_t End = Address + Size;
54   if (End > BufferSize)
55     End = BufferSize;
56 
57   assert(static_cast<int64_t>(End - Address) >= 0);
58   Size = End - Address;
59   memcpy(Buf, Address + FirstChar, Size);
60   return Size;
61 }
62 
getPointer(uint64_t address,uint64_t size) const63 const uint8_t *RawMemoryObject::getPointer(uint64_t address,
64                                            uint64_t size) const {
65   return FirstChar + address;
66 }
67 } // anonymous namespace
68 
69 namespace llvm {
70 // If the bitcode has a header, then its size is known, and we don't have to
71 // block until we actually want to read it.
isValidAddress(uint64_t address) const72 bool StreamingMemoryObject::isValidAddress(uint64_t address) const {
73   if (ObjectSize && address < ObjectSize)
74     return true;
75   return fetchToPos(address);
76 }
77 
getExtent() const78 uint64_t StreamingMemoryObject::getExtent() const {
79   if (ObjectSize)
80     return ObjectSize;
81   size_t pos = BytesRead + kChunkSize;
82   // keep fetching until we run out of bytes
83   while (fetchToPos(pos))
84     pos += kChunkSize;
85   return ObjectSize;
86 }
87 
readBytes(uint8_t * Buf,uint64_t Size,uint64_t Address) const88 uint64_t StreamingMemoryObject::readBytes(uint8_t *Buf, uint64_t Size,
89                                           uint64_t Address) const {
90   fetchToPos(Address + Size - 1);
91   // Note: For wrapped bitcode files will set ObjectSize after the
92   // first call to fetchToPos. In such cases, ObjectSize can be
93   // smaller than BytesRead.
94   size_t MaxAddress =
95       (ObjectSize && ObjectSize < BytesRead) ? ObjectSize : BytesRead;
96   if (Address >= MaxAddress)
97     return 0;
98 
99   uint64_t End = Address + Size;
100   if (End > MaxAddress)
101     End = MaxAddress;
102   assert(End >= Address);
103   Size = End - Address;
104   memcpy(Buf, &Bytes[Address + BytesSkipped], Size);
105   return Size;
106 }
107 
getPointer(uint64_t Address,uint64_t Size) const108 const uint8_t *StreamingMemoryObject::getPointer(uint64_t Address,
109                                                  uint64_t Size) const {
110   fetchToPos(Address + Size - 1);
111   return &Bytes[Address + BytesSkipped];
112 }
113 
dropLeadingBytes(size_t s)114 bool StreamingMemoryObject::dropLeadingBytes(size_t s) {
115   if (BytesRead < s)
116     return true;
117   BytesSkipped = s;
118   BytesRead -= s;
119   return false;
120 }
121 
setKnownObjectSize(size_t size)122 void StreamingMemoryObject::setKnownObjectSize(size_t size) {
123   ObjectSize = size;
124   Bytes.reserve(size);
125   if (ObjectSize <= BytesRead)
126     EOFReached = true;
127 }
128 
getNonStreamedMemoryObject(const unsigned char * Start,const unsigned char * End)129 MemoryObject *getNonStreamedMemoryObject(const unsigned char *Start,
130                                          const unsigned char *End) {
131   return new RawMemoryObject(Start, End);
132 }
133 
StreamingMemoryObject(std::unique_ptr<DataStreamer> Streamer)134 StreamingMemoryObject::StreamingMemoryObject(
135     std::unique_ptr<DataStreamer> Streamer)
136     : Bytes(kChunkSize), Streamer(std::move(Streamer)), BytesRead(0),
137       BytesSkipped(0), ObjectSize(0), EOFReached(false) {
138   BytesRead = this->Streamer->GetBytes(&Bytes[0], kChunkSize);
139 }
140 } // namespace llvm
141