From 4808617ef54d40efcf1a3ed30525898defb74e10 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 9 Oct 2008 20:20:34 +0000 Subject: More cache development. I'm going to have to switch from template functions to functors. I like template functions a little more, but functors can be at least as fast. It won't be much of a change. --- src/cachable.cpp | 15 +++++++++++++++ src/cachable.h | 19 +++++++++++++++++++ src/cache.h | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ src/cptr.h | 14 +++++++++++--- src/membuf.cpp | 2 +- src/tests/cache.cpp | 31 +++++++++++++++++++++++++++++++ 6 files changed, 126 insertions(+), 4 deletions(-) create mode 100644 src/cachable.cpp create mode 100644 src/cachable.h create mode 100644 src/tests/cache.cpp diff --git a/src/cachable.cpp b/src/cachable.cpp new file mode 100644 index 0000000..7fa2d23 --- /dev/null +++ b/src/cachable.cpp @@ -0,0 +1,15 @@ +#include "bu/cachable.h" + +Bu::Cachable::Cachable() +{ +} + +Bu::Cachable::~Cachable() +{ +} + +template<> long Bu::getCacheId( const Bu::Cachable *o ) +{ + return o->getCacheId(); +} + diff --git a/src/cachable.h b/src/cachable.h new file mode 100644 index 0000000..529da6f --- /dev/null +++ b/src/cachable.h @@ -0,0 +1,19 @@ +#ifndef BU_CACHABLE_H +#define BU_CACHABLE_H + +namespace Bu +{ + class Cachable + { + public: + Cachable(); + virtual ~Cachable(); + + virtual long getCacheId() const =0; + }; + + template long getCacheId( const obtype *o ); + template<> long getCacheId( const Cachable *o ); +}; + +#endif diff --git a/src/cache.h b/src/cache.h index 24ef652..344d1c6 100644 --- a/src/cache.h +++ b/src/cache.h @@ -1,20 +1,69 @@ #ifndef BU_CACHE_H #define BU_CACHE_H +#include "bu/cptr.h" +#include "bu/hash.h" + +#define BU_TRACE +#include "bu/trace.h" + namespace Bu { + template class Cache; + template long getCacheId( const obtype *o ); + template class Cache { + friend class Bu::CPtr; + typedef Bu::CPtr Ptr; + public: + typedef long cid; /**< Cache ID type. Unique cache entry ID. */ + public: Cache() { + TRACE(); } virtual ~Cache() { + TRACE(); + } + + Ptr insert( obtype *pData ) + { + TRACE(); + CacheEntry e = {pData, 0}; + hEnt.insert( getCacheId( pData ), e ); + return Ptr( *this, pData ); } + private: + void incRef( obtype *pData ) + { + TRACE(); + hEnt.get( getCacheId( pData ) ).iRefs++; + } + + void decRef( obtype *pData ) + { + TRACE(); + CacheEntry &e = hEnt.get( getCacheId( pData ) ); + e.iRefs--; + } + + private: + typedef struct CacheEntry + { + obtype *pData; + int iRefs; + } CacheEntry; + + //typedef Bu::Hash RefHash; + typedef Bu::Hash CidHash; + //RefHash hRefs; + CidHash hEnt; }; }; diff --git a/src/cptr.h b/src/cptr.h index 1f8c43d..138ace8 100644 --- a/src/cptr.h +++ b/src/cptr.h @@ -1,23 +1,31 @@ #ifndef BU_C_PTR_H #define BU_C_PTR_H -#include "bu/cache.h" - namespace Bu { + template class Cache; + template class CPtr { friend class Bu::Cache; private: - CPtr( Cache &rCache, obtype &rData ) + CPtr( Cache &rCache, obtype *pData ) : + rCache( rCache ), + pData( pData ) { + rCache.incRef( pData ); } public: virtual ~CPtr() { + rCache.decRef( pData ); } + + private: + Bu::Cache &rCache; + obtype *pData; }; }; diff --git a/src/membuf.cpp b/src/membuf.cpp index e7c7ac8..f22a8de 100644 --- a/src/membuf.cpp +++ b/src/membuf.cpp @@ -52,7 +52,7 @@ size_t Bu::MemBuf::write( const void *pBuf, size_t nBytes ) { // Trickier, we must do this in two parts, overwrite, then append // Frist, overwrite. - int iOver = sBuf.getSize() - nPos; + size_t iOver = sBuf.getSize() - nPos; if( iOver > nBytes ) iOver = nBytes; memcpy( sBuf.getStr()+nPos, pBuf, iOver ); diff --git a/src/tests/cache.cpp b/src/tests/cache.cpp new file mode 100644 index 0000000..e51c31b --- /dev/null +++ b/src/tests/cache.cpp @@ -0,0 +1,31 @@ +#include + +#include "bu/cache.h" +#include "bu/cachable.h" + +class Bob : public Bu::Cachable +{ +public: + Bob() + { + } + + virtual ~Bob() + { + } + + long getCacheId() const + { + return 0; + } + + int iInt; +}; + +int main() +{ + Bu::Cache bobCache; + +// Bu::CPtr pB = bobCache.insert( new Bob() ); +} + -- cgit v1.2.3