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 (void)
82 for (size_t i
= 0; i
< this->size_
; i
++)
84 if (this->hashtable_
[i
])
87 ACE_DES_FREE_TEMPLATE3(this->hashtable_
[i
],
88 this->allocator_
->free
,
89 JAWS_Hash_Bucket_Manager
,
98 this->hashtable_
[i
] = 0;
101 this->allocator_
->free (this->hashtable_
);
102 this->hashtable_
= 0;
105 this->allocator_
= 0;
108 template <class EXT_ID
, class HASH_FUNC
, class EQ_FUNC
> int
109 JAWS_Cache_Hash
<EXT_ID
,HASH_FUNC
,EQ_FUNC
>::find (const EXT_ID
&ext_id
) const
111 unsigned long hash_idx
= this->hash (ext_id
);
113 if (this->hashtable_
[hash_idx
] == 0)
116 return this->hashtable_
[hash_idx
]->find (ext_id
);
119 template <class EXT_ID
, class HASH_FUNC
, class EQ_FUNC
> int
120 JAWS_Cache_Hash
<EXT_ID
,HASH_FUNC
,EQ_FUNC
>::find (const EXT_ID
&ext_id
,
121 JAWS_Cache_Object
*&int_id
) const
123 unsigned long hash_idx
= this->hash (ext_id
);
125 if (this->hashtable_
[hash_idx
] == 0)
128 return this->hashtable_
[hash_idx
]->find (ext_id
, int_id
);
131 template <class EXT_ID
, class HASH_FUNC
, class EQ_FUNC
> int
132 JAWS_Cache_Hash
<EXT_ID
,HASH_FUNC
,EQ_FUNC
>::bind (const EXT_ID
&ext_id
,
133 JAWS_Cache_Object
*const &int_id
)
136 unsigned long hash_idx
= this->hash (ext_id
);
138 if (this->hashtable_
[hash_idx
] == 0)
140 ACE_GUARD_RETURN (ACE_SYNCH_MUTEX
, g
, this->lock_
, -1);
142 if (this->new_cachebucket (hash_idx
) == -1)
145 result
= this->hashtable_
[hash_idx
]->bind (ext_id
, int_id
);
148 result
= this->hashtable_
[hash_idx
]->bind (ext_id
, int_id
);
153 template <class EXT_ID
, class HASH_FUNC
, class EQ_FUNC
> int
154 JAWS_Cache_Hash
<EXT_ID
,HASH_FUNC
,EQ_FUNC
>::trybind (const EXT_ID
&ext_id
,
155 JAWS_Cache_Object
*&int_id
)
158 unsigned long hash_idx
= this->hash (ext_id
);
160 if (this->hashtable_
[hash_idx
] == 0)
162 ACE_GUARD_RETURN (ACE_SYNCH_MUTEX
, g
, this->lock_
, -1);
164 if (this->new_cachebucket (hash_idx
) == -1)
167 result
= this->hashtable_
[hash_idx
]->trybind (ext_id
, int_id
);
170 result
= this->hashtable_
[hash_idx
]->trybind (ext_id
, int_id
);
175 template <class EXT_ID
, class HASH_FUNC
, class EQ_FUNC
> int
176 JAWS_Cache_Hash
<EXT_ID
,HASH_FUNC
,EQ_FUNC
>::rebind (const EXT_ID
&ext_id
,
177 JAWS_Cache_Object
*const &int_id
,
179 JAWS_Cache_Object
*&old_int_id
)
182 unsigned long hash_idx
= this->hash (ext_id
);
184 if (this->hashtable_
[hash_idx
] == 0)
186 ACE_GUARD_RETURN (ACE_SYNCH_MUTEX
, g
, this->lock_
, -1);
188 if (this->new_cachebucket (hash_idx
) == -1)
191 result
= this->hashtable_
[hash_idx
]->rebind (ext_id
, int_id
,
192 old_ext_id
, old_int_id
);
195 result
= this->hashtable_
[hash_idx
]->rebind (ext_id
, int_id
,
196 old_ext_id
, old_int_id
);
202 template <class EXT_ID
, class HASH_FUNC
, class EQ_FUNC
> int
203 JAWS_Cache_Hash
<EXT_ID
,HASH_FUNC
,EQ_FUNC
>::unbind (const EXT_ID
&ext_id
)
205 unsigned long hash_idx
= this->hash (ext_id
);
207 if (this->hashtable_
[hash_idx
] == 0)
210 return this->hashtable_
[hash_idx
]->unbind (ext_id
);
213 template <class EXT_ID
, class HASH_FUNC
, class EQ_FUNC
> int
214 JAWS_Cache_Hash
<EXT_ID
,HASH_FUNC
,EQ_FUNC
>::unbind (const EXT_ID
&ext_id
,
215 JAWS_Cache_Object
*&int_id
)
217 unsigned long hash_idx
= this->hash (ext_id
);
219 if (this->hashtable_
[hash_idx
] == 0)
222 return this->hashtable_
[hash_idx
]->unbind (ext_id
, int_id
);
226 template <class EXT_ID
, class HASH_FUNC
, class EQ_FUNC
> size_t
227 JAWS_Cache_Hash
<EXT_ID
,HASH_FUNC
,EQ_FUNC
>::size (void) const
235 #endif /* JAWS_CACHEHASH_T_CPP */