Teach lint about address spaces
[oota-llvm.git] / lib / Support / StreamableMemoryObject.cpp
1 //===- StreamableMemoryObject.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/StreamableMemoryObject.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 StreamableMemoryObject {
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   virtual uint64_t getBase() const override { return 0; }
29   virtual uint64_t getExtent() const override {
30     return LastChar - FirstChar;
31   }
32   virtual int readByte(uint64_t address, uint8_t* ptr) const override;
33   virtual int readBytes(uint64_t address,
34                         uint64_t size,
35                         uint8_t *buf) const override;
36   virtual const uint8_t *getPointer(uint64_t address,
37                                     uint64_t size) const override;
38   virtual bool isValidAddress(uint64_t address) const override {
39     return validAddress(address);
40   }
41   virtual bool isObjectEnd(uint64_t address) const override {
42     return objectEnd(address);
43   }
44
45 private:
46   const uint8_t* const FirstChar;
47   const uint8_t* const LastChar;
48
49   // These are implemented as inline functions here to avoid multiple virtual
50   // calls per public function
51   bool validAddress(uint64_t address) const {
52     return static_cast<std::ptrdiff_t>(address) < LastChar - FirstChar;
53   }
54   bool objectEnd(uint64_t address) const {
55     return static_cast<std::ptrdiff_t>(address) == LastChar - FirstChar;
56   }
57
58   RawMemoryObject(const RawMemoryObject&) LLVM_DELETED_FUNCTION;
59   void operator=(const RawMemoryObject&) LLVM_DELETED_FUNCTION;
60 };
61
62 int RawMemoryObject::readByte(uint64_t address, uint8_t* ptr) const {
63   if (!validAddress(address)) return -1;
64   *ptr = *((uint8_t *)(uintptr_t)(address + FirstChar));
65   return 0;
66 }
67
68 int RawMemoryObject::readBytes(uint64_t address,
69                                uint64_t size,
70                                uint8_t *buf) const {
71   if (!validAddress(address) || !validAddress(address + size - 1)) return -1;
72   memcpy(buf, (uint8_t *)(uintptr_t)(address + FirstChar), size);
73   return size;
74 }
75
76 const uint8_t *RawMemoryObject::getPointer(uint64_t address,
77                                            uint64_t size) const {
78   return FirstChar + address;
79 }
80 } // anonymous namespace
81
82 namespace llvm {
83 // If the bitcode has a header, then its size is known, and we don't have to
84 // block until we actually want to read it.
85 bool StreamingMemoryObject::isValidAddress(uint64_t address) const {
86   if (ObjectSize && address < ObjectSize) return true;
87     return fetchToPos(address);
88 }
89
90 bool StreamingMemoryObject::isObjectEnd(uint64_t address) const {
91   if (ObjectSize) return address == ObjectSize;
92   fetchToPos(address);
93   return address == ObjectSize && address != 0;
94 }
95
96 uint64_t StreamingMemoryObject::getExtent() const {
97   if (ObjectSize) return ObjectSize;
98   size_t pos = BytesRead + kChunkSize;
99   // keep fetching until we run out of bytes
100   while (fetchToPos(pos)) pos += kChunkSize;
101   return ObjectSize;
102 }
103
104 int StreamingMemoryObject::readByte(uint64_t address, uint8_t* ptr) const {
105   if (!fetchToPos(address)) return -1;
106   *ptr = Bytes[address + BytesSkipped];
107   return 0;
108 }
109
110 int StreamingMemoryObject::readBytes(uint64_t address,
111                                      uint64_t size,
112                                      uint8_t *buf) const {
113   if (!fetchToPos(address + size - 1)) return -1;
114   memcpy(buf, &Bytes[address + BytesSkipped], size);
115   return 0;
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 StreamableMemoryObject *getNonStreamedMemoryObject(
131     const unsigned char *Start, const unsigned char *End) {
132   return new RawMemoryObject(Start, End);
133 }
134
135 StreamableMemoryObject::~StreamableMemoryObject() { }
136
137 StreamingMemoryObject::StreamingMemoryObject(DataStreamer *streamer) :
138   Bytes(kChunkSize), Streamer(streamer), BytesRead(0), BytesSkipped(0),
139   ObjectSize(0), EOFReached(false) {
140   BytesRead = streamer->GetBytes(&Bytes[0], kChunkSize);
141 }
142 }