summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Support/StreamingMemoryObject.cpp
blob: 7187ce013faf62d8c32812918f721411c74431fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//===- StreamingMemoryObject.cpp - Streamable data interface -------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "llvm/Support/StreamingMemoryObject.h"
#include "llvm/Support/Compiler.h"
#include <cassert>
#include <cstddef>
#include <cstring>


using namespace llvm;

namespace {

class RawMemoryObject : public MemoryObject {
public:
  RawMemoryObject(const unsigned char *Start, const unsigned char *End) :
    FirstChar(Start), LastChar(End) {
    assert(LastChar >= FirstChar && "Invalid start/end range");
  }

  uint64_t getExtent() const override {
    return LastChar - FirstChar;
  }
  int readBytes(uint64_t address, uint64_t size,
                uint8_t *buf) const override;
  const uint8_t *getPointer(uint64_t address, uint64_t size) const override;
  bool isValidAddress(uint64_t address) const override {
    return validAddress(address);
  }
  bool isObjectEnd(uint64_t address) const override {
    return objectEnd(address);
  }

private:
  const uint8_t* const FirstChar;
  const uint8_t* const LastChar;

  // These are implemented as inline functions here to avoid multiple virtual
  // calls per public function
  bool validAddress(uint64_t address) const {
    return static_cast<std::ptrdiff_t>(address) < LastChar - FirstChar;
  }
  bool objectEnd(uint64_t address) const {
    return static_cast<std::ptrdiff_t>(address) == LastChar - FirstChar;
  }

  RawMemoryObject(const RawMemoryObject&) LLVM_DELETED_FUNCTION;
  void operator=(const RawMemoryObject&) LLVM_DELETED_FUNCTION;
};

int RawMemoryObject::readBytes(uint64_t address,
                               uint64_t size,
                               uint8_t *buf) const {
  if (!validAddress(address) || !validAddress(address + size - 1)) return -1;
  memcpy(buf, (uint8_t *)(uintptr_t)(address + FirstChar), size);
  return size;
}

const uint8_t *RawMemoryObject::getPointer(uint64_t address,
                                           uint64_t size) const {
  return FirstChar + address;
}
} // anonymous namespace

namespace llvm {
// If the bitcode has a header, then its size is known, and we don't have to
// block until we actually want to read it.
bool StreamingMemoryObject::isValidAddress(uint64_t address) const {
  if (ObjectSize && address < ObjectSize) return true;
    return fetchToPos(address);
}

bool StreamingMemoryObject::isObjectEnd(uint64_t address) const {
  if (ObjectSize) return address == ObjectSize;
  fetchToPos(address);
  return address == ObjectSize && address != 0;
}

uint64_t StreamingMemoryObject::getExtent() const {
  if (ObjectSize) return ObjectSize;
  size_t pos = BytesRead + kChunkSize;
  // keep fetching until we run out of bytes
  while (fetchToPos(pos)) pos += kChunkSize;
  return ObjectSize;
}

int StreamingMemoryObject::readBytes(uint64_t address,
                                     uint64_t size,
                                     uint8_t *buf) const {
  if (!fetchToPos(address + size - 1)) return -1;
  memcpy(buf, &Bytes[address + BytesSkipped], size);
  return 0;
}

bool StreamingMemoryObject::dropLeadingBytes(size_t s) {
  if (BytesRead < s) return true;
  BytesSkipped = s;
  BytesRead -= s;
  return false;
}

void StreamingMemoryObject::setKnownObjectSize(size_t size) {
  ObjectSize = size;
  Bytes.reserve(size);
}

MemoryObject *getNonStreamedMemoryObject(const unsigned char *Start,
                                         const unsigned char *End) {
  return new RawMemoryObject(Start, End);
}

StreamingMemoryObject::StreamingMemoryObject(DataStreamer *streamer) :
  Bytes(kChunkSize), Streamer(streamer), BytesRead(0), BytesSkipped(0),
  ObjectSize(0), EOFReached(false) {
  BytesRead = streamer->GetBytes(&Bytes[0], kChunkSize);
}
}
OpenPOWER on IntegriCloud