Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / ACE / apps / JAWS2 / JAWS / Cache_Hash_T.cpp
blob022553c65ba90c1bdcb40319933fe576103673a4
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 (void)
80 if (this->hashtable_)
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,
90 EXT_ID,
91 JAWS_Cache_Object *,
92 EQ_FUNC);
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)
114 return -1;
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)
126 return -1;
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)
135 int result;
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)
143 return -1;
145 result = this->hashtable_[hash_idx]->bind (ext_id, int_id);
147 else
148 result = this->hashtable_[hash_idx]->bind (ext_id, int_id);
150 return result;
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)
157 int result;
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)
165 return -1;
167 result = this->hashtable_[hash_idx]->trybind (ext_id, int_id);
169 else
170 result = this->hashtable_[hash_idx]->trybind (ext_id, int_id);
172 return result;
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,
178 EXT_ID &old_ext_id,
179 JAWS_Cache_Object *&old_int_id)
181 int result;
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)
189 return -1;
191 result = this->hashtable_[hash_idx]->rebind (ext_id, int_id,
192 old_ext_id, old_int_id);
194 else
195 result = this->hashtable_[hash_idx]->rebind (ext_id, int_id,
196 old_ext_id, old_int_id);
198 return result;
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)
208 return -1;
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)
220 return -1;
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
229 return this->size_;
235 #endif /* JAWS_CACHEHASH_T_CPP */