diff options
| -rw-r--r-- | src/cache.h | 57 | ||||
| -rw-r--r-- | src/cachehandler.cpp | 2 | ||||
| -rw-r--r-- | src/cachehandler.h | 34 | ||||
| -rw-r--r-- | src/cachestore.cpp | 2 | ||||
| -rw-r--r-- | src/cachestore.h | 35 | ||||
| -rw-r--r-- | src/cachestorenids.cpp (renamed from src/cachehandlernids.cpp) | 0 | ||||
| -rw-r--r-- | src/cachestorenids.h (renamed from src/cachehandlernids.h) | 0 | ||||
| -rw-r--r-- | src/cptr.cpp | 1 | ||||
| -rw-r--r-- | src/cptr.h | 18 | ||||
| -rw-r--r-- | src/tests/cache.cpp | 66 |
10 files changed, 118 insertions, 97 deletions
diff --git a/src/cache.h b/src/cache.h index f7b71ce..0c3994c 100644 --- a/src/cache.h +++ b/src/cache.h | |||
| @@ -4,22 +4,30 @@ | |||
| 4 | #include "bu/cptr.h" | 4 | #include "bu/cptr.h" |
| 5 | #include "bu/hash.h" | 5 | #include "bu/hash.h" |
| 6 | #include "bu/list.h" | 6 | #include "bu/list.h" |
| 7 | #include "bu/cachehandler.h" | 7 | #include "bu/cachestore.h" |
| 8 | 8 | ||
| 9 | #define BU_TRACE | 9 | #define BU_TRACE |
| 10 | #include "bu/trace.h" | 10 | #include "bu/trace.h" |
| 11 | 11 | ||
| 12 | namespace Bu | 12 | namespace Bu |
| 13 | { | 13 | { |
| 14 | template<class obtype> | 14 | template<class obtype, class keytype> |
| 15 | class Cache | 15 | class Cache |
| 16 | { | 16 | { |
| 17 | friend class Bu::CPtr<obtype>; | 17 | friend class Bu::CPtr<obtype, keytype>; |
| 18 | typedef Bu::CPtr<obtype> Ptr; | ||
| 19 | typedef Bu::CacheHandler<obtype> Handler; | ||
| 20 | typedef Bu::List<Handler *> HandlerList; | ||
| 21 | public: | 18 | public: |
| 22 | typedef long cid_t; /**< Cache ID type. Unique cache entry ID. */ | 19 | typedef Bu::CPtr<obtype, keytype> Ptr; |
| 20 | private: | ||
| 21 | typedef Bu::CacheStore<obtype, keytype> Store; | ||
| 22 | typedef Bu::List<Store *> StoreList; | ||
| 23 | |||
| 24 | typedef struct CacheEntry | ||
| 25 | { | ||
| 26 | obtype *pData; | ||
| 27 | int iRefs; | ||
| 28 | } CacheEntry; | ||
| 29 | |||
| 30 | typedef Bu::Hash<keytype, CacheEntry> CidHash; | ||
| 23 | 31 | ||
| 24 | public: | 32 | public: |
| 25 | Cache() | 33 | Cache() |
| @@ -30,51 +38,51 @@ namespace Bu | |||
| 30 | virtual ~Cache() | 38 | virtual ~Cache() |
| 31 | { | 39 | { |
| 32 | TRACE(); | 40 | TRACE(); |
| 33 | /* for( HandlerList::iterator i = lHandler.begin(); | 41 | for( typename StoreList::iterator i = lStore.begin(); |
| 34 | i != lHandler.end(); i++ ) | 42 | i != lStore.end(); i++ ) |
| 35 | { | 43 | { |
| 36 | delete *i; | 44 | delete *i; |
| 37 | } | 45 | } |
| 38 | */ } | 46 | } |
| 39 | 47 | ||
| 40 | void appendHandler( Handler *pHand ) | 48 | void appendStore( Store *pHand ) |
| 41 | { | 49 | { |
| 42 | lHandler.append( pHand ); | 50 | lStore.append( pHand ); |
| 43 | } | 51 | } |
| 44 | 52 | ||
| 45 | void prependHandler( Handler *pHand ) | 53 | void prependStore( Store *pHand ) |
| 46 | { | 54 | { |
| 47 | lHandler.prepend( pHand ); | 55 | lStore.prepend( pHand ); |
| 48 | } | 56 | } |
| 49 | 57 | ||
| 50 | Ptr insert( obtype *pData ) | 58 | Ptr insert( obtype *pData ) |
| 51 | { | 59 | { |
| 52 | TRACE(); | 60 | TRACE(); |
| 53 | CacheEntry e = {pData, 0}; | 61 | CacheEntry e = {pData, 0}; |
| 54 | hEnt.insert( /*pData*/ 0 , e ); | 62 | hEnt.insert( 0 , e ); |
| 55 | return Ptr( *this, pData ); | 63 | return Ptr( *this, pData ); |
| 56 | } | 64 | } |
| 57 | 65 | ||
| 58 | Ptr get( cid_t cId ) | 66 | Ptr get( keytype cId ) |
| 59 | { | 67 | { |
| 60 | TRACE(); | 68 | TRACE(); |
| 61 | return Ptr( *this, hEnt.get( cId ).pData ); | 69 | return Ptr( *this, hEnt.get( cId ).pData ); |
| 62 | } | 70 | } |
| 63 | 71 | ||
| 64 | int getRefCnt( cid_t cId ) | 72 | int getRefCnt( keytype cId ) |
| 65 | { | 73 | { |
| 66 | TRACE(); | 74 | TRACE(); |
| 67 | return hEnt.get( cId ).iRefs; | 75 | return hEnt.get( cId ).iRefs; |
| 68 | } | 76 | } |
| 69 | 77 | ||
| 70 | private: | 78 | private: |
| 71 | void incRef( cid_t cId ) | 79 | void incRef( keytype cId ) |
| 72 | { | 80 | { |
| 73 | TRACE(); | 81 | TRACE(); |
| 74 | hEnt.get( cId ).iRefs++; | 82 | hEnt.get( cId ).iRefs++; |
| 75 | } | 83 | } |
| 76 | 84 | ||
| 77 | void decRef( cid_t cId ) | 85 | void decRef( keytype cId ) |
| 78 | { | 86 | { |
| 79 | TRACE(); | 87 | TRACE(); |
| 80 | CacheEntry &e = hEnt.get( cId ); | 88 | CacheEntry &e = hEnt.get( cId ); |
| @@ -82,17 +90,8 @@ namespace Bu | |||
| 82 | } | 90 | } |
| 83 | 91 | ||
| 84 | private: | 92 | private: |
| 85 | typedef struct CacheEntry | ||
| 86 | { | ||
| 87 | obtype *pData; | ||
| 88 | int iRefs; | ||
| 89 | } CacheEntry; | ||
| 90 | |||
| 91 | //typedef Bu::Hash<ptrdiff_t, int> RefHash; | ||
| 92 | typedef Bu::Hash<cid_t, CacheEntry> CidHash; | ||
| 93 | //RefHash hRefs; | ||
| 94 | CidHash hEnt; | 93 | CidHash hEnt; |
| 95 | HandlerList lHandler; | 94 | StoreList lStore; |
| 96 | }; | 95 | }; |
| 97 | }; | 96 | }; |
| 98 | 97 | ||
diff --git a/src/cachehandler.cpp b/src/cachehandler.cpp deleted file mode 100644 index d208e98..0000000 --- a/src/cachehandler.cpp +++ /dev/null | |||
| @@ -1,2 +0,0 @@ | |||
| 1 | #include "bu/cachehandler.h" | ||
| 2 | |||
diff --git a/src/cachehandler.h b/src/cachehandler.h deleted file mode 100644 index 3b26970..0000000 --- a/src/cachehandler.h +++ /dev/null | |||
| @@ -1,34 +0,0 @@ | |||
| 1 | #ifndef BU_CACHE_HANDLER_H | ||
| 2 | #define BU_CACHE_HANDLER_H | ||
| 3 | |||
| 4 | #include "bu/cptr.h" | ||
| 5 | |||
| 6 | namespace Bu | ||
| 7 | { | ||
| 8 | /** | ||
| 9 | * Handles I/O for data in the cache. This also assigns ID's to the newly | ||
| 10 | * created objects that are requested through this system. | ||
| 11 | */ | ||
| 12 | template<class T> | ||
| 13 | class CacheHandler | ||
| 14 | { | ||
| 15 | public: | ||
| 16 | CacheHandler() | ||
| 17 | { | ||
| 18 | } | ||
| 19 | |||
| 20 | virtual ~CacheHandler() | ||
| 21 | { | ||
| 22 | } | ||
| 23 | |||
| 24 | virtual Bu::CPtr<T> load()=0; | ||
| 25 | virtual void unload( Bu::CPtr<T> pObj )=0; | ||
| 26 | virtual Bu::CPtr<T> create()=0; | ||
| 27 | virtual Bu::CPtr<T> create( T &rSrc )=0; | ||
| 28 | virtual void destroy( Bu::CPtr<T> pObj )=0; | ||
| 29 | |||
| 30 | private: | ||
| 31 | }; | ||
| 32 | }; | ||
| 33 | |||
| 34 | #endif | ||
diff --git a/src/cachestore.cpp b/src/cachestore.cpp new file mode 100644 index 0000000..7f26f17 --- /dev/null +++ b/src/cachestore.cpp | |||
| @@ -0,0 +1,2 @@ | |||
| 1 | #include "bu/cachestore.h" | ||
| 2 | |||
diff --git a/src/cachestore.h b/src/cachestore.h new file mode 100644 index 0000000..3211b6a --- /dev/null +++ b/src/cachestore.h | |||
| @@ -0,0 +1,35 @@ | |||
| 1 | #ifndef BU_CACHE_STORE_H | ||
| 2 | #define BU_CACHE_STORE_H | ||
| 3 | |||
| 4 | #include "bu/cptr.h" | ||
| 5 | |||
| 6 | namespace Bu | ||
| 7 | { | ||
| 8 | /** | ||
| 9 | * Handles I/O for data in the cache. This also assigns ID's to the newly | ||
| 10 | * created objects that are requested through this system. | ||
| 11 | */ | ||
| 12 | template<class obtype, class keytype> | ||
| 13 | class CacheStore | ||
| 14 | { | ||
| 15 | public: | ||
| 16 | CacheStore() | ||
| 17 | { | ||
| 18 | } | ||
| 19 | |||
| 20 | virtual ~CacheStore() | ||
| 21 | { | ||
| 22 | } | ||
| 23 | |||
| 24 | typedef Bu::CPtr<obtype, keytype> Ptr; | ||
| 25 | |||
| 26 | virtual obtype *load( const keytype &key )=0; | ||
| 27 | virtual void unload( obtype *pObj )=0; | ||
| 28 | virtual keytype create( obtype *pSrc )=0; | ||
| 29 | virtual void destroy( obtype *pObj, const keytype &key )=0; | ||
| 30 | |||
| 31 | private: | ||
| 32 | }; | ||
| 33 | }; | ||
| 34 | |||
| 35 | #endif | ||
diff --git a/src/cachehandlernids.cpp b/src/cachestorenids.cpp index e69de29..e69de29 100644 --- a/src/cachehandlernids.cpp +++ b/src/cachestorenids.cpp | |||
diff --git a/src/cachehandlernids.h b/src/cachestorenids.h index e69de29..e69de29 100644 --- a/src/cachehandlernids.h +++ b/src/cachestorenids.h | |||
diff --git a/src/cptr.cpp b/src/cptr.cpp index e69de29..65d668b 100644 --- a/src/cptr.cpp +++ b/src/cptr.cpp | |||
| @@ -0,0 +1 @@ | |||
| #include "bu/cptr.h" | |||
| @@ -3,31 +3,29 @@ | |||
| 3 | 3 | ||
| 4 | namespace Bu | 4 | namespace Bu |
| 5 | { | 5 | { |
| 6 | template<class obtype> class Cache; | 6 | template<class obtype, class keytype> class Cache; |
| 7 | 7 | ||
| 8 | /** | 8 | /** |
| 9 | * Cache Pointer - Provides access to data that is held within the cache. | 9 | * Cache Pointer - Provides access to data that is held within the cache. |
| 10 | * This provides safe, refcounting access to data stored in the cache, with | 10 | * This provides safe, refcounting access to data stored in the cache, with |
| 11 | * support for lazy loading. | 11 | * support for lazy loading. |
| 12 | */ | 12 | */ |
| 13 | template<class obtype> | 13 | template<class obtype, class keytype> |
| 14 | class CPtr | 14 | class CPtr |
| 15 | { | 15 | { |
| 16 | friend class Bu::Cache<obtype>; | 16 | friend class Bu::Cache<obtype, keytype>; |
| 17 | public: | ||
| 18 | typedef long cid_t; /**< Cache ID type. Unique cache entry ID. */ | ||
| 19 | private: | 17 | private: |
| 20 | CPtr( Cache<obtype> &rCache, obtype *pData ) : | 18 | CPtr( Cache<obtype, keytype> &rCache, obtype *pData ) : |
| 21 | rCache( rCache ), | 19 | rCache( rCache ), |
| 22 | pData( pData ) | 20 | pData( pData ) |
| 23 | { | 21 | { |
| 24 | rCache.incRef( cId ); | 22 | rCache.incRef( kId ); |
| 25 | } | 23 | } |
| 26 | 24 | ||
| 27 | public: | 25 | public: |
| 28 | virtual ~CPtr() | 26 | virtual ~CPtr() |
| 29 | { | 27 | { |
| 30 | rCache.decRef( cId ); | 28 | rCache.decRef( kId ); |
| 31 | } | 29 | } |
| 32 | 30 | ||
| 33 | obtype &operator*() | 31 | obtype &operator*() |
| @@ -41,9 +39,9 @@ namespace Bu | |||
| 41 | } | 39 | } |
| 42 | 40 | ||
| 43 | private: | 41 | private: |
| 44 | Bu::Cache<obtype> &rCache; | 42 | Bu::Cache<obtype, keytype> &rCache; |
| 45 | obtype *pData; | 43 | obtype *pData; |
| 46 | cid_t cId; | 44 | keytype kId; |
| 47 | }; | 45 | }; |
| 48 | }; | 46 | }; |
| 49 | 47 | ||
diff --git a/src/tests/cache.cpp b/src/tests/cache.cpp index 55ec5be..1cc008a 100644 --- a/src/tests/cache.cpp +++ b/src/tests/cache.cpp | |||
| @@ -1,4 +1,7 @@ | |||
| 1 | #include <stdio.h> | 1 | #include <stdio.h> |
| 2 | #include <sys/stat.h> | ||
| 3 | #include <sys/types.h> | ||
| 4 | #include <errno.h> | ||
| 2 | 5 | ||
| 3 | #include "bu/cache.h" | 6 | #include "bu/cache.h" |
| 4 | 7 | ||
| @@ -35,55 +38,74 @@ public: | |||
| 35 | int iInt; | 38 | int iInt; |
| 36 | }; | 39 | }; |
| 37 | 40 | ||
| 38 | class BobHandler : public Bu::CacheHandler<Bob> | 41 | class BobStore : public Bu::CacheStore<Bob, long> |
| 39 | { | 42 | { |
| 40 | public: | 43 | public: |
| 41 | BobHandler() : | 44 | BobStore() : |
| 42 | cLastId( 0 ) | 45 | cLastId( 0 ) |
| 43 | { | 46 | { |
| 47 | TRACE(); | ||
| 44 | } | 48 | } |
| 45 | 49 | ||
| 46 | ~BobHandler() | 50 | ~BobStore() |
| 47 | { | ||
| 48 | } | ||
| 49 | |||
| 50 | virtual Bu::CPtr<Bob> load() | ||
| 51 | { | 51 | { |
| 52 | TRACE(); | ||
| 52 | } | 53 | } |
| 53 | 54 | ||
| 54 | virtual void unload( Bu::CPtr<Bob> pObj ) | 55 | virtual Bob *load( const long &key ) |
| 55 | { | 56 | { |
| 57 | TRACE(); | ||
| 58 | return NULL; | ||
| 56 | } | 59 | } |
| 57 | 60 | ||
| 58 | virtual Bu::CPtr<Bob> create() | 61 | virtual void unload( Bob *pObj ) |
| 59 | { | 62 | { |
| 63 | TRACE(); | ||
| 64 | delete pObj; | ||
| 60 | } | 65 | } |
| 61 | 66 | ||
| 62 | virtual Bu::CPtr<Bob> create( Bob &rSrc ) | 67 | virtual long create( Bob *rSrc ) |
| 63 | { | 68 | { |
| 69 | TRACE(); | ||
| 70 | return ++cLastId; | ||
| 64 | } | 71 | } |
| 65 | 72 | ||
| 66 | virtual void destroy( Bu::CPtr<Bob> pObj ) | 73 | virtual void destroy( Bob *pObj, const long &key ) |
| 67 | { | 74 | { |
| 75 | TRACE(); | ||
| 76 | delete pObj; | ||
| 68 | } | 77 | } |
| 69 | 78 | ||
| 70 | private: | 79 | private: |
| 71 | Bu::Cache<Bob>::cid_t cLastId; | 80 | long cLastId; |
| 72 | }; | 81 | }; |
| 73 | 82 | ||
| 74 | int main() | 83 | int main( int argc, char *argv[] ) |
| 75 | { | 84 | { |
| 76 | typedef Bu::Cache<Bob> BobCache; | 85 | TRACE(); |
| 77 | typedef Bu::CPtr<Bob> BobPtr; | 86 | if( argc < 3 ) |
| 87 | { | ||
| 88 | printf("Try: %s [icufd] [<id/value>]\n\n", argv[0] ); | ||
| 89 | return 0; | ||
| 90 | } | ||
| 91 | |||
| 92 | switch( argv[1][0] ) | ||
| 93 | { | ||
| 94 | case 'i': | ||
| 95 | mkdir("bobcache", 0755 ); | ||
| 96 | printf("Initialized cache: %s\n", strerror( errno ) ); | ||
| 97 | return 0; | ||
| 98 | |||
| 99 | case 'c': | ||
| 100 | typedef Bu::Cache<Bob, long> BobCache; | ||
| 101 | typedef BobCache::Ptr BobPtr; | ||
| 78 | 102 | ||
| 79 | BobCache bobCache; | 103 | BobCache cBob; |
| 80 | 104 | ||
| 81 | BobPtr pB1 = bobCache.insert( new Bob() ); | 105 | cBob.appendStore( new BobStore() ); |
| 82 | (*pB1).setInt( 44 ); | 106 | |
| 83 | printf("RefCnt = %d\n", bobCache.getRefCnt( 0 ) ); | 107 | return 0; |
| 108 | } | ||
| 84 | 109 | ||
| 85 | BobPtr pB2 = bobCache.get( 0 ); | ||
| 86 | printf("RefCnt = %d\n", bobCache.getRefCnt( 0 ) ); | ||
| 87 | printf("Int = %d\n", pB2->getInt() ); | ||
| 88 | } | 110 | } |
| 89 | 111 | ||
