aboutsummaryrefslogtreecommitdiff
path: root/src/cache.h
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/cache.h34
1 files changed, 31 insertions, 3 deletions
diff --git a/src/cache.h b/src/cache.h
index 9488044..cc13fc8 100644
--- a/src/cache.h
+++ b/src/cache.h
@@ -5,6 +5,7 @@
5#include "bu/hash.h" 5#include "bu/hash.h"
6#include "bu/list.h" 6#include "bu/list.h"
7#include "bu/cachestore.h" 7#include "bu/cachestore.h"
8#include "bu/cachecalc.h"
8 9
9#define BU_TRACE 10#define BU_TRACE
10#include "bu/trace.h" 11#include "bu/trace.h"
@@ -20,6 +21,7 @@ namespace Bu
20 private: 21 private:
21 typedef Bu::CacheStore<obtype, keytype> Store; 22 typedef Bu::CacheStore<obtype, keytype> Store;
22 typedef Bu::List<Store *> StoreList; 23 typedef Bu::List<Store *> StoreList;
24 typedef Bu::CacheCalc<obtype, keytype> Calc;
23 25
24 typedef struct CacheEntry 26 typedef struct CacheEntry
25 { 27 {
@@ -30,7 +32,8 @@ namespace Bu
30 typedef Bu::Hash<keytype, CacheEntry> CidHash; 32 typedef Bu::Hash<keytype, CacheEntry> CidHash;
31 33
32 public: 34 public:
33 Cache() 35 Cache() :
36 pCalc( NULL )
34 { 37 {
35 TRACE(); 38 TRACE();
36 } 39 }
@@ -47,6 +50,10 @@ namespace Bu
47 __tracer_format( i.getKey() ); 50 __tracer_format( i.getKey() );
48 printf("!\n"); 51 printf("!\n");
49 } 52 }
53 if( pCalc ) pCalc->onUnload(
54 i.getValue().pData,
55 i.getKey()
56 );
50 lStore.first()->unload( 57 lStore.first()->unload(
51 i.getValue().pData, 58 i.getValue().pData,
52 i.getKey() 59 i.getKey()
@@ -71,6 +78,16 @@ namespace Bu
71 lStore.prepend( pHand ); 78 lStore.prepend( pHand );
72 } 79 }
73 80
81 void setCalc( Calc *pCalc )
82 {
83 TRACE();
84 if( this->pCalc )
85 {
86 delete this->pCalc;
87 }
88 this->pCalc = pCalc;
89 }
90
74 Ptr insert( obtype *pData ) 91 Ptr insert( obtype *pData )
75 { 92 {
76 TRACE( pData ); 93 TRACE( pData );
@@ -78,10 +95,12 @@ namespace Bu
78 keytype k = lStore.first()->create( pData ); 95 keytype k = lStore.first()->create( pData );
79 hEnt.insert( k, e ); 96 hEnt.insert( k, e );
80 97
98 if( pCalc ) pCalc->onLoad( pData, k );
99
81 return Ptr( *this, pData, k ); 100 return Ptr( *this, pData, k );
82 } 101 }
83 102
84 Ptr get( keytype cId ) 103 Ptr get( const keytype &cId )
85 { 104 {
86 TRACE( cId ); 105 TRACE( cId );
87 try { 106 try {
@@ -94,7 +113,12 @@ namespace Bu
94 } 113 }
95 } 114 }
96 115
97 void erase( keytype cId ) 116 int getRefCount( const keytype &cId )
117 {
118 return hEnt.get( cId ).iRefs;
119 }
120
121 void erase( const keytype &cId )
98 { 122 {
99 TRACE( cId ); 123 TRACE( cId );
100 try { 124 try {
@@ -107,6 +131,9 @@ namespace Bu
107 catch( Bu::HashException &e ) { 131 catch( Bu::HashException &e ) {
108 get( cId ); 132 get( cId );
109 } 133 }
134
135 if( pCalc ) pCalc->onUnload( hEnt.get( cId ).pData, cId );
136
110 lStore.first()->destroy( hEnt.get( cId ).pData, cId ); 137 lStore.first()->destroy( hEnt.get( cId ).pData, cId );
111 hEnt.erase( cId ); 138 hEnt.erase( cId );
112 } 139 }
@@ -134,6 +161,7 @@ namespace Bu
134 private: 161 private:
135 CidHash hEnt; 162 CidHash hEnt;
136 StoreList lStore; 163 StoreList lStore;
164 Calc *pCalc;
137 }; 165 };
138}; 166};
139 167