+void
+ev_feed_event (EV_P_ void *w, int revents)
+{
+ W w_ = (W)w;
+
+ if (w_->pending)
+ {
+ pendings [ABSPRI (w_)][w_->pending - 1].events |= revents;
+ return;
+ }
+
+ w_->pending = ++pendingcnt [ABSPRI (w_)];
+ array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], EMPTY2);
+ pendings [ABSPRI (w_)][w_->pending - 1].w = w_;
+ pendings [ABSPRI (w_)][w_->pending - 1].events = revents;
+}
+
+static void
+queue_events (EV_P_ W *events, int eventcnt, int type)
+{
+ int i;
+
+ for (i = 0; i < eventcnt; ++i)
+ ev_feed_event (EV_A_ events [i], type);
+}
+
+inline void
+fd_event (EV_P_ int fd, int revents)
+{
+ ANFD *anfd = anfds + fd;
+ struct ev_io *w;
+
+ for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
+ {
+ int ev = w->events & revents;
+
+ if (ev)
+ ev_feed_event (EV_A_ (W)w, ev);
+ }
+}
+
+void
+ev_feed_fd_event (EV_P_ int fd, int revents)
+{
+ fd_event (EV_A_ fd, revents);
+}
+
+/*****************************************************************************/
+
+static void
+fd_reify (EV_P)
+{
+ int i;
+
+ for (i = 0; i < fdchangecnt; ++i)
+ {
+ int fd = fdchanges [i];
+ ANFD *anfd = anfds + fd;
+ struct ev_io *w;
+
+ int events = 0;
+
+ for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
+ events |= w->events;
+
+#if EV_SELECT_IS_WINSOCKET
+ if (events)
+ {
+ unsigned long argp;
+ anfd->handle = _get_osfhandle (fd);
+ assert (("libev only supports socket fds in this configuration", ioctlsocket (anfd->handle, FIONREAD, &argp) == 0));
+ }
+#endif
+
+ anfd->reify = 0;
+
+ method_modify (EV_A_ fd, anfd->events, events);
+ anfd->events = events;
+ }
+
+ fdchangecnt = 0;
+}
+
+static void
+fd_change (EV_P_ int fd)
+{
+ if (anfds [fd].reify)
+ return;
+
+ anfds [fd].reify = 1;
+
+ ++fdchangecnt;
+ array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2);
+ fdchanges [fdchangecnt - 1] = fd;
+}
+
+static void
+fd_kill (EV_P_ int fd)
+{
+ struct ev_io *w;
+
+ while ((w = (struct ev_io *)anfds [fd].head))
+ {
+ ev_io_stop (EV_A_ w);
+ ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
+ }
+}
+
+static int
+fd_valid (int fd)
+{
+#ifdef _WIN32
+ return _get_osfhandle (fd) != -1;
+#else
+ return fcntl (fd, F_GETFD) != -1;
+#endif
+}
+
+/* called on EBADF to verify fds */
+static void
+fd_ebadf (EV_P)
+{
+ int fd;
+
+ for (fd = 0; fd < anfdmax; ++fd)
+ if (anfds [fd].events)
+ if (!fd_valid (fd) == -1 && errno == EBADF)
+ fd_kill (EV_A_ fd);
+}
+
+/* called on ENOMEM in select/poll to kill some fds and retry */
+static void
+fd_enomem (EV_P)
+{
+ int fd;
+
+ for (fd = anfdmax; fd--; )
+ if (anfds [fd].events)
+ {
+ fd_kill (EV_A_ fd);
+ return;
+ }
+}
+
+/* usually called after fork if method needs to re-arm all fds from scratch */
+static void
+fd_rearm_all (EV_P)
+{
+ int fd;
+
+ /* this should be highly optimised to not do anything but set a flag */
+ for (fd = 0; fd < anfdmax; ++fd)
+ if (anfds [fd].events)
+ {
+ anfds [fd].events = 0;
+ fd_change (EV_A_ fd);
+ }
+}
+
+/*****************************************************************************/
+
+static void
+upheap (WT *heap, int k)
+{
+ WT w = heap [k];
+
+ while (k && heap [k >> 1]->at > w->at)
+ {
+ heap [k] = heap [k >> 1];
+ ((W)heap [k])->active = k + 1;
+ k >>= 1;
+ }
+
+ heap [k] = w;
+ ((W)heap [k])->active = k + 1;
+
+}
+
+static void
+downheap (WT *heap, int N, int k)
+{
+ WT w = heap [k];
+
+ while (k < (N >> 1))
+ {
+ int j = k << 1;
+
+ if (j + 1 < N && heap [j]->at > heap [j + 1]->at)
+ ++j;
+
+ if (w->at <= heap [j]->at)
+ break;
+
+ heap [k] = heap [j];
+ ((W)heap [k])->active = k + 1;
+ k = j;
+ }
+
+ heap [k] = w;
+ ((W)heap [k])->active = k + 1;
+}
+
+inline void
+adjustheap (WT *heap, int N, int k)
+{
+ upheap (heap, k);
+ downheap (heap, N, k);
+}
+
+/*****************************************************************************/
+