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