<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Nov 12, 2014 at 9:11 AM, Rafael Espindola <span dir="ltr"><<a href="mailto:rafael.espindola@gmail.com" target="_blank">rafael.espindola@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: rafael<br>
Date: Wed Nov 12 11:11:16 2014<br>
New Revision: 221794<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=221794&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=221794&view=rev</a><br>
Log:<br>
Return the number of read bytes in MemoryObject::readBytes.<br>
<br>
Returning more information will allow BitstreamReader to be simplified a bit<br>
and changed to read 64 bits at a time.<br>
<br>
Modified:<br>
    llvm/trunk/include/llvm/Bitcode/BitstreamReader.h<br>
    llvm/trunk/include/llvm/Support/MemoryObject.h<br>
    llvm/trunk/include/llvm/Support/StreamingMemoryObject.h<br>
    llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp<br>
    llvm/trunk/lib/Support/StreamingMemoryObject.cpp<br>
<br>
Modified: llvm/trunk/include/llvm/Bitcode/BitstreamReader.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Bitcode/BitstreamReader.h?rev=221794&r1=221793&r2=221794&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Bitcode/BitstreamReader.h?rev=221794&r1=221793&r2=221794&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/Bitcode/BitstreamReader.h (original)<br>
+++ llvm/trunk/include/llvm/Bitcode/BitstreamReader.h Wed Nov 12 11:11:16 2014<br>
@@ -341,7 +341,7 @@ public:<br>
     // Read the next word from the stream.<br>
     uint8_t Array[sizeof(word_t)] = {0};<br>
<br>
-    BitStream->getBitcodeBytes().readBytes(NextChar, sizeof(Array), Array);<br>
+    BitStream->getBitcodeBytes().readBytes(Array, sizeof(Array), NextChar);<br>
<br>
     // Handle big-endian byte-swapping if necessary.<br>
     support::detail::packed_endian_specific_integral<br>
<br>
Modified: llvm/trunk/include/llvm/Support/MemoryObject.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/MemoryObject.h?rev=221794&r1=221793&r2=221794&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/MemoryObject.h?rev=221794&r1=221793&r2=221794&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/Support/MemoryObject.h (original)<br>
+++ llvm/trunk/include/llvm/Support/MemoryObject.h Wed Nov 12 11:11:16 2014<br>
@@ -36,18 +36,16 @@ public:<br>
   virtual uint64_t getExtent() const = 0;<br>
<br>
   /// Tries to read a contiguous range of bytes from the region, up to the end<br>
-  /// of the region. You should override this function if there is a quicker way<br>
-  /// than going back and forth with individual bytes.<br>
+  /// of the region.<br>
   ///<br>
   /// @param address  - The address of the first byte, in the same space as<br>
   ///                   getBase().<br>
   /// @param size     - The number of bytes to copy.<br>
   /// @param buf      - A pointer to a buffer to be filled in.  Must be non-NULL<br>
   ///                   and large enough to hold size bytes.<br>
-  /// @result         - 0 if successful; -1 if not.  Failure may be due to a<br>
-  ///                   bounds violation or an implementation-specific error.<br>
-  virtual int readBytes(uint64_t address, uint64_t size,<br>
-                        uint8_t *buf) const = 0;<br>
+  /// @result         - The number of bytes read.<br>
+  virtual uint64_t readBytes(uint8_t *Buf, uint64_t Size,<br></blockquote><div><br></div><div>Should this instead be a MutableArrayRef<uint8_t> ?</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+                             uint64_t Address) const = 0;<br>
<br>
   /// Ensures that the requested data is in memory, and returns a pointer to it.<br>
   /// More efficient than using readBytes if the data is already in memory. May<br>
<br>
Modified: llvm/trunk/include/llvm/Support/StreamingMemoryObject.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/StreamingMemoryObject.h?rev=221794&r1=221793&r2=221794&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/StreamingMemoryObject.h?rev=221794&r1=221793&r2=221794&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/Support/StreamingMemoryObject.h (original)<br>
+++ llvm/trunk/include/llvm/Support/StreamingMemoryObject.h Wed Nov 12 11:11:16 2014<br>
@@ -27,8 +27,8 @@ class StreamingMemoryObject : public Mem<br>
 public:<br>
   StreamingMemoryObject(DataStreamer *streamer);<br>
   uint64_t getExtent() const override;<br>
-  int readBytes(uint64_t address, uint64_t size,<br>
-                uint8_t *buf) const override;<br>
+  uint64_t readBytes(uint8_t *Buf, uint64_t Size,<br>
+                     uint64_t Address) const override;<br>
   const uint8_t *getPointer(uint64_t address, uint64_t size) const override {<br>
     // This could be fixed by ensuring the bytes are fetched and making a copy,<br>
     // requiring that the bitcode size be known, or otherwise ensuring that<br>
<br>
Modified: llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp?rev=221794&r1=221793&r2=221794&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp?rev=221794&r1=221793&r2=221794&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp (original)<br>
+++ llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp Wed Nov 12 11:11:16 2014<br>
@@ -3430,7 +3430,7 @@ std::error_code BitcodeReader::InitLazyS<br>
   Stream.init(&*StreamFile);<br>
<br>
   unsigned char buf[16];<br>
-  if (Bytes->readBytes(0, 16, buf) == -1)<br>
+  if (Bytes->readBytes(buf, 16, 0) != 16)<br>
     return Error(BitcodeError::InvalidBitcodeSignature);<br>
<br>
   if (!isBitcode(buf, buf + 16))<br>
<br>
Modified: llvm/trunk/lib/Support/StreamingMemoryObject.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/StreamingMemoryObject.cpp?rev=221794&r1=221793&r2=221794&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/StreamingMemoryObject.cpp?rev=221794&r1=221793&r2=221794&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Support/StreamingMemoryObject.cpp (original)<br>
+++ llvm/trunk/lib/Support/StreamingMemoryObject.cpp Wed Nov 12 11:11:16 2014<br>
@@ -28,8 +28,8 @@ public:<br>
   uint64_t getExtent() const override {<br>
     return LastChar - FirstChar;<br>
   }<br>
-  int readBytes(uint64_t address, uint64_t size,<br>
-                uint8_t *buf) const override;<br>
+  uint64_t readBytes(uint8_t *Buf, uint64_t Size,<br>
+                     uint64_t Address) const override;<br>
   const uint8_t *getPointer(uint64_t address, uint64_t size) const override;<br>
   bool isValidAddress(uint64_t address) const override {<br>
     return validAddress(address);<br>
@@ -55,12 +55,20 @@ private:<br>
   void operator=(const RawMemoryObject&) LLVM_DELETED_FUNCTION;<br>
 };<br>
<br>
-int RawMemoryObject::readBytes(uint64_t address,<br>
-                               uint64_t size,<br>
-                               uint8_t *buf) const {<br>
-  if (!validAddress(address) || !validAddress(address + size - 1)) return -1;<br>
-  memcpy(buf, (uint8_t *)(uintptr_t)(address + FirstChar), size);<br>
-  return size;<br>
+uint64_t RawMemoryObject::readBytes(uint8_t *Buf, uint64_t Size,<br>
+                                    uint64_t Address) const {<br>
+  uint64_t BufferSize = LastChar - FirstChar;<br>
+  if (Address >= BufferSize)<br>
+    return 0;<br>
+<br>
+  uint64_t End = Address + Size;<br>
+  if (End > BufferSize)<br>
+    End = BufferSize;<br>
+<br>
+  Size = End - Address;<br>
+  assert(Size >= 0);<br>
+  memcpy(Buf, (uint8_t *)(Address + FirstChar), Size);<br>
+  return Size;<br>
 }<br>
<br>
 const uint8_t *RawMemoryObject::getPointer(uint64_t address,<br>
@@ -91,12 +99,20 @@ uint64_t StreamingMemoryObject::getExten<br>
   return ObjectSize;<br>
 }<br>
<br>
-int StreamingMemoryObject::readBytes(uint64_t address,<br>
-                                     uint64_t size,<br>
-                                     uint8_t *buf) const {<br>
-  if (!fetchToPos(address + size - 1)) return -1;<br>
-  memcpy(buf, &Bytes[address + BytesSkipped], size);<br>
-  return 0;<br>
+uint64_t StreamingMemoryObject::readBytes(uint8_t *Buf, uint64_t Size,<br>
+                                          uint64_t Address) const {<br>
+  fetchToPos(Address + Size - 1);<br>
+  uint64_t BufferSize = Bytes.size() - BytesSkipped;<br>
+  if (Address >= BufferSize)<br>
+    return 0;<br>
+<br>
+  uint64_t End = Address + Size;<br>
+  if (End > BufferSize)<br>
+    End = BufferSize;<br>
+  Size = End - Address;<br>
+  assert(Size >= 0);<br>
+  memcpy(Buf, &Bytes[Address + BytesSkipped], Size);<br>
+  return Size;<br>
 }<br>
<br>
 bool StreamingMemoryObject::dropLeadingBytes(size_t s) {<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@cs.uiuc.edu">llvm-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br></div></div>