Public Types | Public Member Functions | Public Attributes | Private Types | Private Member Functions | Static Private Member Functions | Private Attributes | Static Private Attributes | Friends

ACE_Handle_Set Class Reference

C++ wrapper facade for the socket fd_set abstraction. More...

#include <Handle_Set.h>

List of all members.

Public Types

enum  { MAXSIZE = ACE_DEFAULT_SELECT_REACTOR_SIZE }

Public Member Functions

 ACE_Handle_Set (void)
 Constructor, initializes the bitmask to all 0s.
 ACE_Handle_Set (const fd_set &mask)
void reset (void)
 Initialize the bitmask to all 0s and reset the associated fields.
int is_set (ACE_HANDLE handle) const
void set_bit (ACE_HANDLE handle)
void clr_bit (ACE_HANDLE handle)
int num_set (void) const
 Returns a count of the number of enabled bits.
ACE_HANDLE max_set (void) const
 Returns the number of the large bit.
void sync (ACE_HANDLE max)
 operator fd_set * ()
fd_set * fdset (void)
void dump (void) const
 Dump the state of an object.

Public Attributes

 ACE_ALLOC_HOOK_DECLARE
 Declare the dynamic allocation hooks.

Private Types

enum  { WORDSIZE = NFDBITS, NUM_WORDS = howmany (MAXSIZE, NFDBITS), NBITS = 256 }

Private Member Functions

void set_max (ACE_HANDLE max)

Static Private Member Functions

static int count_bits (u_long n)

Private Attributes

int size_
 Size of the set, i.e., a count of the number of enabled bits.
ACE_HANDLE max_handle_
 Current max handle.
fd_set mask_
 Bitmask.

Static Private Attributes

static const char nbits_ [NBITS]

Friends

class ACE_Handle_Set_Iterator

Detailed Description

C++ wrapper facade for the socket fd_set abstraction.

This abstraction is a very efficient wrapper facade over fd_set. In particular, no range checking is performed, so it's important not to set or clear bits that are outside the ACE_DEFAULT_SELECT_REACTOR_SIZE.

Definition at line 49 of file Handle_Set.h.


Member Enumeration Documentation

anonymous enum
Enumerator:
MAXSIZE 

Definition at line 56 of file Handle_Set.h.

anonymous enum [private]
Enumerator:
WORDSIZE 
NUM_WORDS 
NBITS 

Definition at line 138 of file Handle_Set.h.

  {
    WORDSIZE = NFDBITS,
#if !defined (ACE_WIN32)
    NUM_WORDS = howmany (MAXSIZE, NFDBITS),
#endif /* ACE_WIN32 */
    NBITS = 256
  };


Constructor & Destructor Documentation

ACE_Handle_Set::ACE_Handle_Set ( void   ) 

Constructor, initializes the bitmask to all 0s.

Definition at line 93 of file Handle_Set.cpp.

{
  ACE_TRACE ("ACE_Handle_Set::ACE_Handle_Set");
  this->reset ();
}

ACE_Handle_Set::ACE_Handle_Set ( const fd_set &  mask  ) 

Constructor, initializes the handle set from a given mask.

Definition at line 99 of file Handle_Set.cpp.

{
  ACE_TRACE ("ACE_Handle_Set::ACE_Handle_Set");
  this->reset ();
  ACE_OS::memcpy ((void *) &this->mask_,
                  (void *) &fd_mask,
                  sizeof this->mask_);
#if !defined (ACE_WIN32)
  this->sync (ACE_Handle_Set::MAXSIZE);
#if defined (ACE_HAS_BIG_FD_SET)
  this->min_handle_ = 0;
#endif /* ACE_HAS_BIG_FD_SET */
#endif /* !ACE_WIN32 */
}


Member Function Documentation

void ACE_Handle_Set::clr_bit ( ACE_HANDLE  handle  )  [inline]

Disables the handle. No range checking is performed so handle must be less than ACE_DEFAULT_SELECT_REACTOR_SIZE.

Definition at line 131 of file Handle_Set.inl.

{
  ACE_TRACE ("ACE_Handle_Set::clr_bit");

  if ((handle != ACE_INVALID_HANDLE) &&
      (this->is_set (handle)))
    {
      FD_CLR ((ACE_SOCKET) handle,
              &this->mask_);
      --this->size_;

#if !defined (ACE_WIN32)
      if (handle == this->max_handle_)
        this->set_max (this->max_handle_);
#endif /* !ACE_WIN32 */
    }
}

int ACE_Handle_Set::count_bits ( u_long  n  )  [static, private]

Counts the number of bits enabled in N. Uses a table lookup to speed up the count.

Definition at line 118 of file Handle_Set.cpp.

{

 ACE_TRACE ("ACE_Handle_Set::count_bits");
#if defined (ACE_HAS_HANDLE_SET_OPTIMIZED_FOR_SELECT)
  register int rval = 0;

  // Count the number of enabled bits in <n>.  This algorithm is very
  // fast, i.e., O(enabled bits in n).

  for (register u_long m = n;
       m != 0;
       m &= m - 1)
    rval++;

  return rval;
#else
   return (ACE_Handle_Set::nbits_[n & 0xff]
          + ACE_Handle_Set::nbits_[(n >> 8) & 0xff]
          + ACE_Handle_Set::nbits_[(n >> 16) & 0xff]
          + ACE_Handle_Set::nbits_[(n >> 24) & 0xff]);
#endif /* ACE_HAS_HANDLE_SET_OPTIMIZED_FOR_SELECT */
}

void ACE_Handle_Set::dump ( void   )  const

Dump the state of an object.

Definition at line 35 of file Handle_Set.cpp.

{
#if defined (ACE_HAS_DUMP)
  ACE_TRACE ("ACE_Handle_Set::dump");

  ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));

  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\nsize_ = %d"), this->size_));
  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\nmax_handle_ = %d"), this->max_handle_));
  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n[ ")));

#if defined (ACE_WIN32)
  for (size_t i = 0; i < (size_t) this->mask_.fd_count + 1; i++)
    ACE_DEBUG ((LM_DEBUG, ACE_TEXT (" %x "), this->mask_.fd_array[i]));
#else /* !ACE_WIN32 */
  for (ACE_HANDLE i = 0; i < this->max_handle_ + 1; i++)
    if (this->is_set (i))
      ACE_DEBUG ((LM_DEBUG, ACE_TEXT (" %d "), i));
#endif /* ACE_WIN32 */

  ACE_DEBUG ((LM_DEBUG, ACE_TEXT (" ]\n")));
  ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
#endif /* ACE_HAS_DUMP */
}

fd_set * ACE_Handle_Set::fdset ( void   )  [inline]

Returns a pointer to the underlying fd_set. Returns 0 if there are no handle bits set (<size_> == 0).

Definition at line 178 of file Handle_Set.inl.

{
  ACE_TRACE ("ACE_Handle_Set::fdset");

  if (this->size_ > 0)
    return (fd_set *) &this->mask_;
  else
    return (fd_set *) 0;
}

int ACE_Handle_Set::is_set ( ACE_HANDLE  handle  )  const [inline]

Checks whether handle is enabled. No range checking is performed so handle must be less than ACE_DEFAULT_SELECT_REACTOR_SIZE.

Definition at line 80 of file Handle_Set.inl.

{
  ACE_TRACE ("ACE_Handle_Set::is_set");
#if defined (ACE_HAS_BIG_FD_SET)
  return FD_ISSET (handle,
                   &this->mask_)
    && this->size_ > 0;
#elif defined (ACE_HAS_NONCONST_FD_ISSET)
  return FD_ISSET (handle,
                   const_cast<fd_set*> (&this->mask_));
#else
  return FD_ISSET (handle,
                   &this->mask_);
#endif /* ACE_HAS_BIG_FD_SET */
}

ACE_HANDLE ACE_Handle_Set::max_set ( void   )  const [inline]

Returns the number of the large bit.

Definition at line 71 of file Handle_Set.inl.

{
  ACE_TRACE ("ACE_Handle_Set::max_set");
  return this->max_handle_;
}

int ACE_Handle_Set::num_set ( void   )  const [inline]

Returns a count of the number of enabled bits.

Definition at line 152 of file Handle_Set.inl.

{
  ACE_TRACE ("ACE_Handle_Set::num_set");
#if defined (ACE_WIN32)
  return this->mask_.fd_count;
#else /* !ACE_WIN32 */
  return this->size_;
#endif /* ACE_WIN32 */
}

ACE_Handle_Set::operator fd_set * (  )  [inline]

Returns a pointer to the underlying fd_set. Returns 0 if there are no handle bits set (<size_> == 0).

Definition at line 165 of file Handle_Set.inl.

{
  ACE_TRACE ("ACE_Handle_Set::operator fd_set *");

  if (this->size_ > 0)
    return (fd_set *) &this->mask_;
  else
    return (fd_set *) 0;
}

void ACE_Handle_Set::reset ( void   )  [inline]

Initialize the bitmask to all 0s and reset the associated fields.

Definition at line 29 of file Handle_Set.inl.

{
  ACE_TRACE ("ACE_Handle_Set::reset");
  this->max_handle_ =
    ACE_INVALID_HANDLE;
#if defined (ACE_HAS_BIG_FD_SET)
  this->min_handle_ =
    NUM_WORDS * WORDSIZE;
#endif /* ACE_HAS_BIG_FD_SET */
  this->size_ = 0;
  // #if !defined (ACE_HAS_BIG_FD_SET)      Why is this here?  -Steve Huston
  FD_ZERO (&this->mask_);
  // #endif /* ACE_HAS_BIG_FD_SET */
}

void ACE_Handle_Set::set_bit ( ACE_HANDLE  handle  )  [inline]

Enables the handle. No range checking is performed so handle must be less than ACE_DEFAULT_SELECT_REACTOR_SIZE.

Definition at line 99 of file Handle_Set.inl.

{
  ACE_TRACE ("ACE_Handle_Set::set_bit");
  if ((handle != ACE_INVALID_HANDLE)
      && (!this->is_set (handle)))
    {
#if defined (ACE_WIN32)
      FD_SET ((SOCKET) handle,
              &this->mask_);
      ++this->size_;
#else /* ACE_WIN32 */
#if defined (ACE_HAS_BIG_FD_SET)
      if (this->size_ == 0)
        FD_ZERO (&this->mask_);

      if (handle < this->min_handle_)
        this->min_handle_ = handle;
#endif /* ACE_HAS_BIG_FD_SET */

      FD_SET (handle,
              &this->mask_);
      ++this->size_;

      if (handle > this->max_handle_)
        this->max_handle_ = handle;
#endif /* ACE_WIN32 */
    }
}

void ACE_Handle_Set::set_max ( ACE_HANDLE  max  )  [private]

Resets the <max_handle_> after a clear of the original <max_handle_>.

Definition at line 213 of file Handle_Set.cpp.

{
  ACE_TRACE ("ACE_Handle_Set::set_max");
#if !defined(ACE_WIN32)
  fd_mask * maskp = (fd_mask *)(this->mask_.fds_bits);

  if (this->size_ == 0)
    this->max_handle_ = ACE_INVALID_HANDLE;
  else
    {
      int i;

      for (i = ACE_DIV_BY_WORDSIZE (current_max - 1);
           maskp[i] == 0;
           i--)
        continue;
#if defined (ACE_TANDEM_NSK_BIT_ORDER)
      // bits are in reverse order, MSB (sign bit) = bit 0.
      this->max_handle_ = ACE_MULT_BY_WORDSIZE (i);
      for (fd_mask val = maskp[i];
           (val & ACE_MSB_MASK) != 0;
           val = (val << 1))
        ++this->max_handle_;
#elif 1 /* !defined(ACE_HAS_BIG_FD_SET) */
      this->max_handle_ = ACE_MULT_BY_WORDSIZE (i);
      for (fd_mask val = maskp[i];
           (val & ~1) != 0; // This obscure code is needed since "bit 0" is in location 1...
           val = (val >> 1) & ACE_MSB_MASK)
        ++this->max_handle_;
#else
      register u_long val = this->mask_.fds_bits[i];
      this->max_handle_ = ACE_MULT_BY_WORDSIZE (i)
        + ACE_Handle_Set::bitpos(val & ~(val - 1));
#endif /* 1 */
    }

  // Do some sanity checking...
  if (this->max_handle_ >= ACE_Handle_Set::MAXSIZE)
    this->max_handle_ = ACE_Handle_Set::MAXSIZE - 1;
#else
  ACE_UNUSED_ARG (current_max);
#endif /* !ACE_WIN32 */
}

void ACE_Handle_Set::sync ( ACE_HANDLE  max  ) 

Rescan the underlying fd_set up to handle max to find the new <max_handle> (highest bit set) and <size> (how many bits set) values. This is useful for evaluating the changes after the handle set has been manipulated in some way other than member functions; for example, after <select> modifies the fd_set.

Definition at line 192 of file Handle_Set.cpp.

{
  ACE_TRACE ("ACE_Handle_Set::sync");
#if !defined (ACE_WIN32)
  fd_mask *maskp = (fd_mask *)(this->mask_.fds_bits);
  this->size_ = 0;

  for (int i = ACE_DIV_BY_WORDSIZE (max - 1);
       i >= 0;
       i--)
    this->size_ += ACE_Handle_Set::count_bits (maskp[i]);

  this->set_max (max);
#else
  ACE_UNUSED_ARG (max);
#endif /* !ACE_WIN32 */
}


Friends And Related Function Documentation

friend class ACE_Handle_Set_Iterator [friend]

Definition at line 52 of file Handle_Set.h.


Member Data Documentation

Declare the dynamic allocation hooks.

Definition at line 121 of file Handle_Set.h.

fd_set ACE_Handle_Set::mask_ [private]

Bitmask.

Definition at line 136 of file Handle_Set.h.

ACE_HANDLE ACE_Handle_Set::max_handle_ [private]

Current max handle.

Definition at line 128 of file Handle_Set.h.

const char ACE_Handle_Set::nbits_ [static, private]
Initial value:
{
  0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
  1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8}

Table that maps bytes to counts of the enabled bits in each value from 0 to 255.

Definition at line 162 of file Handle_Set.h.

int ACE_Handle_Set::size_ [private]

Size of the set, i.e., a count of the number of enabled bits.

Definition at line 125 of file Handle_Set.h.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines