blob: f0eb83153e0cc7e4d7754abb2a40fc0778cfb4f3 [file] [log] [blame]
Rafael Espindola79e1f9f2014-11-12 03:55:46 +00001//===- StreamingMemoryObject.cpp - Streamable data interface -------------===//
Derek Schuff8b2dcad2012-02-06 22:30:29 +00002//
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
Rafael Espindola79e1f9f2014-11-12 03:55:46 +000010#include "llvm/Support/StreamingMemoryObject.h"
Craig Toppera60c0f12012-09-15 17:09:36 +000011#include "llvm/Support/Compiler.h"
Derek Schuff8b2dcad2012-02-06 22:30:29 +000012#include <cassert>
Ahmed Charlesf6863f12014-03-06 06:13:54 +000013#include <cstddef>
Derek Schuff8b2dcad2012-02-06 22:30:29 +000014#include <cstring>
15
16
17using namespace llvm;
18
19namespace {
20
Rafael Espindola79e1f9f2014-11-12 03:55:46 +000021class RawMemoryObject : public MemoryObject {
Derek Schuff8b2dcad2012-02-06 22:30:29 +000022public:
23 RawMemoryObject(const unsigned char *Start, const unsigned char *End) :
24 FirstChar(Start), LastChar(End) {
Kaelyn Uhrain2212f802012-06-20 00:16:40 +000025 assert(LastChar >= FirstChar && "Invalid start/end range");
Derek Schuff8b2dcad2012-02-06 22:30:29 +000026 }
27
Craig Topper6ff5aa72014-03-10 03:53:12 +000028 uint64_t getExtent() const override {
Craig Topper3186c012012-09-23 02:12:10 +000029 return LastChar - FirstChar;
30 }
Rafael Espindola2d05db42014-11-12 17:11:16 +000031 uint64_t readBytes(uint8_t *Buf, uint64_t Size,
32 uint64_t Address) const override;
Craig Topper6ff5aa72014-03-10 03:53:12 +000033 const uint8_t *getPointer(uint64_t address, uint64_t size) const override;
34 bool isValidAddress(uint64_t address) const override {
Derek Schuff56b662c2012-02-29 01:09:06 +000035 return validAddress(address);
36 }
Derek Schuff8b2dcad2012-02-06 22:30:29 +000037
38private:
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
Derek Schuff56b662c2012-02-29 01:09:06 +000044 bool validAddress(uint64_t address) const {
Ahmed Charles0a685a92014-03-06 06:05:26 +000045 return static_cast<std::ptrdiff_t>(address) < LastChar - FirstChar;
Derek Schuff8b2dcad2012-02-06 22:30:29 +000046 }
Derek Schuff8b2dcad2012-02-06 22:30:29 +000047
Craig Toppera60c0f12012-09-15 17:09:36 +000048 RawMemoryObject(const RawMemoryObject&) LLVM_DELETED_FUNCTION;
49 void operator=(const RawMemoryObject&) LLVM_DELETED_FUNCTION;
Derek Schuff8b2dcad2012-02-06 22:30:29 +000050};
51
Rafael Espindola2d05db42014-11-12 17:11:16 +000052uint64_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 Size = End - Address;
63 assert(Size >= 0);
64 memcpy(Buf, (uint8_t *)(Address + FirstChar), Size);
65 return Size;
Derek Schuff8b2dcad2012-02-06 22:30:29 +000066}
67
Derek Schuff56b662c2012-02-29 01:09:06 +000068const uint8_t *RawMemoryObject::getPointer(uint64_t address,
69 uint64_t size) const {
Derek Schuff8b2dcad2012-02-06 22:30:29 +000070 return FirstChar + address;
71}
72} // anonymous namespace
73
74namespace 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.
Derek Schuff56b662c2012-02-29 01:09:06 +000077bool StreamingMemoryObject::isValidAddress(uint64_t address) const {
Derek Schuff8b2dcad2012-02-06 22:30:29 +000078 if (ObjectSize && address < ObjectSize) return true;
79 return fetchToPos(address);
80}
81
Derek Schuff56b662c2012-02-29 01:09:06 +000082uint64_t StreamingMemoryObject::getExtent() const {
Derek Schuff8b2dcad2012-02-06 22:30:29 +000083 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
Rafael Espindola2d05db42014-11-12 17:11:16 +000090uint64_t StreamingMemoryObject::readBytes(uint8_t *Buf, uint64_t Size,
91 uint64_t Address) const {
92 fetchToPos(Address + Size - 1);
Rafael Espindolac11bd422014-11-13 07:23:22 +000093 if (Address >= BytesRead)
Rafael Espindola2d05db42014-11-12 17:11:16 +000094 return 0;
95
96 uint64_t End = Address + Size;
Rafael Espindolac11bd422014-11-13 07:23:22 +000097 if (End > BytesRead)
98 End = BytesRead;
Rafael Espindola2d05db42014-11-12 17:11:16 +000099 Size = End - Address;
100 assert(Size >= 0);
101 memcpy(Buf, &Bytes[Address + BytesSkipped], Size);
102 return Size;
Derek Schuff8b2dcad2012-02-06 22:30:29 +0000103}
104
105bool StreamingMemoryObject::dropLeadingBytes(size_t s) {
106 if (BytesRead < s) return true;
107 BytesSkipped = s;
108 BytesRead -= s;
109 return false;
110}
111
112void StreamingMemoryObject::setKnownObjectSize(size_t size) {
113 ObjectSize = size;
114 Bytes.reserve(size);
115}
116
Rafael Espindola79e1f9f2014-11-12 03:55:46 +0000117MemoryObject *getNonStreamedMemoryObject(const unsigned char *Start,
118 const unsigned char *End) {
Derek Schuff8b2dcad2012-02-06 22:30:29 +0000119 return new RawMemoryObject(Start, End);
120}
121
Derek Schuff8b2dcad2012-02-06 22:30:29 +0000122StreamingMemoryObject::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}