26#include "dbus-pollable-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
36 DBusPollableSet parent;
43#define REALLOC_INCREMENT 8
47#ifdef DBUS_ENABLE_EMBEDDED_TESTS
48#define DEFAULT_SIZE_HINT 1
50#define DEFAULT_SIZE_HINT MINIMUM_SIZE
53static inline DBusPollableSetPoll *
54socket_set_poll_cast (DBusPollableSet *set)
56 _dbus_assert (set->cls == &_dbus_pollable_set_poll_class);
57 return (DBusPollableSetPoll *) set;
62socket_set_poll_free (DBusPollableSet *set)
64 DBusPollableSetPoll *self = socket_set_poll_cast (set);
68 _dbus_verbose (
"freed socket set %p\n", self);
72_dbus_pollable_set_poll_new (
int size_hint)
74 DBusPollableSetPoll *ret;
77 size_hint = DEFAULT_SIZE_HINT;
84 ret->parent.cls = &_dbus_pollable_set_poll_class;
86 ret->n_allocated = size_hint;
94 socket_set_poll_free ((DBusPollableSet *) ret);
98 _dbus_verbose (
"new socket set at %p\n", ret);
99 return (DBusPollableSet *) ret;
103watch_flags_to_poll_events (
unsigned int flags)
116socket_set_poll_add (DBusPollableSet *set,
121 DBusPollableSetPoll *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)
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);
154 self->fds[self->n_fds].fd = fd;
155 self->fds[self->n_fds].events = watch_flags_to_poll_events (flags);
163socket_set_poll_enable (DBusPollableSet *set,
167 DBusPollableSetPoll *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);
183 self->fds[self->n_fds].fd = fd;
184 self->fds[self->n_fds].events = watch_flags_to_poll_events (flags);
189socket_set_poll_disable (DBusPollableSet *set,
192 DBusPollableSetPoll *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;
212socket_set_poll_remove (DBusPollableSet *set,
215 DBusPollableSetPoll *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);
225 if (self->n_reserved + MINIMUM_SIZE < self->n_allocated / 2)
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))
242 self->n_allocated = self->n_reserved;
247watch_flags_from_poll_revents (
short revents)
249 unsigned int condition = 0;
261 condition |= _DBUS_WATCH_NVAL;
269socket_set_poll_poll (DBusPollableSet *set,
270 DBusPollableEvent *revents,
274 DBusPollableSetPoll *self = socket_set_poll_cast (set);
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);
303 if (n_events == max_events)
311DBusPollableSetClass _dbus_pollable_set_poll_class = {
312 socket_set_poll_free,
314 socket_set_poll_remove,
315 socket_set_poll_enable,
316 socket_set_poll_disable,
@ DBUS_WATCH_READABLE
As in POLLIN.
@ DBUS_WATCH_WRITABLE
As in POLLOUT.
@ DBUS_WATCH_HANGUP
As in POLLHUP (can't watch for it, but can be present in current state passed to dbus_watch_handle())...
@ DBUS_WATCH_ERROR
As in POLLERR (can't watch for this, but can be present in current state passed to dbus_watch_handle(...
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
#define NULL
A null pointer, defined appropriately for C or C++.
#define TRUE
Expands to "1".
#define FALSE
Expands to "0".
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
void * dbus_realloc(void *memory, size_t bytes)
Resizes a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
#define _DBUS_POLLOUT
Writing now will not block.
#define _DBUS_POLLERR
Error condition.
#define _DBUS_POLLHUP
Hung up.
#define _DBUS_POLLNVAL
Invalid request: fd not open.
#define _DBUS_POLLIN
There is data to read.
int _dbus_poll(DBusPollFD *fds, int n_fds, int timeout_milliseconds)
Wrapper for poll().
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.