diff options
author | Mike Buland <eichlan@xagasoft.com> | 2008-10-09 20:20:34 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2008-10-09 20:20:34 +0000 |
commit | 4808617ef54d40efcf1a3ed30525898defb74e10 (patch) | |
tree | e16763eb646fdf3dea7c32a8d57c147356be9299 /src/cache.h | |
parent | fecbea5970bb89c15b35f5df5b09914b4c91efe0 (diff) | |
download | libbu++-4808617ef54d40efcf1a3ed30525898defb74e10.tar.gz libbu++-4808617ef54d40efcf1a3ed30525898defb74e10.tar.bz2 libbu++-4808617ef54d40efcf1a3ed30525898defb74e10.tar.xz libbu++-4808617ef54d40efcf1a3ed30525898defb74e10.zip |
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.
Diffstat (limited to 'src/cache.h')
-rw-r--r-- | src/cache.h | 49 |
1 files changed, 49 insertions, 0 deletions
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 @@ | |||
1 | #ifndef BU_CACHE_H | 1 | #ifndef BU_CACHE_H |
2 | #define BU_CACHE_H | 2 | #define BU_CACHE_H |
3 | 3 | ||
4 | #include "bu/cptr.h" | ||
5 | #include "bu/hash.h" | ||
6 | |||
7 | #define BU_TRACE | ||
8 | #include "bu/trace.h" | ||
9 | |||
4 | namespace Bu | 10 | namespace Bu |
5 | { | 11 | { |
12 | template<class obtype> class Cache; | ||
13 | template<class obtype> long getCacheId( const obtype *o ); | ||
14 | |||
6 | template<class obtype> | 15 | template<class obtype> |
7 | class Cache | 16 | class Cache |
8 | { | 17 | { |
18 | friend class Bu::CPtr<obtype>; | ||
19 | typedef Bu::CPtr<obtype> Ptr; | ||
20 | public: | ||
21 | typedef long cid; /**< Cache ID type. Unique cache entry ID. */ | ||
22 | |||
9 | public: | 23 | public: |
10 | Cache() | 24 | Cache() |
11 | { | 25 | { |
26 | TRACE(); | ||
12 | } | 27 | } |
13 | 28 | ||
14 | virtual ~Cache() | 29 | virtual ~Cache() |
15 | { | 30 | { |
31 | TRACE(); | ||
32 | } | ||
33 | |||
34 | Ptr insert( obtype *pData ) | ||
35 | { | ||
36 | TRACE(); | ||
37 | CacheEntry e = {pData, 0}; | ||
38 | hEnt.insert( getCacheId( pData ), e ); | ||
39 | return Ptr( *this, pData ); | ||
16 | } | 40 | } |
17 | 41 | ||
42 | private: | ||
43 | void incRef( obtype *pData ) | ||
44 | { | ||
45 | TRACE(); | ||
46 | hEnt.get( getCacheId( pData ) ).iRefs++; | ||
47 | } | ||
48 | |||
49 | void decRef( obtype *pData ) | ||
50 | { | ||
51 | TRACE(); | ||
52 | CacheEntry &e = hEnt.get( getCacheId( pData ) ); | ||
53 | e.iRefs--; | ||
54 | } | ||
55 | |||
56 | private: | ||
57 | typedef struct CacheEntry | ||
58 | { | ||
59 | obtype *pData; | ||
60 | int iRefs; | ||
61 | } CacheEntry; | ||
62 | |||
63 | //typedef Bu::Hash<ptrdiff_t, int> RefHash; | ||
64 | typedef Bu::Hash<cid, CacheEntry> CidHash; | ||
65 | //RefHash hRefs; | ||
66 | CidHash hEnt; | ||
18 | }; | 67 | }; |
19 | }; | 68 | }; |
20 | 69 | ||