Add register cache support in HardwareRegister class

Signed-off-by: Zane Shelley <zshelle@us.ibm.com>
Change-Id: Ic671a099fdcd98d1f0158289ef321b968f1908ca
diff --git a/src/hei_includes.hpp b/src/hei_includes.hpp
index 3c29a95..82b38b3 100644
--- a/src/hei_includes.hpp
+++ b/src/hei_includes.hpp
@@ -9,6 +9,7 @@
 // Standard library includes
 #include <stdlib.h>
 #include <stdint.h>
+#include <map>
 #include <vector>
 
 // The user application must define "hei_user_defines.hpp" with the following
diff --git a/src/isolator/hei_isolator.cpp b/src/isolator/hei_isolator.cpp
index 1ff52a4..aea45df 100644
--- a/src/isolator/hei_isolator.cpp
+++ b/src/isolator/hei_isolator.cpp
@@ -20,6 +20,10 @@
 
 void Isolator::uninitialize()
 {
+    // Must flush the hardware register cache before deleting any
+    // HardwareRegister objects.
+    HardwareRegister::flushAll();
+
     // BEGIN temporary code
     HEI_INF( "Isolator::uninitialize()" );
     // END temporary code
@@ -33,6 +37,9 @@
     // Flush the isolation data to ensure a clean slate.
     o_isoData.clear();
 
+    // Flush the hardware register cache to avoid using stale data.
+    HardwareRegister::flushAll();
+
     // Analyze active error on each chip.
     for ( auto const & chip : i_chipList )
     {
diff --git a/src/register/hei_hardware_register.cpp b/src/register/hei_hardware_register.cpp
index 5663f3b..58106bf 100755
--- a/src/register/hei_hardware_register.cpp
+++ b/src/register/hei_hardware_register.cpp
@@ -18,7 +18,6 @@
 #include <iipchip.h>
 #include <prdfMain.H>
 #include <prdfRasServices.H>
-#include <prdfRegisterCache.H>
 #include <prdfPlatServices.H>
 #include <prdfExtensibleChip.H>
 
@@ -69,14 +68,16 @@
     // Calling Read() will ensure that an entry exists in the cache and the
     // entry has at been synched with hardware at least once. Note that we
     // cannot read hardware for write-only registers. In this case, an entry
-    // will be created in the cache, if it does not exist, when readCache() is
-    // called below.
+    // will be created in the cache, if it does not exist, when the cache is
+    // read below.
+
     if ( ( ACCESS_NONE != iv_operationType ) &&
-            ( ACCESS_WO != iv_operationType ) )
+         ( ACCESS_WO   != iv_operationType ) )
     {
         Read();
     }
-    return &(readCache());
+
+    return &( accessCache() );
 }
 
 //------------------------------------------------------------------------------
@@ -86,15 +87,16 @@
     // Calling Read() will ensure that an entry exists in the cache and the
     // entry has at been synched with hardware at least once. Note that we
     // cannot read hardware for write-only registers. In this case, an entry
-    // will be created in the cache, if it does not exist, when readCache() is
-    // called below.
+    // will be created in the cache, if it does not exist, when the cache is
+    // read below.
+
     if ( ( ACCESS_NONE != iv_operationType ) &&
-            ( ACCESS_WO != iv_operationType ) )
+         ( ACCESS_WO   != iv_operationType ) )
     {
         Read();
     }
 
-    return readCache();
+    return accessCache();
 }
 #endif
 
@@ -104,17 +106,17 @@
 {
     ReturnCode rc;
 
-#if 0
     // Read from hardware only if the read is forced or the entry for this
     // instance does not exist in the cache.
     if ( i_force || !queryCache() )
     {
+#if 0
         // This register must be readable.
         HEI_ASSERT( ( ACCESS_NONE != iv_operationType ) &&
                     ( ACCESS_WO   != iv_operationType ) );
 
         // Get the buffer from the register cache.
-        BitString & bs = readCache();
+        BitString & bs = accessCache();
 
         // Get the byte size of the buffer.
         size_t sz_buffer = BitString::getMinBytes( bs.getBitLen() );
@@ -122,20 +124,22 @@
         // Read this register from hardware.
         rc = registerRead( getAccessorChip().getChip(), bs.getBufAddr(),
                            sz_buffer, getRegisterType(), getAddress() );
+#endif
         if ( RC_SUCCESS != rc )
         {
             // The read failed and we can't trust what was put in the register
             // cache. So remove this instance's entry from the cache.
-            flushCache( getAccessorChip() );
+            cv_cache.flush( getAccessorChip(), this );
         }
+#if 0
         else
         {
             // Sanity check. The returned size of the data written to the buffer
             // should match the register size.
             HEI_ASSERT( getSize() == sz_buffer );
         }
-    }
 #endif
+    }
 
     return rc;
 }
@@ -157,7 +161,7 @@
     HEI_ASSERT( queryCache() );
 
     // Get the buffer from the register cache.
-    BitString & bs = readCache();
+    BitString & bs = accessCache();
 
     // Get the byte size of the buffer.
     size_t sz_buffer = BitString::getMinBytes( bs.getBitLen() );
@@ -182,38 +186,6 @@
 #if 0
 //------------------------------------------------------------------------------
 
-bool HardwareRegister::queryCache() const
-{
-    RegDataCache & cache = RegDataCache::getCachedRegisters();
-    BitString * bs = cache.queryCache( getAccessorChip(), this );
-    return ( nullptr != bs );
-}
-
-//------------------------------------------------------------------------------
-
-BitString & HardwareRegister::readCache() const
-{
-    RegDataCache & cache = RegDataCache::getCachedRegisters();
-    return cache.read( getAccessorChip(), this );
-}
-
-//------------------------------------------------------------------------------
-
-void HardwareRegister::flushCache( ExtensibleChip *i_pChip ) const
-{
-     RegDataCache & regDump = RegDataCache::getCachedRegisters();
-    if( nullptr == i_pChip )
-    {
-        regDump.flush();
-    }
-    else
-    {
-        regDump.flush( i_pChip ,this );
-    }
-}
-
-//-----------------------------------------------------------------------------
-
 bool HardwareRegister::operator == ( const HardwareRegister & i_rightRegister ) const
 {
     if( iv_scomAddress == i_rightRegister.GetAddress() )
@@ -254,5 +226,95 @@
 
 //------------------------------------------------------------------------------
 
+HardwareRegister::Cache HardwareRegister::cv_cache {};
+
+//------------------------------------------------------------------------------
+
+bool HardwareRegister::Cache::query( const Chip & i_chip,
+                                     const HardwareRegister * i_hwReg ) const
+{
+    // Does i_chip exist in the cache?
+    auto chipPairItr = iv_cache.find( i_chip );
+    if ( iv_cache.end() != chipPairItr )
+    {
+        auto & hwRegMap = chipPairItr->second; // for ease of use
+
+        // Does i_hwReg exist in the cache?
+        auto hwRegPairItr = hwRegMap.find( i_hwReg );
+        if ( hwRegMap.end() != hwRegPairItr )
+        {
+            return true;
+        }
+    }
+
+    return false;
+}
+
+//------------------------------------------------------------------------------
+
+BitString & HardwareRegister::Cache::access( const Chip & i_chip,
+                                             const HardwareRegister * i_hwReg )
+{
+    // If the entry does not exist, create a new entry.
+    if ( !query(i_chip, i_hwReg) )
+    {
+        BitString * bs = new BitStringBuffer { i_hwReg->getByteSize() * 8 };
+        iv_cache[i_chip][i_hwReg] = bs;
+    }
+
+    // Return a reference to the target entry.
+    return *(iv_cache[i_chip][i_hwReg]);
+}
+
+//------------------------------------------------------------------------------
+
+void HardwareRegister::Cache::flush()
+{
+    // Delete all of the BitStrings.
+    for ( auto & chipPair : iv_cache )
+    {
+        for ( auto & hwRegPair : chipPair.second )
+        {
+            delete hwRegPair.second;
+        }
+    }
+
+    // !!! Do not delete the HardwareRegisters !!!
+    // Those are deleted when the main uninitialize() API is called.
+
+    // Flush the rest of the cache.
+    iv_cache.clear();
+}
+
+//------------------------------------------------------------------------------
+
+void HardwareRegister::Cache::flush( const Chip & i_chip,
+                                     const HardwareRegister * i_hwReg )
+{
+    // Does i_chip exist in the cache?
+    auto chipPairItr = iv_cache.find( i_chip );
+    if ( iv_cache.end() != chipPairItr )
+    {
+        auto & hwRegMap = chipPairItr->second; // for ease of use
+
+        // Does i_hwReg exist in the cache?
+        auto hwRegPairItr = hwRegMap.find( i_hwReg );
+        if ( hwRegMap.end() != hwRegPairItr )
+        {
+            delete hwRegPairItr->second; // delete the BitString
+            hwRegMap.erase(i_hwReg);     // remove the entry for this register
+        }
+
+        // If i_hwReg was the only entry for i_chip, we can remove i_chip from
+        // the cache.
+        if ( hwRegMap.empty() )
+        {
+            iv_cache.erase(i_chip);
+        }
+    }
+}
+
+//------------------------------------------------------------------------------
+
 } // end namespace libhei
 
diff --git a/src/register/hei_hardware_register.hpp b/src/register/hei_hardware_register.hpp
index 066f6db..7588a6c 100755
--- a/src/register/hei_hardware_register.hpp
+++ b/src/register/hei_hardware_register.hpp
@@ -17,13 +17,6 @@
 namespace libhei
 {
 
-#if 0
-// Forward References
-class CHIP_CLASS;
-class MopsRegisterAccess;
-class ExtensibleChip;
-#endif
-
 /**
  * @brief Stores information (e.g. address, type, length, etc.) for an actual
  *        hardware register.
@@ -40,6 +33,20 @@
  *   - Perform all necessary hardware accesses to that chip.
  *   - Call HardwareRegister::clearAccessor() to remove the chip access. This
  *     helps ensure we don't try to access the wrong chip.
+ *
+ * Register cache:
+ *
+ *  In order to save memory space, each instance of this class does not store
+ *  the contents of the target hardware register. Instead, that data is stored
+ *  in a register cache, which a static variable defined in this class. This
+ *  allows us to store only what we need. The cache can also be thought of as a
+ *  snapshot of the registers at the time of isolation, which can be useful if
+ *  the hardware is still running and register values could change.
+ *
+ *  In order to ensure stale data isn't used from the cache, call
+ *  HardwareRegister::flushAll() before beginning isolation on a new attention.
+ *  Also, HardwareRegister::flushAll() should be called when the isolator is
+ *  uninitialized before the rest of the isolation objects are deleted.
  */
 class HardwareRegister : public Register
 {
@@ -95,6 +102,7 @@
      */
     uint32_t GetBitLength(void) const { return iv_bitLength ;}
 #endif
+    size_t getByteSize() const { return 8; } // TODO
 
     /**
      * @brief  Reads a register from hardware via the user interface APIs.
@@ -196,22 +204,6 @@
 
   friend class CaptureData;
 
-    /** @return TRUE if entry for this register exist in this cache. */
-    bool queryCache() const;
-
-    /**
-     * @brief  Reads register contents from cache.
-     * @return Reference to bit string buffer maintained in cache.
-     */
-    BitString & readCache() const;
-
-    /**
-     * @brief     Deletes one or all entry in the cache
-     * @param     RuleChip pointer associated with register
-     * @return    Nil
-     */
-    void flushCache( ExtensibleChip *i_pChip = nullptr ) const;
-
   private: // Data
 
     uint32_t        iv_bitLength;     // bit length of scom
@@ -305,6 +297,103 @@
 
         return cv_accessor->getChip();
     }
+
+  private: // Register cache class variable
+
+    /**
+     * @brief Caches the contents of registers read from hardware.
+     *
+     * The goal is to create a snapshot of the hardware register contents as
+     * close to the reported attention as possible. This snapshot is then used
+     * for additional analysis/debug when needed.
+     */
+    class Cache
+    {
+      public:
+
+        /** @brief Default constructor. */
+        Cache() = default;
+
+        /** @brief Destructor. */
+        ~Cache() = default;
+
+        /** @brief Copy constructor. */
+        Cache( const Cache & ) = delete;
+
+        /** @brief Assignment operator. */
+        Cache & operator=( const Cache & ) = delete;
+
+        /**
+         * @brief  Queries if a specific entry exists in the cache.
+         * @param  i_chip  The target chip.
+         * @param  i_hwReg The target register.
+         * @return True if the entry exists, false otherwise.
+         */
+        bool query( const Chip & i_chip,
+                    const HardwareRegister * i_hwReg ) const;
+
+        /**
+         * @brief  Returns the data buffer for the given chip and register.
+         * @param  i_chip  The target chip.
+         * @param  i_hwReg The target register.
+         * @return A reference to the BitString containing the register data.
+         * @note   If an entry does not exist in the cache, an entry will be
+         *         created and the BitString will be initialized to 0.
+         */
+        BitString & access( const Chip & i_chip,
+                            const HardwareRegister * i_hwReg );
+
+        /** @brief Flushes entire contents from cache. */
+        void flush();
+
+        /**
+         * @brief Removes a single register from the cache.
+         * @param i_chip  The target chip.
+         * @param i_hwReg The target register.
+         */
+        void flush( const Chip & i_chip, const HardwareRegister * i_hwReg );
+
+      private:
+
+        /**
+         * @brief Stores a BitStringBuffer for each HardwareRegister per Chip.
+         *
+         * The HardwareRegister keys will just be pointers to the isolation
+         * objects created in the main initialize() API. Those should exist
+         * until the main uninitialize() API is called. It is important that the
+         * cache is flushed at the beginning of the uninitialize() API before
+         * the rest of the isolation objects are deleted.
+         *
+         * The Chip keys are copies of the objects passed to the isolator
+         * because the user application is responsible for storage of the
+         * objects passed to the isolator. We don't want to chance a Chip was
+         * created as a local variable that goes out of scope, or other similar
+         * situations.
+         */
+        std::map<Chip, std::map<const HardwareRegister*, BitString*>> iv_cache;
+    };
+
+    /** This allows all HardwareRegister objects access to the cache. */
+    static Cache cv_cache;
+
+  public: // Register cache management functions.
+
+    /** @brief Flushes the entire register cache. */
+    static void flushAll() { cv_cache.flush(); }
+
+  private: // Register cache management functions.
+
+    /** @return True if an entry for this register exist in this cache. */
+    bool queryCache() const
+    {
+        return cv_cache.query( getAccessorChip(), this );
+    }
+
+    /** @return A reference to this register's BitString in cache. */
+    BitString & accessCache() const
+    {
+        return cv_cache.access( getAccessorChip(), this );
+    }
 };
 
 } // end namespace libhei
diff --git a/src/register/prdfRegisterCache.C b/src/register/prdfRegisterCache.C
deleted file mode 100644
index f73b4cd..0000000
--- a/src/register/prdfRegisterCache.C
+++ /dev/null
@@ -1,126 +0,0 @@
-/* IBM_PROLOG_BEGIN_TAG                                                   */
-/* This is an automatically generated prolog.                             */
-/*                                                                        */
-/* $Source: src/usr/diag/prdf/common/framework/register/prdfRegisterCache.C $ */
-/*                                                                        */
-/* OpenPOWER HostBoot Project                                             */
-/*                                                                        */
-/* Contributors Listed Below - COPYRIGHT 2012,2017                        */
-/* [+] International Business Machines Corp.                              */
-/*                                                                        */
-/*                                                                        */
-/* Licensed under the Apache License, Version 2.0 (the "License");        */
-/* you may not use this file except in compliance with the License.       */
-/* You may obtain a copy of the License at                                */
-/*                                                                        */
-/*     http://www.apache.org/licenses/LICENSE-2.0                         */
-/*                                                                        */
-/* Unless required by applicable law or agreed to in writing, software    */
-/* distributed under the License is distributed on an "AS IS" BASIS,      */
-/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or        */
-/* implied. See the License for the specific language governing           */
-/* permissions and limitations under the License.                         */
-/*                                                                        */
-/* IBM_PROLOG_END_TAG                                                     */
-
-#include <hei_includes.hpp>
-
-#include <prdfRegisterCache.H>
-
-namespace libhei
-{
-
-//------------------------------------------------------------------------------
-
-RegDataCache & RegDataCache::getCachedRegisters()
-{
-    return PRDF_GET_SINGLETON( ReadCache );
-}
-
-//------------------------------------------------------------------------------
-
-RegDataCache::~RegDataCache()
-{
-    flush();
-}
-
-//------------------------------------------------------------------------------
-
-BitString & RegDataCache::read( ExtensibleChip * i_chip,
-                                       const Register * i_reg )
-{
-    ScomRegisterAccess l_scomAccessKey ( *i_reg, i_chip );
-    BitString * l_pBitString = queryCache( l_scomAccessKey );
-
-    if ( nullptr == l_pBitString )
-    {
-        // Creating new entry
-        l_pBitString = new BitStringBuffer( i_reg->GetBitLength() );
-        // Adding register in the cache
-        iv_cachedRead[l_scomAccessKey] = l_pBitString;
-    }
-
-    return *l_pBitString;
-}
-
-//------------------------------------------------------------------------------
-
-void RegDataCache::flush()
-{
-    for ( CacheDump::iterator it = iv_cachedRead.begin();
-          it != iv_cachedRead.end(); it++ )
-    {
-        // Freeing up the bit string memory reserved on heap
-        delete it->second;
-    }
-
-    // Deleting all the entry from the cache
-    iv_cachedRead.clear();
-}
-
-//------------------------------------------------------------------------------
-
-void RegDataCache::flush( ExtensibleChip* i_pChip,
-                          const Register * i_pRegister )
-{
-    ScomRegisterAccess l_scomAccessKey ( *i_pRegister,i_pChip );
-    // Find the entries associated with the given target in the map
-    CacheDump::iterator it = iv_cachedRead.find( l_scomAccessKey );
-
-    // If entry exists delete the entry for given scom address
-    if ( it !=iv_cachedRead.end() )
-    {
-        delete it->second;
-        iv_cachedRead.erase( it );
-    }
-}
-
-//------------------------------------------------------------------------------
-
-BitString * RegDataCache::queryCache(
-                            ExtensibleChip* i_pChip,
-                            const Register * i_pRegister )const
-{
-    ScomRegisterAccess l_scomAccessKey ( *i_pRegister,i_pChip );
-    return queryCache( l_scomAccessKey );
-}
-
-//------------------------------------------------------------------------------
-
-BitString * RegDataCache::queryCache(
-                        const ScomRegisterAccess & i_scomAccessKey ) const
-{
-    BitString * l_pBitString = nullptr;
-    CacheDump::const_iterator itDump = iv_cachedRead.find( i_scomAccessKey );
-    if( iv_cachedRead.end() != itDump )
-    {
-        l_pBitString = itDump->second ;
-    }
-
-    return l_pBitString;
-}
-
-//------------------------------------------------------------------------------
-
-} // end namespace libhei
-
diff --git a/src/register/prdfRegisterCache.H b/src/register/prdfRegisterCache.H
deleted file mode 100644
index 9176b0e..0000000
--- a/src/register/prdfRegisterCache.H
+++ /dev/null
@@ -1,123 +0,0 @@
-/* IBM_PROLOG_BEGIN_TAG                                                   */
-/* This is an automatically generated prolog.                             */
-/*                                                                        */
-/* $Source: src/usr/diag/prdf/common/framework/register/prdfRegisterCache.H $ */
-/*                                                                        */
-/* OpenPOWER HostBoot Project                                             */
-/*                                                                        */
-/* Contributors Listed Below - COPYRIGHT 2012,2017                        */
-/* [+] International Business Machines Corp.                              */
-/*                                                                        */
-/*                                                                        */
-/* Licensed under the Apache License, Version 2.0 (the "License");        */
-/* you may not use this file except in compliance with the License.       */
-/* You may obtain a copy of the License at                                */
-/*                                                                        */
-/*     http://www.apache.org/licenses/LICENSE-2.0                         */
-/*                                                                        */
-/* Unless required by applicable law or agreed to in writing, software    */
-/* distributed under the License is distributed on an "AS IS" BASIS,      */
-/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or        */
-/* implied. See the License for the specific language governing           */
-/* permissions and limitations under the License.                         */
-/*                                                                        */
-/* IBM_PROLOG_END_TAG                                                     */
-
-#ifndef REG_CACHE_H
-#define REG_CACHE_H
-
-/** @file prdfRegisterCache.H */
-
-#include <map>
-
-#include <util/hei_bit_string.hpp>
-
-#include <prdfGlobal.H>
-#include <prdfScanFacility.H>
-#include <prdfTargetFwdRef.H>
-
-namespace libhei
-{
-/**
- * @brief Caches the contents of registers used during analysis.
- *
- * It maintains the latest content of a register in a map. If contents of the
- * register remain unchanged, register read returns contents stored in
- * cache rather than reading from hardware. Hence it brings efficiency in read.
- * Whenever write to actual hardware takes place, it is expected that once write
- * to hardware succeeds, the user of cache shall call flush. It drops the
- * particular register from map. As a result, when read takes place from same
- * register next time, read from cache fails and actual access to hardware
- * takes place.
- */
-class RegDataCache
-{
-  public:
-
-    /**
-     * @brief Constructor
-     */
-    RegDataCache()
-    { }
-
-    /**
-     * @brief Destructor
-     */
-    ~RegDataCache();
-
-    /**
-     * @brief  Returns reference to singleton instance of the RegDataCache.
-     * @return The singleton reference.
-     */
-     static RegDataCache & getCachedRegisters();
-
-    /**
-     * @brief Returns the data buffer for the given target and address.
-     * @param i_chip The target associated with the register.
-     * @param i_reg  Pointer to register to be read.
-     * @return A reference to the data buffer associated with the register.
-     */
-    BitString & read( ExtensibleChip * i_chip,
-                             const Register * i_reg );
-
-    /**
-     * @brief Flushes entire contents from cache.
-     */
-    void flush();
-
-    /**
-     * @brief Removes a single entry from the cache.
-     * @param i_pChip       The rulechip  associated with the register.
-     * @param i_pRegister   points to the register to be flushed from cache.
-     */
-    void flush( ExtensibleChip* i_pChip,
-                const Register * i_pRegister );
-    /**
-     * @brief Queries if a specific entry exist in cache.
-     * @param i_pChip       The rulechip  associated with the register.
-     * @param i_pRegister   base part of register entry to be queried in cache.
-     * @return pointer to cache entry associated with a given register
-     */
-     BitString * queryCache( ExtensibleChip* i_pChip,
-                            const Register * i_pRegister )const;
-    /**
-     * @brief Queries if a specific entry exist in cache.
-     * @param i_scomAccessKey Reference to register to be queried.
-     * @return pointer to cache entry associated with a given register
-     */
-
-    BitString * queryCache(
-                        const ScomRegisterAccess & i_scomAccessKey )const;
-  private: // data
-
-    typedef std::map<ScomRegisterAccess, BitString *> CacheDump;
-    CacheDump iv_cachedRead;
-
-};
-
-PRDF_DECLARE_SINGLETON(RegDataCache, ReadCache);
-
-} // end namespace libhei
-
-#endif // REG_CACHE_H
-
diff --git a/src/util/hei_bit_string.cpp b/src/util/hei_bit_string.cpp
index 2cbb3a1..55da5cd 100755
--- a/src/util/hei_bit_string.cpp
+++ b/src/util/hei_bit_string.cpp
@@ -16,13 +16,13 @@
 //##############################################################################
 
 // number of bits in a uint64_t
-constexpr uint32_t BitString::UINT64_BIT_LEN = sizeof(uint64_t) * 8;
+constexpr uint64_t BitString::UINT64_BIT_LEN = sizeof(uint64_t) * 8;
 // number of bits in a uint8_t
-constexpr uint32_t BitString::UINT8_BIT_LEN = sizeof(uint8_t) * 8;
+constexpr uint64_t BitString::UINT8_BIT_LEN = sizeof(uint8_t) * 8;
 
 //------------------------------------------------------------------------------
 
-uint64_t BitString::getFieldRight( uint32_t i_pos, uint32_t i_len ) const
+uint64_t BitString::getFieldRight( uint64_t i_pos, uint64_t i_len ) const
 {
     HEI_ASSERT( nullptr != getBufAddr() );      // must to have a valid address
     HEI_ASSERT( 0 < i_len );                    // must have at least one bit
@@ -31,13 +31,13 @@
 
     // Get the relative address of this byte and the relative starting position
     // within the byte.
-    uint32_t relPos = 0;
+    uint64_t relPos = 0;
     uint8_t * relAddr = getRelativePosition( relPos, i_pos );
 
     // Get the length of the target bit field within this byte and the length of
     // the bit field for any remaining bits.
-    uint32_t bf_len     = i_len;
-    uint32_t remain_len = 0;
+    uint64_t bf_len     = i_len;
+    uint64_t remain_len = 0;
     if ( UINT8_BIT_LEN < relPos + i_len )
     {
         // The target bit field crosses a byte boundary. So truncate the bit
@@ -66,7 +66,7 @@
 
 //------------------------------------------------------------------------------
 
-void BitString::setFieldLeft( uint32_t i_pos, uint32_t i_len, uint64_t i_val )
+void BitString::setFieldLeft( uint64_t i_pos, uint64_t i_len, uint64_t i_val )
 {
     HEI_ASSERT( nullptr != getBufAddr() );      // must to have a valid address
     HEI_ASSERT( 0 < i_len );                    // must have at least one bit
@@ -75,13 +75,13 @@
 
     // Get the relative address of this byte and the relative starting position
     // within the byte.
-    uint32_t relPos = 0;
+    uint64_t relPos = 0;
     uint8_t * relAddr = getRelativePosition( relPos, i_pos );
 
     // Get the length of the target bit field within this byte and the length of
     // the bit field for any remaining bits.
-    uint32_t bf_len     = i_len;
-    uint32_t remain_len = 0;
+    uint64_t bf_len     = i_len;
+    uint64_t remain_len = 0;
     if ( UINT8_BIT_LEN < relPos + i_len )
     {
         // The target bit field crosses a byte boundary. So truncate the bit
@@ -93,15 +93,15 @@
     // It is possible there are bits in this byte on either side of the target
     // bit field that must be preserved. Get the length of each of those bit
     // fields.
-    uint32_t bf_l_len = relPos;
-    uint32_t bf_r_len = UINT8_BIT_LEN - (bf_l_len + bf_len);
+    uint64_t bf_l_len = relPos;
+    uint64_t bf_r_len = UINT8_BIT_LEN - (bf_l_len + bf_len);
 
     // Get the target bit field from the left justified inputed value.
     uint8_t bf = (i_val >> (UINT64_BIT_LEN - bf_len)) << bf_r_len;
 
     // Get the bit fields on either side of the target bit field.
-    uint32_t bf_l_shift = UINT8_BIT_LEN - bf_l_len;
-    uint32_t bf_r_shift = UINT8_BIT_LEN - bf_r_len;
+    uint64_t bf_l_shift = UINT8_BIT_LEN - bf_l_len;
+    uint64_t bf_r_shift = UINT8_BIT_LEN - bf_r_len;
     uint8_t bf_l = *relAddr; bf_l >>= bf_l_shift; bf_l <<= bf_l_shift;
     uint8_t bf_r = *relAddr; bf_r <<= bf_r_shift; bf_r >>= bf_r_shift;
 
@@ -118,8 +118,8 @@
 
 //------------------------------------------------------------------------------
 
-void BitString::setPattern( uint32_t i_sPos, uint32_t i_sLen,
-                            uint64_t i_pattern, uint32_t i_pLen )
+void BitString::setPattern( uint64_t i_sPos, uint64_t i_sLen,
+                            uint64_t i_pattern, uint64_t i_pLen )
 {
 
     HEI_ASSERT(nullptr != getBufAddr());        // must to have a valid address
@@ -137,11 +137,11 @@
     bs.setFieldRight(0, i_pLen, i_pattern);
 
     // Iterate the range in chunks the size of i_pLen.
-    uint32_t endPos = i_sPos + i_sLen;
-    for ( uint32_t pos = i_sPos; pos < endPos; pos += i_pLen )
+    uint64_t endPos = i_sPos + i_sLen;
+    for ( uint64_t pos = i_sPos; pos < endPos; pos += i_pLen )
     {
         // The true chunk size is either i_pLen or the leftovers at the end.
-        uint32_t len = std::min( i_pLen, endPos - pos );
+        uint64_t len = std::min( i_pLen, endPos - pos );
 
         // Get this chunk's pattern value, truncate (right justified) if needed.
         uint64_t pattern = bs.getFieldRight( 0, len );
@@ -153,8 +153,8 @@
 
 //------------------------------------------------------------------------------
 
-void BitString::setString( const BitString & i_sStr, uint32_t i_sPos,
-                           uint32_t i_sLen, uint32_t i_dPos )
+void BitString::setString( const BitString & i_sStr, uint64_t i_sPos,
+                           uint64_t i_sLen, uint64_t i_dPos )
 {
     // Ensure the source parameters are valid.
     HEI_ASSERT( nullptr != i_sStr.getBufAddr() );
@@ -167,11 +167,11 @@
 
     // If the source length is greater than the destination length than the
     // extra source bits are ignored.
-    uint32_t actLen = std::min( i_sLen, getBitLen() - i_dPos );
+    uint64_t actLen = std::min( i_sLen, getBitLen() - i_dPos );
 
     // The bit strings may be in overlapping memory spaces. So we need to copy
     // the data in the correct direction to prevent overlapping.
-    uint32_t sRelOffset = 0, dRelOffset = 0;
+    uint64_t sRelOffset = 0, dRelOffset = 0;
     uint8_t * sRelAddr = i_sStr.getRelativePosition( sRelOffset, i_sPos );
     uint8_t * dRelAddr =        getRelativePosition( dRelOffset, i_dPos );
 
@@ -184,9 +184,9 @@
               ((dRelAddr == sRelAddr) && (dRelOffset < sRelOffset)) )
     {
         // Copy the data forward.
-        for ( uint32_t pos = 0; pos < actLen; pos += UINT64_BIT_LEN )
+        for ( uint64_t pos = 0; pos < actLen; pos += UINT64_BIT_LEN )
         {
-            uint32_t len = std::min( actLen - pos, UINT64_BIT_LEN );
+            uint64_t len = std::min( actLen - pos, UINT64_BIT_LEN );
 
             uint64_t value = i_sStr.getFieldRight( i_sPos + pos, len );
             setFieldRight( i_dPos + pos, len, value );
@@ -195,12 +195,12 @@
     else // Copy the data backwards.
     {
         // Get the first position of the last chunk (byte aligned).
-        uint32_t lastPos = ((actLen-1) / UINT64_BIT_LEN) * UINT64_BIT_LEN;
+        uint64_t lastPos = ((actLen-1) / UINT64_BIT_LEN) * UINT64_BIT_LEN;
 
         // Start with the last chunk and work backwards.
         for ( int32_t pos = lastPos; 0 <= pos; pos -= UINT64_BIT_LEN )
         {
-            uint32_t len = std::min( actLen - pos, UINT64_BIT_LEN );
+            uint64_t len = std::min( actLen - pos, UINT64_BIT_LEN );
             uint64_t value = i_sStr.getFieldRight( i_sPos + pos, len );
             setFieldRight( i_dPos + pos, len, value );
         }
@@ -212,11 +212,11 @@
 void BitString::maskString( const BitString & i_mask )
 {
     // Get the length of the smallest string.
-    uint32_t actLen = std::min( getBitLen(), i_mask.getBitLen() );
+    uint64_t actLen = std::min( getBitLen(), i_mask.getBitLen() );
 
-    for ( uint32_t pos = 0; pos < actLen; pos += UINT64_BIT_LEN )
+    for ( uint64_t pos = 0; pos < actLen; pos += UINT64_BIT_LEN )
     {
-        uint32_t len = std::min( actLen - pos, UINT64_BIT_LEN );
+        uint64_t len = std::min( actLen - pos, UINT64_BIT_LEN );
 
         uint64_t dVal =        getFieldRight( pos, len );
         uint64_t sVal = i_mask.getFieldRight( pos, len );
@@ -232,9 +232,9 @@
     if ( getBitLen() != i_str.getBitLen() )
         return false; // size not equal
 
-    for ( uint32_t pos = 0; pos < getBitLen(); pos += UINT64_BIT_LEN )
+    for ( uint64_t pos = 0; pos < getBitLen(); pos += UINT64_BIT_LEN )
     {
-        uint32_t len = std::min( getBitLen() - pos, UINT64_BIT_LEN );
+        uint64_t len = std::min( getBitLen() - pos, UINT64_BIT_LEN );
 
         if ( getFieldRight(pos, len) != i_str.getFieldRight(pos, len) )
             return false; // bit strings do not match
@@ -247,9 +247,9 @@
 
 bool BitString::isZero() const
 {
-    for ( uint32_t pos = 0; pos < getBitLen(); pos += UINT64_BIT_LEN )
+    for ( uint64_t pos = 0; pos < getBitLen(); pos += UINT64_BIT_LEN )
     {
-        uint32_t len = std::min( getBitLen() - pos, UINT64_BIT_LEN );
+        uint64_t len = std::min( getBitLen() - pos, UINT64_BIT_LEN );
 
         if ( 0 != getFieldRight(pos, len) )
             return false; // something is non-zero
@@ -260,15 +260,15 @@
 
 //------------------------------------------------------------------------------
 
-uint32_t BitString::getSetCount( uint32_t i_pos, uint32_t i_len ) const
+uint64_t BitString::getSetCount( uint64_t i_pos, uint64_t i_len ) const
 {
-    uint32_t endPos = i_pos + i_len;
+    uint64_t endPos = i_pos + i_len;
 
     HEI_ASSERT( endPos <= getBitLen() );
 
-    uint32_t count = 0;
+    uint64_t count = 0;
 
-    for ( uint32_t i = i_pos; i < endPos; i++ )
+    for ( uint64_t i = i_pos; i < endPos; i++ )
     {
         if ( isBitSet(i) ) count++;
     }
@@ -282,9 +282,9 @@
 {
     BitStringBuffer bsb( getBitLen() );
 
-    for ( uint32_t pos = 0; pos < getBitLen(); pos += UINT64_BIT_LEN )
+    for ( uint64_t pos = 0; pos < getBitLen(); pos += UINT64_BIT_LEN )
     {
-        uint32_t len = std::min( getBitLen() - pos, UINT64_BIT_LEN );
+        uint64_t len = std::min( getBitLen() - pos, UINT64_BIT_LEN );
 
         uint64_t dVal = getFieldRight( pos, len );
 
@@ -299,13 +299,13 @@
 BitStringBuffer BitString::operator&( const BitString & i_bs ) const
 {
     // Get the length of the smallest string.
-    uint32_t actLen = std::min( getBitLen(), i_bs.getBitLen() );
+    uint64_t actLen = std::min( getBitLen(), i_bs.getBitLen() );
 
     BitStringBuffer bsb( actLen );
 
-    for ( uint32_t pos = 0; pos < actLen; pos += UINT64_BIT_LEN )
+    for ( uint64_t pos = 0; pos < actLen; pos += UINT64_BIT_LEN )
     {
-        uint32_t len = std::min( actLen - pos, UINT64_BIT_LEN );
+        uint64_t len = std::min( actLen - pos, UINT64_BIT_LEN );
 
         uint64_t dVal =      getFieldRight( pos, len );
         uint64_t sVal = i_bs.getFieldRight( pos, len );
@@ -321,13 +321,13 @@
 BitStringBuffer BitString::operator|( const BitString & i_bs ) const
 {
     // Get the length of the smallest string.
-    uint32_t actLen = std::min( getBitLen(), i_bs.getBitLen() );
+    uint64_t actLen = std::min( getBitLen(), i_bs.getBitLen() );
 
     BitStringBuffer bsb( actLen );
 
-    for ( uint32_t pos = 0; pos < actLen; pos += UINT64_BIT_LEN )
+    for ( uint64_t pos = 0; pos < actLen; pos += UINT64_BIT_LEN )
     {
-        uint32_t len = std::min( actLen - pos, UINT64_BIT_LEN );
+        uint64_t len = std::min( actLen - pos, UINT64_BIT_LEN );
 
         uint64_t dVal =      getFieldRight( pos, len );
         uint64_t sVal = i_bs.getFieldRight( pos, len );
@@ -340,7 +340,7 @@
 
 //------------------------------------------------------------------------------
 
-BitStringBuffer BitString::operator>>( uint32_t i_shift ) const
+BitStringBuffer BitString::operator>>( uint64_t i_shift ) const
 {
     BitStringBuffer bsb( getBitLen() ); // default all zeros
 
@@ -358,7 +358,7 @@
 
 //------------------------------------------------------------------------------
 
-BitStringBuffer BitString::operator<<( uint32_t i_shift ) const
+BitStringBuffer BitString::operator<<( uint64_t i_shift ) const
 {
     BitStringBuffer bsb( getBitLen() ); // default all zeros
 
@@ -377,8 +377,8 @@
 
 //------------------------------------------------------------------------------
 
-uint8_t * BitString::getRelativePosition( uint32_t & o_relPos,
-                                           uint32_t   i_absPos ) const
+uint8_t * BitString::getRelativePosition( uint64_t & o_relPos,
+                                          uint64_t   i_absPos ) const
 {
     HEI_ASSERT( nullptr != getBufAddr() ); // must to have a valid address
     HEI_ASSERT( i_absPos < getBitLen() );  // must be a valid position
@@ -392,7 +392,7 @@
 //                          BitStringBuffer class
 //##############################################################################
 
-BitStringBuffer::BitStringBuffer( uint32_t i_bitLen ) :
+BitStringBuffer::BitStringBuffer( uint64_t i_bitLen ) :
     BitString( i_bitLen, nullptr )
 {
     initBuffer();
diff --git a/src/util/hei_bit_string.hpp b/src/util/hei_bit_string.hpp
index 889dc41..127ce61 100755
--- a/src/util/hei_bit_string.hpp
+++ b/src/util/hei_bit_string.hpp
@@ -60,8 +60,8 @@
 {
   private: // constants
 
-    static const uint32_t UINT64_BIT_LEN;
-    static const uint32_t UINT8_BIT_LEN;
+    static const uint64_t UINT64_BIT_LEN;
+    static const uint64_t UINT8_BIT_LEN;
 
   public: // functions
 
@@ -76,8 +76,8 @@
      * @pre   Use getMinBytes() to calulate the minimum number of bytes needed
      *        to allocate sufficient memory space for this bit string.
      */
-    BitString( uint32_t i_bitLen, void * i_bufAddr,
-               uint32_t i_offset = 0 ) :
+    BitString( uint64_t i_bitLen, void * i_bufAddr,
+               uint64_t i_offset = 0 ) :
         iv_bitLen(i_bitLen), iv_bufAddr(i_bufAddr), iv_offset(i_offset)
     {}
 
@@ -85,7 +85,7 @@
     virtual ~BitString() {}
 
     /** @return The number of bits in the bit string buffer. */
-    uint32_t getBitLen() const { return iv_bitLen; }
+    uint64_t getBitLen() const { return iv_bitLen; }
 
     /** @return The address of the bit string buffer. Note that this may
      *          return nullptr. */
@@ -98,7 +98,7 @@
      * @return The minimum number of bytes required to allocate sufficient
      *         memory space for a bit string.
      */
-    static uint32_t getMinBytes( uint32_t i_bitLen, uint32_t i_offset = 0 )
+    static uint64_t getMinBytes( uint64_t i_bitLen, uint64_t i_offset = 0 )
     {
         return (i_bitLen + i_offset + UINT8_BIT_LEN-1) / UINT8_BIT_LEN;
     }
@@ -114,7 +114,7 @@
      * @pre    i_len <= UINT64_BIT_LEN
      * @pre    i_pos + i_len <= getBitLen()
      */
-    uint64_t getFieldLeft( uint32_t i_pos, uint32_t i_len ) const
+    uint64_t getFieldLeft( uint64_t i_pos, uint64_t i_len ) const
     {
         return getFieldRight(i_pos, i_len) << (UINT64_BIT_LEN - i_len);
     }
@@ -130,7 +130,7 @@
      * @pre    i_len <= UINT64_BIT_LEN
      * @pre    i_pos + i_len <= getBitLen()
      */
-    uint64_t getFieldRight( uint32_t i_pos, uint32_t i_len ) const;
+    uint64_t getFieldRight( uint64_t i_pos, uint64_t i_len ) const;
 
     /**
      * @brief  Sets a left-justified value of the given length into the bit
@@ -143,7 +143,7 @@
      * @pre   i_len <= UINT64_BIT_LEN
      * @pre   i_pos + i_len <= getBitLen()
      */
-    void setFieldLeft( uint32_t i_pos, uint32_t i_len, uint64_t i_val );
+    void setFieldLeft( uint64_t i_pos, uint64_t i_len, uint64_t i_val );
 
     /**
      * @brief  Sets a right-justified value of the given length into the bit
@@ -156,7 +156,7 @@
      * @pre   i_len <= UINT64_BIT_LEN
      * @pre   i_pos + i_len <= getBitLen()
      */
-    void setFieldRight( uint32_t i_pos, uint32_t i_len, uint64_t i_val )
+    void setFieldRight( uint64_t i_pos, uint64_t i_len, uint64_t i_val )
     {
         setFieldLeft( i_pos, i_len, i_val << (UINT64_BIT_LEN - i_len) );
     }
@@ -166,7 +166,7 @@
      * @return True if the bit at the given position is set(1), false otherwise.
      * @pre    i_pos < getBitLen().
      */
-    bool isBitSet( uint32_t i_pos ) const
+    bool isBitSet( uint64_t i_pos ) const
     {
         return 0 != getFieldRight(i_pos, 1);
     }
@@ -176,7 +176,7 @@
      * @param i_pos The target position.
      * @pre   i_pos < getBitLen().
      */
-    void setBit( uint32_t i_pos ) { setFieldRight( i_pos, 1, 1 ); }
+    void setBit( uint64_t i_pos ) { setFieldRight( i_pos, 1, 1 ); }
 
     /** @brief Sets the entire bit string to 1's. */
     void setAll() { setPattern(UINT64_MAX); }
@@ -186,7 +186,7 @@
      * @param i_pos The target position.
      * @pre   i_pos < getBitLen().
      */
-    void clearBit( uint32_t i_pos ) { setFieldRight( i_pos, 1, 0 ); }
+    void clearBit( uint64_t i_pos ) { setFieldRight( i_pos, 1, 0 ); }
 
     /** @brief Sets the entire bit string to 0's. */
     void clearAll() { setPattern(0); }
@@ -212,8 +212,8 @@
      *            Old String: 0001001000
      *            New String: 0000110000
      */
-    void setPattern( uint32_t i_sPos, uint32_t i_sLen,
-                     uint64_t i_pattern, uint32_t i_pLen );
+    void setPattern( uint64_t i_sPos, uint64_t i_sLen,
+                     uint64_t i_pattern, uint64_t i_pLen );
 
     /**
      * @brief Sets entire string based on the pattern and length provided.
@@ -223,7 +223,7 @@
      * @post  The entire string is filled with the pattern.
      * @post  The pattern is repeated/truncated as needed.
      */
-    void setPattern( uint64_t i_pattern, uint32_t i_pLen )
+    void setPattern( uint64_t i_pattern, uint64_t i_pLen )
     {
         setPattern( 0, getBitLen(), i_pattern, i_pLen );
     }
@@ -258,8 +258,8 @@
      *        string, then the extra bits in this string are not modified.
      * @note  This string and the source string may specify overlapping memory.
      */
-    void setString( const BitString & i_sStr, uint32_t i_sPos,
-                    uint32_t i_sLen, uint32_t i_dPos = 0 );
+    void setString( const BitString & i_sStr, uint64_t i_sPos,
+                    uint64_t i_sLen, uint64_t i_dPos = 0 );
 
     /**
      * @brief Set bits in this string based on the provided string.
@@ -304,10 +304,10 @@
      * @pre    nullptr != getBufAddr()
      * @pre    i_pos + i_len <= getBitLen()
      */
-    uint32_t getSetCount( uint32_t i_pos, uint32_t i_len ) const;
+    uint64_t getSetCount( uint64_t i_pos, uint64_t i_len ) const;
 
     /** @return The number of bits that are set(1) in this string. */
-    uint32_t getSetCount() const { return getSetCount( 0, getBitLen() ); }
+    uint64_t getSetCount() const { return getSetCount( 0, getBitLen() ); }
 
     /** @brief Comparison operator. */
     bool operator==( const BitString & i_str ) const { return isEqual(i_str); }
@@ -322,10 +322,18 @@
     BitStringBuffer operator|( const BitString & i_bs ) const;
 
     /** @brief Right shift operator. */
-    BitStringBuffer operator>>( uint32_t i_shift ) const;
+    BitStringBuffer operator>>( uint64_t i_shift ) const;
 
     /** @brief Left shift operator. */
-    BitStringBuffer operator<<( uint32_t i_shift ) const;
+    BitStringBuffer operator<<( uint64_t i_shift ) const;
+
+    /**
+     * @brief Explicitly disables copy from BitString.
+     *
+     * Prevents assigning a BitString & to a BitString, which would strip
+     * polymorphism.
+     */
+    BitString( const BitString & i_bs ) = delete;
 
     /**
      * @brief Explicitly disables assignment from BitStringBuffer.
@@ -353,7 +361,7 @@
     void setBufAddr( void * i_newBufAddr ) { iv_bufAddr = i_newBufAddr; }
 
     /** @param i_newBitLen The new bit length of this bit string buffer. */
-    void setBitLen( uint32_t i_newBitLen ) { iv_bitLen = i_newBitLen; }
+    void setBitLen( uint64_t i_newBitLen ) { iv_bitLen = i_newBitLen; }
 
   private: // functions
 
@@ -367,14 +375,14 @@
      * @pre    nullptr != getBufAddr()
      * @pre    i_absPos < getBitLen()
      */
-    uint8_t * getRelativePosition( uint32_t & o_relPos,
-                                    uint32_t   i_absPos ) const;
+    uint8_t * getRelativePosition( uint64_t & o_relPos,
+                                   uint64_t   i_absPos ) const;
 
   private: // instance variables
 
-    uint32_t   iv_bitLen;  ///< The bit length of this buffer.
+    uint64_t   iv_bitLen;  ///< The bit length of this buffer.
     void     * iv_bufAddr; ///< The beginning address of this buffer.
-    uint32_t   iv_offset;  ///< Start position offset
+    uint64_t   iv_offset;  ///< Start position offset
 };
 
 //##############################################################################
@@ -394,7 +402,7 @@
      * @brief Constructor
      * @param i_bitLen Number of bits in the string.
      */
-    explicit BitStringBuffer( uint32_t i_bitLen );
+    explicit BitStringBuffer( uint64_t i_bitLen );
 
     /** @brief Destructor */
     ~BitStringBuffer();