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