llvm.org GIT mirror llvm / d0ab582
Return the number of read bytes in MemoryObject::readBytes. Returning more information will allow BitstreamReader to be simplified a bit and changed to read 64 bits at a time. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@221794 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 5 years ago
5 changed file(s) with 38 addition(s) and 24 deletion(s). Raw diff Collapse all Expand all
340340 // Read the next word from the stream.
341341 uint8_t Array[sizeof(word_t)] = {0};
342342
343 BitStream->getBitcodeBytes().readBytes(NextChar, sizeof(Array), Array);
343 BitStream->getBitcodeBytes().readBytes(Array, sizeof(Array), NextChar);
344344
345345 // Handle big-endian byte-swapping if necessary.
346346 support::detail::packed_endian_specific_integral
3535 virtual uint64_t getExtent() const = 0;
3636
3737 /// Tries to read a contiguous range of bytes from the region, up to the end
38 /// of the region. You should override this function if there is a quicker way
39 /// than going back and forth with individual bytes.
38 /// of the region.
4039 ///
4140 /// @param address - The address of the first byte, in the same space as
4241 /// getBase().
4342 /// @param size - The number of bytes to copy.
4443 /// @param buf - A pointer to a buffer to be filled in. Must be non-NULL
4544 /// and large enough to hold size bytes.
46 /// @result - 0 if successful; -1 if not. Failure may be due to a
47 /// bounds violation or an implementation-specific error.
48 virtual int readBytes(uint64_t address, uint64_t size,
49 uint8_t *buf) const = 0;
45 /// @result - The number of bytes read.
46 virtual uint64_t readBytes(uint8_t *Buf, uint64_t Size,
47 uint64_t Address) const = 0;
5048
5149 /// Ensures that the requested data is in memory, and returns a pointer to it.
5250 /// More efficient than using readBytes if the data is already in memory. May
2626 public:
2727 StreamingMemoryObject(DataStreamer *streamer);
2828 uint64_t getExtent() const override;
29 int readBytes(uint64_t address, uint64_t size,
30 uint8_t *buf) const override;
29 uint64_t readBytes(uint8_t *Buf, uint64_t Size,
30 uint64_t Address) const override;
3131 const uint8_t *getPointer(uint64_t address, uint64_t size) const override {
3232 // This could be fixed by ensuring the bytes are fetched and making a copy,
3333 // requiring that the bitcode size be known, or otherwise ensuring that
34293429 Stream.init(&*StreamFile);
34303430
34313431 unsigned char buf[16];
3432 if (Bytes->readBytes(0, 16, buf) == -1)
3432 if (Bytes->readBytes(buf, 16, 0) != 16)
34333433 return Error(BitcodeError::InvalidBitcodeSignature);
34343434
34353435 if (!isBitcode(buf, buf + 16))
2727 uint64_t getExtent() const override {
2828 return LastChar - FirstChar;
2929 }
30 int readBytes(uint64_t address, uint64_t size,
31 uint8_t *buf) const override;
30 uint64_t readBytes(uint8_t *Buf, uint64_t Size,
31 uint64_t Address) const override;
3232 const uint8_t *getPointer(uint64_t address, uint64_t size) const override;
3333 bool isValidAddress(uint64_t address) const override {
3434 return validAddress(address);
5454 void operator=(const RawMemoryObject&) LLVM_DELETED_FUNCTION;
5555 };
5656
57 int RawMemoryObject::readBytes(uint64_t address,
58 uint64_t size,
59 uint8_t *buf) const {
60 if (!validAddress(address) || !validAddress(address + size - 1)) return -1;
61 memcpy(buf, (uint8_t *)(uintptr_t)(address + FirstChar), size);
62 return size;
57 uint64_t RawMemoryObject::readBytes(uint8_t *Buf, uint64_t Size,
58 uint64_t Address) const {
59 uint64_t BufferSize = LastChar - FirstChar;
60 if (Address >= BufferSize)
61 return 0;
62
63 uint64_t End = Address + Size;
64 if (End > BufferSize)
65 End = BufferSize;
66
67 Size = End - Address;
68 assert(Size >= 0);
69 memcpy(Buf, (uint8_t *)(Address + FirstChar), Size);
70 return Size;
6371 }
6472
6573 const uint8_t *RawMemoryObject::getPointer(uint64_t address,
9098 return ObjectSize;
9199 }
92100
93 int StreamingMemoryObject::readBytes(uint64_t address,
94 uint64_t size,
95 uint8_t *buf) const {
96 if (!fetchToPos(address + size - 1)) return -1;
97 memcpy(buf, &Bytes[address + BytesSkipped], size);
98 return 0;
101 uint64_t StreamingMemoryObject::readBytes(uint8_t *Buf, uint64_t Size,
102 uint64_t Address) const {
103 fetchToPos(Address + Size - 1);
104 uint64_t BufferSize = Bytes.size() - BytesSkipped;
105 if (Address >= BufferSize)
106 return 0;
107
108 uint64_t End = Address + Size;
109 if (End > BufferSize)
110 End = BufferSize;
111 Size = End - Address;
112 assert(Size >= 0);
113 memcpy(Buf, &Bytes[Address + BytesSkipped], Size);
114 return Size;
99115 }
100116
101117 bool StreamingMemoryObject::dropLeadingBytes(size_t s) {