aboutsummaryrefslogtreecommitdiff
path: root/src/cache.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/cache.h')
-rw-r--r--src/cache.h58
1 files changed, 50 insertions, 8 deletions
diff --git a/src/cache.h b/src/cache.h
index 0c3994c..9488044 100644
--- a/src/cache.h
+++ b/src/cache.h
@@ -38,6 +38,20 @@ namespace Bu
38 virtual ~Cache() 38 virtual ~Cache()
39 { 39 {
40 TRACE(); 40 TRACE();
41 for( typename CidHash::iterator i = hEnt.begin();
42 i != hEnt.end(); i++ )
43 {
44 if( i.getValue().iRefs > 0 )
45 {
46 printf("Error? iRefs=%d for key ", i.getValue().iRefs );
47 __tracer_format( i.getKey() );
48 printf("!\n");
49 }
50 lStore.first()->unload(
51 i.getValue().pData,
52 i.getKey()
53 );
54 }
41 for( typename StoreList::iterator i = lStore.begin(); 55 for( typename StoreList::iterator i = lStore.begin();
42 i != lStore.end(); i++ ) 56 i != lStore.end(); i++ )
43 { 57 {
@@ -47,44 +61,72 @@ namespace Bu
47 61
48 void appendStore( Store *pHand ) 62 void appendStore( Store *pHand )
49 { 63 {
64 TRACE();
50 lStore.append( pHand ); 65 lStore.append( pHand );
51 } 66 }
52 67
53 void prependStore( Store *pHand ) 68 void prependStore( Store *pHand )
54 { 69 {
70 TRACE();
55 lStore.prepend( pHand ); 71 lStore.prepend( pHand );
56 } 72 }
57 73
58 Ptr insert( obtype *pData ) 74 Ptr insert( obtype *pData )
59 { 75 {
60 TRACE(); 76 TRACE( pData );
61 CacheEntry e = {pData, 0}; 77 CacheEntry e = {pData, 0};
62 hEnt.insert( 0 , e ); 78 keytype k = lStore.first()->create( pData );
63 return Ptr( *this, pData ); 79 hEnt.insert( k, e );
80
81 return Ptr( *this, pData, k );
64 } 82 }
65 83
66 Ptr get( keytype cId ) 84 Ptr get( keytype cId )
67 { 85 {
68 TRACE(); 86 TRACE( cId );
69 return Ptr( *this, hEnt.get( cId ).pData ); 87 try {
88 return Ptr( *this, hEnt.get( cId ).pData, cId );
89 }
90 catch( Bu::HashException &e ) {
91 CacheEntry e = {lStore.first()->load( cId ), 0};
92 hEnt.insert( cId, e );
93 return Ptr( *this, e.pData, cId );
94 }
95 }
96
97 void erase( keytype cId )
98 {
99 TRACE( cId );
100 try {
101 if( hEnt.get( cId ).iRefs > 0 )
102 {
103 printf("Shouldn't delete, references still exist!\n");
104 return;
105 }
106 }
107 catch( Bu::HashException &e ) {
108 get( cId );
109 }
110 lStore.first()->destroy( hEnt.get( cId ).pData, cId );
111 hEnt.erase( cId );
70 } 112 }
71 113
72 int getRefCnt( keytype cId ) 114 int getRefCnt( keytype cId )
73 { 115 {
74 TRACE(); 116 TRACE( cId );
75 return hEnt.get( cId ).iRefs; 117 return hEnt.get( cId ).iRefs;
76 } 118 }
77 119
78 private: 120 private:
79 void incRef( keytype cId ) 121 void incRef( keytype cId )
80 { 122 {
81 TRACE(); 123 TRACE( cId );
82 hEnt.get( cId ).iRefs++; 124 hEnt.get( cId ).iRefs++;
83 } 125 }
84 126
85 void decRef( keytype cId ) 127 void decRef( keytype cId )
86 { 128 {
87 TRACE(); 129 TRACE( cId );
88 CacheEntry &e = hEnt.get( cId ); 130 CacheEntry &e = hEnt.get( cId );
89 e.iRefs--; 131 e.iRefs--;
90 } 132 }