aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/cachable.cpp15
-rw-r--r--src/cachable.h19
-rw-r--r--src/cache.h49
-rw-r--r--src/cptr.h14
-rw-r--r--src/membuf.cpp2
-rw-r--r--src/tests/cache.cpp31
6 files changed, 126 insertions, 4 deletions
diff --git a/src/cachable.cpp b/src/cachable.cpp
new file mode 100644
index 0000000..7fa2d23
--- /dev/null
+++ b/src/cachable.cpp
@@ -0,0 +1,15 @@
1#include "bu/cachable.h"
2
3Bu::Cachable::Cachable()
4{
5}
6
7Bu::Cachable::~Cachable()
8{
9}
10
11template<> long Bu::getCacheId<Bu::Cachable>( const Bu::Cachable *o )
12{
13 return o->getCacheId();
14}
15
diff --git a/src/cachable.h b/src/cachable.h
new file mode 100644
index 0000000..529da6f
--- /dev/null
+++ b/src/cachable.h
@@ -0,0 +1,19 @@
1#ifndef BU_CACHABLE_H
2#define BU_CACHABLE_H
3
4namespace Bu
5{
6 class Cachable
7 {
8 public:
9 Cachable();
10 virtual ~Cachable();
11
12 virtual long getCacheId() const =0;
13 };
14
15 template<class obtype> long getCacheId( const obtype *o );
16 template<> long getCacheId<Cachable>( const Cachable *o );
17};
18
19#endif
diff --git a/src/cache.h b/src/cache.h
index 24ef652..344d1c6 100644
--- a/src/cache.h
+++ b/src/cache.h
@@ -1,20 +1,69 @@
1#ifndef BU_CACHE_H 1#ifndef BU_CACHE_H
2#define BU_CACHE_H 2#define BU_CACHE_H
3 3
4#include "bu/cptr.h"
5#include "bu/hash.h"
6
7#define BU_TRACE
8#include "bu/trace.h"
9
4namespace Bu 10namespace Bu
5{ 11{
12 template<class obtype> class Cache;
13 template<class obtype> long getCacheId( const obtype *o );
14
6 template<class obtype> 15 template<class obtype>
7 class Cache 16 class Cache
8 { 17 {
18 friend class Bu::CPtr<obtype>;
19 typedef Bu::CPtr<obtype> Ptr;
20 public:
21 typedef long cid; /**< Cache ID type. Unique cache entry ID. */
22
9 public: 23 public:
10 Cache() 24 Cache()
11 { 25 {
26 TRACE();
12 } 27 }
13 28
14 virtual ~Cache() 29 virtual ~Cache()
15 { 30 {
31 TRACE();
32 }
33
34 Ptr insert( obtype *pData )
35 {
36 TRACE();
37 CacheEntry e = {pData, 0};
38 hEnt.insert( getCacheId( pData ), e );
39 return Ptr( *this, pData );
16 } 40 }
17 41
42 private:
43 void incRef( obtype *pData )
44 {
45 TRACE();
46 hEnt.get( getCacheId( pData ) ).iRefs++;
47 }
48
49 void decRef( obtype *pData )
50 {
51 TRACE();
52 CacheEntry &e = hEnt.get( getCacheId( pData ) );
53 e.iRefs--;
54 }
55
56 private:
57 typedef struct CacheEntry
58 {
59 obtype *pData;
60 int iRefs;
61 } CacheEntry;
62
63 //typedef Bu::Hash<ptrdiff_t, int> RefHash;
64 typedef Bu::Hash<cid, CacheEntry> CidHash;
65 //RefHash hRefs;
66 CidHash hEnt;
18 }; 67 };
19}; 68};
20 69
diff --git a/src/cptr.h b/src/cptr.h
index 1f8c43d..138ace8 100644
--- a/src/cptr.h
+++ b/src/cptr.h
@@ -1,23 +1,31 @@
1#ifndef BU_C_PTR_H 1#ifndef BU_C_PTR_H
2#define BU_C_PTR_H 2#define BU_C_PTR_H
3 3
4#include "bu/cache.h"
5
6namespace Bu 4namespace Bu
7{ 5{
6 template<class obtype> class Cache;
7
8 template<class obtype> 8 template<class obtype>
9 class CPtr 9 class CPtr
10 { 10 {
11 friend class Bu::Cache<obtype>; 11 friend class Bu::Cache<obtype>;
12 private: 12 private:
13 CPtr( Cache<obtype> &rCache, obtype &rData ) 13 CPtr( Cache<obtype> &rCache, obtype *pData ) :
14 rCache( rCache ),
15 pData( pData )
14 { 16 {
17 rCache.incRef( pData );
15 } 18 }
16 19
17 public: 20 public:
18 virtual ~CPtr() 21 virtual ~CPtr()
19 { 22 {
23 rCache.decRef( pData );
20 } 24 }
25
26 private:
27 Bu::Cache<obtype> &rCache;
28 obtype *pData;
21 }; 29 };
22}; 30};
23 31
diff --git a/src/membuf.cpp b/src/membuf.cpp
index e7c7ac8..f22a8de 100644
--- a/src/membuf.cpp
+++ b/src/membuf.cpp
@@ -52,7 +52,7 @@ size_t Bu::MemBuf::write( const void *pBuf, size_t nBytes )
52 { 52 {
53 // Trickier, we must do this in two parts, overwrite, then append 53 // Trickier, we must do this in two parts, overwrite, then append
54 // Frist, overwrite. 54 // Frist, overwrite.
55 int iOver = sBuf.getSize() - nPos; 55 size_t iOver = sBuf.getSize() - nPos;
56 if( iOver > nBytes ) 56 if( iOver > nBytes )
57 iOver = nBytes; 57 iOver = nBytes;
58 memcpy( sBuf.getStr()+nPos, pBuf, iOver ); 58 memcpy( sBuf.getStr()+nPos, pBuf, iOver );
diff --git a/src/tests/cache.cpp b/src/tests/cache.cpp
new file mode 100644
index 0000000..e51c31b
--- /dev/null
+++ b/src/tests/cache.cpp
@@ -0,0 +1,31 @@
1#include <stdio.h>
2
3#include "bu/cache.h"
4#include "bu/cachable.h"
5
6class Bob : public Bu::Cachable
7{
8public:
9 Bob()
10 {
11 }
12
13 virtual ~Bob()
14 {
15 }
16
17 long getCacheId() const
18 {
19 return 0;
20 }
21
22 int iInt;
23};
24
25int main()
26{
27 Bu::Cache<Bob> bobCache;
28
29// Bu::CPtr<Bob> pB = bobCache.insert( new Bob() );
30}
31