00001
00002
00003 #ifndef ACE_MAP_MANAGER_CPP
00004 #define ACE_MAP_MANAGER_CPP
00005
00006 #include "ace/Map_Manager.h"
00007
00008 #if !defined (ACE_LACKS_PRAGMA_ONCE)
00009 # pragma once
00010 #endif
00011
00012 #include "ace/Malloc_Base.h"
00013
00014 #if !defined (__ACE_INLINE__)
00015 #include "ace/Map_Manager.inl"
00016 #endif
00017
00018 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00019
00020 ACE_ALLOC_HOOK_DEFINE(ACE_Map_Entry)
00021 ACE_ALLOC_HOOK_DEFINE(ACE_Map_Manager)
00022 ACE_ALLOC_HOOK_DEFINE(ACE_Map_Const_Iterator_Base)
00023 ACE_ALLOC_HOOK_DEFINE(ACE_Map_Iterator_Base)
00024 ACE_ALLOC_HOOK_DEFINE(ACE_Map_Const_Iterator)
00025 ACE_ALLOC_HOOK_DEFINE(ACE_Map_Iterator)
00026 ACE_ALLOC_HOOK_DEFINE(ACE_Map_Reverse_Iterator)
00027
00028 template <class EXT_ID, class INT_ID, class ACE_LOCK> int
00029 ACE_Map_Manager<EXT_ID, INT_ID, ACE_LOCK>::open (size_t size,
00030 ACE_Allocator *alloc)
00031 {
00032 ACE_WRITE_GUARD_RETURN (ACE_LOCK, ace_mon, this->lock_, -1);
00033
00034
00035 this->close_i ();
00036
00037
00038 if (alloc == 0)
00039 alloc = ACE_Allocator::instance ();
00040
00041 this->allocator_ = alloc;
00042
00043
00044
00045 ACE_ASSERT (size != 0);
00046
00047
00048
00049
00050
00051
00052
00053
00054 ACE_ASSERT (size <= ACE_UINT32_MAX);
00055
00056
00057
00058 return this->resize_i ((ACE_UINT32) size);
00059 }
00060
00061 template <class EXT_ID, class INT_ID, class ACE_LOCK> int
00062 ACE_Map_Manager<EXT_ID, INT_ID, ACE_LOCK>::close_i (void)
00063 {
00064
00065 this->free_search_structure ();
00066
00067
00068 this->total_size_ = 0;
00069 this->cur_size_ = 0;
00070
00071
00072 this->free_list_.next (this->free_list_id ());
00073 this->free_list_.prev (this->free_list_id ());
00074
00075
00076 this->occupied_list_.next (this->occupied_list_id ());
00077 this->occupied_list_.prev (this->occupied_list_id ());
00078
00079 return 0;
00080 }
00081
00082 template <class EXT_ID, class INT_ID, class ACE_LOCK> int
00083 ACE_Map_Manager<EXT_ID, INT_ID, ACE_LOCK>::bind_i (const EXT_ID &ext_id,
00084 const INT_ID &int_id)
00085 {
00086
00087 ACE_UINT32 slot = 0;
00088 int result = this->find_and_return_index (ext_id,
00089 slot);
00090
00091 if (result == 0)
00092
00093 return 1;
00094 else
00095
00096 return this->shared_bind (ext_id,
00097 int_id);
00098 }
00099
00100 template <class EXT_ID, class INT_ID, class ACE_LOCK> int
00101 ACE_Map_Manager<EXT_ID, INT_ID, ACE_LOCK>::next_free (ACE_UINT32 &free_slot)
00102 {
00103
00104 free_slot = this->free_list_.next ();
00105
00106
00107 if (free_slot != this->free_list_id ())
00108 return 0;
00109
00110 #if defined (ACE_HAS_LAZY_MAP_MANAGER)
00111
00112
00113 this->move_all_free_slots_from_occupied_list ();
00114
00115
00116 free_slot = this->free_list_.next ();
00117
00118
00119 if (free_slot != this->free_list_id ())
00120 return 0;
00121
00122 #endif
00123
00124
00125 int result = this->resize_i (this->new_size ());
00126
00127
00128 if (result == 0)
00129
00130 free_slot = this->free_list_.next ();
00131
00132 return result;
00133 }
00134
00135 #if defined (ACE_HAS_LAZY_MAP_MANAGER)
00136
00137 template <class EXT_ID, class INT_ID, class ACE_LOCK> void
00138 ACE_Map_Manager<EXT_ID, INT_ID, ACE_LOCK>::move_all_free_slots_from_occupied_list (void)
00139 {
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149 for (ACE_UINT32 i = this->occupied_list_.next ();
00150 i != this->occupied_list_id ();
00151 )
00152 {
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163 ACE_Map_Entry<EXT_ID, INT_ID> ¤t_slot = this->search_structure_[i];
00164 ACE_UINT32 position_of_current_slot = i;
00165
00166
00167 i = this->search_structure_[i].next ();
00168
00169
00170 if (current_slot.free_)
00171 {
00172
00173 current_slot.free_ = 0;
00174
00175
00176 this->move_from_occupied_list_to_free_list (position_of_current_slot);
00177 }
00178 }
00179 }
00180
00181 #endif
00182
00183 template <class EXT_ID, class INT_ID, class ACE_LOCK> void
00184 ACE_Map_Manager<EXT_ID, INT_ID, ACE_LOCK>::shared_move (ACE_UINT32 slot,
00185 ACE_Map_Entry<EXT_ID, INT_ID> ¤t_list,
00186 ACE_UINT32 current_list_id,
00187 ACE_Map_Entry<EXT_ID, INT_ID> &new_list,
00188 ACE_UINT32 new_list_id)
00189 {
00190
00191 ENTRY &entry = this->search_structure_[slot];
00192
00193
00194
00195
00196 ACE_UINT32 current_list_prev = entry.prev ();
00197
00198 if (current_list_prev == current_list_id)
00199 current_list.next (entry.next ());
00200 else
00201 this->search_structure_[current_list_prev].next (entry.next ());
00202
00203
00204 ACE_UINT32 current_list_next = entry.next ();
00205
00206 if (current_list_next == current_list_id)
00207 current_list.prev (entry.prev ());
00208 else
00209 this->search_structure_[current_list_next].prev (entry.prev ());
00210
00211
00212
00213
00214 ACE_UINT32 new_list_next = new_list.next ();
00215 entry.next (new_list_next);
00216 entry.prev (new_list_id);
00217
00218
00219 new_list.next (slot);
00220
00221
00222 if (new_list_next == new_list_id)
00223 new_list.prev (slot);
00224 else
00225 this->search_structure_[new_list_next].prev (slot);
00226 }
00227
00228 template <class EXT_ID, class INT_ID, class ACE_LOCK> int
00229 ACE_Map_Manager<EXT_ID, INT_ID, ACE_LOCK>::shared_bind (const EXT_ID &ext_id,
00230 const INT_ID &int_id)
00231 {
00232
00233
00234
00235
00236 ACE_UINT32 slot = 0;
00237 int result = this->next_free (slot);
00238
00239 if (result == 0)
00240 {
00241
00242 this->search_structure_[slot].int_id_ = int_id;
00243 this->search_structure_[slot].ext_id_ = ext_id;
00244
00245
00246 this->move_from_free_list_to_occupied_list (slot);
00247
00248
00249 ++this->cur_size_;
00250 }
00251
00252 return result;
00253 }
00254
00255 template <class EXT_ID, class INT_ID, class ACE_LOCK> int
00256 ACE_Map_Manager<EXT_ID, INT_ID, ACE_LOCK>::rebind_i (const EXT_ID &ext_id,
00257 const INT_ID &int_id,
00258 EXT_ID &old_ext_id,
00259 INT_ID &old_int_id)
00260 {
00261
00262 ACE_UINT32 slot = 0;
00263 int result = this->find_and_return_index (ext_id,
00264 slot);
00265 if (result == 0)
00266 {
00267
00268
00269 ENTRY &ss = this->search_structure_[slot];
00270 old_ext_id = ss.ext_id_;
00271 old_int_id = ss.int_id_;
00272 ss.ext_id_ = ext_id;
00273 ss.int_id_ = int_id;
00274
00275
00276 this->allocator_->sync (&ss, sizeof ss);
00277
00278 return 1;
00279 }
00280 else
00281
00282 return this->shared_bind (ext_id,
00283 int_id);
00284 }
00285
00286 template <class EXT_ID, class INT_ID, class ACE_LOCK> int
00287 ACE_Map_Manager<EXT_ID, INT_ID, ACE_LOCK>::rebind_i (const EXT_ID &ext_id,
00288 const INT_ID &int_id,
00289 INT_ID &old_int_id)
00290 {
00291
00292 ACE_UINT32 slot = 0;
00293 int result = this->find_and_return_index (ext_id,
00294 slot);
00295 if (result == 0)
00296 {
00297
00298
00299 ENTRY &ss = this->search_structure_[slot];
00300 old_int_id = ss.int_id_;
00301 ss.ext_id_ = ext_id;
00302 ss.int_id_ = int_id;
00303
00304
00305 this->allocator_->sync (&ss, sizeof ss);
00306
00307 return 1;
00308 }
00309 else
00310
00311 return this->shared_bind (ext_id,
00312 int_id);
00313 }
00314
00315 template <class EXT_ID, class INT_ID, class ACE_LOCK> int
00316 ACE_Map_Manager<EXT_ID, INT_ID, ACE_LOCK>::rebind_i (const EXT_ID &ext_id,
00317 const INT_ID &int_id)
00318 {
00319
00320 ACE_UINT32 slot = 0;
00321 int result = this->find_and_return_index (ext_id,
00322 slot);
00323 if (result == 0)
00324 {
00325
00326 ENTRY &ss = this->search_structure_[slot];
00327 ss.ext_id_ = ext_id;
00328 ss.int_id_ = int_id;
00329
00330
00331 this->allocator_->sync (&ss, sizeof ss);
00332
00333 return 1;
00334 }
00335 else
00336
00337 return this->shared_bind (ext_id,
00338 int_id);
00339 }
00340
00341 template <class EXT_ID, class INT_ID, class ACE_LOCK> int
00342 ACE_Map_Manager<EXT_ID, INT_ID, ACE_LOCK>::trybind_i (const EXT_ID &ext_id,
00343 INT_ID &int_id)
00344 {
00345
00346 ACE_UINT32 slot = 0;
00347 int result = this->find_and_return_index (ext_id,
00348 slot);
00349 if (result == 0)
00350 {
00351
00352
00353 int_id = this->search_structure_[slot].int_id_;
00354 return 1;
00355 }
00356 else
00357
00358 return this->bind_i (ext_id,
00359 int_id);
00360 }
00361
00362 template <class EXT_ID, class INT_ID, class ACE_LOCK> int
00363 ACE_Map_Manager<EXT_ID, INT_ID, ACE_LOCK>::find_and_return_index (const EXT_ID &ext_id,
00364 ACE_UINT32 &slot)
00365 {
00366
00367 for (ACE_UINT32 i = this->occupied_list_.next ();
00368 i != this->occupied_list_id ();
00369 i = this->search_structure_[i].next ())
00370 {
00371 #if defined (ACE_HAS_LAZY_MAP_MANAGER)
00372 if (this->search_structure_[i].free_)
00373 continue;
00374 #endif
00375
00376 if (this->equal (this->search_structure_[i].ext_id_,
00377 ext_id))
00378 {
00379
00380 slot = i;
00381 return 0;
00382 }
00383 }
00384
00385
00386 return -1;
00387 }
00388
00389 template <class EXT_ID, class INT_ID, class ACE_LOCK> void
00390 ACE_Map_Manager<EXT_ID, INT_ID, ACE_LOCK>::unbind_all (void)
00391 {
00392
00393 for (ACE_UINT32 i = this->occupied_list_.next ();
00394 i != this->occupied_list_id ();
00395 )
00396 {
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407 ACE_Map_Entry<EXT_ID, INT_ID> ¤t_slot =
00408 this->search_structure_[i];
00409 ACE_UINT32 position_of_current_slot = i;
00410
00411
00412 i = current_slot.next ();
00413
00414 #if defined (ACE_HAS_LAZY_MAP_MANAGER)
00415 if (current_slot.free_)
00416 continue;
00417 #endif
00418
00419 this->unbind_slot (position_of_current_slot);
00420 }
00421 }
00422
00423 template <class EXT_ID, class INT_ID, class ACE_LOCK> int
00424 ACE_Map_Manager<EXT_ID, INT_ID, ACE_LOCK>::find_i (const EXT_ID &ext_id,
00425 INT_ID &int_id)
00426 {
00427
00428 ACE_UINT32 slot = 0;
00429 int result = this->find_and_return_index (ext_id,
00430 slot);
00431 if (result == 0)
00432
00433 int_id = this->search_structure_[slot].int_id_;
00434
00435 return result;
00436 }
00437
00438 template <class EXT_ID, class INT_ID, class ACE_LOCK> int
00439 ACE_Map_Manager<EXT_ID, INT_ID, ACE_LOCK>::unbind_and_return_index (const EXT_ID &ext_id,
00440 ACE_UINT32 &slot)
00441 {
00442
00443 int result = this->find_and_return_index (ext_id,
00444 slot);
00445
00446 if (result == 0)
00447 this->unbind_slot (slot);
00448
00449 return result;
00450 }
00451
00452 template <class EXT_ID, class INT_ID, class ACE_LOCK> void
00453 ACE_Map_Manager<EXT_ID, INT_ID, ACE_LOCK>::unbind_slot (ACE_UINT32 slot)
00454 {
00455
00456 #if defined (ACE_HAS_LAZY_MAP_MANAGER)
00457
00458
00459
00460
00461
00462
00463
00464 this->search_structure_[slot].free_ = 1;
00465
00466 #else
00467
00468
00469 this->move_from_occupied_list_to_free_list (slot);
00470
00471 #endif
00472
00473
00474 --this->cur_size_;
00475 }
00476
00477 template <class EXT_ID, class INT_ID, class ACE_LOCK> int
00478 ACE_Map_Manager<EXT_ID, INT_ID, ACE_LOCK>::unbind_i (const EXT_ID &ext_id,
00479 INT_ID &int_id)
00480 {
00481
00482 ACE_UINT32 slot = 0;
00483 int result = this->unbind_and_return_index (ext_id,
00484 slot);
00485 if (result == 0)
00486
00487 int_id = this->search_structure_[slot].int_id_;
00488
00489 return result;
00490 }
00491
00492 template <class EXT_ID, class INT_ID, class ACE_LOCK> int
00493 ACE_Map_Manager<EXT_ID, INT_ID, ACE_LOCK>::resize_i (ACE_UINT32 new_size)
00494 {
00495 ACE_UINT32 i;
00496 ENTRY *temp = 0;
00497
00498
00499 ACE_ALLOCATOR_RETURN (temp,
00500 (ENTRY *) this->allocator_->malloc (new_size * sizeof (ENTRY)),
00501 -1);
00502
00503
00504 for (i = this->occupied_list_.next ();
00505 i != this->occupied_list_id ();
00506 i = this->search_structure_[i].next ())
00507
00508 new (&(temp[i])) ENTRY (this->search_structure_[i]);
00509
00510
00511 for (i = this->free_list_.next ();
00512 i != this->free_list_id ();
00513 i = this->search_structure_[i].next ())
00514
00515 new (&(temp[i])) ENTRY (this->search_structure_[i]);
00516
00517
00518 for (i = this->total_size_; i < new_size; i++)
00519 {
00520
00521
00522
00523 new (&(temp[i])) ENTRY;
00524 temp[i].next (i + 1);
00525 temp[i].prev (i - 1);
00526
00527 #if defined (ACE_HAS_LAZY_MAP_MANAGER)
00528
00529
00530
00531
00532
00533 temp[i].free_ = 0;
00534
00535 #endif
00536
00537 }
00538
00539
00540 this->free_list_.next (this->total_size_);
00541 this->free_list_.prev (new_size - 1);
00542 temp[new_size - 1].next (this->free_list_id ());
00543 temp[this->total_size_].prev (this->free_list_id ());
00544
00545
00546 this->free_search_structure ();
00547 this->total_size_ = new_size;
00548
00549
00550 this->search_structure_ = temp;
00551
00552 return 0;
00553 }
00554
00555 template <class EXT_ID, class INT_ID, class ACE_LOCK> ACE_UINT32
00556 ACE_Map_Manager<EXT_ID, INT_ID, ACE_LOCK>::new_size (void)
00557 {
00558
00559 ACE_UINT32 current_size = this->total_size_;
00560
00561 if (current_size < MAX_EXPONENTIAL)
00562
00563 current_size *= 2;
00564 else
00565
00566 current_size += LINEAR_INCREASE;
00567
00568
00569 return current_size;
00570 }
00571
00572 template <class EXT_ID, class INT_ID, class ACE_LOCK> void
00573 ACE_Map_Manager<EXT_ID, INT_ID, ACE_LOCK>::free_search_structure (void)
00574 {
00575
00576 if (this->search_structure_ != 0)
00577 {
00578 for (ACE_UINT32 i = 0; i < this->total_size_; i++)
00579
00580 {
00581 ENTRY *ss = &this->search_structure_[i];
00582
00583
00584 ACE_DES_FREE_TEMPLATE2 (ss, ACE_NOOP,
00585 ACE_Map_Entry, EXT_ID, INT_ID);
00586 }
00587
00588
00589 this->allocator_->free (this->search_structure_);
00590 this->search_structure_ = 0;
00591 }
00592 }
00593
00594 template <class EXT_ID, class INT_ID> void
00595 ACE_Map_Entry<EXT_ID, INT_ID>::dump (void) const
00596 {
00597 #if defined (ACE_HAS_DUMP)
00598 ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00599 ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("next_ = %d"), this->next_));
00600 ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("prev_ = %d"), this->prev_));
00601
00602 #if defined (ACE_HAS_LAZY_MAP_MANAGER)
00603 ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("free_ = %d"), this->free_));
00604 #endif
00605
00606 ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00607 #endif
00608 }
00609
00610 template <class EXT_ID, class INT_ID, class ACE_LOCK> void
00611 ACE_Map_Manager<EXT_ID, INT_ID, ACE_LOCK>::dump (void) const
00612 {
00613 #if defined (ACE_HAS_DUMP)
00614 ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00615 ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("total_size_ = %d"), this->total_size_));
00616 ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("\ncur_size_ = %d"), this->cur_size_));
00617 this->allocator_->dump ();
00618 this->lock_.dump ();
00619 ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00620 #endif
00621 }
00622
00623 template <class EXT_ID, class INT_ID, class ACE_LOCK> void
00624 ACE_Map_Iterator_Base<EXT_ID, INT_ID, ACE_LOCK>::dump_i (void) const
00625 {
00626 #if defined (ACE_HAS_DUMP)
00627 ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00628 ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("next_ = %d"), this->next_));
00629 ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00630 #endif
00631 }
00632
00633 template <class EXT_ID, class INT_ID, class ACE_LOCK> void
00634 ACE_Map_Const_Iterator_Base<EXT_ID, INT_ID, ACE_LOCK>::dump_i (void) const
00635 {
00636 #if defined (ACE_HAS_DUMP)
00637 ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00638 ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("next_ = %d"), this->next_));
00639 ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00640 #endif
00641 }
00642
00643 template <class EXT_ID, class INT_ID, class ACE_LOCK>
00644 ACE_Map_Entry<EXT_ID, INT_ID>&
00645 ACE_Map_Iterator_Base<EXT_ID, INT_ID, ACE_LOCK>::operator* (void) const
00646 {
00647
00648
00649
00650 ACE_Map_Entry<EXT_ID, INT_ID> *retv = 0;
00651
00652 int result = this->next (retv);
00653 ACE_ASSERT (result != 0);
00654 ACE_UNUSED_ARG (result);
00655
00656 return *retv;
00657 }
00658
00659 template <class EXT_ID, class INT_ID, class ACE_LOCK>
00660 ACE_Map_Entry<EXT_ID, INT_ID>&
00661 ACE_Map_Const_Iterator_Base<EXT_ID, INT_ID, ACE_LOCK>::operator* (void) const
00662 {
00663
00664
00665
00666 ACE_Map_Entry<EXT_ID, INT_ID> *retv = 0;
00667
00668 int result = this->next (retv);
00669 ACE_ASSERT (result != 0);
00670 ACE_UNUSED_ARG (result);
00671
00672 return *retv;
00673 }
00674
00675 template <class EXT_ID, class INT_ID, class ACE_LOCK> void
00676 ACE_Map_Iterator<EXT_ID, INT_ID, ACE_LOCK>::dump (void) const
00677 {
00678 #if defined (ACE_HAS_DUMP)
00679 this->dump_i ();
00680 #endif
00681 }
00682
00683 template <class EXT_ID, class INT_ID, class ACE_LOCK> void
00684 ACE_Map_Const_Iterator<EXT_ID, INT_ID, ACE_LOCK>::dump (void) const
00685 {
00686 #if defined (ACE_HAS_DUMP)
00687 this->dump_i ();
00688 #endif
00689 }
00690
00691 template <class EXT_ID, class INT_ID, class ACE_LOCK> void
00692 ACE_Map_Reverse_Iterator<EXT_ID, INT_ID, ACE_LOCK>::dump (void) const
00693 {
00694 #if defined (ACE_HAS_DUMP)
00695 this->dump_i ();
00696 #endif
00697 }
00698
00699 ACE_END_VERSIONED_NAMESPACE_DECL
00700
00701 #endif