00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #ifndef ACE_HASH_MAP_MANAGER_T_CPP
00017 #define ACE_HASH_MAP_MANAGER_T_CPP
00018
00019 #include "ace/Hash_Map_Manager_T.h"
00020
00021 #if !defined (ACE_LACKS_PRAGMA_ONCE)
00022 # pragma once
00023 #endif
00024
00025 #if !defined (__ACE_INLINE__)
00026 # include "ace/Hash_Map_Manager_T.i"
00027 #elif defined (__SUNPRO_CC) && (__SUNPRO_CC == 0x420)
00028
00029
00030
00031
00032 # undef ACE_INLINE
00033 # define ACE_INLINE
00034 # include "ace/Hash_Map_Manager_T.i"
00035 # undef ACE_INLINE
00036 # define ACE_INLINE inline
00037 #endif
00038
00039 #include "ace/Synch.h"
00040 #include "ace/Service_Config.h"
00041 #include "ace/Malloc.h"
00042
00043 ACE_RCSID(ace, Hash_Map_Manager_T, "$Id: Hash_Map_Manager_T.cpp,v 1.1.1.4 2003/02/21 18:36:32 chad Exp $")
00044
00045 template <class EXT_ID, class INT_ID>
00046 ACE_Hash_Map_Entry<EXT_ID, INT_ID>::ACE_Hash_Map_Entry (ACE_Hash_Map_Entry<EXT_ID, INT_ID> *next,
00047 ACE_Hash_Map_Entry<EXT_ID, INT_ID> *prev)
00048 : next_ (next),
00049 prev_ (prev)
00050 {
00051 }
00052
00053 template <class EXT_ID, class INT_ID>
00054 ACE_Hash_Map_Entry<EXT_ID, INT_ID>::ACE_Hash_Map_Entry (const EXT_ID &ext_id,
00055 const INT_ID &int_id,
00056 ACE_Hash_Map_Entry<EXT_ID, INT_ID> *next,
00057 ACE_Hash_Map_Entry<EXT_ID, INT_ID> *prev)
00058 : ext_id_ (ext_id),
00059 int_id_ (int_id),
00060 next_ (next),
00061 prev_ (prev)
00062 {
00063 }
00064
00065 # if ! defined (ACE_HAS_BROKEN_NOOP_DTORS)
00066 template <class EXT_ID, class INT_ID>
00067 ACE_Hash_Map_Entry<EXT_ID, INT_ID>::~ACE_Hash_Map_Entry (void)
00068 {
00069 }
00070 # endif
00071
00072 template <class EXT_ID, class INT_ID> void
00073 ACE_Hash_Map_Entry<EXT_ID, INT_ID>::dump (void) const
00074 {
00075 ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00076 ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("next_ = %d"), this->next_));
00077 ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("prev_ = %d"), this->prev_));
00078 ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00079 }
00080
00081 template <class EXT_ID, class INT_ID, class HASH_KEY, class COMPARE_KEYS, class ACE_LOCK> void
00082 ACE_Hash_Map_Manager_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::dump (void) const
00083 {
00084 ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00085 ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("total_size_ = %d"), this->total_size_));
00086 ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("\ncur_size_ = %d"), this->cur_size_));
00087 this->allocator_->dump ();
00088 this->lock_.dump ();
00089 ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00090 }
00091
00092 template <class EXT_ID, class INT_ID, class HASH_KEY, class COMPARE_KEYS, class ACE_LOCK> int
00093 ACE_Hash_Map_Manager_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::create_buckets (size_t size)
00094 {
00095 size_t bytes = size * sizeof (ACE_Hash_Map_Entry<EXT_ID, INT_ID>);
00096 void *ptr;
00097
00098 ACE_ALLOCATOR_RETURN (ptr,
00099 this->allocator_->malloc (bytes),
00100 -1);
00101
00102 this->table_ = (ACE_Hash_Map_Entry<EXT_ID, INT_ID> *) ptr;
00103
00104 this->total_size_ = size;
00105
00106
00107
00108
00109 for (size_t i = 0; i < size; i++)
00110 new (&this->table_[i]) ACE_Hash_Map_Entry<EXT_ID, INT_ID> (&this->table_[i],
00111 &this->table_[i]);
00112 return 0;
00113 }
00114
00115 template <class EXT_ID, class INT_ID, class HASH_KEY, class COMPARE_KEYS, class ACE_LOCK> int
00116 ACE_Hash_Map_Manager_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::open (size_t size,
00117 ACE_Allocator *alloc)
00118 {
00119 ACE_WRITE_GUARD_RETURN (ACE_LOCK, ace_mon, this->lock_, -1);
00120
00121
00122
00123 this->close_i ();
00124
00125 if (alloc == 0)
00126 alloc = ACE_Allocator::instance ();
00127
00128 this->allocator_ = alloc;
00129
00130
00131
00132
00133
00134 ACE_ASSERT (size != 0);
00135
00136 return this->create_buckets (size);
00137 }
00138
00139 template <class EXT_ID, class INT_ID, class HASH_KEY, class COMPARE_KEYS, class ACE_LOCK> int
00140 ACE_Hash_Map_Manager_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::close_i (void)
00141 {
00142
00143
00144 if (this->table_ != 0)
00145 {
00146
00147 this->unbind_all_i ();
00148
00149
00150 for (size_t i = 0; i < this->total_size_; i++)
00151 {
00152
00153 ACE_Hash_Map_Entry<EXT_ID, INT_ID> *entry = &this->table_[i];
00154
00155
00156 ACE_DES_FREE_TEMPLATE2 (entry, ACE_NOOP,
00157 ACE_Hash_Map_Entry, EXT_ID, INT_ID);
00158 }
00159
00160
00161 this->total_size_ = 0;
00162
00163
00164 this->allocator_->free (this->table_);
00165
00166
00167 this->table_ = 0;
00168 }
00169
00170 return 0;
00171 }
00172
00173 template <class EXT_ID, class INT_ID, class HASH_KEY, class COMPARE_KEYS, class ACE_LOCK> int
00174 ACE_Hash_Map_Manager_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::unbind_all_i (void)
00175 {
00176
00177
00178 for (size_t i = 0; i < this->total_size_; i++)
00179 {
00180 for (ACE_Hash_Map_Entry<EXT_ID, INT_ID> *temp_ptr = this->table_[i].next_;
00181 temp_ptr != &this->table_[i];
00182 )
00183 {
00184 ACE_Hash_Map_Entry<EXT_ID, INT_ID> *hold_ptr = temp_ptr;
00185 temp_ptr = temp_ptr->next_;
00186
00187
00188 ACE_DES_FREE_TEMPLATE2 (hold_ptr, this->allocator_->free,
00189 ACE_Hash_Map_Entry, EXT_ID, INT_ID);
00190 }
00191
00192
00193 this->table_[i].next_ = &this->table_[i];
00194 this->table_[i].prev_ = &this->table_[i];
00195 }
00196
00197 this->cur_size_ = 0;
00198
00199 return 0;
00200 }
00201
00202 template <class EXT_ID, class INT_ID, class HASH_KEY, class COMPARE_KEYS, class ACE_LOCK> int
00203 ACE_Hash_Map_Manager_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::bind_i (const EXT_ID &ext_id,
00204 const INT_ID &int_id,
00205 ACE_Hash_Map_Entry<EXT_ID, INT_ID> *&entry)
00206 {
00207 size_t loc;
00208 int result = this->shared_find (ext_id, entry, loc);
00209
00210 if (result == -1)
00211 {
00212 void *ptr;
00213
00214 ACE_ALLOCATOR_RETURN (ptr,
00215 this->allocator_->malloc (sizeof (ACE_Hash_Map_Entry<EXT_ID, INT_ID>)),
00216 -1);
00217
00218 entry = new (ptr) ACE_Hash_Map_Entry<EXT_ID, INT_ID> (ext_id,
00219 int_id,
00220 this->table_[loc].next_,
00221 &this->table_[loc]);
00222 this->table_[loc].next_ = entry;
00223 entry->next_->prev_ = entry;
00224 this->cur_size_++;
00225 return 0;
00226 }
00227 else
00228 return 1;
00229 }
00230
00231 template <class EXT_ID, class INT_ID, class HASH_KEY, class COMPARE_KEYS, class ACE_LOCK> int
00232 ACE_Hash_Map_Manager_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::trybind_i (const EXT_ID &ext_id,
00233 INT_ID &int_id,
00234 ACE_Hash_Map_Entry<EXT_ID, INT_ID> *&entry)
00235 {
00236 size_t loc;
00237 int result = this->shared_find (ext_id, entry, loc);
00238
00239 if (result == -1)
00240 {
00241
00242 void *ptr;
00243 ACE_ALLOCATOR_RETURN (ptr,
00244 this->allocator_->malloc (sizeof (ACE_Hash_Map_Entry<EXT_ID, INT_ID>)),
00245 -1);
00246
00247 entry = new (ptr) ACE_Hash_Map_Entry<EXT_ID, INT_ID> (ext_id,
00248 int_id,
00249 this->table_[loc].next_,
00250 &this->table_[loc]);
00251 this->table_[loc].next_ = entry;
00252 entry->next_->prev_ = entry;
00253 this->cur_size_++;
00254 return 0;
00255 }
00256 else
00257 return 1;
00258 }
00259
00260 template <class EXT_ID, class INT_ID, class HASH_KEY, class COMPARE_KEYS, class ACE_LOCK> int
00261 ACE_Hash_Map_Manager_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::unbind_i (const EXT_ID &ext_id,
00262 INT_ID &int_id)
00263 {
00264 ACE_Hash_Map_Entry<EXT_ID, INT_ID> *temp;
00265
00266 size_t loc;
00267 int result = this->shared_find (ext_id, temp, loc);
00268
00269 if (result == -1)
00270 {
00271 errno = ENOENT;
00272 return -1;
00273 }
00274
00275 int_id = temp->int_id_;
00276
00277 return this->unbind_i (temp);
00278 }
00279
00280 template <class EXT_ID, class INT_ID, class HASH_KEY, class COMPARE_KEYS, class ACE_LOCK> int
00281 ACE_Hash_Map_Manager_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::unbind_i (ACE_Hash_Map_Entry<EXT_ID, INT_ID> *entry)
00282 {
00283 entry->next_->prev_ = entry->prev_;
00284 entry->prev_->next_ = entry->next_;
00285
00286
00287 ACE_DES_FREE_TEMPLATE2 (entry, this->allocator_->free,
00288 ACE_Hash_Map_Entry, EXT_ID, INT_ID);
00289 this->cur_size_--;
00290 return 0;
00291 }
00292
00293 template <class EXT_ID, class INT_ID, class HASH_KEY, class COMPARE_KEYS, class ACE_LOCK> int
00294 ACE_Hash_Map_Manager_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::shared_find (const EXT_ID &ext_id,
00295 ACE_Hash_Map_Entry<EXT_ID, INT_ID> *&entry,
00296 size_t &loc)
00297 {
00298 loc = this->hash (ext_id) % this->total_size_;
00299
00300 ACE_Hash_Map_Entry<EXT_ID, INT_ID> *temp = this->table_[loc].next_;
00301
00302 while (temp != &this->table_[loc] && this->equal (temp->ext_id_, ext_id) == 0)
00303 temp = temp->next_;
00304
00305 if (temp == &this->table_[loc])
00306 {
00307 errno = ENOENT;
00308 return -1;
00309 }
00310 else
00311 {
00312 entry = temp;
00313 return 0;
00314 }
00315 }
00316
00317 template <class EXT_ID, class INT_ID, class HASH_KEY, class COMPARE_KEYS, class ACE_LOCK> int
00318 ACE_Hash_Map_Manager_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::rebind_i (const EXT_ID &ext_id,
00319 const INT_ID &int_id,
00320 ACE_Hash_Map_Entry<EXT_ID, INT_ID> *&entry)
00321 {
00322 size_t dummy;
00323 if (this->shared_find (ext_id, entry, dummy) == -1)
00324 return this->bind_i (ext_id, int_id);
00325 else
00326 {
00327 entry->ext_id_ = ext_id;
00328 entry->int_id_ = int_id;
00329 return 1;
00330 }
00331 }
00332
00333 template <class EXT_ID, class INT_ID, class HASH_KEY, class COMPARE_KEYS, class ACE_LOCK> int
00334 ACE_Hash_Map_Manager_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::rebind_i (const EXT_ID &ext_id,
00335 const INT_ID &int_id,
00336 INT_ID &old_int_id,
00337 ACE_Hash_Map_Entry<EXT_ID, INT_ID> *&entry)
00338 {
00339 size_t dummy;
00340 if (this->shared_find (ext_id, entry, dummy) == -1)
00341 return this->bind_i (ext_id, int_id);
00342 else
00343 {
00344 old_int_id = entry->int_id_;
00345 entry->ext_id_ = ext_id;
00346 entry->int_id_ = int_id;
00347 return 1;
00348 }
00349 }
00350
00351 template <class EXT_ID, class INT_ID, class HASH_KEY, class COMPARE_KEYS, class ACE_LOCK> int
00352 ACE_Hash_Map_Manager_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::rebind_i (const EXT_ID &ext_id,
00353 const INT_ID &int_id,
00354 EXT_ID &old_ext_id,
00355 INT_ID &old_int_id,
00356 ACE_Hash_Map_Entry<EXT_ID, INT_ID> *&entry)
00357 {
00358 size_t dummy;
00359 if (this->shared_find (ext_id, entry, dummy) == -1)
00360 return this->bind_i (ext_id, int_id);
00361 else
00362 {
00363 old_ext_id = entry->ext_id_;
00364 old_int_id = entry->int_id_;
00365 entry->ext_id_ = ext_id;
00366 entry->int_id_ = int_id;
00367 return 1;
00368 }
00369 }
00370
00371
00372
00373 ACE_ALLOC_HOOK_DEFINE(ACE_Hash_Map_Iterator_Base_Ex)
00374
00375 template <class EXT_ID, class INT_ID, class HASH_KEY, class COMPARE_KEYS, class ACE_LOCK> void
00376 ACE_Hash_Map_Iterator_Base_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::dump_i (void) const
00377 {
00378 ACE_TRACE ("ACE_Hash_Map_Iterator_Base_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::dump_i");
00379
00380 ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00381 ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("index_ = %d "), this->index_));
00382 ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("next_ = %x"), this->next_));
00383 ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00384 }
00385
00386 template <class EXT_ID, class INT_ID, class HASH_KEY, class COMPARE_KEYS, class ACE_LOCK> int
00387 ACE_Hash_Map_Iterator_Base_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::forward_i (void)
00388 {
00389 ACE_TRACE ("ACE_Hash_Map_Iterator_Base_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::forward_i");
00390
00391 if (this->map_man_->table_ == 0)
00392 return -1;
00393
00394 else if (this->index_ == -1)
00395 {
00396 this->index_++;
00397 return this->forward_i ();
00398 }
00399 else if (this->index_ >= ACE_static_cast (ssize_t, this->map_man_->total_size_))
00400 return 0;
00401
00402 this->next_ = this->next_->next_;
00403 if (this->next_ == &this->map_man_->table_[this->index_])
00404 {
00405 while (++this->index_ < ACE_static_cast (ssize_t,
00406 this->map_man_->total_size_))
00407 {
00408 this->next_ = this->map_man_->table_[this->index_].next_;
00409 if (this->next_ != &this->map_man_->table_[this->index_])
00410 break;
00411 }
00412 }
00413
00414 return this->index_ < ACE_static_cast (ssize_t, this->map_man_->total_size_);
00415 }
00416
00417 template <class EXT_ID, class INT_ID, class HASH_KEY, class COMPARE_KEYS, class ACE_LOCK> int
00418 ACE_Hash_Map_Iterator_Base_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::reverse_i (void)
00419 {
00420 ACE_TRACE ("ACE_Hash_Map_Iterator_Base_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::reverse_i");
00421
00422 if (this->map_man_->table_ == 0)
00423 return -1;
00424 else if (this->index_ == ACE_static_cast (ssize_t, this->map_man_->total_size_))
00425 {
00426 this->index_--;
00427 return this->reverse_i ();
00428 }
00429 else if (this->index_ < 0)
00430 return 0;
00431
00432 this->next_ = this->next_->prev_;
00433 if (this->next_ == &this->map_man_->table_[this->index_])
00434 {
00435 while (--this->index_ >= 0)
00436 {
00437 this->next_ = this->map_man_->table_[this->index_].prev_;
00438 if (this->next_ != &this->map_man_->table_[this->index_])
00439 break;
00440 }
00441 }
00442
00443 return this->index_ >= 0;
00444 }
00445
00446
00447
00448 ACE_ALLOC_HOOK_DEFINE(ACE_Hash_Map_Const_Iterator_Base_Ex)
00449
00450 template <class EXT_ID, class INT_ID, class HASH_KEY, class COMPARE_KEYS, class ACE_LOCK> void
00451 ACE_Hash_Map_Const_Iterator_Base_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::dump_i (void) const
00452 {
00453 ACE_TRACE ("ACE_Hash_Map_Const_Iterator_Base_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::dump_i");
00454
00455 ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00456 ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("index_ = %d "), this->index_));
00457 ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("next_ = %x"), this->next_));
00458 ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00459 }
00460
00461 template <class EXT_ID, class INT_ID, class HASH_KEY, class COMPARE_KEYS, class ACE_LOCK> int
00462 ACE_Hash_Map_Const_Iterator_Base_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::forward_i (void)
00463 {
00464 ACE_TRACE ("ACE_Hash_Map_Const_Iterator_Base_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::forward_i");
00465
00466 if (this->map_man_->table_ == 0)
00467 return -1;
00468
00469 else if (this->index_ == -1)
00470 {
00471 this->index_++;
00472 return this->forward_i ();
00473 }
00474 else if (this->index_ >= (ssize_t) this->map_man_->total_size_)
00475 return 0;
00476
00477 this->next_ = this->next_->next_;
00478 if (this->next_ == &this->map_man_->table_[this->index_])
00479 {
00480 while (++this->index_ < (ssize_t) this->map_man_->total_size_)
00481 {
00482 this->next_ = this->map_man_->table_[this->index_].next_;
00483 if (this->next_ != &this->map_man_->table_[this->index_])
00484 break;
00485 }
00486 }
00487
00488 return this->index_ < (ssize_t) this->map_man_->total_size_;
00489 }
00490
00491 template <class EXT_ID, class INT_ID, class HASH_KEY, class COMPARE_KEYS, class ACE_LOCK> int
00492 ACE_Hash_Map_Const_Iterator_Base_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::reverse_i (void)
00493 {
00494 ACE_TRACE ("ACE_Hash_Map_Const_Iterator_Base_Ex<EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK>::reverse_i");
00495
00496 if (this->map_man_->table_ == 0)
00497 return -1;
00498 else if (this->index_ == (ssize_t) this->map_man_->total_size_)
00499 {
00500 this->index_--;
00501 return this->reverse_i ();
00502 }
00503 else if (this->index_ < 0)
00504 return 0;
00505
00506 this->next_ = this->next_->prev_;
00507 if (this->next_ == &this->map_man_->table_[this->index_])
00508 {
00509 while (--this->index_ >= 0)
00510 {
00511 this->next_ = this->map_man_->table_[this->index_].prev_;
00512 if (this->next_ != &this->map_man_->table_[this->index_])
00513 break;
00514 }
00515 }
00516
00517 return this->index_ >= 0;
00518 }
00519
00520 #endif