1 #ifndef JAWS_CACHE_HASH_T_CPP
2 #define JAWS_CACHE_HASH_T_CPP
4 #include "JAWS/Cache_Hash_T.h"
5 #include "JAWS/Hash_Bucket_T.h"
7 template <class EXT_ID
, class HASH_FUNC
, class EQ_FUNC
> unsigned long
8 JAWS_Cache_Hash
<EXT_ID
,HASH_FUNC
,EQ_FUNC
>::hash (const EXT_ID
&ext_id
) const
10 return HASH_FUNC (ext_id
) % this->size_
;
13 template <class EXT_ID
, class HASH_FUNC
, class EQ_FUNC
> bool
14 JAWS_Cache_Hash
<EXT_ID
,HASH_FUNC
,EQ_FUNC
>::isprime (size_t number
) const
18 if (number
<= 2) return (number
== 2);
20 if (number
% 2 == 0) return 0;
24 if (number
% d
== 0) return 0;
31 template <class EXT_ID
, class HASH_FUNC
, class EQ_FUNC
> int
32 JAWS_Cache_Hash
<EXT_ID
,HASH_FUNC
,EQ_FUNC
>::new_cachebucket (size_t hash_idx
)
34 if (this->hashtable_
[hash_idx
] == 0)
36 size_t alloc_size
= sizeof (CACHE_BUCKET_MANAGER
);
37 ACE_NEW_MALLOC_RETURN (this->hashtable_
[hash_idx
],
38 (CACHE_BUCKET_MANAGER
*)
39 this->allocator_
->malloc (alloc_size
),
40 CACHE_BUCKET_MANAGER (this->allocator_
), -1);
46 template <class EXT_ID
, class HASH_FUNC
, class EQ_FUNC
>
47 JAWS_Cache_Hash
<EXT_ID
,HASH_FUNC
,EQ_FUNC
>::JAWS_Cache_Hash (ACE_Allocator
*alloc
,
52 while (!this->isprime (size
))
57 if (this->allocator_
== 0)
58 this->allocator_
= ACE_Allocator::instance ();
60 size_t memsize
= this->size_
* sizeof (CACHE_BUCKET_MANAGER
*);
63 = (CACHE_BUCKET_MANAGER
**) this->allocator_
->malloc (memsize
);
67 for (size_t i
= 0; i
< this->size_
; i
++)
68 this->hashtable_
[i
] = 0;
73 // should indicate something is wrong to the user.
77 template <class EXT_ID
, class HASH_FUNC
, class EQ_FUNC
>
78 JAWS_Cache_Hash
<EXT_ID
,HASH_FUNC
,EQ_FUNC
>::~JAWS_Cache_Hash ()
82 for (size_t i
= 0; i
< this->size_
; i
++)
84 if (this->hashtable_
[i
])
86 ACE_DES_FREE_TEMPLATE3(this->hashtable_
[i
],
87 this->allocator_
->free
,
88 JAWS_Hash_Bucket_Manager
,
96 this->hashtable_
[i
] = 0;
99 this->allocator_
->free (this->hashtable_
);
100 this->hashtable_
= 0;
103 this->allocator_
= 0;
106 template <class EXT_ID
, class HASH_FUNC
, class EQ_FUNC
> int
107 JAWS_Cache_Hash
<EXT_ID
,HASH_FUNC
,EQ_FUNC
>::find (const EXT_ID
&ext_id
) const
109 unsigned long hash_idx
= this->hash (ext_id
);
111 if (this->hashtable_
[hash_idx
] == 0)
114 return this->hashtable_
[hash_idx
]->find (ext_id
);
117 template <class EXT_ID
, class HASH_FUNC
, class EQ_FUNC
> int
118 JAWS_Cache_Hash
<EXT_ID
,HASH_FUNC
,EQ_FUNC
>::find (const EXT_ID
&ext_id
,
119 JAWS_Cache_Object
*&int_id
) const
121 unsigned long hash_idx
= this->hash (ext_id
);
123 if (this->hashtable_
[hash_idx
] == 0)
126 return this->hashtable_
[hash_idx
]->find (ext_id
, int_id
);
129 template <class EXT_ID
, class HASH_FUNC
, class EQ_FUNC
> int
130 JAWS_Cache_Hash
<EXT_ID
,HASH_FUNC
,EQ_FUNC
>::bind (const EXT_ID
&ext_id
,
131 JAWS_Cache_Object
*const &int_id
)
134 unsigned long hash_idx
= this->hash (ext_id
);
136 if (this->hashtable_
[hash_idx
] == 0)
138 ACE_GUARD_RETURN (ACE_SYNCH_MUTEX
, g
, this->lock_
, -1);
140 if (this->new_cachebucket (hash_idx
) == -1)
143 result
= this->hashtable_
[hash_idx
]->bind (ext_id
, int_id
);
146 result
= this->hashtable_
[hash_idx
]->bind (ext_id
, int_id
);
151 template <class EXT_ID
, class HASH_FUNC
, class EQ_FUNC
> int
152 JAWS_Cache_Hash
<EXT_ID
,HASH_FUNC
,EQ_FUNC
>::trybind (const EXT_ID
&ext_id
,
153 JAWS_Cache_Object
*&int_id
)
156 unsigned long hash_idx
= this->hash (ext_id
);
158 if (this->hashtable_
[hash_idx
] == 0)
160 ACE_GUARD_RETURN (ACE_SYNCH_MUTEX
, g
, this->lock_
, -1);
162 if (this->new_cachebucket (hash_idx
) == -1)
165 result
= this->hashtable_
[hash_idx
]->trybind (ext_id
, int_id
);
168 result
= this->hashtable_
[hash_idx
]->trybind (ext_id
, int_id
);
173 template <class EXT_ID
, class HASH_FUNC
, class EQ_FUNC
> int
174 JAWS_Cache_Hash
<EXT_ID
,HASH_FUNC
,EQ_FUNC
>::rebind (const EXT_ID
&ext_id
,
175 JAWS_Cache_Object
*const &int_id
,
177 JAWS_Cache_Object
*&old_int_id
)
180 unsigned long hash_idx
= this->hash (ext_id
);
182 if (this->hashtable_
[hash_idx
] == 0)
184 ACE_GUARD_RETURN (ACE_SYNCH_MUTEX
, g
, this->lock_
, -1);
186 if (this->new_cachebucket (hash_idx
) == -1)
189 result
= this->hashtable_
[hash_idx
]->rebind (ext_id
, int_id
,
190 old_ext_id
, old_int_id
);
193 result
= this->hashtable_
[hash_idx
]->rebind (ext_id
, int_id
,
194 old_ext_id
, old_int_id
);
200 template <class EXT_ID
, class HASH_FUNC
, class EQ_FUNC
> int
201 JAWS_Cache_Hash
<EXT_ID
,HASH_FUNC
,EQ_FUNC
>::unbind (const EXT_ID
&ext_id
)
203 unsigned long hash_idx
= this->hash (ext_id
);
205 if (this->hashtable_
[hash_idx
] == 0)
208 return this->hashtable_
[hash_idx
]->unbind (ext_id
);
211 template <class EXT_ID
, class HASH_FUNC
, class EQ_FUNC
> int
212 JAWS_Cache_Hash
<EXT_ID
,HASH_FUNC
,EQ_FUNC
>::unbind (const EXT_ID
&ext_id
,
213 JAWS_Cache_Object
*&int_id
)
215 unsigned long hash_idx
= this->hash (ext_id
);
217 if (this->hashtable_
[hash_idx
] == 0)
220 return this->hashtable_
[hash_idx
]->unbind (ext_id
, int_id
);
224 template <class EXT_ID
, class HASH_FUNC
, class EQ_FUNC
> size_t
225 JAWS_Cache_Hash
<EXT_ID
,HASH_FUNC
,EQ_FUNC
>::size () const
232 #endif /* JAWS_CACHEHASH_T_CPP */