diff options
author | Mike Buland <eichlan@xagasoft.com> | 2008-12-20 02:01:44 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2008-12-20 02:01:44 +0000 |
commit | b7a687b08e32adafbb609bec7aa79b54f161ee4c (patch) | |
tree | 9c0214eb7fa5c69619bb4226cb292d19445b92df /src/cache.h | |
parent | 5db154c3fb36a677c551e39c3c6661207eb51778 (diff) | |
download | libbu++-b7a687b08e32adafbb609bec7aa79b54f161ee4c.tar.gz libbu++-b7a687b08e32adafbb609bec7aa79b54f161ee4c.tar.bz2 libbu++-b7a687b08e32adafbb609bec7aa79b54f161ee4c.tar.xz libbu++-b7a687b08e32adafbb609bec7aa79b54f161ee4c.zip |
All of the basic, core workings of the Cache are complete, and tested. Even
added some more tests and whatnot. A lot happened, but I can't remember
everything.
Also, Bu::File reports errors in more error cases.
Diffstat (limited to 'src/cache.h')
-rw-r--r-- | src/cache.h | 55 |
1 files changed, 32 insertions, 23 deletions
diff --git a/src/cache.h b/src/cache.h index cc13fc8..313d9fa 100644 --- a/src/cache.h +++ b/src/cache.h | |||
@@ -7,7 +7,6 @@ | |||
7 | #include "bu/cachestore.h" | 7 | #include "bu/cachestore.h" |
8 | #include "bu/cachecalc.h" | 8 | #include "bu/cachecalc.h" |
9 | 9 | ||
10 | #define BU_TRACE | ||
11 | #include "bu/trace.h" | 10 | #include "bu/trace.h" |
12 | 11 | ||
13 | namespace Bu | 12 | namespace Bu |
@@ -32,10 +31,11 @@ namespace Bu | |||
32 | typedef Bu::Hash<keytype, CacheEntry> CidHash; | 31 | typedef Bu::Hash<keytype, CacheEntry> CidHash; |
33 | 32 | ||
34 | public: | 33 | public: |
35 | Cache() : | 34 | Cache( Calc &rCalc ) : |
36 | pCalc( NULL ) | 35 | rCalc( rCalc ) |
37 | { | 36 | { |
38 | TRACE(); | 37 | TRACE(); |
38 | rCalc.setCache( this ); | ||
39 | } | 39 | } |
40 | 40 | ||
41 | virtual ~Cache() | 41 | virtual ~Cache() |
@@ -50,7 +50,7 @@ namespace Bu | |||
50 | __tracer_format( i.getKey() ); | 50 | __tracer_format( i.getKey() ); |
51 | printf("!\n"); | 51 | printf("!\n"); |
52 | } | 52 | } |
53 | if( pCalc ) pCalc->onUnload( | 53 | rCalc.onUnload( |
54 | i.getValue().pData, | 54 | i.getValue().pData, |
55 | i.getKey() | 55 | i.getKey() |
56 | ); | 56 | ); |
@@ -78,16 +78,6 @@ namespace Bu | |||
78 | lStore.prepend( pHand ); | 78 | lStore.prepend( pHand ); |
79 | } | 79 | } |
80 | 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 | |||
91 | Ptr insert( obtype *pData ) | 81 | Ptr insert( obtype *pData ) |
92 | { | 82 | { |
93 | TRACE( pData ); | 83 | TRACE( pData ); |
@@ -95,7 +85,7 @@ namespace Bu | |||
95 | keytype k = lStore.first()->create( pData ); | 85 | keytype k = lStore.first()->create( pData ); |
96 | hEnt.insert( k, e ); | 86 | hEnt.insert( k, e ); |
97 | 87 | ||
98 | if( pCalc ) pCalc->onLoad( pData, k ); | 88 | rCalc.onLoad( pData, k ); |
99 | 89 | ||
100 | return Ptr( *this, pData, k ); | 90 | return Ptr( *this, pData, k ); |
101 | } | 91 | } |
@@ -108,6 +98,7 @@ namespace Bu | |||
108 | } | 98 | } |
109 | catch( Bu::HashException &e ) { | 99 | catch( Bu::HashException &e ) { |
110 | CacheEntry e = {lStore.first()->load( cId ), 0}; | 100 | CacheEntry e = {lStore.first()->load( cId ), 0}; |
101 | rCalc.onLoad( e.pData, cId ); | ||
111 | hEnt.insert( cId, e ); | 102 | hEnt.insert( cId, e ); |
112 | return Ptr( *this, e.pData, cId ); | 103 | return Ptr( *this, e.pData, cId ); |
113 | } | 104 | } |
@@ -115,9 +106,33 @@ namespace Bu | |||
115 | 106 | ||
116 | int getRefCount( const keytype &cId ) | 107 | int getRefCount( const keytype &cId ) |
117 | { | 108 | { |
109 | TRACE( cId ); | ||
118 | return hEnt.get( cId ).iRefs; | 110 | return hEnt.get( cId ).iRefs; |
119 | } | 111 | } |
120 | 112 | ||
113 | void unload( const keytype &cId ) | ||
114 | { | ||
115 | TRACE( cId ); | ||
116 | try { | ||
117 | if( hEnt.get( cId ).iRefs > 0 ) | ||
118 | { | ||
119 | printf("Shouldn't delete, references still exist!\n"); | ||
120 | return; | ||
121 | } | ||
122 | } | ||
123 | catch( Bu::HashException &e ) { | ||
124 | // It's not here? Eh, return. | ||
125 | return; | ||
126 | } | ||
127 | obtype *pObj = hEnt.get( cId ).pData; | ||
128 | rCalc.onUnload( pObj, cId ); | ||
129 | hEnt.erase( cId ); | ||
130 | |||
131 | // The unload has to happen last just in case cId is a reference | ||
132 | // to data that is about to be deleted from memory by the unload. | ||
133 | lStore.first()->unload( pObj, cId ); | ||
134 | } | ||
135 | |||
121 | void erase( const keytype &cId ) | 136 | void erase( const keytype &cId ) |
122 | { | 137 | { |
123 | TRACE( cId ); | 138 | TRACE( cId ); |
@@ -132,18 +147,12 @@ namespace Bu | |||
132 | get( cId ); | 147 | get( cId ); |
133 | } | 148 | } |
134 | 149 | ||
135 | if( pCalc ) pCalc->onUnload( hEnt.get( cId ).pData, cId ); | 150 | rCalc.onUnload( hEnt.get( cId ).pData, cId ); |
136 | 151 | ||
137 | lStore.first()->destroy( hEnt.get( cId ).pData, cId ); | 152 | lStore.first()->destroy( hEnt.get( cId ).pData, cId ); |
138 | hEnt.erase( cId ); | 153 | hEnt.erase( cId ); |
139 | } | 154 | } |
140 | 155 | ||
141 | int getRefCnt( keytype cId ) | ||
142 | { | ||
143 | TRACE( cId ); | ||
144 | return hEnt.get( cId ).iRefs; | ||
145 | } | ||
146 | |||
147 | private: | 156 | private: |
148 | void incRef( keytype cId ) | 157 | void incRef( keytype cId ) |
149 | { | 158 | { |
@@ -161,7 +170,7 @@ namespace Bu | |||
161 | private: | 170 | private: |
162 | CidHash hEnt; | 171 | CidHash hEnt; |
163 | StoreList lStore; | 172 | StoreList lStore; |
164 | Calc *pCalc; | 173 | Calc &rCalc; |
165 | }; | 174 | }; |
166 | }; | 175 | }; |
167 | 176 | ||