[Lldb-commits] [lldb] 8ac053e - [LLDB] Cleanup the DataEncoder utility. (NFC)

Jonas Devlieghere via lldb-commits lldb-commits at lists.llvm.org
Wed Nov 13 15:44:59 PST 2019


Author: Jonas Devlieghere
Date: 2019-11-13T15:44:51-08:00
New Revision: 8ac053eea20b56f80653191a210682f8bd6fc10d

URL: https://github.com/llvm/llvm-project/commit/8ac053eea20b56f80653191a210682f8bd6fc10d
DIFF: https://github.com/llvm/llvm-project/commit/8ac053eea20b56f80653191a210682f8bd6fc10d.diff

LOG: [LLDB] Cleanup the DataEncoder utility. (NFC)

This commit removes unused methods from the DataEncoder class and cleans
up the API by making all the internal methods private.

Added: 
    

Modified: 
    lldb/include/lldb/Utility/DataEncoder.h
    lldb/source/Expression/DWARFExpression.cpp
    lldb/source/Plugins/Platform/Android/AdbClient.cpp
    lldb/source/Utility/DataEncoder.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/include/lldb/Utility/DataEncoder.h b/lldb/include/lldb/Utility/DataEncoder.h
index 7d44afd2ce69..f4964b250b9d 100644
--- a/lldb/include/lldb/Utility/DataEncoder.h
+++ b/lldb/include/lldb/Utility/DataEncoder.h
@@ -21,8 +21,9 @@
 
 namespace lldb_private {
 
-/// \class DataEncoder DataEncoder.h "lldb/Core/DataEncoder.h" An binary data
-/// encoding class.
+/// \class DataEncoder
+///
+/// An binary data encoding class.
 ///
 /// DataEncoder is a class that can encode binary data (swapping if needed) to
 /// a data buffer. The data buffer can be caller owned, or can be shared data
@@ -86,74 +87,6 @@ class DataEncoder {
   /// any references to shared data that this object may contain.
   void Clear();
 
-  /// Get the current address size.
-  ///
-  /// Return the size in bytes of any address values this object will extract.
-  ///
-  /// \return
-  ///     The size in bytes of address values that will be extracted.
-  uint8_t GetAddressByteSize() const { return m_addr_size; }
-
-  /// Get the number of bytes contained in this object.
-  ///
-  /// \return
-  ///     The total number of bytes of data this object refers to.
-  size_t GetByteSize() const { return m_end - m_start; }
-
-  /// Get the data end pointer.
-  ///
-  /// \return
-  ///     Returns a pointer to the next byte contained in this
-  ///     object's data, or NULL of there is no data in this object.
-  uint8_t *GetDataEnd() { return m_end; }
-
-  const uint8_t *GetDataEnd() const { return m_end; }
-
-  /// Get the shared data offset.
-  ///
-  /// Get the offset of the first byte of data in the shared data (if any).
-  ///
-  /// \return
-  ///     If this object contains shared data, this function returns
-  ///     the offset in bytes into that shared data, zero otherwise.
-  size_t GetSharedDataOffset() const;
-
-  /// Get the current byte order value.
-  ///
-  /// \return
-  ///     The current byte order value from this object's internal
-  ///     state.
-  lldb::ByteOrder GetByteOrder() const { return m_byte_order; }
-
-  /// Get the data start pointer.
-  ///
-  /// \return
-  ///     Returns a pointer to the first byte contained in this
-  ///     object's data, or NULL of there is no data in this object.
-  uint8_t *GetDataStart() { return m_start; }
-
-  const uint8_t *GetDataStart() const { return m_start; }
-
-  /// Encode unsigned integer values into the data at \a offset.
-  ///
-  /// \param[in] offset
-  ///     The offset within the contained data at which to put the
-  ///     data.
-  ///
-  /// \param[in] value
-  ///     The value to encode into the data.
-  ///
-  /// \return
-  ///     The next offset in the bytes of this data if the data
-  ///     was successfully encoded, UINT32_MAX if the encoding failed.
-  uint32_t PutU8(uint32_t offset, uint8_t value);
-
-  uint32_t PutU16(uint32_t offset, uint16_t value);
-
-  uint32_t PutU32(uint32_t offset, uint32_t value);
-
-  uint32_t PutU64(uint32_t offset, uint64_t value);
-
   /// Encode an unsigned integer of size \a byte_size to \a offset.
   ///
   /// Encode a single integer value at \a offset and return the offset that
@@ -176,7 +109,7 @@ class DataEncoder {
   /// \return
   ///     The next offset in the bytes of this data if the integer
   ///     was successfully encoded, UINT32_MAX if the encoding failed.
-  uint32_t PutMaxU64(uint32_t offset, uint32_t byte_size, uint64_t value);
+  uint32_t PutUnsigned(uint32_t offset, uint32_t byte_size, uint64_t value);
 
   /// Encode an arbitrary number of bytes.
   ///
@@ -232,36 +165,27 @@ class DataEncoder {
   ///     NULL will be returned.
   uint32_t PutCString(uint32_t offset, const char *cstr);
 
-  lldb::DataBufferSP &GetSharedDataBuffer() { return m_data_sp; }
+private:
+  uint32_t PutU8(uint32_t offset, uint8_t value);
+  uint32_t PutU16(uint32_t offset, uint16_t value);
+  uint32_t PutU32(uint32_t offset, uint32_t value);
+  uint32_t PutU64(uint32_t offset, uint64_t value);
 
-  /// Set the address byte size.
-  ///
-  /// Set the size in bytes that will be used when extracting any address and
-  /// pointer values from data contained in this object.
-  ///
-  /// \param[in] addr_size
-  ///     The size in bytes to use when extracting addresses.
-  void SetAddressByteSize(uint8_t addr_size) { m_addr_size = addr_size; }
+  uint32_t BytesLeft(uint32_t offset) const {
+    const uint32_t size = GetByteSize();
+    if (size > offset)
+      return size - offset;
+    return 0;
+  }
 
-  /// Set data with a buffer that is caller owned.
-  ///
-  /// Use data that is owned by the caller when extracting values. The data
-  /// must stay around as long as this object, or any object that copies a
-  /// subset of this object's data, is valid. If \a bytes is NULL, or \a
-  /// length is zero, this object will contain no data.
-  ///
-  /// \param[in] bytes
-  ///     A pointer to caller owned data.
-  ///
-  /// \param[in] length
-  ///     The length in bytes of \a bytes.
-  ///
-  /// \param[in] byte_order
-  ///     A byte order of the data that we are extracting from.
+  /// Test the availability of \a length bytes of data from \a offset.
   ///
   /// \return
-  ///     The number of bytes that this object now contains.
-  uint32_t SetData(void *bytes, uint32_t length, lldb::ByteOrder byte_order);
+  ///     \b true if \a offset is a valid offset and there are \a
+  ///     length bytes available at that offset, \b false otherwise.
+  bool ValidOffsetForDataOfSize(uint32_t offset, uint32_t length) const {
+    return length <= BytesLeft(offset);
+  }
 
   /// Adopt a subset of shared data in \a data_sp.
   ///
@@ -288,15 +212,6 @@ class DataEncoder {
   uint32_t SetData(const lldb::DataBufferSP &data_sp, uint32_t offset = 0,
                    uint32_t length = UINT32_MAX);
 
-  /// Set the byte_order value.
-  ///
-  /// Sets the byte order of the data to extract. Extracted values will be
-  /// swapped if necessary when decoding.
-  ///
-  /// \param[in] byte_order
-  ///     The byte order value to use when extracting data.
-  void SetByteOrder(lldb::ByteOrder byte_order) { m_byte_order = byte_order; }
-
   /// Test the validity of \a offset.
   ///
   /// \return
@@ -304,34 +219,30 @@ class DataEncoder {
   ///     object, \b false otherwise.
   bool ValidOffset(uint32_t offset) const { return offset < GetByteSize(); }
 
-  /// Test the availability of \a length bytes of data from \a offset.
+  /// Get the number of bytes contained in this object.
   ///
   /// \return
-  ///     \b true if \a offset is a valid offset and there are \a
-  ///     length bytes available at that offset, \b false otherwise.
-  bool ValidOffsetForDataOfSize(uint32_t offset, uint32_t length) const {
-    return length <= BytesLeft(offset);
-  }
+  ///     The total number of bytes of data this object refers to.
+  size_t GetByteSize() const { return m_end - m_start; }
 
-  uint32_t BytesLeft(uint32_t offset) const {
-    const uint32_t size = GetByteSize();
-    if (size > offset)
-      return size - offset;
-    return 0;
-  }
+private:
+  /// A pointer to the first byte of data.
+  uint8_t *m_start;
 
-protected:
-  // Member variables
-  uint8_t *m_start; ///< A pointer to the first byte of data.
-  uint8_t *m_end;   ///< A pointer to the byte that is past the end of the data.
-  lldb::ByteOrder
-      m_byte_order;    ///< The byte order of the data we are extracting from.
-  uint8_t m_addr_size; ///< The address size to use when extracting pointers or
-                       /// addresses
-  mutable lldb::DataBufferSP m_data_sp; ///< The shared pointer to data that can
-                                        /// be shared among multiple instances
+  /// A pointer to the byte that is past the end of the data.
+  uint8_t *m_end;
+
+  /// The byte order of the data we are extracting from.
+  lldb::ByteOrder m_byte_order;
+
+  /// The address size to use when extracting pointers or
+  /// addresses
+  uint8_t m_addr_size;
+
+  /// The shared pointer to data that can
+  /// be shared among multiple instances
+  mutable lldb::DataBufferSP m_data_sp;
 
-private:
   DISALLOW_COPY_AND_ASSIGN(DataEncoder);
 };
 

diff  --git a/lldb/source/Expression/DWARFExpression.cpp b/lldb/source/Expression/DWARFExpression.cpp
index 3789d9147737..79e155e7e8fc 100644
--- a/lldb/source/Expression/DWARFExpression.cpp
+++ b/lldb/source/Expression/DWARFExpression.cpp
@@ -477,7 +477,7 @@ bool DWARFExpression::Update_DW_OP_addr(lldb::addr_t file_addr) {
                           m_data.GetByteOrder(), addr_byte_size);
 
       // Replace the address in the new buffer
-      if (encoder.PutMaxU64(offset, addr_byte_size, file_addr) == UINT32_MAX)
+      if (encoder.PutUnsigned(offset, addr_byte_size, file_addr) == UINT32_MAX)
         return false;
 
       // All went well, so now we can reset the data using a shared pointer to
@@ -583,8 +583,8 @@ bool DWARFExpression::LinkThreadLocalStorage(
         if (linked_file_addr == LLDB_INVALID_ADDRESS)
           return false;
         // Replace the address in the new buffer
-        if (encoder.PutMaxU64(const_offset, const_byte_size,
-                              linked_file_addr) == UINT32_MAX)
+        if (encoder.PutUnsigned(const_offset, const_byte_size,
+                                linked_file_addr) == UINT32_MAX)
           return false;
       }
       break;

diff  --git a/lldb/source/Plugins/Platform/Android/AdbClient.cpp b/lldb/source/Plugins/Platform/Android/AdbClient.cpp
index 04f88da7ac25..037e5f856702 100644
--- a/lldb/source/Plugins/Platform/Android/AdbClient.cpp
+++ b/lldb/source/Plugins/Platform/Android/AdbClient.cpp
@@ -595,7 +595,7 @@ Status AdbClient::SyncService::SendSyncRequest(const char *request_id,
   const DataBufferSP data_sp(new DataBufferHeap(kSyncPacketLen, 0));
   DataEncoder encoder(data_sp, eByteOrderLittle, sizeof(void *));
   auto offset = encoder.PutData(0, request_id, strlen(request_id));
-  encoder.PutU32(offset, data_len);
+  encoder.PutUnsigned(offset, 4, data_len);
 
   Status error;
   ConnectionStatus status;

diff  --git a/lldb/source/Utility/DataEncoder.cpp b/lldb/source/Utility/DataEncoder.cpp
index 13c505e34e82..0b6456deec35 100644
--- a/lldb/source/Utility/DataEncoder.cpp
+++ b/lldb/source/Utility/DataEncoder.cpp
@@ -61,42 +61,6 @@ void DataEncoder::Clear() {
   m_data_sp.reset();
 }
 
-// If this object contains shared data, this function returns the offset into
-// that shared data. Else zero is returned.
-size_t DataEncoder::GetSharedDataOffset() const {
-  if (m_start != nullptr) {
-    const DataBuffer *data = m_data_sp.get();
-    if (data != nullptr) {
-      const uint8_t *data_bytes = data->GetBytes();
-      if (data_bytes != nullptr) {
-        assert(m_start >= data_bytes);
-        return m_start - data_bytes;
-      }
-    }
-  }
-  return 0;
-}
-
-// Set the data with which this object will extract from to data starting at
-// BYTES and set the length of the data to LENGTH bytes long. The data is
-// externally owned must be around at least as long as this object points to
-// the data. No copy of the data is made, this object just refers to this data
-// and can extract from it. If this object refers to any shared data upon
-// entry, the reference to that data will be released. Is SWAP is set to true,
-// any data extracted will be endian swapped.
-uint32_t DataEncoder::SetData(void *bytes, uint32_t length, ByteOrder endian) {
-  m_byte_order = endian;
-  m_data_sp.reset();
-  if (bytes == nullptr || length == 0) {
-    m_start = nullptr;
-    m_end = nullptr;
-  } else {
-    m_start = static_cast<uint8_t *>(bytes);
-    m_end = m_start + length;
-  }
-  return GetByteSize();
-}
-
 // Assign the data for this object to be a subrange of the shared data in
 // "data_sp" starting "data_offset" bytes into "data_sp" and ending
 // "data_length" bytes later. If "data_offset" is not a valid offset into
@@ -187,16 +151,8 @@ uint32_t DataEncoder::PutU64(uint32_t offset, uint64_t value) {
   return UINT32_MAX;
 }
 
-// Extract a single integer value from the data and update the offset pointed
-// to by "offset_ptr". The size of the extracted integer is specified by the
-// "byte_size" argument. "byte_size" should have a value >= 1 and <= 8 since
-// the return value is only 64 bits wide. Any "byte_size" values less than 1 or
-// greater than 8 will result in nothing being extracted, and zero being
-// returned.
-//
-// RETURNS the integer value that was extracted, or zero on failure.
-uint32_t DataEncoder::PutMaxU64(uint32_t offset, uint32_t byte_size,
-                                uint64_t value) {
+uint32_t DataEncoder::PutUnsigned(uint32_t offset, uint32_t byte_size,
+                                  uint64_t value) {
   switch (byte_size) {
   case 1:
     return PutU8(offset, value);
@@ -225,7 +181,7 @@ uint32_t DataEncoder::PutData(uint32_t offset, const void *src,
 }
 
 uint32_t DataEncoder::PutAddress(uint32_t offset, lldb::addr_t addr) {
-  return PutMaxU64(offset, GetAddressByteSize(), addr);
+  return PutUnsigned(offset, m_addr_size, addr);
 }
 
 uint32_t DataEncoder::PutCString(uint32_t offset, const char *cstr) {


        


More information about the lldb-commits mailing list