#include "evmap.h"
#include "mm-internal.h"
-#define GET_SLOT(map, slot, type) (struct type *)((map)->entries[slot])
+#define GET_SLOT(x, map, slot, type) \
+ (x) = (struct type *)((map)->entries[slot])
+#define GET_SLOT_AND_CTOR(x, map, slot, type, ctor) \
+ do { \
+ if ((map)->entries[slot] == NULL) { \
+ assert(ctor != NULL); \
+ (map)->entries[slot]=mm_malloc(sizeof(struct type)); \
+ assert((map)->entries[slot] != NULL); \
+ (ctor)((struct type *)(map)->entries[slot]); \
+ } \
+ (x) = (struct type *)((map)->entries[slot]); \
+ } while (0) \
static int
-evmap_make_space(
- struct event_map *map, int slot, int msize, void (*ctor)(void *))
+evmap_make_space(struct event_map *map, int slot, int msize)
{
if (map->nentries <= slot) {
- int i;
int nentries = map->nentries ? map->nentries : 32;
void **tmp;
if (tmp == NULL)
return (-1);
- for (i = map->nentries; i < nentries; ++i) {
- tmp[i] = mm_malloc(msize);
- assert(tmp[i] != NULL);
- (*ctor)(tmp[i]);
- }
+ memset(&tmp[map->nentries], 0,
+ (nentries - map->nentries) * sizeof(void *));
map->nentries = nentries;
map->entries = tmp;
ctx->nentries = 0;
if (ctx->entries != NULL) {
int i;
- for (i = 0; i < ctx->nentries; ++i)
- mm_free(ctx->entries[i]);
+ for (i = 0; i < ctx->nentries; ++i) {
+ if (ctx->entries[i] != NULL)
+ mm_free(ctx->entries[i]);
+ }
mm_free(ctx->entries);
ctx->entries = NULL;
}
short res = 0, old = 0;
if (fd >= io->nentries) {
- if (evmap_make_space(io, fd,
- sizeof(struct evmap_io), evmap_io_init) == -1)
+ if (evmap_make_space(io, fd, sizeof(struct evmap_io)) == -1)
return (-1);
}
- ctx = GET_SLOT(io, fd, evmap_io);
+ GET_SLOT_AND_CTOR(ctx, io, fd, evmap_io, evmap_io_init);
nread = ctx->nread;
nwrite = ctx->nwrite;
if (fd >= io->nentries)
return (-1);
- ctx = GET_SLOT(io, fd, evmap_io);
+ GET_SLOT(ctx, io, fd, evmap_io);
nread = ctx->nread;
nwrite = ctx->nwrite;
struct event *ev;
assert(fd < io->nentries);
- ctx = GET_SLOT(io, fd, evmap_io);
+ GET_SLOT(ctx, io, fd, evmap_io);
TAILQ_FOREACH(ev, &ctx->events, ev_io_next) {
if (ev->ev_events & events)
if (sig >= map->nentries) {
if (evmap_make_space(
- map, sig, sizeof(struct evmap_signal),
- evmap_signal_init) == -1)
+ map, sig, sizeof(struct evmap_signal)) == -1)
return (-1);
}
- ctx = GET_SLOT(map, sig, evmap_signal);
+ GET_SLOT_AND_CTOR(ctx, map, sig, evmap_signal, evmap_signal_init);
if (TAILQ_EMPTY(&ctx->events)) {
if (evsel->add(base, EVENT_SIGNAL(ev), 0, EV_SIGNAL) == -1)
if (sig >= map->nentries)
return (-1);
- ctx = GET_SLOT(map, sig, evmap_signal);
+ GET_SLOT(ctx, map, sig, evmap_signal);
if (TAILQ_FIRST(&ctx->events) == TAILQ_LAST(&ctx->events, event_list)) {
if (evsel->del(base, EVENT_SIGNAL(ev), 0, EV_SIGNAL) == -1)
struct event *ev;
assert(sig < map->nentries);
- ctx = GET_SLOT(map, sig, evmap_signal);
+ GET_SLOT(ctx, map, sig, evmap_signal);
TAILQ_FOREACH(ev, &ctx->events, ev_signal_next)
event_active(ev, EV_SIGNAL, ncalls);