From: Niels Provos Date: Fri, 2 Jan 2009 18:18:30 +0000 (+0000) Subject: malloc fd only if it is needed. X-Git-Tag: release-2.0.1-alpha~193 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=172b657567777078296442f5363b9a1e764886b9;p=libevent malloc fd only if it is needed. svn:r982 --- diff --git a/evmap.c b/evmap.c index 1a753bc3..183fcfad 100644 --- a/evmap.c +++ b/evmap.c @@ -56,14 +56,23 @@ #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; @@ -74,11 +83,8 @@ evmap_make_space( 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; @@ -94,8 +100,10 @@ evmap_clear(struct event_map *ctx) 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; } @@ -130,11 +138,10 @@ evmap_io_add(struct event_base *base, int fd, struct event *ev) 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; @@ -181,7 +188,7 @@ evmap_io_del(struct event_base *base, int fd, struct event *ev) 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; @@ -222,7 +229,7 @@ evmap_io_active(struct event_base *base, int fd, short events) 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) @@ -254,11 +261,10 @@ evmap_signal_add(struct event_base *base, int sig, struct event *ev) 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) @@ -280,7 +286,7 @@ evmap_signal_del(struct event_base *base, int sig, struct event *ev) 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) @@ -300,7 +306,7 @@ evmap_signal_active(struct event_base *base, int sig, int ncalls) 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);