00001
00002
00003
00004 #include "ace/Handle_Set.h"
00005
00006 #if !defined (__ACE_INLINE__)
00007 #include "ace/Handle_Set.inl"
00008 #endif
00009
00010 #include "ace/OS_NS_string.h"
00011
00012 ACE_RCSID(ace, Handle_Set, "$Id: Handle_Set.cpp 83306 2008-10-17 12:19:53Z johnnyw $")
00013
00014 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00015
00016 ACE_ALLOC_HOOK_DEFINE(ACE_Handle_Set)
00017
00018
00019
00020 #if defined (ACE_WIN32)
00021
00022 # define ACE_MSB_MASK (~(1 << (NFDBITS - 1)))
00023 #else
00024 # define ACE_MSB_MASK (~((fd_mask) 1 << (NFDBITS - 1)))
00025 #endif
00026
00027 #if defined (linux) && __GLIBC__ > 1 && __GLIBC_MINOR__ >= 1 && !defined (_XOPEN_SOURCE)
00028
00029
00030
00031 #define fds_bits __fds_bits
00032 #endif
00033
00034 void
00035 ACE_Handle_Set::dump (void) const
00036 {
00037 #if defined (ACE_HAS_DUMP)
00038 ACE_TRACE ("ACE_Handle_Set::dump");
00039
00040 ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00041
00042 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\nsize_ = %d"), this->size_));
00043 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\nmax_handle_ = %d"), this->max_handle_));
00044 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n[ ")));
00045
00046 #if defined (ACE_WIN32)
00047 for (size_t i = 0; i < (size_t) this->mask_.fd_count + 1; i++)
00048 ACE_DEBUG ((LM_DEBUG, ACE_TEXT (" %x "), this->mask_.fd_array[i]));
00049 #else
00050 for (ACE_HANDLE i = 0; i < this->max_handle_ + 1; i++)
00051 if (this->is_set (i))
00052 ACE_DEBUG ((LM_DEBUG, ACE_TEXT (" %d "), i));
00053 #endif
00054
00055 ACE_DEBUG ((LM_DEBUG, ACE_TEXT (" ]\n")));
00056 ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00057 #endif
00058 }
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072 const char ACE_Handle_Set::nbits_[256] =
00073 {
00074 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
00075 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
00076 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
00077 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
00078 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
00079 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
00080 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
00081 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
00082 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
00083 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
00084 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
00085 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
00086 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
00087 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
00088 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
00089 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8};
00090
00091
00092
00093 ACE_Handle_Set::ACE_Handle_Set (void)
00094 {
00095 ACE_TRACE ("ACE_Handle_Set::ACE_Handle_Set");
00096 this->reset ();
00097 }
00098
00099 ACE_Handle_Set::ACE_Handle_Set (const fd_set &fd_mask)
00100 {
00101 ACE_TRACE ("ACE_Handle_Set::ACE_Handle_Set");
00102 this->reset ();
00103 ACE_OS::memcpy ((void *) &this->mask_,
00104 (void *) &fd_mask,
00105 sizeof this->mask_);
00106 #if !defined (ACE_WIN32)
00107 this->sync (ACE_Handle_Set::MAXSIZE);
00108 #if defined (ACE_HAS_BIG_FD_SET)
00109 this->min_handle_ = 0;
00110 #endif
00111 #endif
00112 }
00113
00114
00115
00116
00117 int
00118 ACE_Handle_Set::count_bits (u_long n)
00119 {
00120
00121 ACE_TRACE ("ACE_Handle_Set::count_bits");
00122 #if defined (ACE_HAS_HANDLE_SET_OPTIMIZED_FOR_SELECT)
00123 register int rval = 0;
00124
00125
00126
00127
00128 for (register u_long m = n;
00129 m != 0;
00130 m &= m - 1)
00131 rval++;
00132
00133 return rval;
00134 #else
00135 return (ACE_Handle_Set::nbits_[n & 0xff]
00136 + ACE_Handle_Set::nbits_[(n >> 8) & 0xff]
00137 + ACE_Handle_Set::nbits_[(n >> 16) & 0xff]
00138 + ACE_Handle_Set::nbits_[(n >> 24) & 0xff]);
00139 #endif
00140 }
00141
00142 #if defined (ACE_HAS_BIG_FD_SET)
00143
00144
00145
00146 int
00147 ACE_Handle_Set::bitpos (u_long bit)
00148 {
00149 register int l = 0;
00150 register u_long n = bit - 1;
00151
00152
00153
00154 while (n >> 8)
00155 {
00156 n >>= 8;
00157 l += 8;
00158 }
00159
00160
00161 if (n & 16)
00162 {
00163 n >>= 4;
00164 l += 4;
00165 }
00166
00167
00168 while (n != 0)
00169 {
00170 n &= n - 1;
00171 l++;
00172 }
00173 return l;
00174 }
00175 #endif
00176
00177
00178
00179 #if defined (ACE_USE_SHIFT_FOR_EFFICIENCY)
00180
00181
00182
00183
00184 #define ACE_DIV_BY_WORDSIZE(x) ((x) >> ((int) ACE_Handle_Set::WORDSIZE))
00185 #define ACE_MULT_BY_WORDSIZE(x) ((x) << ((int) ACE_Handle_Set::WORDSIZE))
00186 #else
00187 #define ACE_DIV_BY_WORDSIZE(x) ((x) / ((int) ACE_Handle_Set::WORDSIZE))
00188 #define ACE_MULT_BY_WORDSIZE(x) ((x) * ((int) ACE_Handle_Set::WORDSIZE))
00189 #endif
00190
00191 void
00192 ACE_Handle_Set::sync (ACE_HANDLE max)
00193 {
00194 ACE_TRACE ("ACE_Handle_Set::sync");
00195 #if !defined (ACE_WIN32)
00196 fd_mask *maskp = (fd_mask *)(this->mask_.fds_bits);
00197 this->size_ = 0;
00198
00199 for (int i = ACE_DIV_BY_WORDSIZE (max - 1);
00200 i >= 0;
00201 i--)
00202 this->size_ += ACE_Handle_Set::count_bits (maskp[i]);
00203
00204 this->set_max (max);
00205 #else
00206 ACE_UNUSED_ARG (max);
00207 #endif
00208 }
00209
00210
00211
00212 void
00213 ACE_Handle_Set::set_max (ACE_HANDLE current_max)
00214 {
00215 ACE_TRACE ("ACE_Handle_Set::set_max");
00216 #if !defined(ACE_WIN32)
00217 fd_mask * maskp = (fd_mask *)(this->mask_.fds_bits);
00218
00219 if (this->size_ == 0)
00220 this->max_handle_ = ACE_INVALID_HANDLE;
00221 else
00222 {
00223 int i;
00224
00225 for (i = ACE_DIV_BY_WORDSIZE (current_max - 1);
00226 maskp[i] == 0;
00227 i--)
00228 continue;
00229 #if defined (ACE_TANDEM_NSK_BIT_ORDER)
00230
00231 this->max_handle_ = ACE_MULT_BY_WORDSIZE (i);
00232 for (fd_mask val = maskp[i];
00233 (val & ACE_MSB_MASK) != 0;
00234 val = (val << 1))
00235 ++this->max_handle_;
00236 #elif 1
00237 this->max_handle_ = ACE_MULT_BY_WORDSIZE (i);
00238 for (fd_mask val = maskp[i];
00239 (val & ~1) != 0;
00240 val = (val >> 1) & ACE_MSB_MASK)
00241 ++this->max_handle_;
00242 #else
00243 register u_long val = this->mask_.fds_bits[i];
00244 this->max_handle_ = ACE_MULT_BY_WORDSIZE (i)
00245 + ACE_Handle_Set::bitpos(val & ~(val - 1));
00246 #endif
00247 }
00248
00249
00250 if (this->max_handle_ >= ACE_Handle_Set::MAXSIZE)
00251 this->max_handle_ = ACE_Handle_Set::MAXSIZE - 1;
00252 #else
00253 ACE_UNUSED_ARG (current_max);
00254 #endif
00255 }
00256
00257 ACE_ALLOC_HOOK_DEFINE(ACE_Handle_Set_Iterator)
00258
00259 void
00260 ACE_Handle_Set_Iterator::dump (void) const
00261 {
00262 #if defined (ACE_HAS_DUMP)
00263 ACE_TRACE ("ACE_Handle_Set_Iterator::dump");
00264
00265 ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00266 #if defined(ACE_WIN32) || !defined(ACE_HAS_BIG_FD_SET)
00267 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\nhandle_index_ = %d"), this->handle_index_));
00268 #elif defined(ACE_HAS_BIG_FD_SET)
00269 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\nword_max_ = %d"), this->word_max_));
00270 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\nword_val_ = %d"), this->word_val_));
00271 #endif
00272 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\nword_num_ = %d"), this->word_num_));
00273 ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00274 #endif
00275 }
00276
00277 ACE_HANDLE
00278 ACE_Handle_Set_Iterator::operator () (void)
00279 {
00280 ACE_TRACE ("ACE_Handle_Set_Iterator::operator");
00281 #if defined (ACE_WIN32)
00282 if (this->handle_index_ < this->handles_.mask_.fd_count)
00283
00284 return (ACE_HANDLE) this->handles_.mask_.fd_array[this->handle_index_++];
00285 else
00286 return ACE_INVALID_HANDLE;
00287
00288 #elif !defined (ACE_HAS_BIG_FD_SET)
00289
00290 ACE_HANDLE maxhandlep1 = this->handles_.max_handle_ + 1;
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302 fd_mask * maskp = (fd_mask *)(this->handles_.mask_.fds_bits);
00303
00304 if (this->handle_index_ >= maxhandlep1)
00305
00306
00307 return ACE_INVALID_HANDLE;
00308 else
00309 {
00310 ACE_HANDLE result = this->handle_index_;
00311
00312
00313
00314 this->handle_index_++;
00315 #if defined (ACE_TANDEM_NSK_BIT_ORDER)
00316
00317 this->word_val_ = (this->word_val_ << 1);
00318 # else
00319 this->word_val_ = (this->word_val_ >> 1) & ACE_MSB_MASK;
00320 # endif
00321
00322
00323
00324
00325 if (this->word_val_ == 0)
00326 {
00327
00328
00329
00330
00331 for (this->handle_index_ = ACE_MULT_BY_WORDSIZE(++this->word_num_);
00332 this->handle_index_ < maxhandlep1
00333 && maskp[this->word_num_] == 0;
00334 this->word_num_++)
00335 this->handle_index_ += ACE_Handle_Set::WORDSIZE;
00336
00337
00338
00339
00340
00341
00342 if (this->handle_index_ >= maxhandlep1)
00343 {
00344 this->handle_index_ = maxhandlep1;
00345 return result;
00346 }
00347 else
00348
00349 this->word_val_ = maskp[this->word_num_];
00350 }
00351
00352
00353
00354
00355
00356
00357 #if defined (ACE_TANDEM_NSK_BIT_ORDER)
00358
00359 for (;
00360 this->word_val_ > 0;
00361 this->word_val_ = (this->word_val_ << 1))
00362 this->handle_index_++;
00363 # else
00364 for (;
00365 ACE_BIT_DISABLED (this->word_val_, 1);
00366 this->handle_index_++)
00367 this->word_val_ = (this->word_val_ >> 1) & ACE_MSB_MASK;
00368 # endif
00369
00370 return result;
00371 }
00372 #else
00373
00374 register u_long lsb = this->word_val_;
00375
00376 if (lsb == 0)
00377 {
00378 do
00379 {
00380
00381 if (++this->word_num_ >= this->word_max_)
00382 return ACE_INVALID_HANDLE;
00383
00384 lsb = this->handles_.mask_.fds_bits[this->word_num_];
00385 }
00386 while (lsb == 0);
00387
00388
00389 this->handle_index_ = ACE_MULT_BY_WORDSIZE (this->word_num_);
00390
00391
00392 this->word_val_ = lsb;
00393
00394
00395 lsb &= ~(lsb - 1);
00396
00397
00398 this->word_val_ ^= lsb;
00399
00400
00401 this->oldlsb_ = lsb;
00402
00403
00404 while (lsb >>= 1)
00405 this->handle_index_++;
00406 }
00407 else
00408 {
00409
00410 lsb &= ~(lsb - 1);
00411
00412
00413 this->word_val_ ^= lsb;
00414
00415 register u_long n = lsb - this->oldlsb_;
00416
00417
00418 do
00419 {
00420 this->handle_index_++;
00421 n &= n >> 1;
00422 }
00423 while (n != 0);
00424
00425 this->oldlsb_ = lsb;
00426 }
00427
00428 return this->handle_index_;
00429 #endif
00430 }
00431
00432 ACE_Handle_Set_Iterator::ACE_Handle_Set_Iterator (const ACE_Handle_Set &hs)
00433 : handles_ (hs),
00434 #if !defined (ACE_HAS_BIG_FD_SET) || defined (ACE_WIN32)
00435 handle_index_ (0),
00436 word_num_ (-1)
00437 #elif defined (ACE_HAS_BIG_FD_SET)
00438 oldlsb_ (0),
00439 word_max_ (hs.max_handle_ == ACE_INVALID_HANDLE
00440 ? 0
00441 : ((ACE_DIV_BY_WORDSIZE (hs.max_handle_)) + 1))
00442 #endif
00443 {
00444 ACE_TRACE ("ACE_Handle_Set_Iterator::ACE_Handle_Set_Iterator");
00445 #if !defined (ACE_WIN32) && !defined (ACE_HAS_BIG_FD_SET)
00446
00447 ACE_HANDLE maxhandlep1 =
00448 this->handles_.max_handle_ + 1;
00449
00450 fd_mask *maskp =
00451 (fd_mask *)(this->handles_.mask_.fds_bits);
00452
00453
00454
00455 while (this->handle_index_ < maxhandlep1
00456 && maskp[++this->word_num_] == 0)
00457 this->handle_index_ += ACE_Handle_Set::WORDSIZE;
00458
00459
00460
00461
00462
00463 if (this->handle_index_ >= maxhandlep1)
00464 this->handle_index_ = maxhandlep1;
00465 else
00466
00467
00468
00469 #if defined (ACE_TANDEM_NSK_BIT_ORDER)
00470
00471 for (this->word_val_ = maskp[this->word_num_];
00472 this->word_val_ > 0;
00473 this->word_val_ = (this->word_val_ << 1))
00474 this->handle_index_++;
00475 # else
00476 for (this->word_val_ = maskp[this->word_num_];
00477 ACE_BIT_DISABLED (this->word_val_, 1)
00478 && this->handle_index_ < maxhandlep1;
00479 this->handle_index_++)
00480 this->word_val_ = (this->word_val_ >> 1) & ACE_MSB_MASK;
00481 # endif
00482 #elif !defined (ACE_WIN32) && defined (ACE_HAS_BIG_FD_SET)
00483 if (this->word_max_==0)
00484 {
00485 this->word_num_ = -1;
00486 this->word_val_ = 0;
00487 }
00488 else
00489 {
00490 this->word_num_ =
00491 ACE_DIV_BY_WORDSIZE (this->handles_.min_handle_) - 1;
00492 this->word_val_ = 0;
00493 }
00494 #endif
00495 }
00496
00497
00498 void
00499 ACE_Handle_Set_Iterator::reset_state (void)
00500 {
00501 ACE_TRACE ("ACE_Handle_Set_Iterator::reset_state");
00502
00503 #if !defined (ACE_HAS_BIG_FD_SET) || defined (ACE_WIN32)
00504 this->handle_index_ = 0;
00505 this->word_num_ = -1;
00506 #elif defined (ACE_HAS_BIG_FD_SET)
00507 this->oldlsb_ = 0;
00508 this->word_max_ =
00509 this->handles_.max_handle_ == ACE_INVALID_HANDLE ? 0
00510 : ((ACE_DIV_BY_WORDSIZE (this->handles_.max_handle_)) + 1);
00511 #endif
00512
00513 #if !defined (ACE_WIN32) && !defined (ACE_HAS_BIG_FD_SET)
00514
00515 ACE_HANDLE maxhandlep1 =
00516 this->handles_.max_handle_ + 1;
00517
00518 fd_mask *maskp =
00519 (fd_mask *)(this->handles_.mask_.fds_bits);
00520
00521
00522
00523 while (this->handle_index_ < maxhandlep1
00524 && maskp[++this->word_num_] == 0)
00525 this->handle_index_ += ACE_Handle_Set::WORDSIZE;
00526
00527
00528
00529
00530
00531 if (this->handle_index_ >= maxhandlep1)
00532 this->handle_index_ = maxhandlep1;
00533 else
00534
00535
00536
00537 #if defined (ACE_TANDEM_NSK_BIT_ORDER)
00538
00539 for (this->word_val_ = maskp[this->word_num_];
00540 this->word_val_ > 0;
00541 this->word_val_ = (this->word_val_ << 1))
00542 this->handle_index_++;
00543 # else
00544 for (this->word_val_ = maskp[this->word_num_];
00545 ACE_BIT_DISABLED (this->word_val_, 1)
00546 && this->handle_index_ < maxhandlep1;
00547 this->handle_index_++)
00548 this->word_val_ = (this->word_val_ >> 1) & ACE_MSB_MASK;
00549 # endif
00550 #elif !defined (ACE_WIN32) && defined (ACE_HAS_BIG_FD_SET)
00551 if (this->word_max_==0)
00552 {
00553 this->word_num_ = -1;
00554 this->word_val_ = 0;
00555 }
00556 else
00557 {
00558 this->word_num_ =
00559 ACE_DIV_BY_WORDSIZE (this->handles_.min_handle_) - 1;
00560 this->word_val_ = 0;
00561 }
00562 #endif
00563 }
00564
00565 ACE_END_VERSIONED_NAMESPACE_DECL