Merge StreamableMemoryObject into MemoryObject.
[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   int readBytes(uint64_t address, uint64_t size,
32                 uint8_t *buf) 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 int RawMemoryObject::readBytes(uint64_t address,
59                                uint64_t size,
60                                uint8_t *buf) const {
61   if (!validAddress(address) || !validAddress(address + size - 1)) return -1;
62   memcpy(buf, (uint8_t *)(uintptr_t)(address + FirstChar), size);
63   return size;
64 }
65
66 const uint8_t *RawMemoryObject::getPointer(uint64_t address,
67                                            uint64_t size) const {
68   return FirstChar + address;
69 }
70 } // anonymous namespace
71
72 namespace llvm {
73 // If the bitcode has a header, then its size is known, and we don't have to
74 // block until we actually want to read it.
75 bool StreamingMemoryObject::isValidAddress(uint64_t address) const {
76   if (ObjectSize && address < ObjectSize) return true;
77     return fetchToPos(address);
78 }
79
80 bool StreamingMemoryObject::isObjectEnd(uint64_t address) const {
81   if (ObjectSize) return address == ObjectSize;
82   fetchToPos(address);
83   return address == ObjectSize && address != 0;
84 }
85
86 uint64_t StreamingMemoryObject::getExtent() const {
87   if (ObjectSize) return ObjectSize;
88   size_t pos = BytesRead + kChunkSize;
89   // keep fetching until we run out of bytes
90   while (fetchToPos(pos)) pos += kChunkSize;
91   return ObjectSize;
92 }
93
94 int StreamingMemoryObject::readBytes(uint64_t address,
95                                      uint64_t size,
96                                      uint8_t *buf) const {
97   if (!fetchToPos(address + size - 1)) return -1;
98   memcpy(buf, &Bytes[address + BytesSkipped], size);
99   return 0;
100 }
101
102 bool StreamingMemoryObject::dropLeadingBytes(size_t s) {
103   if (BytesRead < s) return true;
104   BytesSkipped = s;
105   BytesRead -= s;
106   return false;
107 }
108
109 void StreamingMemoryObject::setKnownObjectSize(size_t size) {
110   ObjectSize = size;
111   Bytes.reserve(size);
112 }
113
114 MemoryObject *getNonStreamedMemoryObject(const unsigned char *Start,
115                                          const unsigned char *End) {
116   return new RawMemoryObject(Start, End);
117 }
118
119 StreamingMemoryObject::StreamingMemoryObject(DataStreamer *streamer) :
120   Bytes(kChunkSize), Streamer(streamer), BytesRead(0), BytesSkipped(0),
121   ObjectSize(0), EOFReached(false) {
122   BytesRead = streamer->GetBytes(&Bytes[0], kChunkSize);
123 }
124 }