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