aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2009-01-27 00:31:24 +0000
committerMike Buland <eichlan@xagasoft.com>2009-01-27 00:31:24 +0000
commit8bc5ac336d5d684341a05e97d1cb1b18ecba0331 (patch)
treef4564108f73b3bb9be177633eca4053810c225e9
parentdfd5f8696787d18ae688b662040289f84b667fdd (diff)
downloadlibbu++-8bc5ac336d5d684341a05e97d1cb1b18ecba0331.tar.gz
libbu++-8bc5ac336d5d684341a05e97d1cb1b18ecba0331.tar.bz2
libbu++-8bc5ac336d5d684341a05e97d1cb1b18ecba0331.tar.xz
libbu++-8bc5ac336d5d684341a05e97d1cb1b18ecba0331.zip
Hey, I think that'll fix some things valgrind was bitching about. Apparently
BitString is...not so good...I may have to rewrite big chunks.
Diffstat (limited to '')
-rw-r--r--src/bitstring.cpp10
-rw-r--r--src/cache.h81
-rw-r--r--src/cachestorenids.h7
-rw-r--r--src/tafreader.cpp1
-rw-r--r--src/tests/cache.cpp11
5 files changed, 90 insertions, 20 deletions
diff --git a/src/bitstring.cpp b/src/bitstring.cpp
index 85f89e2..b3bcde5 100644
--- a/src/bitstring.cpp
+++ b/src/bitstring.cpp
@@ -1,8 +1,10 @@
1#include "bitstring.h" 1#include "bu/bitstring.h"
2#include <stdlib.h> 2#include <stdlib.h>
3#include <stdio.h> 3#include <stdio.h>
4#include <string.h> 4#include <string.h>
5 5
6#include "bu/exceptionbase.h"
7
6#ifdef _WIN32 8#ifdef _WIN32
7#define random() rand() 9#define random() rand()
8#endif 10#endif
@@ -187,6 +189,8 @@ void Bu::BitString::fixup()
187 189
188void Bu::BitString::setBit( long iBit, bool bBitState ) 190void Bu::BitString::setBit( long iBit, bool bBitState )
189{ 191{
192 if( iBit < 0 || iBit >= iBits )
193 throw Bu::ExceptionBase("bit out of range: %d in (0-%d)", iBit, iBits );
190 if( bBitState ) 194 if( bBitState )
191 { 195 {
192 caData[iBit/8] |= (1<<(iBit%8)); 196 caData[iBit/8] |= (1<<(iBit%8));
@@ -342,7 +346,7 @@ bool Bu::BitString::setSize( long iLength, bool bClear )
342 { 346 {
343 // Ok, reallocate and copy... 347 // Ok, reallocate and copy...
344 iBits = iLength; 348 iBits = iLength;
345 long iNewBytes = bitsToBytes( iLength ); 349// long iNewBytes = bitsToBytes( iLength );
346 if( bClear ) 350 if( bClear )
347 { 351 {
348 delete[] caData; 352 delete[] caData;
@@ -352,7 +356,7 @@ bool Bu::BitString::setSize( long iLength, bool bClear )
352 else 356 else
353 { 357 {
354 unsigned char *tmp = caData; 358 unsigned char *tmp = caData;
355 caData = new unsigned char[iBytes]; 359 caData = new unsigned char[iNewBytes];
356 if( iNewBytes < iBytes ) 360 if( iNewBytes < iBytes )
357 { 361 {
358 memcpy( caData, tmp, iNewBytes ); 362 memcpy( caData, tmp, iNewBytes );
diff --git a/src/cache.h b/src/cache.h
index 259278c..ccc4966 100644
--- a/src/cache.h
+++ b/src/cache.h
@@ -11,19 +11,18 @@
11 11
12namespace Bu 12namespace Bu
13{ 13{
14// template<class obtype, class keytype>
15// keytype __cacheGetKey( obtype *&pObj );
16
14 template<class obtype, class keytype> 17 template<class obtype, class keytype>
15 class Cache 18 class Cache
16 { 19 {
17 //friend class Bu::CPtr<obtype, keytype>;
18 public: 20 public:
19 // typedef Bu::CPtr<obtype, keytype> Ptr;
20
21 /** 21 /**
22 * Cache Pointer - Provides access to data that is held within the 22 * Cache Pointer - Provides access to data that is held within the
23 * cache. This provides safe, refcounting access to data stored in 23 * cache. This provides safe, refcounting access to data stored in
24 * the cache, with support for lazy loading. 24 * the cache, with support for lazy loading.
25 */ 25 */
26 //template<class obtype, class keytype>
27 class Ptr 26 class Ptr
28 { 27 {
29 friend class Bu::Cache<obtype, keytype>; 28 friend class Bu::Cache<obtype, keytype>;
@@ -37,6 +36,13 @@ namespace Bu
37 if( pCache ) 36 if( pCache )
38 pCache->incRef( kId ); 37 pCache->incRef( kId );
39 } 38 }
39
40 Ptr( Cache<obtype, keytype> *pCache, const keytype &kId ) :
41 pCache( pCache ),
42 pData( NULL ),
43 kId( kId )
44 {
45 }
40 46
41 public: 47 public:
42 Ptr( const Ptr &rSrc ) : 48 Ptr( const Ptr &rSrc ) :
@@ -44,7 +50,7 @@ namespace Bu
44 pData( rSrc.pData ), 50 pData( rSrc.pData ),
45 kId( rSrc.kId ) 51 kId( rSrc.kId )
46 { 52 {
47 if( pCache ) 53 if( pCache && pData )
48 pCache->incRef( kId ); 54 pCache->incRef( kId );
49 } 55 }
50 56
@@ -56,40 +62,69 @@ namespace Bu
56 62
57 virtual ~Ptr() 63 virtual ~Ptr()
58 { 64 {
59 if( pCache ) 65 if( pCache && pData )
60 pCache->decRef( kId ); 66 pCache->decRef( kId );
61 } 67 }
62 68
63 obtype &operator*() 69 obtype &operator*()
64 { 70 {
71 checkPtr();
72 return *pData;
73 }
74
75 const obtype &operator*() const
76 {
77 checkPtr();
65 return *pData; 78 return *pData;
66 } 79 }
67 80
68 obtype *operator->() 81 obtype *operator->()
69 { 82 {
83 checkPtr();
84 return pData;
85 }
86
87 const obtype *operator->() const
88 {
89 checkPtr();
70 return pData; 90 return pData;
71 } 91 }
72 92
73 const keytype &getKey() 93 bool isLoaded() const
94 {
95 return pData != NULL;
96 }
97
98 const keytype &getKey() const
74 { 99 {
75 return kId; 100 return kId;
76 } 101 }
77 102
78 Ptr &operator=( const Ptr &rRhs ) 103 Ptr &operator=( const Ptr &rRhs )
79 { 104 {
80 if( pCache ) 105 if( pCache && pData )
81 pCache->decRef( kId ); 106 pCache->decRef( kId );
82 pCache = rRhs.pCache; 107 pCache = rRhs.pCache;
83 pData = rRhs.pData; 108 pData = rRhs.pData;
84 kId = rRhs.kId; 109 kId = rRhs.kId;
85 if( pCache ) 110 if( pCache && pData )
86 pCache->incRef( kId ); 111 pCache->incRef( kId );
87 } 112 }
88 113
89 private: 114 private:
115 void checkPtr() const
116 {
117 if( pCache && !pData )
118 {
119 pData = pCache->getRaw( kId );
120 pCache->incRef( kId );
121 }
122 }
123
124 private:
90 Bu::Cache<obtype, keytype> *pCache; 125 Bu::Cache<obtype, keytype> *pCache;
91 obtype *pData; 126 mutable obtype *pData;
92 keytype kId; 127 mutable keytype kId;
93 }; 128 };
94 129
95 private: 130 private:
@@ -165,6 +200,12 @@ namespace Bu
165 } 200 }
166 } 201 }
167 202
203 Ptr getLazy( const keytype &cId )
204 {
205 TRACE( cId );
206 return Ptr( this, cId );
207 }
208
168 int getRefCount( const keytype &cId ) 209 int getRefCount( const keytype &cId )
169 { 210 {
170 TRACE( cId ); 211 TRACE( cId );
@@ -220,19 +261,33 @@ namespace Bu
220 } 261 }
221 262
222 private: 263 private:
223 void incRef( keytype cId ) 264 void incRef( const keytype &cId )
224 { 265 {
225 TRACE( cId ); 266 TRACE( cId );
226 hEnt.get( cId ).iRefs++; 267 hEnt.get( cId ).iRefs++;
227 } 268 }
228 269
229 void decRef( keytype cId ) 270 void decRef( const keytype &cId )
230 { 271 {
231 TRACE( cId ); 272 TRACE( cId );
232 CacheEntry &e = hEnt.get( cId ); 273 CacheEntry &e = hEnt.get( cId );
233 e.iRefs--; 274 e.iRefs--;
234 } 275 }
235 276
277 obtype *getRaw( const keytype &cId )
278 {
279 TRACE( cId );
280 try {
281 return hEnt.get( cId ).pData;
282 }
283 catch( Bu::HashException &e ) {
284 CacheEntry e = {pStore->load( cId ), 0};
285 pCalc->onLoad( e.pData, cId );
286 hEnt.insert( cId, e );
287 return e.pData;
288 }
289 }
290
236 private: 291 private:
237 CidHash hEnt; 292 CidHash hEnt;
238 Calc *pCalc; 293 Calc *pCalc;
diff --git a/src/cachestorenids.h b/src/cachestorenids.h
index 0d8ff64..62d1555 100644
--- a/src/cachestorenids.h
+++ b/src/cachestorenids.h
@@ -10,6 +10,9 @@
10namespace Bu 10namespace Bu
11{ 11{
12 template<class obtype, class keytype> 12 template<class obtype, class keytype>
13 keytype __cacheGetKey( const obtype *pObj );
14
15 template<class obtype, class keytype>
13 class CacheStoreNids : public CacheStore<obtype, keytype> 16 class CacheStoreNids : public CacheStore<obtype, keytype>
14 { 17 {
15 public: 18 public:
@@ -62,11 +65,9 @@ namespace Bu
62 delete pObj; 65 delete pObj;
63 } 66 }
64 67
65 virtual keytype getKey( obtype *pSrc )=0;
66
67 virtual keytype create( obtype *pSrc ) 68 virtual keytype create( obtype *pSrc )
68 { 69 {
69 keytype key = getKey( pSrc ); 70 keytype key = __cacheGetKey<obtype,keytype>( pSrc );
70 int iStream = nStore.createStream(); 71 int iStream = nStore.createStream();
71 hId.insert( key, iStream ); 72 hId.insert( key, iStream );
72 printf("Creating stream: %d\n", iStream ); 73 printf("Creating stream: %d\n", iStream );
diff --git a/src/tafreader.cpp b/src/tafreader.cpp
index 4408cce..ceed9be 100644
--- a/src/tafreader.cpp
+++ b/src/tafreader.cpp
@@ -14,6 +14,7 @@ using namespace Bu;
14 14
15Bu::TafReader::TafReader( Bu::Stream &sIn ) : 15Bu::TafReader::TafReader( Bu::Stream &sIn ) :
16 c( 0 ), 16 c( 0 ),
17 la( 0 ),
17 sIn( sIn ), 18 sIn( sIn ),
18 iLine( 1 ), iCol( -1 ) 19 iLine( 1 ), iCol( -1 )
19{ 20{
diff --git a/src/tests/cache.cpp b/src/tests/cache.cpp
index bacd7fc..323edda 100644
--- a/src/tests/cache.cpp
+++ b/src/tests/cache.cpp
@@ -168,7 +168,7 @@ int main( int argc, char *argv[] )
168 168
169 if( argc < 3 ) 169 if( argc < 3 )
170 { 170 {
171 printf("Try: %s [crud] [<id/value>]\n\n", argv[0] ); 171 printf("Try: %s [crudl] [<id/value>]\n\n", argv[0] );
172 return 0; 172 return 0;
173 } 173 }
174 174
@@ -203,6 +203,15 @@ int main( int argc, char *argv[] )
203 cBob.erase( strtol( argv[2], NULL, 0 ) ); 203 cBob.erase( strtol( argv[2], NULL, 0 ) );
204 } 204 }
205 return 0; 205 return 0;
206
207 case 'l':
208 {
209 BobCache::Ptr pBob = cBob.getLazy( strtol( argv[2], NULL, 0 ) );
210 printf("isLoaded: %s\n", pBob.isLoaded()?"yes":"no");
211 printf("Value = %d\n", pBob->getInt() );
212 printf("isLoaded: %s\n", pBob.isLoaded()?"yes":"no");
213 }
214 return 0;
206 } 215 }
207 216
208} 217}