[Lldb-commits] [lldb] r138949 - in /lldb/trunk: include/lldb/Core/DataEncoder.h lldb.xcodeproj/project.pbxproj source/Core/DataEncoder.cpp

Greg Clayton gclayton at apple.com
Thu Sep 1 11:10:09 PDT 2011


Author: gclayton
Date: Thu Sep  1 13:10:09 2011
New Revision: 138949

URL: http://llvm.org/viewvc/llvm-project?rev=138949&view=rev
Log:
Added a DataEncoder class for the new IR evaluation expression parser so it
can reserve a block of memory and store stuff into it.


Added:
    lldb/trunk/include/lldb/Core/DataEncoder.h
    lldb/trunk/source/Core/DataEncoder.cpp
Modified:
    lldb/trunk/lldb.xcodeproj/project.pbxproj

Added: lldb/trunk/include/lldb/Core/DataEncoder.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Core/DataEncoder.h?rev=138949&view=auto
==============================================================================
--- lldb/trunk/include/lldb/Core/DataEncoder.h (added)
+++ lldb/trunk/include/lldb/Core/DataEncoder.h Thu Sep  1 13:10:09 2011
@@ -0,0 +1,447 @@
+//===-- DataEncoder.h -------------------------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef liblldb_DataEncoder_h_
+#define liblldb_DataEncoder_h_
+
+#if defined (__cplusplus)
+
+#include "lldb/lldb-private.h"
+#include <limits.h>
+#include <stdint.h>
+
+namespace lldb_private {
+
+//----------------------------------------------------------------------
+/// @class DataEncoder DataEncoder.h "lldb/Core/DataEncoder.h"
+/// @brief 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 that can be shared between multiple DataEncoder or
+/// DataEncoder instances. 
+///
+/// @see DataBuffer
+//----------------------------------------------------------------------
+class DataEncoder
+{
+public:
+    //------------------------------------------------------------------
+    /// Default constructor.
+    ///
+    /// Initialize all members to a default empty state.
+    //------------------------------------------------------------------
+    DataEncoder ();
+
+    //------------------------------------------------------------------
+    /// Construct with a buffer that is owned by the caller.
+    ///
+    /// This constructor allows us to use data that is owned by the
+    /// caller. The data must stay around as long as this object is
+    /// valid.
+    ///
+    /// @param[in] data
+    ///     A pointer to caller owned data.
+    ///
+    /// @param[in] data_length
+    ///     The length in bytes of \a data.
+    ///
+    /// @param[in] byte_order
+    ///     A byte order of the data that we are extracting from.
+    ///
+    /// @param[in] addr_size
+    ///     A new address byte size value.
+    //------------------------------------------------------------------
+    DataEncoder (void* data, uint32_t data_length, lldb::ByteOrder byte_order, uint8_t addr_size);
+
+    //------------------------------------------------------------------
+    /// Construct with shared data.
+    ///
+    /// Copies the data shared pointer which adds a reference to the
+    /// contained in \a data_sp. The shared data reference is reference
+    /// counted to ensure the data lives as long as anyone still has a
+    /// valid shared pointer to the data in \a data_sp.
+    ///
+    /// @param[in] data_sp
+    ///     A shared pointer to data.
+    ///
+    /// @param[in] byte_order
+    ///     A byte order of the data that we are extracting from.
+    ///
+    /// @param[in] addr_size
+    ///     A new address byte size value.
+    //------------------------------------------------------------------
+    DataEncoder (const lldb::DataBufferSP& data_sp, lldb::ByteOrder byte_order, uint8_t addr_size);
+
+    //------------------------------------------------------------------
+    /// Destructor
+    ///
+    /// If this object contains a valid shared data reference, the
+    /// reference count on the data will be decremented, and if zero,
+    /// the data will be freed.
+    //------------------------------------------------------------------
+    ~DataEncoder ();
+
+    //------------------------------------------------------------------
+    /// Clears the object state.
+    ///
+    /// Clears the object contents back to a default invalid state, and
+    /// release 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 a 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 follows the newly encoded integer when the data is successfully
+    /// encoded into the existing data. There must be enough room in the 
+    /// data, else UINT32_MAX will be returned to indicate that encoding
+    /// failed.
+    ///
+    /// @param[in] offset
+    ///     The offset within the contained data at which to put the
+    ///     encoded integer.
+    ///
+    /// @param[in] byte_size
+    ///     The size in byte of the integer to encode.
+    ///
+    /// @param[in] value
+    ///     The integer value to write. The least significate bytes of
+    ///     the integer value will be written if the size is less than
+    ///     8 bytes.
+    ///
+    /// @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);
+
+    //------------------------------------------------------------------
+    /// Encode an arbitrary number of bytes.
+    ///
+    /// @param[in] offset
+    ///     The offset in bytes into the contained data at which to
+    ///     start encoding.
+    ///
+    /// @param[int] src
+    ///     The buffer that contains the the bytes to encode.
+    ///
+    /// @param[in] src_len
+    ///     The number of bytes to encode.
+    ///
+    /// @return
+    ///     The next valid offset within data if the put operation 
+    ///     was successful, else UINT32_MAX to indicate the put failed.
+    //------------------------------------------------------------------
+    uint32_t
+    PutData (uint32_t offset, 
+             const void *src, 
+             uint32_t src_len);
+    
+    //------------------------------------------------------------------
+    /// Encode an address in the existing buffer at \a offset bytes into
+    /// the buffer.
+    ///
+    /// Encode a single address (honoring the m_addr_size member) to
+    /// the data and return the next offset where subsequent data would
+    /// go.
+    /// pointed to by \a offset_ptr. The size of the extracted address
+    /// comes from the \a m_addr_size member variable and should be
+    /// set correctly prior to extracting any address values.
+    ///
+    /// @param[in,out] offset_ptr
+    ///     A pointer to an offset within the data that will be advanced
+    ///     by the appropriate number of bytes if the value is extracted
+    ///     correctly. If the offset is out of bounds or there are not
+    ///     enough bytes to extract this value, the offset will be left
+    ///     unmodified.
+    ///
+    /// @return
+    ///     The next valid offset within data if the put operation 
+    ///     was successful, else UINT32_MAX to indicate the put failed.
+    //------------------------------------------------------------------
+    uint32_t
+    PutAddress (uint32_t offset, lldb::addr_t addr);
+    
+    //------------------------------------------------------------------
+    /// Put a C string to \a offset.
+    ///
+    /// Encodes a C string into the existing data including the 
+    /// terminating
+    ///
+    /// @param[in,out] offset_ptr
+    ///     A pointer to an offset within the data that will be advanced
+    ///     by the appropriate number of bytes if the value is extracted
+    ///     correctly. If the offset is out of bounds or there are not
+    ///     enough bytes to extract this value, the offset will be left
+    ///     unmodified.
+    ///
+    /// @return
+    ///     A pointer to the C string value in the data. If the offset
+    ///     pointed to by \a offset_ptr is out of bounds, or if the
+    ///     offset plus the length of the C string is out of bounds,
+    ///     NULL will be returned.
+    //------------------------------------------------------------------
+    uint32_t
+    PutCString (uint32_t offset_ptr, const char *cstr);
+    
+    lldb::DataBufferSP &
+    GetSharedDataBuffer ()
+    {
+        return m_data_sp;
+    }
+
+    //------------------------------------------------------------------
+    /// 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;
+    }
+
+    //------------------------------------------------------------------
+    /// 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.
+    ///
+    /// @return
+    ///     The number of bytes that this object now contains.
+    //------------------------------------------------------------------
+    uint32_t
+    SetData (const void *bytes, uint32_t length, lldb::ByteOrder byte_order);
+
+    //------------------------------------------------------------------
+    /// Adopt a subset of shared data in \a data_sp.
+    ///
+    /// Copies the data shared pointer which adds a reference to the
+    /// contained in \a data_sp. The shared data reference is reference
+    /// counted to ensure the data lives as long as anyone still has a
+    /// valid shared pointer to the data in \a data_sp. The byte order
+    /// and address byte size settings remain the same. If
+    /// \a offset is not a valid offset in \a data_sp, then no reference
+    /// to the shared data will be added. If there are not \a length
+    /// bytes available in \a data starting at \a offset, the length
+    /// will be truncated to contains as many bytes as possible.
+    ///
+    /// @param[in] data_sp
+    ///     A shared pointer to data.
+    ///
+    /// @param[in] offset
+    ///     The offset into \a data_sp at which the subset starts.
+    ///
+    /// @param[in] length
+    ///     The length in bytes of the subset of \a data_sp.
+    ///
+    /// @return
+    ///     The number of bytes that this object now contains.
+    //------------------------------------------------------------------
+    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
+    ///     \b true if \a offset is a valid offset into the data in this
+    ///     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.
+    ///
+    /// @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;
+
+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 multilple instances
+    
+private:
+    DISALLOW_COPY_AND_ASSIGN (DataEncoder);
+
+};
+
+} // namespace lldb_private
+
+#endif  // #if defined (__cplusplus)
+#endif  // #ifndef liblldb_DataEncoder_h_

Modified: lldb/trunk/lldb.xcodeproj/project.pbxproj
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/lldb.xcodeproj/project.pbxproj?rev=138949&r1=138948&r2=138949&view=diff
==============================================================================
--- lldb/trunk/lldb.xcodeproj/project.pbxproj (original)
+++ lldb/trunk/lldb.xcodeproj/project.pbxproj Thu Sep  1 13:10:09 2011
@@ -330,6 +330,7 @@
 		2689FFFB13353DB600698AC0 /* BreakpointLocationList.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 26BC7E1010F1B83100F91463 /* BreakpointLocationList.cpp */; };
 		2689FFFD13353DB600698AC0 /* BreakpointOptions.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 26BC7E1110F1B83100F91463 /* BreakpointOptions.cpp */; };
 		2689FFFF13353DB600698AC0 /* BreakpointResolver.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 26BC7E1210F1B83100F91463 /* BreakpointResolver.cpp */; };
+		268ED0A5140FF54200DE830F /* DataEncoder.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 268ED0A4140FF54200DE830F /* DataEncoder.cpp */; };
 		268F9D53123AA15200B91E9B /* SBSymbolContextList.h in Headers */ = {isa = PBXBuildFile; fileRef = 268F9D52123AA15200B91E9B /* SBSymbolContextList.h */; settings = {ATTRIBUTES = (Public, ); }; };
 		268F9D55123AA16600B91E9B /* SBSymbolContextList.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 268F9D54123AA16600B91E9B /* SBSymbolContextList.cpp */; };
 		2690B3711381D5C300ECFBAE /* Memory.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2690B3701381D5C300ECFBAE /* Memory.cpp */; };
@@ -703,6 +704,8 @@
 		268A813F115B19D000F645B0 /* UniqueCStringMap.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = UniqueCStringMap.h; path = include/lldb/Core/UniqueCStringMap.h; sourceTree = "<group>"; };
 		268DA871130095D000C9483A /* Terminal.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Terminal.h; path = include/lldb/Host/Terminal.h; sourceTree = "<group>"; };
 		268DA873130095ED00C9483A /* Terminal.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Terminal.cpp; sourceTree = "<group>"; };
+		268ED0A2140FF52F00DE830F /* DataEncoder.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = DataEncoder.h; path = include/lldb/Core/DataEncoder.h; sourceTree = "<group>"; };
+		268ED0A4140FF54200DE830F /* DataEncoder.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = DataEncoder.cpp; path = source/Core/DataEncoder.cpp; sourceTree = "<group>"; };
 		268F9D52123AA15200B91E9B /* SBSymbolContextList.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SBSymbolContextList.h; path = include/lldb/API/SBSymbolContextList.h; sourceTree = "<group>"; };
 		268F9D54123AA16600B91E9B /* SBSymbolContextList.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = SBSymbolContextList.cpp; path = source/API/SBSymbolContextList.cpp; sourceTree = "<group>"; };
 		2690B36F1381D5B600ECFBAE /* Memory.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = Memory.h; path = include/lldb/Target/Memory.h; sourceTree = "<group>"; };
@@ -1927,6 +1930,8 @@
 				26BC7E7210F1B85900F91463 /* DataBufferHeap.cpp */,
 				26BC7D5C10F1B77400F91463 /* DataBufferMemoryMap.h */,
 				26BC7E7310F1B85900F91463 /* DataBufferMemoryMap.cpp */,
+				268ED0A2140FF52F00DE830F /* DataEncoder.h */,
+				268ED0A4140FF54200DE830F /* DataEncoder.cpp */,
 				26BC7D5A10F1B77400F91463 /* DataExtractor.h */,
 				26BC7E7110F1B85900F91463 /* DataExtractor.cpp */,
 				9470A8EE1402DF940056FF61 /* DataVisualization.h */,
@@ -3302,6 +3307,7 @@
 				9470A8F01402DFFB0056FF61 /* DataVisualization.cpp in Sources */,
 				26274FA214030EEF006BA130 /* OperatingSystemDarwinKernel.cpp in Sources */,
 				26274FA714030F79006BA130 /* DynamicLoaderDarwinKernel.cpp in Sources */,
+				268ED0A5140FF54200DE830F /* DataEncoder.cpp in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};

Added: lldb/trunk/source/Core/DataEncoder.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Core/DataEncoder.cpp?rev=138949&view=auto
==============================================================================
--- lldb/trunk/source/Core/DataEncoder.cpp (added)
+++ lldb/trunk/source/Core/DataEncoder.cpp Thu Sep  1 13:10:09 2011
@@ -0,0 +1,361 @@
+//===-- DataEncoder.cpp ---------------------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "lldb/Core/DataEncoder.h"
+
+#include <assert.h>
+#include <stddef.h>
+
+#include "llvm/Support/MathExtras.h"
+
+#include "lldb/Core/DataBuffer.h"
+#include "lldb/Host/Endian.h"
+
+using namespace lldb;
+using namespace lldb_private;
+
+static inline void
+WriteInt16(const unsigned char* ptr, unsigned offset, uint16_t value) 
+{
+    *(uint16_t *)(ptr + offset) = value;
+}
+static inline void
+WriteInt32 (const unsigned char* ptr, unsigned offset, uint32_t value) 
+{
+    *(uint32_t *)(ptr + offset) = value;
+}
+
+static inline void
+WriteInt64(const unsigned char* ptr, unsigned offset, uint64_t value) 
+{
+    *(uint64_t *)(ptr + offset) = value;
+}
+
+static inline void
+WriteSwappedInt16(const unsigned char* ptr, unsigned offset, uint16_t value) 
+{
+    *(uint16_t *)(ptr + offset) = llvm::ByteSwap_16(value);
+}
+
+static inline void
+WriteSwappedInt32 (const unsigned char* ptr, unsigned offset, uint32_t value) 
+{
+    *(uint32_t *)(ptr + offset) = llvm::ByteSwap_32(value);
+}
+
+static inline void 
+WriteSwappedInt64(const unsigned char* ptr, unsigned offset, uint64_t value) 
+{
+    *(uint64_t *)(ptr + offset) = llvm::ByteSwap_64(value);
+}
+
+//----------------------------------------------------------------------
+// Default constructor.
+//----------------------------------------------------------------------
+DataEncoder::DataEncoder () :
+    m_start     (NULL),
+    m_end       (NULL),
+    m_byte_order(lldb::endian::InlHostByteOrder()),
+    m_addr_size (sizeof(void*)),
+    m_data_sp   ()
+{
+}
+
+//----------------------------------------------------------------------
+// This constructor allows us to use data that is owned by someone else.
+// The data must stay around as long as this object is valid.
+//----------------------------------------------------------------------
+DataEncoder::DataEncoder (void* data, uint32_t length, ByteOrder endian, uint8_t addr_size) :
+    m_start     ((uint8_t*)data),
+    m_end       ((uint8_t*)data + length),
+    m_byte_order(endian),
+    m_addr_size (addr_size),
+    m_data_sp   ()
+{
+}
+
+//----------------------------------------------------------------------
+// Make a shared pointer reference to the shared data in "data_sp" and
+// set the endian swapping setting to "swap", and the address size to
+// "addr_size". The shared data reference will ensure the data lives
+// as long as any DataEncoder objects exist that have a reference to
+// this data.
+//----------------------------------------------------------------------
+DataEncoder::DataEncoder (const DataBufferSP& data_sp, ByteOrder endian, uint8_t addr_size) :
+    m_start     (NULL),
+    m_end       (NULL),
+    m_byte_order(endian),
+    m_addr_size (addr_size),
+    m_data_sp   ()
+{
+    SetData (data_sp);
+}
+
+//----------------------------------------------------------------------
+// Destructor
+//----------------------------------------------------------------------
+DataEncoder::~DataEncoder ()
+{
+}
+
+//------------------------------------------------------------------
+// Clears the object contents back to a default invalid state, and
+// release any references to shared data that this object may
+// contain.
+//------------------------------------------------------------------
+void
+DataEncoder::Clear ()
+{
+    m_start = NULL;
+    m_end = NULL;
+    m_byte_order = lldb::endian::InlHostByteOrder();
+    m_addr_size = sizeof(void*);
+    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 != NULL)
+    {
+        const DataBuffer * data = m_data_sp.get();
+        if (data != NULL)
+        {
+            const uint8_t * data_bytes = data->GetBytes();
+            if (data_bytes != NULL)
+            {
+                assert(m_start >= data_bytes);
+                return m_start - data_bytes;
+            }
+        }
+    }
+    return 0;
+}
+
+//------------------------------------------------------------------
+// Returns true if there are LENGTH bytes availabe starting OFFSET
+// into the data that is in this object.
+//------------------------------------------------------------------
+bool
+DataEncoder::ValidOffsetForDataOfSize (uint32_t offset, uint32_t length) const
+{
+    size_t size = GetByteSize();
+    if (offset >= size)
+        return false;   // offset isn't valid
+
+    if (length == 0)
+        return true;    // No bytes requested at this offset, return true
+
+    // If we flip the bits in offset we can figure out how
+    // many bytes we have left before "offset + length"
+    // could overflow when doing unsigned arithmetic.
+    if (length > ~offset)
+        return false;   // unsigned overflow
+
+    // Make sure "offset + length" is a valid offset as well.
+    // length must be greater than zero for this to be a
+    // valid expression, and we have already checked for this.
+    return ((offset + length) <= size);
+}
+
+//----------------------------------------------------------------------
+// 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 (const void *bytes, uint32_t length, ByteOrder endian)
+{
+    m_byte_order = endian;
+    m_data_sp.reset();
+    if (bytes == NULL || length == 0)
+    {
+        m_start = NULL;
+        m_end = NULL;
+    }
+    else
+    {
+        m_start = (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 "data_sp", then this object will contain no
+// bytes. If "data_offset" is within "data_sp" yet "data_length" is
+// too large, the length will be capped at the number of bytes
+// remaining in "data_sp". A ref counted pointer to the data in
+// "data_sp" will be made in this object IF the number of bytes this
+// object refers to in greater than zero (if at least one byte was
+// available starting at "data_offset") to ensure the data stays
+// around as long as it is needed. The address size and endian swap
+// settings will remain unchanged from their current settings.
+//----------------------------------------------------------------------
+uint32_t
+DataEncoder::SetData (const DataBufferSP& data_sp, uint32_t data_offset, uint32_t data_length)
+{
+    m_start = m_end = NULL;
+
+    if (data_length > 0)
+    {
+        m_data_sp = data_sp;
+        if (data_sp.get())
+        {
+            const size_t data_size = data_sp->GetByteSize();
+            if (data_offset < data_size)
+            {
+                m_start = data_sp->GetBytes() + data_offset;
+                const size_t bytes_left = data_size - data_offset;
+                // Cap the length of we asked for too many
+                if (data_length <= bytes_left)
+                    m_end = m_start + data_length;  // We got all the bytes we wanted
+                else
+                    m_end = m_start + bytes_left;   // Not all the bytes requested were available in the shared data
+            }
+        }
+    }
+
+    uint32_t new_size = GetByteSize();
+
+    // Don't hold a shared pointer to the data buffer if we don't share
+    // any valid bytes in the shared buffer.
+    if (new_size == 0)
+        m_data_sp.reset();
+
+    return new_size;
+}
+
+//----------------------------------------------------------------------
+// Extract a single unsigned char from the binary data and update
+// the offset pointed to by "offset_ptr".
+//
+// RETURNS the byte that was extracted, or zero on failure.
+//----------------------------------------------------------------------
+uint32_t
+DataEncoder::PutU8 (uint32_t offset, uint8_t value)
+{
+    if (ValidOffset(offset))
+    {
+        m_start[offset] = value;
+        return offset + 1;
+    }
+    return UINT32_MAX;
+}
+
+uint32_t
+DataEncoder::PutU16 (uint32_t offset, uint16_t value)
+{
+    if (ValidOffsetForDataOfSize(offset, sizeof(value)))
+    {
+        if (m_byte_order != lldb::endian::InlHostByteOrder())
+            WriteSwappedInt16 (m_start, offset, value);
+        else
+            WriteInt16 (m_start, offset, value);
+
+        return offset + sizeof (value);
+    }
+    return UINT32_MAX;
+}
+
+uint32_t
+DataEncoder::PutU32 (uint32_t offset, uint32_t value)
+{
+    if (ValidOffsetForDataOfSize(offset, sizeof(value)))
+    {
+        if (m_byte_order != lldb::endian::InlHostByteOrder())
+            WriteSwappedInt32 (m_start, offset, value);
+        else
+            WriteInt32 (m_start, offset, value);
+        
+        return offset + sizeof (value);
+    }
+    return UINT32_MAX;
+}
+
+uint32_t
+DataEncoder::PutU64 (uint32_t offset, uint64_t value)
+{
+    if (ValidOffsetForDataOfSize(offset, sizeof(value)))
+    {
+        if (m_byte_order != lldb::endian::InlHostByteOrder())
+            WriteSwappedInt64 (m_start, offset, value);
+        else
+            WriteInt64 (m_start, offset, value);
+        
+        return offset + sizeof (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)
+{
+    switch (byte_size)
+    {
+    case 1: return PutU8 (offset, value);
+    case 2: return PutU16(offset, value);
+    case 4: return PutU32(offset, value);
+    case 8: return PutU64(offset, value);
+    default:
+        assert(!"GetMax64 unhandled case!");
+        break;
+    }
+    return UINT32_MAX;
+}
+
+uint32_t
+DataEncoder::PutData (uint32_t offset, const void *src, uint32_t src_len)
+{
+    if (src == NULL || src_len == 0)
+        return offset;
+
+    if (ValidOffsetForDataOfSize(offset, src_len))
+    {
+        memcpy (m_start + offset, src, src_len);
+        return offset + src_len;
+    }
+    return UINT32_MAX;
+}
+
+uint32_t
+DataEncoder::PutAddress (uint32_t offset, lldb::addr_t addr)
+{
+    return PutMaxU64 (offset, GetAddressByteSize(), addr);
+}
+
+uint32_t
+DataEncoder::PutCString (uint32_t offset, const char *cstr)
+{
+    if (cstr)
+        return PutData (offset, cstr, strlen(cstr));
+    return UINT32_MAX;
+}





More information about the lldb-commits mailing list