Merge branch 'master' into jwi-bcc64xsingletonwarning
[ACE_TAO.git] / ACE / apps / JAWS2 / JAWS / Cache_Hash_T.cpp
blob662f15cbd3f3c7bee9f82a35175e9672a18b69b7
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
16 size_t d = 3;
18 if (number <= 2) return (number == 2);
20 if (number % 2 == 0) return 0;
22 while (d <= number/d)
24 if (number % d == 0) return 0;
25 d += 2;
28 return 1;
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);
43 return 0;
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,
48 size_t size)
49 : allocator_ (alloc),
50 hashtable_ (0)
52 while (!this->isprime (size))
53 size++;
55 this->size_ = size;
57 if (this->allocator_ == 0)
58 this->allocator_ = ACE_Allocator::instance ();
60 size_t memsize = this->size_ * sizeof (CACHE_BUCKET_MANAGER *);
62 this->hashtable_
63 = (CACHE_BUCKET_MANAGER **) this->allocator_->malloc (memsize);
65 if (this->hashtable_)
67 for (size_t i = 0; i < this->size_; i++)
68 this->hashtable_[i] = 0;
70 else
72 this->size_ = 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 ()
80 if (this->hashtable_)
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,
89 EXT_ID,
90 JAWS_Cache_Object *,
91 EQ_FUNC);
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)
112 return -1;
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)
124 return -1;
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)
133 int result;
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)
141 return -1;
143 result = this->hashtable_[hash_idx]->bind (ext_id, int_id);
145 else
146 result = this->hashtable_[hash_idx]->bind (ext_id, int_id);
148 return result;
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)
155 int result;
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)
163 return -1;
165 result = this->hashtable_[hash_idx]->trybind (ext_id, int_id);
167 else
168 result = this->hashtable_[hash_idx]->trybind (ext_id, int_id);
170 return result;
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,
176 EXT_ID &old_ext_id,
177 JAWS_Cache_Object *&old_int_id)
179 int result;
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)
187 return -1;
189 result = this->hashtable_[hash_idx]->rebind (ext_id, int_id,
190 old_ext_id, old_int_id);
192 else
193 result = this->hashtable_[hash_idx]->rebind (ext_id, int_id,
194 old_ext_id, old_int_id);
196 return result;
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)
206 return -1;
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)
218 return -1;
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
227 return this->size_;
232 #endif /* JAWS_CACHEHASH_T_CPP */