aboutsummaryrefslogtreecommitdiff
path: root/src/cache.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/cache.h')
-rw-r--r--src/cache.h49
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
4namespace Bu 10namespace 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