1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-socket-set-poll.c - a socket set implemented via _dbus_poll
4 * Copyright © 2011 Nokia Corporation
6 * Licensed under the Academic Free License version 2.1
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
26 #include "dbus-socket-set.h"
28 #include <dbus/dbus-internals.h>
29 #include <dbus/dbus-list.h>
30 #include <dbus/dbus-sysdeps.h>
31 #include <dbus/dbus-watch.h>
33 #ifndef DOXYGEN_SHOULD_SKIP_THIS
43 #define REALLOC_INCREMENT 8
44 #define MINIMUM_SIZE 8
46 /* If we're in the regression tests, force reallocation to happen sooner */
47 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
48 #define DEFAULT_SIZE_HINT 1
50 #define DEFAULT_SIZE_HINT MINIMUM_SIZE
53 static inline DBusSocketSetPoll *
54 socket_set_poll_cast (DBusSocketSet *set)
56 _dbus_assert (set->cls == &_dbus_socket_set_poll_class);
57 return (DBusSocketSetPoll *) set;
60 /* this is safe to call on a partially-allocated socket set */
62 socket_set_poll_free (DBusSocketSet *set)
64 DBusSocketSetPoll *self = socket_set_poll_cast (set);
66 dbus_free (self->fds);
68 _dbus_verbose ("freed socket set %p\n", self);
72 _dbus_socket_set_poll_new (int size_hint)
74 DBusSocketSetPoll *ret;
77 size_hint = DEFAULT_SIZE_HINT;
79 ret = dbus_new0 (DBusSocketSetPoll, 1);
84 ret->parent.cls = &_dbus_socket_set_poll_class;
86 ret->n_allocated = size_hint;
88 ret->fds = dbus_new0 (DBusPollFD, size_hint);
92 /* socket_set_poll_free specifically supports half-constructed
94 socket_set_poll_free ((DBusSocketSet *) ret);
98 _dbus_verbose ("new socket set at %p\n", ret);
99 return (DBusSocketSet *) ret;
103 watch_flags_to_poll_events (unsigned int flags)
107 if (flags & DBUS_WATCH_READABLE)
108 events |= _DBUS_POLLIN;
109 if (flags & DBUS_WATCH_WRITABLE)
110 events |= _DBUS_POLLOUT;
116 socket_set_poll_add (DBusSocketSet *set,
121 DBusSocketSetPoll *self = socket_set_poll_cast (set);
122 #ifndef DBUS_DISABLE_ASSERT
125 for (i = 0; i < self->n_fds; i++)
126 _dbus_assert (!DBUS_POLLABLE_EQUALS (self->fds[i].fd, fd));
129 if (self->n_reserved >= self->n_allocated)
131 DBusPollFD *new_fds = dbus_realloc (self->fds,
132 sizeof (DBusPollFD) * (self->n_allocated + REALLOC_INCREMENT));
134 _dbus_verbose ("inflating set %p from %d en/%d res/%d alloc to %d\n",
135 self, self->n_fds, self->n_reserved, self->n_allocated,
136 self->n_allocated + REALLOC_INCREMENT);
142 self->n_allocated += REALLOC_INCREMENT;
145 _dbus_verbose ("before adding fd %" DBUS_POLLABLE_FORMAT " to %p, %d en/%d res/%d alloc\n",
146 DBUS_POLLABLE_PRINTABLE (fd), self, self->n_fds, self->n_reserved, self->n_allocated);
147 _dbus_assert (self->n_reserved >= self->n_fds);
148 _dbus_assert (self->n_allocated > self->n_reserved);
154 self->fds[self->n_fds].fd = fd;
155 self->fds[self->n_fds].events = watch_flags_to_poll_events (flags);
163 socket_set_poll_enable (DBusSocketSet *set,
167 DBusSocketSetPoll *self = socket_set_poll_cast (set);
170 for (i = 0; i < self->n_fds; i++)
172 if (DBUS_POLLABLE_EQUALS (self->fds[i].fd, fd))
174 self->fds[i].events = watch_flags_to_poll_events (flags);
179 /* we allocated space when the socket was added */
180 _dbus_assert (self->n_fds < self->n_reserved);
181 _dbus_assert (self->n_reserved <= self->n_allocated);
183 self->fds[self->n_fds].fd = fd;
184 self->fds[self->n_fds].events = watch_flags_to_poll_events (flags);
189 socket_set_poll_disable (DBusSocketSet *set,
192 DBusSocketSetPoll *self = socket_set_poll_cast (set);
195 for (i = 0; i < self->n_fds; i++)
197 if (DBUS_POLLABLE_EQUALS (self->fds[i].fd, fd))
199 if (i != self->n_fds - 1)
201 self->fds[i].fd = self->fds[self->n_fds - 1].fd;
202 self->fds[i].events = self->fds[self->n_fds - 1].events;
212 socket_set_poll_remove (DBusSocketSet *set,
215 DBusSocketSetPoll *self = socket_set_poll_cast (set);
217 socket_set_poll_disable (set, fd);
220 _dbus_verbose ("after removing fd %" DBUS_POLLABLE_FORMAT " from %p, %d en/%d res/%d alloc\n",
221 DBUS_POLLABLE_PRINTABLE (fd), self, self->n_fds, self->n_reserved, self->n_allocated);
222 _dbus_assert (self->n_fds <= self->n_reserved);
223 _dbus_assert (self->n_reserved <= self->n_allocated);
225 if (self->n_reserved + MINIMUM_SIZE < self->n_allocated / 2)
227 /* Our array is twice as big as it needs to be - deflate it until it's
228 * only slightly larger than the number reserved. */
229 DBusPollFD *new_fds = dbus_realloc (self->fds,
230 sizeof (DBusPollFD) * (self->n_reserved + MINIMUM_SIZE));
232 _dbus_verbose ("before deflating %p, %d en/%d res/%d alloc\n",
233 self, self->n_fds, self->n_reserved, self->n_allocated);
235 if (_DBUS_UNLIKELY (new_fds == NULL))
237 /* Weird. Oh well, never mind, the too-big array is untouched */
242 self->n_allocated = self->n_reserved;
247 watch_flags_from_poll_revents (short revents)
249 unsigned int condition = 0;
251 if (revents & _DBUS_POLLIN)
252 condition |= DBUS_WATCH_READABLE;
253 if (revents & _DBUS_POLLOUT)
254 condition |= DBUS_WATCH_WRITABLE;
255 if (revents & _DBUS_POLLHUP)
256 condition |= DBUS_WATCH_HANGUP;
257 if (revents & _DBUS_POLLERR)
258 condition |= DBUS_WATCH_ERROR;
260 if (_DBUS_UNLIKELY (revents & _DBUS_POLLNVAL))
261 condition |= _DBUS_WATCH_NVAL;
266 /** This is basically Linux's epoll_wait(2) implemented in terms of poll(2);
267 * it returns results into a caller-supplied buffer so we can be reentrant. */
269 socket_set_poll_poll (DBusSocketSet *set,
270 DBusSocketEvent *revents,
274 DBusSocketSetPoll *self = socket_set_poll_cast (set);
279 _dbus_assert (max_events > 0);
281 for (i = 0; i < self->n_fds; i++)
282 self->fds[i].revents = 0;
284 n_ready = _dbus_poll (self->fds, self->n_fds, timeout_ms);
291 for (i = 0; i < self->n_fds; i++)
293 if (self->fds[i].revents != 0)
295 revents[n_events].fd = self->fds[i].fd;
296 revents[n_events].flags = watch_flags_from_poll_revents (self->fds[i].revents);
300 /* We ignore events beyond max_events because we have nowhere to
301 * put them. _dbus_poll is level-triggered, so we'll just be told
302 * about them next time round the main loop anyway. */
303 if (n_events == max_events)
311 DBusSocketSetClass _dbus_socket_set_poll_class = {
312 socket_set_poll_free,
314 socket_set_poll_remove,
315 socket_set_poll_enable,
316 socket_set_poll_disable,
320 #endif /* !DOXYGEN_SHOULD_SKIP_THIS */