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