From 8bc5ac336d5d684341a05e97d1cb1b18ecba0331 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 27 Jan 2009 00:31:24 +0000 Subject: Hey, I think that'll fix some things valgrind was bitching about. Apparently BitString is...not so good...I may have to rewrite big chunks. --- src/bitstring.cpp | 10 +++++-- src/cache.h | 81 +++++++++++++++++++++++++++++++++++++++++++--------- src/cachestorenids.h | 7 +++-- src/tafreader.cpp | 1 + src/tests/cache.cpp | 11 ++++++- 5 files changed, 90 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/bitstring.cpp b/src/bitstring.cpp index 85f89e2..b3bcde5 100644 --- a/src/bitstring.cpp +++ b/src/bitstring.cpp @@ -1,8 +1,10 @@ -#include "bitstring.h" +#include "bu/bitstring.h" #include #include #include +#include "bu/exceptionbase.h" + #ifdef _WIN32 #define random() rand() #endif @@ -187,6 +189,8 @@ void Bu::BitString::fixup() void Bu::BitString::setBit( long iBit, bool bBitState ) { + if( iBit < 0 || iBit >= iBits ) + throw Bu::ExceptionBase("bit out of range: %d in (0-%d)", iBit, iBits ); if( bBitState ) { caData[iBit/8] |= (1<<(iBit%8)); @@ -342,7 +346,7 @@ bool Bu::BitString::setSize( long iLength, bool bClear ) { // Ok, reallocate and copy... iBits = iLength; - long iNewBytes = bitsToBytes( iLength ); +// long iNewBytes = bitsToBytes( iLength ); if( bClear ) { delete[] caData; @@ -352,7 +356,7 @@ bool Bu::BitString::setSize( long iLength, bool bClear ) else { unsigned char *tmp = caData; - caData = new unsigned char[iBytes]; + caData = new unsigned char[iNewBytes]; if( iNewBytes < iBytes ) { memcpy( caData, tmp, iNewBytes ); diff --git a/src/cache.h b/src/cache.h index 259278c..ccc4966 100644 --- a/src/cache.h +++ b/src/cache.h @@ -11,19 +11,18 @@ namespace Bu { +// template +// keytype __cacheGetKey( obtype *&pObj ); + template class Cache { - //friend class Bu::CPtr; public: - // typedef Bu::CPtr Ptr; - /** * Cache Pointer - Provides access to data that is held within the * cache. This provides safe, refcounting access to data stored in * the cache, with support for lazy loading. */ - //template class Ptr { friend class Bu::Cache; @@ -37,6 +36,13 @@ namespace Bu if( pCache ) pCache->incRef( kId ); } + + Ptr( Cache *pCache, const keytype &kId ) : + pCache( pCache ), + pData( NULL ), + kId( kId ) + { + } public: Ptr( const Ptr &rSrc ) : @@ -44,7 +50,7 @@ namespace Bu pData( rSrc.pData ), kId( rSrc.kId ) { - if( pCache ) + if( pCache && pData ) pCache->incRef( kId ); } @@ -56,40 +62,69 @@ namespace Bu virtual ~Ptr() { - if( pCache ) + if( pCache && pData ) pCache->decRef( kId ); } obtype &operator*() { + checkPtr(); + return *pData; + } + + const obtype &operator*() const + { + checkPtr(); return *pData; } obtype *operator->() { + checkPtr(); + return pData; + } + + const obtype *operator->() const + { + checkPtr(); return pData; } - const keytype &getKey() + bool isLoaded() const + { + return pData != NULL; + } + + const keytype &getKey() const { return kId; } Ptr &operator=( const Ptr &rRhs ) { - if( pCache ) + if( pCache && pData ) pCache->decRef( kId ); pCache = rRhs.pCache; pData = rRhs.pData; kId = rRhs.kId; - if( pCache ) + if( pCache && pData ) pCache->incRef( kId ); } + private: + void checkPtr() const + { + if( pCache && !pData ) + { + pData = pCache->getRaw( kId ); + pCache->incRef( kId ); + } + } + private: Bu::Cache *pCache; - obtype *pData; - keytype kId; + mutable obtype *pData; + mutable keytype kId; }; private: @@ -165,6 +200,12 @@ namespace Bu } } + Ptr getLazy( const keytype &cId ) + { + TRACE( cId ); + return Ptr( this, cId ); + } + int getRefCount( const keytype &cId ) { TRACE( cId ); @@ -220,19 +261,33 @@ namespace Bu } private: - void incRef( keytype cId ) + void incRef( const keytype &cId ) { TRACE( cId ); hEnt.get( cId ).iRefs++; } - void decRef( keytype cId ) + void decRef( const keytype &cId ) { TRACE( cId ); CacheEntry &e = hEnt.get( cId ); e.iRefs--; } + obtype *getRaw( const keytype &cId ) + { + TRACE( cId ); + try { + return hEnt.get( cId ).pData; + } + catch( Bu::HashException &e ) { + CacheEntry e = {pStore->load( cId ), 0}; + pCalc->onLoad( e.pData, cId ); + hEnt.insert( cId, e ); + return e.pData; + } + } + private: CidHash hEnt; Calc *pCalc; diff --git a/src/cachestorenids.h b/src/cachestorenids.h index 0d8ff64..62d1555 100644 --- a/src/cachestorenids.h +++ b/src/cachestorenids.h @@ -9,6 +9,9 @@ namespace Bu { + template + keytype __cacheGetKey( const obtype *pObj ); + template class CacheStoreNids : public CacheStore { @@ -62,11 +65,9 @@ namespace Bu delete pObj; } - virtual keytype getKey( obtype *pSrc )=0; - virtual keytype create( obtype *pSrc ) { - keytype key = getKey( pSrc ); + keytype key = __cacheGetKey( pSrc ); int iStream = nStore.createStream(); hId.insert( key, iStream ); printf("Creating stream: %d\n", iStream ); diff --git a/src/tafreader.cpp b/src/tafreader.cpp index 4408cce..ceed9be 100644 --- a/src/tafreader.cpp +++ b/src/tafreader.cpp @@ -14,6 +14,7 @@ using namespace Bu; Bu::TafReader::TafReader( Bu::Stream &sIn ) : c( 0 ), + la( 0 ), sIn( sIn ), iLine( 1 ), iCol( -1 ) { diff --git a/src/tests/cache.cpp b/src/tests/cache.cpp index bacd7fc..323edda 100644 --- a/src/tests/cache.cpp +++ b/src/tests/cache.cpp @@ -168,7 +168,7 @@ int main( int argc, char *argv[] ) if( argc < 3 ) { - printf("Try: %s [crud] []\n\n", argv[0] ); + printf("Try: %s [crudl] []\n\n", argv[0] ); return 0; } @@ -203,6 +203,15 @@ int main( int argc, char *argv[] ) cBob.erase( strtol( argv[2], NULL, 0 ) ); } return 0; + + case 'l': + { + BobCache::Ptr pBob = cBob.getLazy( strtol( argv[2], NULL, 0 ) ); + printf("isLoaded: %s\n", pBob.isLoaded()?"yes":"no"); + printf("Value = %d\n", pBob->getInt() ); + printf("isLoaded: %s\n", pBob.isLoaded()?"yes":"no"); + } + return 0; } } -- cgit v1.2.3