[Lldb-commits] [lldb] r342044 - Remove manual byte counting from internal Stream methods.

Raphael Isemann via lldb-commits lldb-commits at lists.llvm.org
Wed Sep 12 03:20:42 PDT 2018


Author: teemperor
Date: Wed Sep 12 03:20:41 2018
New Revision: 342044

URL: http://llvm.org/viewvc/llvm-project?rev=342044&view=rev
Log:
Remove manual byte counting from internal Stream methods.

Summary:
This patch removes the manual byte counting in all internal Stream methods.
This is now done by the automatic byte counting provided by calling `GetWrittenBytes()`
before and after writing the data (which is automatically done for us by the `ByteDelta`
utility class).

Reviewers: #lldb, JDevlieghere

Reviewed By: JDevlieghere

Subscribers: JDevlieghere, labath, lldb-commits

Differential Revision: https://reviews.llvm.org/D50681

Modified:
    lldb/trunk/include/lldb/Utility/Stream.h
    lldb/trunk/source/Utility/Stream.cpp

Modified: lldb/trunk/include/lldb/Utility/Stream.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Utility/Stream.h?rev=342044&r1=342043&r2=342044&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Utility/Stream.h (original)
+++ lldb/trunk/include/lldb/Utility/Stream.h Wed Sep 12 03:20:41 2018
@@ -38,6 +38,25 @@ public:
                        /// string mode.
   };
 
+  /// Utility class for counting the bytes that were written to a stream in a
+  /// certain time span.
+  /// @example
+  ///   ByteDelta delta(*this);
+  ///   WriteDataToStream("foo");
+  ///   return *delta;
+  /// @endcode
+  class ByteDelta {
+    Stream *m_stream;
+    /// Bytes we have written so far when ByteDelta was created.
+    size_t m_start;
+
+  public:
+    ByteDelta(Stream &s) : m_stream(&s), m_start(s.GetWrittenBytes()) {}
+    /// Returns the number of bytes written to the given Stream since this
+    /// ByteDelta object was created.
+    size_t operator*() const { return m_stream->GetWrittenBytes() - m_start; }
+  };
+
   //------------------------------------------------------------------
   /// Construct with flags and address size and byte order.
   ///
@@ -550,7 +569,7 @@ protected:
   int m_indent_level; ///< Indention level.
   std::size_t m_bytes_written = 0; ///< Number of bytes written so far.
 
-  size_t _PutHex8(uint8_t uvalue, bool add_prefix);
+  void _PutHex8(uint8_t uvalue, bool add_prefix);
 
   //------------------------------------------------------------------
   /// Output character bytes to the stream.

Modified: lldb/trunk/source/Utility/Stream.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Utility/Stream.cpp?rev=342044&r1=342043&r2=342044&view=diff
==============================================================================
--- lldb/trunk/source/Utility/Stream.cpp (original)
+++ lldb/trunk/source/Utility/Stream.cpp Wed Sep 12 03:20:41 2018
@@ -313,26 +313,25 @@ size_t Stream::PrintfAsRawHex8(const cha
   llvm::SmallString<1024> buf;
   VASprintf(buf, format, args);
 
-  size_t length = 0;
+  ByteDelta delta(*this);
   for (char C : buf)
-    length += _PutHex8(C, false);
+    _PutHex8(C, false);
 
   va_end(args);
 
-  return length;
+  return *delta;
 }
 
 size_t Stream::PutNHex8(size_t n, uint8_t uvalue) {
-  size_t bytes_written = 0;
+  ByteDelta delta(*this);
   for (size_t i = 0; i < n; ++i)
-    bytes_written += _PutHex8(uvalue, false);
-  return bytes_written;
+    _PutHex8(uvalue, false);
+  return *delta;
 }
 
-size_t Stream::_PutHex8(uint8_t uvalue, bool add_prefix) {
-  size_t bytes_written = 0;
+void Stream::_PutHex8(uint8_t uvalue, bool add_prefix) {
   if (m_flags.Test(eBinary)) {
-    bytes_written = Write(&uvalue, 1);
+    Write(&uvalue, 1);
   } else {
     if (add_prefix)
       PutCString("0x");
@@ -343,56 +342,62 @@ size_t Stream::_PutHex8(uint8_t uvalue,
     char nibble_chars[2];
     nibble_chars[0] = g_hex_to_ascii_hex_char[(uvalue >> 4) & 0xf];
     nibble_chars[1] = g_hex_to_ascii_hex_char[(uvalue >> 0) & 0xf];
-    bytes_written = Write(nibble_chars, sizeof(nibble_chars));
+    Write(nibble_chars, sizeof(nibble_chars));
   }
-  return bytes_written;
 }
 
-size_t Stream::PutHex8(uint8_t uvalue) { return _PutHex8(uvalue, false); }
+size_t Stream::PutHex8(uint8_t uvalue) {
+  ByteDelta delta(*this);
+  _PutHex8(uvalue, false);
+  return *delta;
+}
 
 size_t Stream::PutHex16(uint16_t uvalue, ByteOrder byte_order) {
+  ByteDelta delta(*this);
+
   if (byte_order == eByteOrderInvalid)
     byte_order = m_byte_order;
 
-  size_t bytes_written = 0;
   if (byte_order == eByteOrderLittle) {
     for (size_t byte = 0; byte < sizeof(uvalue); ++byte)
-      bytes_written += _PutHex8((uint8_t)(uvalue >> (byte * 8)), false);
+      _PutHex8((uint8_t)(uvalue >> (byte * 8)), false);
   } else {
     for (size_t byte = sizeof(uvalue) - 1; byte < sizeof(uvalue); --byte)
-      bytes_written += _PutHex8((uint8_t)(uvalue >> (byte * 8)), false);
+      _PutHex8((uint8_t)(uvalue >> (byte * 8)), false);
   }
-  return bytes_written;
+  return *delta;
 }
 
 size_t Stream::PutHex32(uint32_t uvalue, ByteOrder byte_order) {
+  ByteDelta delta(*this);
+
   if (byte_order == eByteOrderInvalid)
     byte_order = m_byte_order;
 
-  size_t bytes_written = 0;
   if (byte_order == eByteOrderLittle) {
     for (size_t byte = 0; byte < sizeof(uvalue); ++byte)
-      bytes_written += _PutHex8((uint8_t)(uvalue >> (byte * 8)), false);
+      _PutHex8((uint8_t)(uvalue >> (byte * 8)), false);
   } else {
     for (size_t byte = sizeof(uvalue) - 1; byte < sizeof(uvalue); --byte)
-      bytes_written += _PutHex8((uint8_t)(uvalue >> (byte * 8)), false);
+      _PutHex8((uint8_t)(uvalue >> (byte * 8)), false);
   }
-  return bytes_written;
+  return *delta;
 }
 
 size_t Stream::PutHex64(uint64_t uvalue, ByteOrder byte_order) {
+  ByteDelta delta(*this);
+
   if (byte_order == eByteOrderInvalid)
     byte_order = m_byte_order;
 
-  size_t bytes_written = 0;
   if (byte_order == eByteOrderLittle) {
     for (size_t byte = 0; byte < sizeof(uvalue); ++byte)
-      bytes_written += _PutHex8((uint8_t)(uvalue >> (byte * 8)), false);
+      _PutHex8((uint8_t)(uvalue >> (byte * 8)), false);
   } else {
     for (size_t byte = sizeof(uvalue) - 1; byte < sizeof(uvalue); --byte)
-      bytes_written += _PutHex8((uint8_t)(uvalue >> (byte * 8)), false);
+      _PutHex8((uint8_t)(uvalue >> (byte * 8)), false);
   }
-  return bytes_written;
+  return *delta;
 }
 
 size_t Stream::PutMaxHex64(uint64_t uvalue, size_t byte_size,
@@ -438,65 +443,66 @@ size_t Stream::PutLongDouble(long double
 
 size_t Stream::PutRawBytes(const void *s, size_t src_len,
                            ByteOrder src_byte_order, ByteOrder dst_byte_order) {
+  ByteDelta delta(*this);
+
   if (src_byte_order == eByteOrderInvalid)
     src_byte_order = m_byte_order;
 
   if (dst_byte_order == eByteOrderInvalid)
     dst_byte_order = m_byte_order;
 
-  size_t bytes_written = 0;
   const uint8_t *src = (const uint8_t *)s;
   bool binary_was_set = m_flags.Test(eBinary);
   if (!binary_was_set)
     m_flags.Set(eBinary);
   if (src_byte_order == dst_byte_order) {
     for (size_t i = 0; i < src_len; ++i)
-      bytes_written += _PutHex8(src[i], false);
+      _PutHex8(src[i], false);
   } else {
     for (size_t i = src_len - 1; i < src_len; --i)
-      bytes_written += _PutHex8(src[i], false);
+      _PutHex8(src[i], false);
   }
   if (!binary_was_set)
     m_flags.Clear(eBinary);
 
-  return bytes_written;
+  return *delta;
 }
 
 size_t Stream::PutBytesAsRawHex8(const void *s, size_t src_len,
                                  ByteOrder src_byte_order,
                                  ByteOrder dst_byte_order) {
+  ByteDelta delta(*this);
   if (src_byte_order == eByteOrderInvalid)
     src_byte_order = m_byte_order;
 
   if (dst_byte_order == eByteOrderInvalid)
     dst_byte_order = m_byte_order;
 
-  size_t bytes_written = 0;
   const uint8_t *src = (const uint8_t *)s;
   bool binary_is_set = m_flags.Test(eBinary);
   m_flags.Clear(eBinary);
   if (src_byte_order == dst_byte_order) {
     for (size_t i = 0; i < src_len; ++i)
-      bytes_written += _PutHex8(src[i], false);
+      _PutHex8(src[i], false);
   } else {
     for (size_t i = src_len - 1; i < src_len; --i)
-      bytes_written += _PutHex8(src[i], false);
+      _PutHex8(src[i], false);
   }
   if (binary_is_set)
     m_flags.Set(eBinary);
 
-  return bytes_written;
+  return *delta;
 }
 
 size_t Stream::PutCStringAsRawHex8(const char *s) {
-  size_t bytes_written = 0;
+  ByteDelta delta(*this);
   bool binary_is_set = m_flags.Test(eBinary);
   m_flags.Clear(eBinary);
   while(*s) {
-    bytes_written += _PutHex8(*s, false);
+    _PutHex8(*s, false);
     ++s;
   }
   if (binary_is_set)
     m_flags.Set(eBinary);
-  return bytes_written;
+  return *delta;
 }




More information about the lldb-commits mailing list