summaryrefslogtreecommitdiff
path: root/src/cache.h
blob: 313d9fa605980e3008977974c0ca395252d9938b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#ifndef BU_CACHE_H
#define BU_CACHE_H

#include "bu/cptr.h"
#include "bu/hash.h"
#include "bu/list.h"
#include "bu/cachestore.h"
#include "bu/cachecalc.h"

#include "bu/trace.h"

namespace Bu
{
	template<class obtype, class keytype>
	class Cache
	{
	friend class Bu::CPtr<obtype, keytype>;
	public:
		typedef Bu::CPtr<obtype, keytype> Ptr;
	private:
		typedef Bu::CacheStore<obtype, keytype> Store;
		typedef Bu::List<Store *> StoreList;
		typedef Bu::CacheCalc<obtype, keytype> Calc;
		
		typedef struct CacheEntry
		{
			obtype *pData;
			int iRefs;
		} CacheEntry;

		typedef Bu::Hash<keytype, CacheEntry> CidHash;

	public:
		Cache( Calc &rCalc ) :
			rCalc( rCalc )
		{
			TRACE();
			rCalc.setCache( this );
		}

		virtual ~Cache()
		{
			TRACE();
			for( typename CidHash::iterator i = hEnt.begin();
				 i != hEnt.end(); i++ )
			{
				if( i.getValue().iRefs > 0 )
				{
					printf("Error?  iRefs=%d for key ", i.getValue().iRefs );
					__tracer_format( i.getKey() );
					printf("!\n");
				}
				rCalc.onUnload(
					i.getValue().pData,
					i.getKey()
					);
				lStore.first()->unload(
					i.getValue().pData,
					i.getKey()
					);
			}
			for( typename StoreList::iterator i = lStore.begin();
				 i != lStore.end(); i++ )
			{
				delete *i;
			}
		}

		void appendStore( Store *pHand )
		{
			TRACE();
			lStore.append( pHand );
		}

		void prependStore( Store *pHand )
		{
			TRACE();
			lStore.prepend( pHand );
		}

		Ptr insert( obtype *pData )
		{
			TRACE( pData );
			CacheEntry e = {pData, 0};
			keytype k = lStore.first()->create( pData );
			hEnt.insert( k, e );

			rCalc.onLoad( pData, k );

			return Ptr( *this, pData, k );
		}

		Ptr get( const keytype &cId )
		{
			TRACE( cId );
			try {
				return Ptr( *this, hEnt.get( cId ).pData, cId );
			}
			catch( Bu::HashException &e ) {
				CacheEntry e = {lStore.first()->load( cId ), 0};
				rCalc.onLoad( e.pData, cId );
				hEnt.insert( cId, e );
				return Ptr( *this, e.pData, cId );
			}
		}

		int getRefCount( const keytype &cId )
		{
			TRACE( cId );
			return hEnt.get( cId ).iRefs;
		}

		void unload( const keytype &cId )
		{
			TRACE( cId );
			try {
				if( hEnt.get( cId ).iRefs > 0 )
				{
					printf("Shouldn't delete, references still exist!\n");
					return;
				}
			}
			catch( Bu::HashException &e ) {
				// It's not here?  Eh, return.
				return;
			}
			obtype *pObj = hEnt.get( cId ).pData;
			rCalc.onUnload( pObj, cId );
			hEnt.erase( cId );

			// The unload has to happen last just in case cId is a reference
			// to data that is about to be deleted from memory by the unload.
			lStore.first()->unload( pObj, cId );
		}

		void erase( const keytype &cId )
		{
			TRACE( cId );
			try {
				if( hEnt.get( cId ).iRefs > 0 )
				{
					printf("Shouldn't delete, references still exist!\n");
					return;
				}
			}
			catch( Bu::HashException &e ) {
				get( cId );
			}
			
			rCalc.onUnload( hEnt.get( cId ).pData, cId );
			
			lStore.first()->destroy( hEnt.get( cId ).pData, cId );
			hEnt.erase( cId );
		}

	private:
		void incRef( keytype cId )
		{
			TRACE( cId );
			hEnt.get( cId ).iRefs++;
		}

		void decRef( keytype cId )
		{
			TRACE( cId );
			CacheEntry &e = hEnt.get( cId );
			e.iRefs--;
		}

	private:
		CidHash hEnt;
		StoreList lStore;
		Calc &rCalc;
	};
};

#endif