lib/poll.cpp
// this function returns the number of pollfd elements inserted in
// out_table.  The calling code should allocate an uninitialised
// bytevector of size sizeof(pollfd) times the sum of the number of
// file descriptors in readfds and writefds.  The a_sync_sizeof_pollfd
// function is available to help with this.  If there is both a read
// and write watch on the same descriptor then we would waste a small
// amount of space in allocating the bytevector, but that is uncommon
// and not worth troubling ourselves over - this approach results in a
// nice clean interface for the chez FFI without having to worry about
// memory management.  Since the number of pollfd elements is
// returned, if the user really is worried she can always copy
// out_table to a smaller bytevector herself and dispose of out_table
// after doing so.  (Note, we cannot return an array of pollfd's
// directly, from which chez scheme's FFI can construct a bytevector
// for us, because the first NULL byte encountered will be treated by
// the FFI as terminating the array.)
this function returns the number of pollfd elements inserted in
out_table.  The calling code should allocate an uninitialised
bytevector of size sizeof(pollfd) times the sum of the number of
file descriptors in readfds and writefds.  The a_sync_sizeof_pollfd
function is available to help with this.  If there is both a read
and write watch on the same descriptor then we would waste a small
amount of space in allocating the bytevector, but that is uncommon
and not worth troubling ourselves over - this approach results in a
nice clean interface for the chez FFI without having to worry about
memory management.  Since the number of pollfd elements is
returned, if the user really is worried she can always copy
out_table to a smaller bytevector herself and dispose of out_table
after doing so.  (Note, we cannot return an array of pollfd's
directly, from which chez scheme's FFI can construct a bytevector
for us, because the first NULL byte encountered will be treated by
the FFI as terminating the array.)
this function returns the number of pollfd elements inserted in out_table . The calling code should allocate an uninitialised bytevector of size sizeof ( pollfd ) times the sum of the number of file descriptors in readfds and writefds . The a_sync_sizeof_pollfd function is available to help with this . If there is both a read and write watch on the same descriptor then we would waste a small amount of space in allocating the bytevector but that is uncommon and not worth troubling ourselves over - this approach results in a nice clean interface for the chez FFI without having to worry about memory management . Since the number of pollfd elements is returned if the user really is worried she can always copy out_table to a smaller bytevector herself and dispose of out_table after doing so . ( Note we cannot return an array of pollfd ' s directly from which chez scheme ' s FFI can construct a bytevector for us because the first NULL byte encountered will be treated by the FFI as terminating the array . )
int a_sync_make_poll_table(const int32_t* readfds, const int32_t* writefds,
			   pollfd* out_table) {
  std::map<int, pollfd> fd_map;
  if (readfds) {
    for (int index = 0; readfds[index] != -1; ++index) {
      int fd = readfds[index];
      pollfd elem;
      elem.fd = fd;
      elem.events = POLLIN | POLLPRI;
      elem.revents = 0;
      fd_map.emplace(fd, elem);
    }
  }
  if (writefds) {
    for (int index = 0; writefds[index] != -1; ++index) {
      int fd = writefds[index];
      pollfd elem;
      elem.fd = fd;
      elem.events = POLLOUT;
      elem.revents = 0;
      auto res = fd_map.emplace(fd, elem);
      if (!res.second)
	res.first->second.events |= POLLOUT;
    }
  }
  int index = 0;
  for (auto&& elt: fd_map) {
    out_table[index++] = elt.second;
  }
  return index;
}
int a_sync_make_poll_table ( const int32_t * readfds , const int32_t * writefds , pollfd * out_table ) { std :: map < int , pollfd > fd_map ; if ( readfds ) { for ( int index = 0 ; readfds [ index ] != -1 ; ++ index ) { int fd = readfds [ index ] ; pollfd elem ; elem . fd = fd ; elem . events = POLLIN | POLLPRI ; elem . revents = 0 ; fd_map . emplace ( fd , elem ) ; } } if ( writefds ) { for ( int index = 0 ; writefds [ index ] != -1 ; ++ index ) { int fd = writefds [ index ] ; pollfd elem ; elem . fd = fd ; elem . events = POLLOUT ; elem . revents = 0 ; auto res = fd_map . emplace ( fd , elem ) ; if ( ! res . second ) res . first -> second . events |= POLLOUT ; } } int index = 0 ; for ( auto && elt : fd_map ) { out_table [ index ++ ] = elt . second ; } return index ; }
this function returns the number of pollfd elements inserted in
out_table.
this function returns the number of pollfd elements inserted in out_table .
// if there are duplicates we can let a duplicate fall aside // if we already have the descriptor in the map because it also // appeared in readfds, we just need to add POLLOUT to the watch // now fill the output table