28#include "dbus-pollable-set.h"
30#include <dbus/dbus-internals.h>
31#include <dbus/dbus-list.h>
32#include <dbus/dbus-sysdeps.h>
33#include <dbus/dbus-watch.h>
35#ifndef DOXYGEN_SHOULD_SKIP_THIS
38 DBusPollableSet parent;
45#define REALLOC_INCREMENT 8
49#ifdef DBUS_ENABLE_EMBEDDED_TESTS
50#define DEFAULT_SIZE_HINT 1
52#define DEFAULT_SIZE_HINT MINIMUM_SIZE
55static inline DBusPollableSetPoll *
56socket_set_poll_cast (DBusPollableSet *set)
58 _dbus_assert (set->cls == &_dbus_pollable_set_poll_class);
59 return (DBusPollableSetPoll *) set;
64socket_set_poll_free (DBusPollableSet *set)
66 DBusPollableSetPoll *self = socket_set_poll_cast (set);
70 _dbus_verbose (
"freed socket set %p\n", self);
74_dbus_pollable_set_poll_new (
int size_hint)
76 DBusPollableSetPoll *ret;
79 size_hint = DEFAULT_SIZE_HINT;
86 ret->parent.cls = &_dbus_pollable_set_poll_class;
88 ret->n_allocated = size_hint;
96 socket_set_poll_free ((DBusPollableSet *) ret);
100 _dbus_verbose (
"new socket set at %p\n", ret);
101 return (DBusPollableSet *) ret;
105watch_flags_to_poll_events (
unsigned int flags)
118socket_set_poll_add (DBusPollableSet *set,
123 DBusPollableSetPoll *self = socket_set_poll_cast (set);
124#ifndef DBUS_DISABLE_ASSERT
127 for (i = 0; i < self->n_fds; i++)
128 _dbus_assert (!_dbus_pollable_equals (self->fds[i].fd, fd));
131 if (self->n_reserved >= self->n_allocated)
134 sizeof (
DBusPollFD) * (self->n_allocated + REALLOC_INCREMENT));
136 _dbus_verbose (
"inflating set %p from %d en/%d res/%d alloc to %d\n",
137 self, self->n_fds, self->n_reserved, self->n_allocated,
138 self->n_allocated + REALLOC_INCREMENT);
144 self->n_allocated += REALLOC_INCREMENT;
147 _dbus_verbose (
"before adding fd %" DBUS_POLLABLE_FORMAT
" to %p, %d en/%d res/%d alloc\n",
148 _dbus_pollable_printable (fd), self, self->n_fds, self->n_reserved, self->n_allocated);
156 self->fds[self->n_fds].fd = fd;
157 self->fds[self->n_fds].events = watch_flags_to_poll_events (flags);
165socket_set_poll_enable (DBusPollableSet *set,
169 DBusPollableSetPoll *self = socket_set_poll_cast (set);
172 for (i = 0; i < self->n_fds; i++)
174 if (_dbus_pollable_equals (self->fds[i].fd, fd))
176 self->fds[i].events = watch_flags_to_poll_events (flags);
185 self->fds[self->n_fds].fd = fd;
186 self->fds[self->n_fds].events = watch_flags_to_poll_events (flags);
191socket_set_poll_disable (DBusPollableSet *set,
194 DBusPollableSetPoll *self = socket_set_poll_cast (set);
197 for (i = 0; i < self->n_fds; i++)
199 if (_dbus_pollable_equals (self->fds[i].fd, fd))
201 if (i != self->n_fds - 1)
203 self->fds[i].fd = self->fds[self->n_fds - 1].fd;
204 self->fds[i].events = self->fds[self->n_fds - 1].events;
214socket_set_poll_remove (DBusPollableSet *set,
217 DBusPollableSetPoll *self = socket_set_poll_cast (set);
219 socket_set_poll_disable (set, fd);
222 _dbus_verbose (
"after removing fd %" DBUS_POLLABLE_FORMAT
" from %p, %d en/%d res/%d alloc\n",
223 _dbus_pollable_printable (fd), self, self->n_fds, self->n_reserved, self->n_allocated);
227 if (self->n_reserved + MINIMUM_SIZE < self->n_allocated / 2)
232 sizeof (
DBusPollFD) * (self->n_reserved + MINIMUM_SIZE));
234 _dbus_verbose (
"before deflating %p, %d en/%d res/%d alloc\n",
235 self, self->n_fds, self->n_reserved, self->n_allocated);
237 if (_DBUS_UNLIKELY (new_fds ==
NULL))
244 self->n_allocated = self->n_reserved;
249watch_flags_from_poll_revents (
short revents)
251 unsigned int condition = 0;
263 condition |= _DBUS_WATCH_NVAL;
271socket_set_poll_poll (DBusPollableSet *set,
272 DBusPollableEvent *revents,
276 DBusPollableSetPoll *self = socket_set_poll_cast (set);
283 for (i = 0; i < self->n_fds; i++)
284 self->fds[i].revents = 0;
286 n_ready =
_dbus_poll (self->fds, self->n_fds, timeout_ms);
293 for (i = 0; i < self->n_fds; i++)
295 if (self->fds[i].revents != 0)
297 revents[n_events].fd = self->fds[i].fd;
298 revents[n_events].flags = watch_flags_from_poll_revents (self->fds[i].revents);
305 if (n_events == max_events)
313DBusPollableSetClass _dbus_pollable_set_poll_class = {
314 socket_set_poll_free,
316 socket_set_poll_remove,
317 socket_set_poll_enable,
318 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().